soiz1 commited on
Commit
10b9827
·
verified ·
1 Parent(s): 1d7c166

Update src/lib/define-dynamic-block.js

Browse files
Files changed (1) hide show
  1. src/lib/define-dynamic-block.js +59 -5
src/lib/define-dynamic-block.js CHANGED
@@ -87,22 +87,76 @@ const defineDynamicBlock = (ScratchBlocks, categoryInfo, staticBlockInfo, extend
87
  // Layout block arguments
88
  // TODO handle E/C Blocks
89
  const blockText = blockInfo.text;
90
- const args = [];
91
  let argCount = 0;
92
  const scratchBlocksStyleText = blockText.replace(/\[(.+?)]/g, (match, argName) => {
93
  const arg = blockInfo.arguments[argName];
94
  switch (arg.type) {
 
95
  case ArgumentType.STRING:
96
- args.push({type: 'input_value', name: argName});
97
  break;
98
  case ArgumentType.BOOLEAN:
99
- args.push({type: 'input_value', name: argName, check: 'Boolean'});
100
  break;
101
  }
 
 
 
 
 
102
  return `%${++argCount}`;
103
  });
104
- this.interpolate_(scratchBlocksStyleText, args);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  }
106
  });
107
 
108
- export default defineDynamicBlock;
 
87
  // Layout block arguments
88
  // TODO handle E/C Blocks
89
  const blockText = blockInfo.text;
90
+ const args = {};
91
  let argCount = 0;
92
  const scratchBlocksStyleText = blockText.replace(/\[(.+?)]/g, (match, argName) => {
93
  const arg = blockInfo.arguments[argName];
94
  switch (arg.type) {
95
+ default: // bruh
96
  case ArgumentType.STRING:
97
+ args[argName] = { type: 'input_value', name: argName };
98
  break;
99
  case ArgumentType.BOOLEAN:
100
+ args[argName] = { type: 'input_value', name: argName, check: 'Boolean' };
101
  break;
102
  }
103
+ if (arg.menu && !categoryInfo.menuInfo[arg.menu].acceptsReporters) {
104
+ args[argName].type = 'field_dropdown';
105
+ args[argName].options = categoryInfo.menuInfo[arg.menu].items;
106
+ args[argName].value = categoryInfo.menuInfo[arg.menu].items[0][1];
107
+ }
108
  return `%${++argCount}`;
109
  });
110
+ this.interpolate_(scratchBlocksStyleText, Object.values(args));
111
+ if (this.isInsertionMarker()) return;
112
+ for (const name in args) {
113
+ if (args[name].type.startsWith('field_')) continue;
114
+ const arg = blockInfo.arguments[name];
115
+ const connection = this.getInput(name).connection;
116
+ const curBlock = connection.targetConnection?.getParentBlock?.();
117
+ if (curBlock && curBlock.type !== 'text' && !curBlock.type.startsWith('math_')) continue;
118
+ if (arg.menu) {
119
+ const fieldId = `${categoryInfo.id}_menu_${arg.menu}`;
120
+ if (curBlock?.type === fieldId) continue;
121
+ if (curBlock) curBlock.dispose();
122
+ const newBlock = this.workspace.newBlock(fieldId);
123
+ if (arg.defaultValue) newBlock.getField(arg.menu).setValue(arg.defaultValue);
124
+ newBlock.setShadow(true);
125
+ newBlock.initSvg();
126
+ newBlock.render();
127
+ continue;
128
+ }
129
+ switch (arg.type) {
130
+ case ArgumentType.STRING: {
131
+ if (curBlock?.type === 'text') break;
132
+ if (curBlock) curBlock.dispose();
133
+ const newBlock = this.workspace.newBlock('text');
134
+ connection.connect(newBlock.outputConnection);
135
+ newBlock.getField('TEXT').setValue(arg.defaultValue ?? '');
136
+ newBlock.setShadow(true);
137
+ newBlock.initSvg();
138
+ newBlock.render();
139
+ break;
140
+ }
141
+ case ArgumentType.NUMBER: {
142
+ if (curBlock && !curBlock.type.startsWith('math_')) break;
143
+ if (curBlock) curBlock.dispose();
144
+ const newBlock = this.workspace.newBlock('math_number');
145
+ connection.connect(newBlock.outputConnection);
146
+ newBlock.getField('NUM').setValue(arg.defaultValue ?? '');
147
+ newBlock.setShadow(true);
148
+ newBlock.initSvg();
149
+ newBlock.render();
150
+ break;
151
+ }
152
+ case ArgumentType.BOOLEAN: {
153
+ if (!curBlock) break;
154
+ curBlock.dispose();
155
+ break;
156
+ }
157
+ }
158
+ }
159
  }
160
  });
161
 
162
+ export default defineDynamicBlock;