scratch-blocks 2.0.0-spork.2 → 2.0.0-spork.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/dist/main.js +1 -1
- package/dist/main.js.LICENSE.txt +0 -12
- package/package.json +4 -4
- package/src/{block_reporting.js → block_reporting.ts} +7 -5
- package/src/blocks/{colour.js → colour.ts} +6 -6
- package/src/blocks/{control.js → control.ts} +21 -54
- package/src/blocks/{data.js → data.ts} +134 -142
- package/src/blocks/{event.js → event.ts} +12 -33
- package/src/blocks/{looks.js → looks.ts} +24 -73
- package/src/blocks/{math.js → math.ts} +6 -11
- package/src/blocks/{matrix.js → matrix.ts} +2 -3
- package/src/blocks/{motion.js → motion.ts} +23 -70
- package/src/blocks/{note.js → note.ts} +2 -3
- package/src/blocks/{operators.js → operators.ts} +18 -55
- package/src/blocks/{procedures.js → procedures.ts} +418 -269
- package/src/blocks/{sensing.js → sensing.ts} +21 -61
- package/src/blocks/{sound.js → sound.ts} +9 -28
- package/src/blocks/{text.js → text.ts} +1 -2
- package/src/blocks/{vertical_extensions.js → vertical_extensions.ts} +63 -100
- package/src/checkable_continuous_flyout.ts +112 -0
- package/src/{checkbox_bubble.js → checkbox_bubble.ts} +40 -58
- package/src/{colours.js → colours.ts} +11 -4
- package/src/{constants.js → constants.ts} +13 -0
- package/src/{context_menu_items.js → context_menu_items.ts} +18 -12
- package/src/{css.js → css.ts} +13 -7
- package/src/{data_category.js → data_category.ts} +216 -150
- package/src/events/{events_block_comment_base.js → events_block_comment_base.ts} +23 -4
- package/src/events/{events_block_comment_change.js → events_block_comment_change.ts} +29 -5
- package/src/events/{events_block_comment_collapse.js → events_block_comment_collapse.ts} +24 -6
- package/src/events/{events_block_comment_create.js → events_block_comment_create.ts} +36 -10
- package/src/events/{events_block_comment_delete.js → events_block_comment_delete.ts} +6 -2
- package/src/events/{events_block_comment_move.js → events_block_comment_move.ts} +36 -6
- package/src/events/events_block_comment_resize.ts +88 -0
- package/src/events/events_block_drag_end.ts +49 -0
- package/src/events/events_block_drag_outside.ts +44 -0
- package/src/events/{events_scratch_variable_create.js → events_scratch_variable_create.ts} +28 -15
- package/src/fields/{field_colour_slider.js → field_colour_slider.ts} +117 -106
- package/src/fields/{field_matrix.js → field_matrix.ts} +189 -215
- package/src/fields/{field_note.js → field_note.ts} +227 -286
- package/src/fields/{field_textinput_removable.js → field_textinput_removable.ts} +17 -20
- package/src/fields/{field_variable_getter.js → field_variable_getter.ts} +28 -17
- package/src/fields/{field_vertical_separator.js → field_vertical_separator.ts} +14 -30
- package/src/fields/{field_angle.js → scratch_field_angle.ts} +124 -80
- package/src/fields/{field_dropdown.js → scratch_field_dropdown.ts} +9 -7
- package/src/fields/{field_number.js → scratch_field_number.ts} +60 -55
- package/src/fields/{field_variable.js → scratch_field_variable.ts} +46 -27
- package/src/{flyout_checkbox_icon.js → flyout_checkbox_icon.ts} +15 -19
- package/src/{glows.js → glows.ts} +29 -18
- package/src/index.ts +62 -63
- package/src/procedures.ts +462 -0
- package/src/recyclable_block_flyout_inflater.ts +51 -0
- package/src/renderer/{bowler_hat.js → bowler_hat.ts} +1 -1
- package/src/renderer/{constants.js → constants.ts} +26 -12
- package/src/renderer/{drawer.js → drawer.ts} +8 -3
- package/src/renderer/{path_object.js → path_object.ts} +2 -2
- package/src/renderer/{render_info.js → render_info.ts} +19 -7
- package/src/renderer/renderer.ts +76 -0
- package/src/{scratch_block_paster.js → scratch_block_paster.ts} +9 -7
- package/src/scratch_blocks_utils.ts +39 -0
- package/src/{scratch_comment_icon.js → scratch_comment_icon.ts} +43 -26
- package/src/scratch_connection_checker.ts +44 -0
- package/src/{scratch_continuous_category.js → scratch_continuous_category.ts} +20 -13
- package/src/scratch_continuous_toolbox.ts +70 -0
- package/src/{scratch_dragger.js → scratch_dragger.ts} +97 -28
- package/src/{scratch_variable_map.js → scratch_variable_map.ts} +4 -1
- package/src/scratch_variable_model.ts +30 -0
- package/src/{shadows.js → shadows.ts} +8 -4
- package/src/{status_indicator_label.js → status_indicator_label.ts} +24 -36
- package/src/{status_indicator_label_flyout_inflater.js → status_indicator_label_flyout_inflater.ts} +13 -9
- package/src/{variables.js → variables.ts} +153 -123
- package/tsconfig.json +5 -0
- package/src/categories.js +0 -15
- package/src/checkable_continuous_flyout.js +0 -138
- package/src/events/events_block_comment_resize.js +0 -52
- package/src/events/events_block_drag_end.js +0 -33
- package/src/events/events_block_drag_outside.js +0 -30
- package/src/procedures.js +0 -425
- package/src/recyclable_block_flyout_inflater.js +0 -194
- package/src/renderer/renderer.js +0 -74
- package/src/scratch_blocks_utils.js +0 -148
- package/src/scratch_connection_checker.js +0 -29
- package/src/scratch_continuous_toolbox.js +0 -78
- package/src/scratch_variable_model.js +0 -24
- /package/{continuous-toolbox.d.ts → types/continuous-toolbox.d.ts} +0 -0
package/src/procedures.js
DELETED
|
@@ -1,425 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license
|
|
3
|
-
* Visual Blocks Editor
|
|
4
|
-
*
|
|
5
|
-
* Copyright 2012 Google Inc.
|
|
6
|
-
* https://developers.google.com/blockly/
|
|
7
|
-
*
|
|
8
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9
|
-
* you may not use this file except in compliance with the License.
|
|
10
|
-
* You may obtain a copy of the License at
|
|
11
|
-
*
|
|
12
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
-
*
|
|
14
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
15
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17
|
-
* See the License for the specific language governing permissions and
|
|
18
|
-
* limitations under the License.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @fileoverview Utility functions for handling procedures.
|
|
23
|
-
* @author fraser@google.com (Neil Fraser)
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
import * as Blockly from "blockly/core";
|
|
27
|
-
import * as Constants from "./constants.js";
|
|
28
|
-
import * as scratchBlocksUtils from "../src/scratch_blocks_utils.js";
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Find all user-created procedure definition mutations in a workspace.
|
|
32
|
-
* @param {!Blockly.Workspace} root Root workspace.
|
|
33
|
-
* @return {!Array.<Element>} Array of mutation xml elements.
|
|
34
|
-
* @package
|
|
35
|
-
*/
|
|
36
|
-
function allProcedureMutations(root) {
|
|
37
|
-
var blocks = root.getAllBlocks();
|
|
38
|
-
var mutations = [];
|
|
39
|
-
for (var i = 0; i < blocks.length; i++) {
|
|
40
|
-
if (blocks[i].type == Constants.PROCEDURES_PROTOTYPE_BLOCK_TYPE) {
|
|
41
|
-
var mutation = blocks[i].mutationToDom(/* opt_generateShadows */ true);
|
|
42
|
-
if (mutation) {
|
|
43
|
-
mutations.push(mutation);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return mutations;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Sorts an array of procedure definition mutations alphabetically.
|
|
52
|
-
* (Does not mutate the given array.)
|
|
53
|
-
* @param {!Array.<Element>} mutations Array of mutation xml elements.
|
|
54
|
-
* @return {!Array.<Element>} Sorted array of mutation xml elements.
|
|
55
|
-
* @private
|
|
56
|
-
*/
|
|
57
|
-
function sortProcedureMutations_(mutations) {
|
|
58
|
-
var newMutations = mutations.slice();
|
|
59
|
-
|
|
60
|
-
newMutations.sort(function (a, b) {
|
|
61
|
-
var procCodeA = a.getAttribute("proccode");
|
|
62
|
-
var procCodeB = b.getAttribute("proccode");
|
|
63
|
-
|
|
64
|
-
return scratchBlocksUtils.compareStrings(procCodeA, procCodeB);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
return newMutations;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Construct the blocks required by the flyout for the procedure category.
|
|
72
|
-
* @param {!Blockly.Workspace} workspace The workspace containing procedures.
|
|
73
|
-
* @return {!Array.<!Element>} Array of XML block elements.
|
|
74
|
-
*/
|
|
75
|
-
function getProceduresCategory(workspace) {
|
|
76
|
-
var xmlList = [];
|
|
77
|
-
|
|
78
|
-
addCreateButton_(workspace, xmlList);
|
|
79
|
-
|
|
80
|
-
// Create call blocks for each procedure defined in the workspace
|
|
81
|
-
var mutations = allProcedureMutations(workspace);
|
|
82
|
-
mutations = sortProcedureMutations_(mutations);
|
|
83
|
-
for (var i = 0; i < mutations.length; i++) {
|
|
84
|
-
var mutation = mutations[i];
|
|
85
|
-
// <block type="procedures_call">
|
|
86
|
-
// <mutation ...></mutation>
|
|
87
|
-
// </block>
|
|
88
|
-
var block = document.createElement("block");
|
|
89
|
-
block.setAttribute("type", "procedures_call");
|
|
90
|
-
block.setAttribute("gap", 16);
|
|
91
|
-
block.appendChild(mutation);
|
|
92
|
-
xmlList.push(block);
|
|
93
|
-
}
|
|
94
|
-
return xmlList;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Create the "Make a Block..." button.
|
|
99
|
-
* @param {!Blockly.Workspace} workspace The workspace contianing procedures.
|
|
100
|
-
* @param {!Array.<!Element>} xmlList Array of XML block elements to add to.
|
|
101
|
-
* @private
|
|
102
|
-
*/
|
|
103
|
-
function addCreateButton_(workspace, xmlList) {
|
|
104
|
-
var button = document.createElement("button");
|
|
105
|
-
var msg = Blockly.Msg.NEW_PROCEDURE;
|
|
106
|
-
var callbackKey = "CREATE_PROCEDURE";
|
|
107
|
-
var callback = function () {
|
|
108
|
-
// Run the callback after a delay to avoid it getting captured by the React
|
|
109
|
-
// modal in scratch-gui and being registered as a click on the scrim that
|
|
110
|
-
// dismisses the dialog.
|
|
111
|
-
requestAnimationFrame(() => {
|
|
112
|
-
setTimeout(() => {
|
|
113
|
-
createProcedureDefCallback(workspace);
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
};
|
|
117
|
-
button.setAttribute("text", msg);
|
|
118
|
-
button.setAttribute("callbackKey", callbackKey);
|
|
119
|
-
workspace.registerButtonCallback(callbackKey, callback);
|
|
120
|
-
xmlList.push(button);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Find all callers of a named procedure.
|
|
125
|
-
* @param {string} name Name of procedure (procCode in scratch-blocks).
|
|
126
|
-
* @param {!Blockly.Workspace} ws The workspace to find callers in.
|
|
127
|
-
* @param {!Blockly.Block} definitionRoot The root of the stack where the
|
|
128
|
-
* procedure is defined.
|
|
129
|
-
* @param {boolean} allowRecursive True if the search should include recursive
|
|
130
|
-
* procedure calls. False if the search should ignore the stack starting
|
|
131
|
-
* with definitionRoot.
|
|
132
|
-
* @return {!Array.<!Blockly.Block>} Array of caller blocks.
|
|
133
|
-
* @package
|
|
134
|
-
*/
|
|
135
|
-
function getCallers(name, ws, definitionRoot, allowRecursive) {
|
|
136
|
-
var allBlocks = [];
|
|
137
|
-
var topBlocks = ws.getTopBlocks();
|
|
138
|
-
|
|
139
|
-
// Start by deciding which stacks to investigate.
|
|
140
|
-
for (var i = 0; i < topBlocks.length; i++) {
|
|
141
|
-
var block = topBlocks[i];
|
|
142
|
-
if (block.id == definitionRoot.id && !allowRecursive) {
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
allBlocks.push.apply(allBlocks, block.getDescendants(false));
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
var callers = [];
|
|
149
|
-
for (var i = 0; i < allBlocks.length; i++) {
|
|
150
|
-
var block = allBlocks[i];
|
|
151
|
-
if (block.type == Constants.PROCEDURES_CALL_BLOCK_TYPE) {
|
|
152
|
-
var procCode = block.getProcCode();
|
|
153
|
-
if (procCode && procCode == name) {
|
|
154
|
-
callers.push(block);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
return callers;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Find and edit all callers with a procCode using a new mutation.
|
|
163
|
-
* @param {string} name Name of procedure (procCode in scratch-blocks).
|
|
164
|
-
* @param {!Blockly.Workspace} ws The workspace to find callers in.
|
|
165
|
-
* @param {!Element} mutation New mutation for the callers.
|
|
166
|
-
* @package
|
|
167
|
-
*/
|
|
168
|
-
function mutateCallersAndPrototype(name, ws, mutation) {
|
|
169
|
-
var defineBlock = getDefineBlock(name, ws);
|
|
170
|
-
var prototypeBlock = getPrototypeBlock(name, ws);
|
|
171
|
-
if (defineBlock && prototypeBlock) {
|
|
172
|
-
var callers = getCallers(
|
|
173
|
-
name,
|
|
174
|
-
defineBlock.workspace,
|
|
175
|
-
defineBlock,
|
|
176
|
-
true /* allowRecursive */
|
|
177
|
-
);
|
|
178
|
-
callers.push(prototypeBlock);
|
|
179
|
-
Blockly.Events.setGroup(true);
|
|
180
|
-
for (var i = 0, caller; (caller = callers[i]); i++) {
|
|
181
|
-
var oldMutationDom = caller.mutationToDom();
|
|
182
|
-
var oldMutation = oldMutationDom && Blockly.Xml.domToText(oldMutationDom);
|
|
183
|
-
caller.domToMutation(mutation);
|
|
184
|
-
var newMutationDom = caller.mutationToDom();
|
|
185
|
-
var newMutation = newMutationDom && Blockly.Xml.domToText(newMutationDom);
|
|
186
|
-
if (oldMutation != newMutation) {
|
|
187
|
-
Blockly.Events.fire(
|
|
188
|
-
new Blockly.Events.BlockChange(
|
|
189
|
-
caller,
|
|
190
|
-
"mutation",
|
|
191
|
-
null,
|
|
192
|
-
oldMutation,
|
|
193
|
-
newMutation
|
|
194
|
-
)
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
Blockly.Events.setGroup(false);
|
|
199
|
-
} else {
|
|
200
|
-
alert("No define block on workspace"); // TODO decide what to do about this.
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Find the definition block for the named procedure.
|
|
206
|
-
* @param {string} procCode The identifier of the procedure.
|
|
207
|
-
* @param {!Blockly.Workspace} workspace The workspace to search.
|
|
208
|
-
* @return {Blockly.Block} The procedure definition block, or null not found.
|
|
209
|
-
* @package
|
|
210
|
-
*/
|
|
211
|
-
function getDefineBlock(procCode, workspace) {
|
|
212
|
-
// Assume that a procedure definition is a top block.
|
|
213
|
-
var blocks = workspace.getTopBlocks(false);
|
|
214
|
-
for (var i = 0; i < blocks.length; i++) {
|
|
215
|
-
if (blocks[i].type == Constants.PROCEDURES_DEFINITION_BLOCK_TYPE) {
|
|
216
|
-
var prototypeBlock = blocks[i]
|
|
217
|
-
.getInput("custom_block")
|
|
218
|
-
.connection.targetBlock();
|
|
219
|
-
if (
|
|
220
|
-
prototypeBlock.getProcCode &&
|
|
221
|
-
prototypeBlock.getProcCode() == procCode
|
|
222
|
-
) {
|
|
223
|
-
return blocks[i];
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
return null;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Find the prototype block for the named procedure.
|
|
232
|
-
* @param {string} procCode The identifier of the procedure.
|
|
233
|
-
* @param {!Blockly.Workspace} workspace The workspace to search.
|
|
234
|
-
* @return {Blockly.Block} The procedure prototype block, or null not found.
|
|
235
|
-
* @package
|
|
236
|
-
*/
|
|
237
|
-
function getPrototypeBlock(procCode, workspace) {
|
|
238
|
-
var defineBlock = getDefineBlock(procCode, workspace);
|
|
239
|
-
if (defineBlock) {
|
|
240
|
-
return defineBlock.getInput("custom_block").connection.targetBlock();
|
|
241
|
-
}
|
|
242
|
-
return null;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* Create a mutation for a brand new custom procedure.
|
|
247
|
-
* @return {Element} The mutation for a new custom procedure
|
|
248
|
-
* @package
|
|
249
|
-
*/
|
|
250
|
-
function newProcedureMutation() {
|
|
251
|
-
var mutationText =
|
|
252
|
-
"<xml>" +
|
|
253
|
-
"<mutation" +
|
|
254
|
-
' proccode="' +
|
|
255
|
-
Blockly.Msg["PROCEDURE_DEFAULT_NAME"] +
|
|
256
|
-
'"' +
|
|
257
|
-
' argumentids="[]"' +
|
|
258
|
-
' argumentnames="[]"' +
|
|
259
|
-
' argumentdefaults="[]"' +
|
|
260
|
-
' warp="false">' +
|
|
261
|
-
"</mutation>" +
|
|
262
|
-
"</xml>";
|
|
263
|
-
return Blockly.utils.xml.textToDom(mutationText).firstChild;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Callback to create a new procedure custom command block.
|
|
268
|
-
* @param {!Blockly.Workspace} workspace The workspace to create the new procedure on.
|
|
269
|
-
* @private
|
|
270
|
-
*/
|
|
271
|
-
function createProcedureDefCallback(workspace) {
|
|
272
|
-
ScratchProcedures.externalProcedureDefCallback(
|
|
273
|
-
newProcedureMutation(),
|
|
274
|
-
createProcedureCallbackFactory_(workspace)
|
|
275
|
-
);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* Callback factory for adding a new custom procedure from a mutation.
|
|
280
|
-
* @param {!Blockly.Workspace} workspace The workspace to create the new procedure on.
|
|
281
|
-
* @return {function(?Element)} callback for creating the new custom procedure.
|
|
282
|
-
* @private
|
|
283
|
-
*/
|
|
284
|
-
function createProcedureCallbackFactory_(workspace) {
|
|
285
|
-
return function (mutation) {
|
|
286
|
-
if (mutation) {
|
|
287
|
-
var blockText =
|
|
288
|
-
"<xml>" +
|
|
289
|
-
'<block type="procedures_definition">' +
|
|
290
|
-
'<statement name="custom_block">' +
|
|
291
|
-
'<shadow type="procedures_prototype">' +
|
|
292
|
-
Blockly.Xml.domToText(mutation) +
|
|
293
|
-
"</shadow>" +
|
|
294
|
-
"</statement>" +
|
|
295
|
-
"</block>" +
|
|
296
|
-
"</xml>";
|
|
297
|
-
var blockDom = Blockly.utils.xml.textToDom(blockText).firstChild;
|
|
298
|
-
Blockly.Events.setGroup(true);
|
|
299
|
-
var block = Blockly.Xml.domToBlock(blockDom, workspace);
|
|
300
|
-
Blockly.renderManagement.finishQueuedRenders().then(() => {
|
|
301
|
-
var scale = workspace.scale; // To convert from pixel units to workspace units
|
|
302
|
-
// Position the block so that it is at the top left of the visible workspace,
|
|
303
|
-
// padded from the edge by 30 units. Position in the top right if RTL.
|
|
304
|
-
var posX = -workspace.scrollX;
|
|
305
|
-
if (workspace.RTL) {
|
|
306
|
-
posX += workspace.getMetrics().contentWidth - 30;
|
|
307
|
-
} else {
|
|
308
|
-
posX += 30;
|
|
309
|
-
}
|
|
310
|
-
block.moveBy(posX / scale, (-workspace.scrollY + 30) / scale);
|
|
311
|
-
block.scheduleSnapAndBump();
|
|
312
|
-
Blockly.Events.setGroup(false);
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
};
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
/**
|
|
319
|
-
* Callback to open the modal for editing custom procedures.
|
|
320
|
-
* @param {!Blockly.Block} block The block that was right-clicked.
|
|
321
|
-
* @private
|
|
322
|
-
*/
|
|
323
|
-
function editProcedureCallback_(block) {
|
|
324
|
-
// Edit can come from one of three block types (call, define, prototype)
|
|
325
|
-
// Normalize by setting the block to the prototype block for the procedure.
|
|
326
|
-
if (block.type == Constants.PROCEDURES_DEFINITION_BLOCK_TYPE) {
|
|
327
|
-
var input = block.getInput("custom_block");
|
|
328
|
-
if (!input) {
|
|
329
|
-
alert("Bad input"); // TODO: Decide what to do about this.
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
var conn = input.connection;
|
|
333
|
-
if (!conn) {
|
|
334
|
-
alert("Bad connection"); // TODO: Decide what to do about this.
|
|
335
|
-
return;
|
|
336
|
-
}
|
|
337
|
-
var innerBlock = conn.targetBlock();
|
|
338
|
-
if (
|
|
339
|
-
!innerBlock ||
|
|
340
|
-
!innerBlock.type == Constants.PROCEDURES_PROTOTYPE_BLOCK_TYPE
|
|
341
|
-
) {
|
|
342
|
-
alert("Bad inner block"); // TODO: Decide what to do about this.
|
|
343
|
-
return;
|
|
344
|
-
}
|
|
345
|
-
block = innerBlock;
|
|
346
|
-
} else if (block.type == Constants.PROCEDURES_CALL_BLOCK_TYPE) {
|
|
347
|
-
// This is a call block, find the prototype corresponding to the procCode.
|
|
348
|
-
// Make sure to search the correct workspace, call block can be in flyout.
|
|
349
|
-
var workspaceToSearch = block.workspace.isFlyout
|
|
350
|
-
? block.workspace.targetWorkspace
|
|
351
|
-
: block.workspace;
|
|
352
|
-
block = getPrototypeBlock(block.getProcCode(), workspaceToSearch);
|
|
353
|
-
}
|
|
354
|
-
// Block now refers to the procedure prototype block, it is safe to proceed.
|
|
355
|
-
ScratchProcedures.externalProcedureDefCallback(
|
|
356
|
-
block.mutationToDom(),
|
|
357
|
-
editProcedureCallbackFactory_(block)
|
|
358
|
-
);
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* Callback factory for editing an existing custom procedure.
|
|
363
|
-
* @param {!Blockly.Block} block The procedure prototype block being edited.
|
|
364
|
-
* @return {function(?Element)} Callback for editing the custom procedure.
|
|
365
|
-
* @private
|
|
366
|
-
*/
|
|
367
|
-
function editProcedureCallbackFactory_(block) {
|
|
368
|
-
return function (mutation) {
|
|
369
|
-
if (mutation) {
|
|
370
|
-
mutateCallersAndPrototype(block.getProcCode(), block.workspace, mutation);
|
|
371
|
-
}
|
|
372
|
-
};
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* Make a context menu option for editing a custom procedure.
|
|
377
|
-
* This appears in the context menu for procedure definitions and procedure
|
|
378
|
-
* calls.
|
|
379
|
-
* @param {!Blockly.BlockSvg} block The block where the right-click originated.
|
|
380
|
-
* @return {!Object} A menu option, containing text, enabled, and a callback.
|
|
381
|
-
* @package
|
|
382
|
-
*/
|
|
383
|
-
function makeEditOption(block) {
|
|
384
|
-
var editOption = {
|
|
385
|
-
enabled: true,
|
|
386
|
-
text: Blockly.Msg.EDIT_PROCEDURE,
|
|
387
|
-
callback: function () {
|
|
388
|
-
editProcedureCallback_(block);
|
|
389
|
-
},
|
|
390
|
-
};
|
|
391
|
-
return editOption;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
/**
|
|
395
|
-
* Callback to try to delete a custom block definitions.
|
|
396
|
-
* @param {string} procCode The identifier of the procedure to delete.
|
|
397
|
-
* @param {!Blockly.Block} definitionRoot The root block of the stack that
|
|
398
|
-
* defines the custom procedure.
|
|
399
|
-
* @return {boolean} True if the custom procedure was deleted, false otherwise.
|
|
400
|
-
* @package
|
|
401
|
-
*/
|
|
402
|
-
function deleteProcedureDefCallback(procCode, definitionRoot) {
|
|
403
|
-
const callers = getCallers(
|
|
404
|
-
procCode,
|
|
405
|
-
definitionRoot.workspace,
|
|
406
|
-
definitionRoot,
|
|
407
|
-
false /* allowRecursive */
|
|
408
|
-
);
|
|
409
|
-
if (callers.length > 0) {
|
|
410
|
-
return false;
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
const workspace = definitionRoot.workspace;
|
|
414
|
-
Blockly.BlockSvg.prototype.checkAndDelete.call(definitionRoot);
|
|
415
|
-
return true;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
const ScratchProcedures = {
|
|
419
|
-
externalProcedureDefCallback: null,
|
|
420
|
-
createProcedureDefCallback,
|
|
421
|
-
deleteProcedureDefCallback,
|
|
422
|
-
getProceduresCategory,
|
|
423
|
-
makeEditOption,
|
|
424
|
-
};
|
|
425
|
-
export { ScratchProcedures };
|
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license
|
|
3
|
-
* Copyright 2024 Google LLC
|
|
4
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import * as Blockly from "blockly/core";
|
|
8
|
-
import { CheckboxBubble } from "./checkbox_bubble.js";
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* A block inflater that caches and reuses blocks to improve performance.
|
|
12
|
-
*/
|
|
13
|
-
export class RecyclableBlockFlyoutInflater extends Blockly.BlockFlyoutInflater {
|
|
14
|
-
/**
|
|
15
|
-
* Whether or not block recycling is enabled.
|
|
16
|
-
* @type {boolean}
|
|
17
|
-
*/
|
|
18
|
-
recyclingEnabled = true;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Map from block type to block instance.
|
|
22
|
-
* @type {Map<string, !Blockly.BlockSvg>}
|
|
23
|
-
*/
|
|
24
|
-
recycledBlocks = new Map();
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Creates a block on the flyout workspace from the given block definition.
|
|
28
|
-
*
|
|
29
|
-
* @param {!Object} state A JSON representation of a block to load.
|
|
30
|
-
* @param {!Blockly.WorkspaceSvg} flyoutWorkspace The flyout's workspace.
|
|
31
|
-
* @returns {!Blockly.BlockSvg} The newly created block.
|
|
32
|
-
*/
|
|
33
|
-
load(state, flyoutWorkspace) {
|
|
34
|
-
const block = super.load(state, flyoutWorkspace);
|
|
35
|
-
if (block.checkboxInFlyout) {
|
|
36
|
-
block.moveBy(
|
|
37
|
-
CheckboxBubble.CHECKBOX_SIZE + CheckboxBubble.CHECKBOX_MARGIN,
|
|
38
|
-
0
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return block;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Toggles whether or not recycling is enabled.
|
|
47
|
-
*
|
|
48
|
-
* @param {boolean} enabled True if recycling should be enabled.
|
|
49
|
-
*/
|
|
50
|
-
setRecyclingEnabled(enabled) {
|
|
51
|
-
this.recyclingEnabled = enabled;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Creates a new block from the given block definition.
|
|
56
|
-
*
|
|
57
|
-
* @param {!Object} blockDefinition The definition to create a block from.
|
|
58
|
-
* @returns {!Blockly.BlockSvg} The newly created block.
|
|
59
|
-
*/
|
|
60
|
-
createBlock(blockDefinition) {
|
|
61
|
-
const blockType = this.getTypeFromDefinition(blockDefinition);
|
|
62
|
-
return (
|
|
63
|
-
this.getRecycledBlock(blockType) ??
|
|
64
|
-
super.createBlock(blockDefinition, this.flyoutWorkspace)
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Returns the type of a block from an XML or JSON block definition.
|
|
70
|
-
*
|
|
71
|
-
* @param blockDefinition {!Object} The block definition to parse.
|
|
72
|
-
* @returns {string} The block type.
|
|
73
|
-
*/
|
|
74
|
-
getTypeFromDefinition(blockDefinition) {
|
|
75
|
-
if (blockDefinition["blockxml"]) {
|
|
76
|
-
const xml =
|
|
77
|
-
typeof blockDefinition["blockxml"] === "string"
|
|
78
|
-
? Blockly.utils.xml.textToDom(blockDefinition["blockxml"])
|
|
79
|
-
: blockDefinition["blockxml"];
|
|
80
|
-
return xml.getAttribute("type");
|
|
81
|
-
} else {
|
|
82
|
-
return blockDefinition["type"];
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Puts a previously created block into the recycle bin and moves it to the
|
|
88
|
-
* top of the workspace. Used during large workspace swaps to limit the number
|
|
89
|
-
* of new DOM elements we need to create.
|
|
90
|
-
*
|
|
91
|
-
* @param {!Blockly.BlockSvg} block The block to recycle.
|
|
92
|
-
*/
|
|
93
|
-
recycleBlock(block) {
|
|
94
|
-
const xy = block.getRelativeToSurfaceXY();
|
|
95
|
-
block.moveBy(-xy.x, -xy.y);
|
|
96
|
-
this.recycledBlocks.set(block.type, block);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Returns a block from the cache of recycled blocks with the given type, or
|
|
101
|
-
* undefined if one cannot be found.
|
|
102
|
-
*
|
|
103
|
-
* @param {string} blockType The type of the block to try to recycle.
|
|
104
|
-
* @returns {?Blockly.BlockSvg} The recycled block, or undefined if
|
|
105
|
-
* one could not be recycled.
|
|
106
|
-
*/
|
|
107
|
-
getRecycledBlock(blockType) {
|
|
108
|
-
const block = this.recycledBlocks.get(blockType);
|
|
109
|
-
this.recycledBlocks.delete(blockType);
|
|
110
|
-
return block;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Returns whether the given block can be recycled or not.
|
|
115
|
-
*
|
|
116
|
-
* @param {!Blockly.BlockSvg} block The block to check for recyclability.
|
|
117
|
-
* @returns {boolean} True if the block can be recycled. False otherwise.
|
|
118
|
-
*/
|
|
119
|
-
blockIsRecyclable(block) {
|
|
120
|
-
if (!this.recyclingEnabled) {
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// If the block needs to parse mutations, never recycle.
|
|
125
|
-
if (block.mutationToDom && block.domToMutation) {
|
|
126
|
-
return false;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (!block.isEnabled()) {
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
for (const input of block.inputList) {
|
|
134
|
-
for (const field of input.fieldRow) {
|
|
135
|
-
// No variables.
|
|
136
|
-
if (field.referencesVariables()) {
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
if (field instanceof Blockly.FieldDropdown) {
|
|
140
|
-
if (field.isOptionListDynamic()) {
|
|
141
|
-
return false;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
// Check children.
|
|
146
|
-
if (input.connection) {
|
|
147
|
-
const targetBlock =
|
|
148
|
-
/** @type {Blockly.BlockSvg} */
|
|
149
|
-
(input.connection.targetBlock());
|
|
150
|
-
if (targetBlock && !this.blockIsRecyclable(targetBlock)) {
|
|
151
|
-
return false;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
return true;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Disposes of the provided block.
|
|
160
|
-
*
|
|
161
|
-
* @param {!Blockly.BlockSvg} element The block to dispose of.
|
|
162
|
-
*/
|
|
163
|
-
disposeElement(element) {
|
|
164
|
-
if (this.blockIsRecyclable(element)) {
|
|
165
|
-
this.removeListeners(element.id);
|
|
166
|
-
this.recycleBlock(element);
|
|
167
|
-
} else {
|
|
168
|
-
super.disposeElement(element);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Clears the cache of recycled blocks.
|
|
174
|
-
*/
|
|
175
|
-
emptyRecycledBlocks() {
|
|
176
|
-
this.recycledBlocks
|
|
177
|
-
.values()
|
|
178
|
-
.forEach((block) => block.dispose(false, false));
|
|
179
|
-
this.recycledBlocks.clear();
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Registers the recyclable block flyout inflater, replacing the standard
|
|
185
|
-
* block flyout inflater.
|
|
186
|
-
*/
|
|
187
|
-
export function registerRecyclableBlockFlyoutInflater() {
|
|
188
|
-
Blockly.registry.unregister(Blockly.registry.Type.FLYOUT_INFLATER, "block");
|
|
189
|
-
Blockly.registry.register(
|
|
190
|
-
Blockly.registry.Type.FLYOUT_INFLATER,
|
|
191
|
-
"block",
|
|
192
|
-
RecyclableBlockFlyoutInflater
|
|
193
|
-
);
|
|
194
|
-
}
|
package/src/renderer/renderer.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license
|
|
3
|
-
* Copyright 2024 Google LLC
|
|
4
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import * as Blockly from "blockly/core";
|
|
8
|
-
import { Drawer } from "./drawer.js";
|
|
9
|
-
import { RenderInfo } from "./render_info.js";
|
|
10
|
-
import { ConstantProvider } from "./constants.js";
|
|
11
|
-
import { PathObject } from "./path_object.js";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Custom renderer for Scratch-style blocks.
|
|
15
|
-
*/
|
|
16
|
-
export class ScratchRenderer extends Blockly.zelos.Renderer {
|
|
17
|
-
/**
|
|
18
|
-
* Create a new instance of the renderer's drawer.
|
|
19
|
-
*
|
|
20
|
-
* @param {!Blockly.BlockSvg} block The block to render.
|
|
21
|
-
* @param info {!Blockly.blockRendering.RenderInfo} An object containing all
|
|
22
|
-
* information needed to render this block.
|
|
23
|
-
* @returns {!Drawer} The drawer.
|
|
24
|
-
*/
|
|
25
|
-
makeDrawer_(block, info) {
|
|
26
|
-
return new Drawer(block, info);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Create a new instance of the renderer's render info object.
|
|
31
|
-
*
|
|
32
|
-
* @param {!Blockly.BlockSvg} block The block to measure.
|
|
33
|
-
* @returns {!RenderInfo} The render info object.
|
|
34
|
-
*/
|
|
35
|
-
makeRenderInfo_(block) {
|
|
36
|
-
return new RenderInfo(this, block);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Create a new instance of the renderer's constant provider.
|
|
41
|
-
*
|
|
42
|
-
* @returns {!ConstantProvider} The constant provider.
|
|
43
|
-
*/
|
|
44
|
-
makeConstants_() {
|
|
45
|
-
return new ConstantProvider();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Create a new instance of a renderer path object.
|
|
50
|
-
*
|
|
51
|
-
* @param {!SVGElement} root The root SVG element.
|
|
52
|
-
* @param {!Blockly.BlockStyle} style The style object to use for colouring.
|
|
53
|
-
* @returns {!PathObject} The renderer path object.
|
|
54
|
-
*/
|
|
55
|
-
makePathObject(root, style) {
|
|
56
|
-
return new PathObject(root, style, this.getConstants());
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Determine whether or not to highlight a connection.
|
|
61
|
-
*
|
|
62
|
-
* @param {!Blockly.RenderedConnection} connection The connection to determine
|
|
63
|
-
* whether or not to highlight.
|
|
64
|
-
* @returns {boolean} True if we should highlight the connection.
|
|
65
|
-
*/
|
|
66
|
-
shouldHighlightConnection(connection) {
|
|
67
|
-
return (
|
|
68
|
-
connection.type === Blockly.ConnectionType.INPUT_VALUE &&
|
|
69
|
-
connection.getCheck()?.includes("Boolean")
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
Blockly.blockRendering.register("scratch", ScratchRenderer);
|