-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathblockly-dom.js
764 lines (722 loc) · 21.3 KB
/
blockly-dom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/* jshint esversion: 6 */
// customised messages
Blockly.Msg["CONTROLS_FOREACH_TITLE"] = "for each item %1 in array %2";
Blockly.Msg["LISTS_CREATE_EMPTY_TITLE"] = "create empty array";
Blockly.Msg["LISTS_CREATE_EMPTY_TOOLTIP"] =
"Returns an array, of length 0, containing no data records";
Blockly.Msg["LISTS_CREATE_WITH_CONTAINER_TITLE_ADD"] = "array";
Blockly.Msg["LISTS_CREATE_WITH_CONTAINER_TOOLTIP"] =
"Add, remove, or reorder sections to reconfigure this array block.";
Blockly.Msg["LISTS_CREATE_WITH_INPUT_WITH"] = "create array with";
Blockly.Msg["LISTS_CREATE_WITH_ITEM_TOOLTIP"] = "Add an item to the array.";
Blockly.Msg["LISTS_CREATE_WITH_TOOLTIP"] =
"Create an array with any number of items.";
// copied from Blockly.Generator.prototype.statementToCode (but doesn't add indentation)
Blockly.JavaScript.statementToCodeNoIndent = function (block, name) {
var targetBlock = block.getInputTargetBlock(name);
var code = this.blockToCode(targetBlock);
// Value blocks must return code and order of operations info.
// Statement blocks must only return code.
if (typeof code != "string") {
throw TypeError(
"Expecting code from statement block: " +
(targetBlock && targetBlock.type)
);
}
return code;
};
Blockly.JavaScript.getWithContextVariable = function () {
if (!(this.contextVariableStack && this.contextVariableStack[0])) {
return "";
}
return this.contextVariableStack[0];
};
Blockly.JavaScript.pushWithContextVariable = function (variableName) {
this.contextVariableStack = this.contextVariableStack || [];
this.contextVariableStack.unshift(variableName);
};
Blockly.JavaScript.popWithContextVariable = function () {
if (!(this.contextVariableStack && this.contextVariableStack[0])) {
return "";
}
return this.contextVariableStack.shift();
};
Blockly.Blocks["arrays_getFirst"] = {
/**
* Block for getting element at index.
* @this {Blockly.Block}
*/
init: function () {
var MODE = [
[Blockly.Msg["LISTS_GET_INDEX_GET"], "GET"],
[Blockly.Msg["LISTS_GET_INDEX_GET_REMOVE"], "GET_REMOVE"],
[Blockly.Msg["LISTS_GET_INDEX_REMOVE"], "REMOVE"],
];
var WHERE_OPTIONS = [
[Blockly.Msg["LISTS_GET_INDEX_FIRST"], "FIRST"],
[Blockly.Msg["LISTS_GET_INDEX_LAST"], "LAST"],
];
this.setStyle("list_blocks");
var modeMenu = new Blockly.FieldDropdown(MODE, function (value) {
var isStatement = value == "REMOVE";
this.getSourceBlock().updateStatement_(isStatement);
});
this.appendDummyInput()
.appendField("", "SPACE")
.appendField(modeMenu, "MODE");
var atMenu = new Blockly.FieldDropdown(WHERE_OPTIONS);
this.appendDummyInput().appendField("the").appendField(atMenu, "WHERE");
this.appendValueInput("VALUE")
.setCheck("Array")
.appendField("item from the array");
this.setInputsInline(true);
this.setOutput(true);
},
/**
* Returns the state of this block as a JSON serializable object.
* @return {{isStatement: boolean}} Whether the block is a statment, otherwise null.
*/
saveExtraState: function () {
if (!this.outputConnection) {
return {
isStatement: true,
};
}
return null;
},
/**
* Applies the given state to this block.
* @param {*} state The state to apply to this block, ie whether it's a statement.
*/
loadExtraState: function (state) {
if (state["isStatement"]) {
this.updateStatement_(true);
}
},
/**
* Switch between a value block and a statement block.
* @param {boolean} newStatement True if the block should be a statement.
* False if the block should be a value.
* @private
* @this {Blockly.Block}
*/
updateStatement_: function (newStatement) {
var oldStatement = !this.outputConnection;
if (newStatement != oldStatement) {
this.unplug(true, true);
if (newStatement) {
this.setOutput(false);
this.setPreviousStatement(true);
this.setNextStatement(true);
} else {
this.setPreviousStatement(false);
this.setNextStatement(false);
this.setOutput(true);
}
}
},
};
WITH_CONTEXTS = [
"add_element",
"with_element_by_id",
"with_element_by_selector",
"with_elements_by_selector",
"element_clicked",
];
function validateInWithContext(_e) {
if (!this.workspace.isDragging || this.workspace.isDragging()) {
return; // Don't change state at the start of a drag.
}
var legal = false;
// Is the block nested in a procedure?
var block = this.getSurroundParent();
while (block) {
if (WITH_CONTEXTS.indexOf(block.type) != -1) {
legal = true;
break;
}
block = block.getSurroundParent();
}
if (legal) {
this.setWarningText(null);
if (!this.isInFlyout) {
this.setEnabled(true);
}
} else {
this.setWarningText(
"This block can only be used inside the 'create a new ... element' and 'find the element with id' blocks"
);
if (!this.isInFlyout && !this.getInheritedDisabled()) {
this.setEnabled(false);
}
}
}
Blockly.Extensions.register("validate_in_with_context", function () {
this.setOnChange(validateInWithContext);
});
Blockly.defineBlocksWithJsonArray([
{
type: "element_clicked",
message0: "%1 %2 %3 %4 %5",
args0: [
{
type: "field_label_serializable",
name: "TEXT1",
text: "when the element with id",
},
{
type: "field_input",
name: "ID",
text: "button",
},
{
type: "field_label_serializable",
name: "TEXT2",
text: "is clicked",
},
{
type: "input_dummy",
},
{
type: "input_statement",
name: "HANDLER",
},
],
colour: "%{BKY_COLOUR_HUE}",
tooltip: "When a button is clicked",
helpUrl: "",
},
{
type: "element_clicked_current",
message0: "%1 %2 %3",
args0: [
{
type: "field_label_serializable",
name: "TEXT1",
text: "when the element is clicked",
},
{
type: "input_dummy",
},
{
type: "input_statement",
name: "HANDLER",
},
],
previousStatement: null,
nextStatement: null,
colour: 60,
tooltip: "When a button is clicked",
helpUrl: "",
extensions: ["validate_in_with_context"],
},
{
type: "on_start",
message0: "%1 %2 %3",
args0: [
{
type: "field_label_serializable",
name: "TEXT1",
text: "at the start (when run is clicked)",
},
{
type: "input_dummy",
},
{
type: "input_statement",
name: "HANDLER",
},
],
inputsInline: false,
colour: "%{BKY_COLOUR_HUE}",
tooltip: "At the start",
helpUrl: "",
},
{
type: "set_attribute",
message0: "set the attribute %1 to %2",
args0: [
{
type: "field_dropdown",
name: "PROPERTY",
options: [
["src", "src"],
["href", "href"],
["background", "backgroundColor"],
["color", "color"],
["id", "id"],
["class", "class"],
["is visible", "visibility"],
],
},
{
type: "input_value",
name: "VALUE",
},
],
previousStatement: null,
nextStatement: null,
colour: 60,
extensions: ["validate_in_with_context"],
},
{
type: "toggle_attribute",
message0: "toggle the class %1",
args0: [
{
type: "input_value",
name: "VALUE",
},
],
previousStatement: null,
nextStatement: null,
colour: 60,
extensions: ["validate_in_with_context"],
},
{
type: "set_content",
message0: "set the text content to %1",
args0: [
{
type: "input_value",
name: "VALUE",
},
],
previousStatement: null,
nextStatement: null,
colour: 60,
extensions: ["validate_in_with_context"],
},
{
type: "get_input_data_with_id",
message0: "get the %1 value of input with id %2",
args0: [
{
type: "field_dropdown",
name: "TYPE",
text: "text",
options: [
["text", "string"],
['numerical','number'],
],
},
{
type: "field_input",
name: "ID",
text: "text",
},
],
output: "String",
colour: 60,
},
{
type: "get_input_value_with_id",
message0: "get the value of the <input> with id %1",
args0: [
{
type: "field_input",
name: "ID",
text: "text",
},
],
output: "String",
colour: 60,
},
{
type: "remove_contents",
message0: "remove the contents of the element",
args0: [],
previousStatement: null,
nextStatement: null,
colour: 60,
extensions: ["validate_in_with_context"],
},
{
type: "arrays_push",
message0: "add %1 at the %2 of array %3",
args0: [
{
type: "input_value",
name: "VALUE",
},
{
type: "field_dropdown",
name: "WHERE",
options: [
["start", "START"],
["end", "END"],
],
},
{
type: "input_value",
name: "LIST",
check: "Array",
},
],
previousStatement: null,
nextStatement: null,
style: "list_blocks",
},
{
type: "arrays_forEach",
message0: "%{BKY_CONTROLS_FOREACH_TITLE}",
args0: [
{
type: "field_variable",
name: "VAR",
variable: null,
},
{
type: "input_value",
name: "LIST",
check: "Array",
},
],
message1: "%{BKY_CONTROLS_REPEAT_INPUT_DO} %1",
args1: [
{
type: "input_statement",
name: "DO",
},
],
previousStatement: null,
nextStatement: null,
style: "loop_blocks",
extensions: ["contextMenu_newGetVariableBlock", "controls_forEach_tooltip"],
},
]);
Blockly.JavaScript["arrays_getFirst"] = function (block) {
var mode = block.getFieldValue("MODE");
var where = block.getFieldValue("WHERE");
var listOrder = Blockly.JavaScript.ORDER_MEMBER;
var list = Blockly.JavaScript.valueToCode(block, "VALUE", listOrder) || "[]";
if (mode == "GET") {
let op = where == "FIRST" ? "[0]" : ".slice(-1)[0]";
let code = list + op;
return [code, Blockly.JavaScript.ORDER_MEMBER];
} else if (mode == "GET_REMOVE") {
let op = where == "FIRST" ? ".shift()" : ".pop()";
let code = list + op;
return [code, Blockly.JavaScript.ORDER_MEMBER];
} else if (mode == "REMOVE") {
let op = where == "FIRST" ? ".shift()" : ".pop()";
return list + op + ";\n";
}
};
Blockly.JavaScript["arrays_push"] = function (block) {
var where = block.getFieldValue("WHERE");
var list =
Blockly.JavaScript.valueToCode(
block,
"LIST",
Blockly.JavaScript.ORDER_MEMBER
) || "[]";
var value =
Blockly.JavaScript.valueToCode(
block,
"VALUE",
Blockly.JavaScript.ORDER_NONE // going to be passed as a single argument to unshift
) || "null";
var op = where == "START" ? ".unshift" : ".push";
return list + op + "(" + value + ");\n";
};
Blockly.JavaScript["arrays_forEach"] = function (block) {
var variable = Blockly.JavaScript.nameDB_.getName(
block.getFieldValue("VAR"),
Blockly.VARIABLE_CATEGORY_NAME
);
var list =
Blockly.JavaScript.valueToCode(
block,
"LIST",
Blockly.JavaScript.ORDER_MEMBER
) || "[]";
var branch = Blockly.JavaScript.statementToCode(block, "DO");
// don't need a loop trap
var code = "";
code += list + ".forEach((" + variable + ") => {\n" + branch + "});\n";
return code;
};
Blockly.JavaScript["element_clicked"] = function (block) {
var text_id = block.getFieldValue("ID");
//let branch = Blockly.JavaScript.statementToCodeNoIndent(block, "STACK");
Blockly.JavaScript.pushWithContextVariable("event.target");
var statements_handler = Blockly.JavaScript.statementToCode(block, "HANDLER");
Blockly.JavaScript.popWithContextVariable();
// TODO: Assemble JavaScript into code variable.
var code = `
document.getElementById('${text_id}').addEventListener('click', (event) => {
${statements_handler}
});`;
return code;
};
/* Find the variables referenced inside the block. Modify the nameDB/variableMap so that
* they think they are called name_<local>.
* returns an array of object.
* The globalName is the name of the global variable
* The localName is the new local name that will be used until restoreGlobalVariables is called.
*/
function renameLocalVariablesUsedInBlock(block) {
let usedVariablesById = {};
block
.getDescendants()
.filter((b) => b.type === "variables_get")
.forEach((b) => {
usedVariablesById[b.getFieldValue("VAR")] = {
id: b.getFieldValue("VAR"),
};
});
let renamedVariables = Object.values(usedVariablesById);
renamedVariables.forEach((variable) => {
variable.blocklyVariable =
Blockly.JavaScript.nameDB_.variableMap_.getVariableById(variable.id);
variable.globalName = variable.blocklyVariable.name;
variable.blocklyVariable.name = variable.globalName + "_local";
// ensure no collisions
variable.localName = Blockly.JavaScript.nameDB_.getName(
variable.id,
Blockly.Variables.NAME_TYPE
);
});
return renamedVariables;
}
function restoreGlobalVariables(localVariables) {
localVariables.forEach((variable) => {
variable.blocklyVariable.name = variable.globalName;
});
}
Blockly.JavaScript["element_clicked_current"] = function (block) {
let localVariables = renameLocalVariablesUsedInBlock(block);
var statements_handler = Blockly.JavaScript.statementToCode(block, "HANDLER");
restoreGlobalVariables(localVariables);
var code = "\n";
localVariables.forEach((variable) => {
code += `// deep copy global variable '${variable.globalName}' so the current
// value is available inside the event listener
let ${variable.localName} = JSON.parse(JSON.stringify(${variable.globalName}));
`;
});
let withContextVariable = Blockly.JavaScript.getWithContextVariable();
code += `${withContextVariable}.addEventListener('click', (event) => {
${statements_handler}
});`;
return code;
};
Blockly.JavaScript["on_start"] = function (block) {
var code = Blockly.JavaScript.statementToCodeNoIndent(block, "HANDLER");
return code;
};
Blockly.Blocks["with_element_by_id"] = {
init: function () {
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(45);
this.appendDummyInput()
.appendField("find the element with id")
.appendField(new Blockly.FieldTextInput("list"), "ID");
this.appendStatementInput("STACK").appendField("and");
},
};
Blockly.JavaScript["with_element_by_id"] = function (block) {
let elementId = block.getFieldValue("ID");
let elementVar = Blockly.JavaScript.nameDB_.getDistinctName(
"element_" + elementId,
Blockly.Variables.NAME_TYPE
);
Blockly.JavaScript.pushWithContextVariable(elementVar);
let branch = Blockly.JavaScript.statementToCodeNoIndent(block, "STACK");
Blockly.JavaScript.popWithContextVariable();
return `let ${elementVar} = document.getElementById(${Blockly.JavaScript.quote_(
elementId
)});
${branch}`;
};
Blockly.Blocks["with_element_by_selector"] = {
init: function () {
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(45);
this.appendDummyInput()
.appendField("find the element using css selector")
.appendField(new Blockly.FieldTextInput("#list"), "QUERY");
this.appendStatementInput("STACK").appendField("and");
},
};
Blockly.JavaScript["with_element_by_selector"] = function (block) {
let elementQuery = block.getFieldValue("QUERY");
let elementVar = Blockly.JavaScript.nameDB_.getDistinctName(
"selectedElement",
Blockly.Variables.NAME_TYPE
);
Blockly.JavaScript.pushWithContextVariable(elementVar);
let branch = Blockly.JavaScript.statementToCodeNoIndent(block, "STACK");
Blockly.JavaScript.popWithContextVariable();
return `let ${elementVar} = document.querySelector(${Blockly.JavaScript.quote_(
elementQuery
)});
${branch}`;
};
Blockly.Blocks["with_elements_by_selector"] = {
init: function () {
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(45);
this.appendDummyInput()
.appendField("find all the elements using css selector")
.appendField(new Blockly.FieldTextInput("#list"), "QUERY");
this.appendStatementInput("STACK").appendField("and with each");
},
};
Blockly.JavaScript["with_elements_by_selector"] = function (block) {
let elementQuery = block.getFieldValue("QUERY");
let elementVar = Blockly.JavaScript.nameDB_.getDistinctName(
"selectedElement",
Blockly.Variables.NAME_TYPE
);
Blockly.JavaScript.pushWithContextVariable(elementVar);
let branch = Blockly.JavaScript.statementToCode(block, "STACK");
Blockly.JavaScript.popWithContextVariable();
return `document.querySelectorAll(${Blockly.JavaScript.quote_(
elementQuery
)}).forEach((${elementVar}) => {
${branch}
});
`;
};
Blockly.JavaScript["set_attribute"] = function (block) {
let value = Blockly.JavaScript.valueToCode(
block,
"VALUE",
Blockly.JavaScript.ORDER_ASSIGNMENT
);
let property = block.getFieldValue("PROPERTY");
if (property == "visibility") {
value = `(${value}) ? 'visible' : 'hidden'`;
}
let styleAttributes = ["color", "backgroundColor", "visibility"];
let withContextVariable = Blockly.JavaScript.getWithContextVariable();
if (styleAttributes.includes(property)) {
return withContextVariable + ".style." + property + " = " + value + ";\n";
} else {
return (
withContextVariable +
'.setAttribute("' +
property +
'", ' +
value +
");\n"
);
}
};
Blockly.JavaScript["toggle_attribute"] = function (block) {
let value = Blockly.JavaScript.valueToCode(
block,
"VALUE",
Blockly.JavaScript.ORDER_NONE
);
let withContextVariable = Blockly.JavaScript.getWithContextVariable();
return withContextVariable + `.classList.toggle(${value});\n`;
};
Blockly.JavaScript["set_content"] = function (block) {
let value = Blockly.JavaScript.valueToCode(
block,
"VALUE",
Blockly.JavaScript.ORDER_ASSIGNMENT
);
let withContextVariable = Blockly.JavaScript.getWithContextVariable();
return withContextVariable + ".innerText = " + value + ";\n";
};
Blockly.JavaScript["get_input_value_with_id"] = function (block) {
let elementId = block.getFieldValue("ID");
let getNumberOrStringActualName = Blockly.JavaScript.provideFunction_(
"getNumberOrString",
[
"function " + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + "(value) {",
" // Convert a string value to a number if possible",
" let number_value = Number(value);",
" if (Number.isNaN(number_value)) {",
" return value",
" } else {",
" return number_value",
" }",
"}",
]
);
return [
`${getNumberOrStringActualName}(document.getElementById(${Blockly.JavaScript.quote_(
elementId
)}).value)`,
Blockly.JavaScript.ORDER_MEMBER,
];
}
Blockly.JavaScript["get_input_data_with_id"] = function (block) {
let elementId = block.getFieldValue("ID");
let inputType = block.getFieldValue("TYPE");
const convertToNumberActualName = Blockly.JavaScript.provideFunction_("convertToNumber", [
"function " + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + "(value) {",
" // Convert a string value to a number if possible",
" let number_value = Number(value);",
" if (Number.isNaN(number_value)) {",
" return 0",
" } else {",
" return number_value",
" }",
"}",
]);
const element = `document.getElementById(${Blockly.JavaScript.quote_(
elementId
)})`;
if (inputType === "number") {
return [
`${convertToNumberActualName}(${element}.value)`,
Blockly.JavaScript.ORDER_MEMBER,
];
}
return [`${element}.value`, Blockly.JavaScript.ORDER_FUNCTION_CALL];
};
Blockly.JavaScript["remove_contents"] = function (block) {
let withContextVariable = Blockly.JavaScript.getWithContextVariable();
return withContextVariable + ".replaceChildren();\n";
};
Blockly.Blocks["add_element"] = {
init: function () {
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(60);
this.appendDummyInput()
.appendField("create a new")
.appendField(
new Blockly.FieldDropdown([
["<li>", "li"],
["<ul>", "ul"],
["<ol>", "ol"],
["<button>", "button"],
["<a>", "a"],
["<img>", "img"],
["<div>", "div"],
["<span>", "span"],
["<input>", "input"],
]),
"ELEMENT"
)
.appendField("element");
this.appendStatementInput("STACK").appendField("and");
this.appendDummyInput().appendField("then add it here");
},
onchange: validateInWithContext,
};
Blockly.JavaScript["add_element"] = function (block) {
let elementName = block.getFieldValue("ELEMENT");
let newElementVar = Blockly.JavaScript.nameDB_.getDistinctName(
"new_" + elementName,
Blockly.Variables.NAME_TYPE
);
Blockly.JavaScript.pushWithContextVariable(newElementVar);
let branch = Blockly.JavaScript.statementToCodeNoIndent(block, "STACK");
Blockly.JavaScript.popWithContextVariable();
let withContextVariable = Blockly.JavaScript.getWithContextVariable();
return `let ${newElementVar} = document.createElement('${elementName}');
${branch}
${withContextVariable}.appendChild(${newElementVar});
`;
};