This repository was archived by the owner on Jul 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathlists_create_with.js
More file actions
executable file
·98 lines (88 loc) · 3.33 KB
/
lists_create_with.js
File metadata and controls
executable file
·98 lines (88 loc) · 3.33 KB
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
import { plusIconDark } from '../../images';
import { translate } from '../../../../utils/lang/i18n';
Blockly.Blocks.lists_create_with = {
init() {
this.jsonInit(this.definition());
// Render a ➕-icon for adding additional `lists_statement` blocks
const fieldImage = new Blockly.FieldImage(plusIconDark, 25, 25, '', () => this.onIconClick());
this.appendDummyInput('ADD_ICON').appendField(fieldImage);
this.moveInputBefore('ADD_ICON', 'STACK');
},
definition(){
return {
message0: translate('set %1 to create list with'),
message1: '%1',
args0 : [
{
type : 'field_variable',
name : 'VARIABLE',
variable: translate('list'),
},
],
args1: [
{
type: 'input_statement',
name: 'STACK',
},
],
colour : Blockly.Colours.Binary.colour,
colourSecondary : Blockly.Colours.Binary.colourSecondary,
colourTertiary : Blockly.Colours.Binary.colourTertiary,
previousStatement: null,
nextStatement : null,
tooltip : translate('Create List with'),
category : Blockly.Categories.List,
};
},
meta(){
return {
'display_name': translate('Create list with'),
'description' : translate('Create list with description'),
};
},
onIconClick() {
if (!this.workspace || this.isInFlyout) {
return;
}
const statementBlock = this.workspace.newBlock('lists_statement');
statementBlock.requiredParentId = this.id;
statementBlock.setMovable(false);
statementBlock.initSvg();
statementBlock.render();
const connection = this.getLastConnectionInStatement('STACK');
connection.connect(statementBlock.previousConnection);
},
onchange(event) {
if (!this.workspace || this.isInFlyout || this.workspace.isDragging()) {
return;
}
if (event.type === Blockly.Events.END_DRAG) {
// Only allow `lists_statement` blocks to be part of the `STACK`
let currentBlock = this.getInputTargetBlock('STACK');
while (currentBlock !== null) {
if (currentBlock.type !== 'lists_statement') {
currentBlock.unplug(false);
}
currentBlock = currentBlock.getNextBlock();
}
}
},
};
Blockly.JavaScript.lists_create_with = block => {
const variable = block.getFieldValue('VARIABLE');
// eslint-disable-next-line no-underscore-dangle
const varName = Blockly.JavaScript.variableDB_.getName(variable, Blockly.Variables.NAME_TYPE);
const elements = [];
let currentBlock = block.getInputTargetBlock('STACK');
while (currentBlock !== null) {
const value = Blockly.JavaScript[currentBlock.type](currentBlock);
if (Array.isArray(value) && value.length === 2) {
elements.push(value[0]);
} else {
elements.push(value);
}
currentBlock = currentBlock.getNextBlock();
}
const code = `${varName} = [${elements.join(', ')}];\n`;
return code;
};