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.
Files changed (85) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/main.js +1 -1
  3. package/dist/main.js.LICENSE.txt +0 -12
  4. package/package.json +4 -4
  5. package/src/{block_reporting.js → block_reporting.ts} +7 -5
  6. package/src/blocks/{colour.js → colour.ts} +6 -6
  7. package/src/blocks/{control.js → control.ts} +21 -54
  8. package/src/blocks/{data.js → data.ts} +134 -142
  9. package/src/blocks/{event.js → event.ts} +12 -33
  10. package/src/blocks/{looks.js → looks.ts} +24 -73
  11. package/src/blocks/{math.js → math.ts} +6 -11
  12. package/src/blocks/{matrix.js → matrix.ts} +2 -3
  13. package/src/blocks/{motion.js → motion.ts} +23 -70
  14. package/src/blocks/{note.js → note.ts} +2 -3
  15. package/src/blocks/{operators.js → operators.ts} +18 -55
  16. package/src/blocks/{procedures.js → procedures.ts} +418 -269
  17. package/src/blocks/{sensing.js → sensing.ts} +21 -61
  18. package/src/blocks/{sound.js → sound.ts} +9 -28
  19. package/src/blocks/{text.js → text.ts} +1 -2
  20. package/src/blocks/{vertical_extensions.js → vertical_extensions.ts} +63 -100
  21. package/src/checkable_continuous_flyout.ts +112 -0
  22. package/src/{checkbox_bubble.js → checkbox_bubble.ts} +40 -58
  23. package/src/{colours.js → colours.ts} +11 -4
  24. package/src/{constants.js → constants.ts} +13 -0
  25. package/src/{context_menu_items.js → context_menu_items.ts} +18 -12
  26. package/src/{css.js → css.ts} +13 -7
  27. package/src/{data_category.js → data_category.ts} +216 -150
  28. package/src/events/{events_block_comment_base.js → events_block_comment_base.ts} +23 -4
  29. package/src/events/{events_block_comment_change.js → events_block_comment_change.ts} +29 -5
  30. package/src/events/{events_block_comment_collapse.js → events_block_comment_collapse.ts} +24 -6
  31. package/src/events/{events_block_comment_create.js → events_block_comment_create.ts} +36 -10
  32. package/src/events/{events_block_comment_delete.js → events_block_comment_delete.ts} +6 -2
  33. package/src/events/{events_block_comment_move.js → events_block_comment_move.ts} +36 -6
  34. package/src/events/events_block_comment_resize.ts +88 -0
  35. package/src/events/events_block_drag_end.ts +49 -0
  36. package/src/events/events_block_drag_outside.ts +44 -0
  37. package/src/events/{events_scratch_variable_create.js → events_scratch_variable_create.ts} +28 -15
  38. package/src/fields/{field_colour_slider.js → field_colour_slider.ts} +117 -106
  39. package/src/fields/{field_matrix.js → field_matrix.ts} +189 -215
  40. package/src/fields/{field_note.js → field_note.ts} +227 -286
  41. package/src/fields/{field_textinput_removable.js → field_textinput_removable.ts} +17 -20
  42. package/src/fields/{field_variable_getter.js → field_variable_getter.ts} +28 -17
  43. package/src/fields/{field_vertical_separator.js → field_vertical_separator.ts} +14 -30
  44. package/src/fields/{field_angle.js → scratch_field_angle.ts} +124 -80
  45. package/src/fields/{field_dropdown.js → scratch_field_dropdown.ts} +9 -7
  46. package/src/fields/{field_number.js → scratch_field_number.ts} +60 -55
  47. package/src/fields/{field_variable.js → scratch_field_variable.ts} +46 -27
  48. package/src/{flyout_checkbox_icon.js → flyout_checkbox_icon.ts} +15 -19
  49. package/src/{glows.js → glows.ts} +29 -18
  50. package/src/index.ts +62 -63
  51. package/src/procedures.ts +462 -0
  52. package/src/recyclable_block_flyout_inflater.ts +51 -0
  53. package/src/renderer/{bowler_hat.js → bowler_hat.ts} +1 -1
  54. package/src/renderer/{constants.js → constants.ts} +26 -12
  55. package/src/renderer/{drawer.js → drawer.ts} +8 -3
  56. package/src/renderer/{path_object.js → path_object.ts} +2 -2
  57. package/src/renderer/{render_info.js → render_info.ts} +19 -7
  58. package/src/renderer/renderer.ts +76 -0
  59. package/src/{scratch_block_paster.js → scratch_block_paster.ts} +9 -7
  60. package/src/scratch_blocks_utils.ts +39 -0
  61. package/src/{scratch_comment_icon.js → scratch_comment_icon.ts} +43 -26
  62. package/src/scratch_connection_checker.ts +44 -0
  63. package/src/{scratch_continuous_category.js → scratch_continuous_category.ts} +20 -13
  64. package/src/scratch_continuous_toolbox.ts +70 -0
  65. package/src/{scratch_dragger.js → scratch_dragger.ts} +97 -28
  66. package/src/{scratch_variable_map.js → scratch_variable_map.ts} +4 -1
  67. package/src/scratch_variable_model.ts +30 -0
  68. package/src/{shadows.js → shadows.ts} +8 -4
  69. package/src/{status_indicator_label.js → status_indicator_label.ts} +24 -36
  70. package/src/{status_indicator_label_flyout_inflater.js → status_indicator_label_flyout_inflater.ts} +13 -9
  71. package/src/{variables.js → variables.ts} +153 -123
  72. package/tsconfig.json +5 -0
  73. package/src/categories.js +0 -15
  74. package/src/checkable_continuous_flyout.js +0 -138
  75. package/src/events/events_block_comment_resize.js +0 -52
  76. package/src/events/events_block_drag_end.js +0 -33
  77. package/src/events/events_block_drag_outside.js +0 -30
  78. package/src/procedures.js +0 -425
  79. package/src/recyclable_block_flyout_inflater.js +0 -194
  80. package/src/renderer/renderer.js +0 -74
  81. package/src/scratch_blocks_utils.js +0 -148
  82. package/src/scratch_connection_checker.js +0 -29
  83. package/src/scratch_continuous_toolbox.js +0 -78
  84. package/src/scratch_variable_model.js +0 -24
  85. /package/{continuous-toolbox.d.ts → types/continuous-toolbox.d.ts} +0 -0
package/src/index.ts CHANGED
@@ -5,94 +5,92 @@
5
5
  */
6
6
 
7
7
  import * as Blockly from "blockly/core";
8
- import "./blocks/colour.js";
9
- import "./blocks/math.js";
10
- import "./blocks/matrix.js";
11
- import "./blocks/note.js";
12
- import "./blocks/text.js";
13
- import "./blocks/vertical_extensions.js";
14
- import "./blocks/control.js";
15
- import "./blocks/data.js";
16
- import "./blocks/event.js";
17
- import "./blocks/looks.js";
18
- import "./blocks/motion.js";
19
- import "./blocks/operators.js";
20
- import "./blocks/procedures.js";
21
- import "./blocks/sensing.js";
22
- import "./blocks/sound.js";
23
- import * as scratchBlocksUtils from "./scratch_blocks_utils.js";
24
- import * as ScratchVariables from "./variables.js";
25
- import "./css.js";
26
- import "./renderer/renderer.js";
27
- import * as contextMenuItems from "./context_menu_items.js";
8
+ import "./blocks/colour";
9
+ import "./blocks/math";
10
+ import "./blocks/matrix";
11
+ import "./blocks/note";
12
+ import "./blocks/text";
13
+ import "./blocks/vertical_extensions";
14
+ import "./blocks/control";
15
+ import "./blocks/data";
16
+ import "./blocks/event";
17
+ import "./blocks/looks";
18
+ import "./blocks/motion";
19
+ import "./blocks/operators";
20
+ import "./blocks/procedures";
21
+ import "./blocks/sensing";
22
+ import "./blocks/sound";
23
+ import * as scratchBlocksUtils from "./scratch_blocks_utils";
24
+ import * as ScratchVariables from "./variables";
25
+ import "./css";
26
+ import "./renderer/renderer";
27
+ import * as contextMenuItems from "./context_menu_items";
28
28
  import {
29
- ContinuousToolbox,
30
- ContinuousFlyout,
29
+ registerContinuousToolbox,
31
30
  ContinuousMetrics,
32
31
  } from "@blockly/continuous-toolbox";
33
- import { CheckableContinuousFlyout } from "./checkable_continuous_flyout.js";
34
- import { buildGlowFilter, glowStack } from "./glows.js";
35
- import { ScratchContinuousToolbox } from "./scratch_continuous_toolbox.js";
36
- import "./scratch_comment_icon.js";
37
- import "./scratch_dragger.js";
38
- import "./scratch_variable_map.js";
39
- import "./scratch_variable_model.js";
40
- import "./scratch_connection_checker.js";
41
- import "./flyout_checkbox_icon.js";
42
- import "./events/events_block_comment_change.js";
43
- import "./events/events_block_comment_collapse.js";
44
- import "./events/events_block_comment_create.js";
45
- import "./events/events_block_comment_delete.js";
46
- import "./events/events_block_comment_move.js";
47
- import "./events/events_block_comment_resize.js";
48
- import "./events/events_scratch_variable_create.js";
49
- import { buildShadowFilter } from "./shadows.js";
50
- import { registerFieldAngle } from "./fields/field_angle.js";
32
+ import { CheckableContinuousFlyout } from "./checkable_continuous_flyout";
33
+ import { buildGlowFilter, glowStack } from "./glows";
34
+ import { ScratchContinuousToolbox } from "./scratch_continuous_toolbox";
35
+ import "./scratch_comment_icon";
36
+ import "./scratch_dragger";
37
+ import "./scratch_variable_map";
38
+ import "./scratch_variable_model";
39
+ import "./scratch_connection_checker";
40
+ import "./flyout_checkbox_icon";
41
+ import "./events/events_block_comment_change";
42
+ import "./events/events_block_comment_collapse";
43
+ import "./events/events_block_comment_create";
44
+ import "./events/events_block_comment_delete";
45
+ import "./events/events_block_comment_move";
46
+ import "./events/events_block_comment_resize";
47
+ import "./events/events_scratch_variable_create";
48
+ import { buildShadowFilter } from "./shadows";
49
+ import { registerScratchFieldAngle } from "./fields/scratch_field_angle";
51
50
  import {
52
51
  registerFieldColourSlider,
53
52
  FieldColourSlider,
54
- } from "./fields/field_colour_slider.js";
55
- import { registerFieldDropdown } from "./fields/field_dropdown.js";
56
- import { registerFieldMatrix } from "./fields/field_matrix.js";
57
- import { registerFieldNote, FieldNote } from "./fields/field_note.js";
58
- import { registerFieldNumber } from "./fields/field_number.js";
59
- import { registerFieldTextInputRemovable } from "./fields/field_textinput_removable.js";
60
- import { registerFieldVariableGetter } from "./fields/field_variable_getter.js";
61
- import { registerFieldVariable } from "./fields/field_variable.js";
62
- import { registerFieldVerticalSeparator } from "./fields/field_vertical_separator.js";
63
- import { registerRecyclableBlockFlyoutInflater } from "./recyclable_block_flyout_inflater.js";
64
- import { registerScratchBlockPaster } from "./scratch_block_paster.js";
65
- import { registerStatusIndicatorLabelFlyoutInflater } from "./status_indicator_label_flyout_inflater.js";
66
- import { registerScratchContinuousCategory } from "./scratch_continuous_category.js";
53
+ } from "./fields/field_colour_slider";
54
+ import { registerScratchFieldDropdown } from "./fields/scratch_field_dropdown";
55
+ import { registerFieldMatrix } from "./fields/field_matrix";
56
+ import { registerFieldNote, FieldNote } from "./fields/field_note";
57
+ import { registerScratchFieldNumber } from "./fields/scratch_field_number";
58
+ import { registerFieldTextInputRemovable } from "./fields/field_textinput_removable";
59
+ import { registerFieldVariableGetter } from "./fields/field_variable_getter";
60
+ import { registerScratchFieldVariable } from "./fields/scratch_field_variable";
61
+ import { registerFieldVerticalSeparator } from "./fields/field_vertical_separator";
62
+ import { registerRecyclableBlockFlyoutInflater } from "./recyclable_block_flyout_inflater";
63
+ import { registerScratchBlockPaster } from "./scratch_block_paster";
64
+ import { registerStatusIndicatorLabelFlyoutInflater } from "./status_indicator_label_flyout_inflater";
65
+ import { registerScratchContinuousCategory } from "./scratch_continuous_category";
67
66
 
68
67
  export * from "blockly/core";
69
- export * from "./block_reporting.js";
70
- export * from "./categories.js";
71
- export * from "./procedures.js";
68
+ export * from "./block_reporting";
69
+ export * from "./procedures";
72
70
  export * from "../msg/scratch_msgs.js";
73
- export * from "./constants.js";
71
+ export * from "./constants";
74
72
  export { glowStack };
75
73
  export { scratchBlocksUtils };
76
74
  export { CheckableContinuousFlyout };
77
75
  export { ScratchVariables };
78
76
  export { contextMenuItems };
79
77
  export { FieldColourSlider, FieldNote };
80
- export { CheckboxBubble } from "./checkbox_bubble.js";
78
+ export { CheckboxBubble } from "./checkbox_bubble";
81
79
  export {
82
80
  StatusIndicatorLabel,
83
81
  StatusButtonState,
84
- } from "./status_indicator_label.js";
82
+ } from "./status_indicator_label";
85
83
 
86
84
  export function inject(container: Element, options: Blockly.BlocklyOptions) {
87
- registerFieldAngle();
85
+ registerScratchFieldAngle();
88
86
  registerFieldColourSlider();
89
- registerFieldDropdown();
87
+ registerScratchFieldDropdown();
90
88
  registerFieldMatrix();
91
89
  registerFieldNote();
92
- registerFieldNumber();
90
+ registerScratchFieldNumber();
93
91
  registerFieldTextInputRemovable();
94
92
  registerFieldVariableGetter();
95
- registerFieldVariable();
93
+ registerScratchFieldVariable();
96
94
  registerFieldVerticalSeparator();
97
95
  registerRecyclableBlockFlyoutInflater();
98
96
  registerScratchBlockPaster();
@@ -121,6 +119,7 @@ export function inject(container: Element, options: Blockly.BlocklyOptions) {
121
119
  return workspace;
122
120
  }
123
121
 
122
+ registerContinuousToolbox();
124
123
  Blockly.Scrollbar.scrollbarThickness = Blockly.Touch.TOUCH_ENABLED ? 14 : 11;
125
124
  Blockly.FlyoutButton.TEXT_MARGIN_X = 40;
126
125
  Blockly.FlyoutButton.TEXT_MARGIN_Y = 10;
@@ -0,0 +1,462 @@
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";
28
+ import * as scratchBlocksUtils from "../src/scratch_blocks_utils";
29
+
30
+ /**
31
+ * Find all user-created procedure definition mutations in a workspace.
32
+ * @param root Root workspace.
33
+ * @return Array of mutation xml elements.
34
+ */
35
+ function allProcedureMutations(root: Blockly.WorkspaceSvg): Element[] {
36
+ const blocks = root.getAllBlocks();
37
+ return blocks
38
+ .filter((b) => b.type === Constants.PROCEDURES_PROTOTYPE_BLOCK_TYPE)
39
+ .map((b) => b.mutationToDom(/* opt_generateShadows */ true));
40
+ }
41
+
42
+ /**
43
+ * Sorts an array of procedure definition mutations alphabetically.
44
+ * (Does not mutate the given array.)
45
+ * @param mutations Array of mutation xml elements.
46
+ * @return Sorted array of mutation xml elements.
47
+ */
48
+ function sortProcedureMutations(mutations: Element[]): Element[] {
49
+ return mutations.slice().sort(function (a, b) {
50
+ const procCodeA = a.getAttribute("proccode");
51
+ const procCodeB = b.getAttribute("proccode");
52
+
53
+ return scratchBlocksUtils.compareStrings(procCodeA, procCodeB);
54
+ });
55
+ }
56
+
57
+ /**
58
+ * Construct the blocks required by the flyout for the procedure category.
59
+ * @param workspace The workspace containing procedures.
60
+ * @return Array of XML block elements.
61
+ */
62
+ function getProceduresCategory(workspace: Blockly.WorkspaceSvg): Element[] {
63
+ var xmlList: Element[] = [];
64
+
65
+ addCreateButton(workspace, xmlList);
66
+
67
+ // Create call blocks for each procedure defined in the workspace
68
+ const mutations = sortProcedureMutations(allProcedureMutations(workspace));
69
+ for (const mutation of mutations) {
70
+ // <block type="procedures_call">
71
+ // <mutation ...></mutation>
72
+ // </block>
73
+ const block = document.createElement("block");
74
+ block.setAttribute("type", "procedures_call");
75
+ block.setAttribute("gap", "16");
76
+ block.appendChild(mutation);
77
+ xmlList.push(block);
78
+ }
79
+ return xmlList;
80
+ }
81
+
82
+ /**
83
+ * Create the "Make a Block..." button.
84
+ * @param workspace The workspace containing procedures.
85
+ * @param xmlList Array of XML block elements to add to.
86
+ */
87
+ function addCreateButton(workspace: Blockly.WorkspaceSvg, xmlList: Element[]) {
88
+ const button = document.createElement("button");
89
+ const msg = Blockly.Msg.NEW_PROCEDURE;
90
+ const callbackKey = "CREATE_PROCEDURE";
91
+ const callback = function () {
92
+ // Run the callback after a delay to avoid it getting captured by the React
93
+ // modal in scratch-gui and being registered as a click on the scrim that
94
+ // dismisses the dialog.
95
+ requestAnimationFrame(() => {
96
+ setTimeout(() => {
97
+ createProcedureDefCallback(workspace);
98
+ });
99
+ });
100
+ };
101
+ button.setAttribute("text", msg);
102
+ button.setAttribute("callbackKey", callbackKey);
103
+ workspace.registerButtonCallback(callbackKey, callback);
104
+ xmlList.push(button);
105
+ }
106
+
107
+ /**
108
+ * Find all callers of a named procedure.
109
+ * @param name Name of procedure (procCode in scratch-blocks).
110
+ * @param workspace The workspace to find callers in.
111
+ * @param definitionRoot The root of the stack where the
112
+ * procedure is defined.
113
+ * @param allowRecursive True if the search should include recursive
114
+ * procedure calls. False if the search should ignore the stack starting
115
+ * with definitionRoot.
116
+ * @return Array of caller blocks.
117
+ */
118
+ export function getCallers(
119
+ name: string,
120
+ workspace: Blockly.WorkspaceSvg,
121
+ definitionRoot: Blockly.BlockSvg,
122
+ allowRecursive: boolean
123
+ ): Blockly.BlockSvg[] {
124
+ return workspace.getTopBlocks().flatMap((block) => {
125
+ if (block.id === definitionRoot.id && !allowRecursive) {
126
+ return [];
127
+ }
128
+
129
+ return block.getDescendants(false).filter((descendant) => {
130
+ return (
131
+ isProcedureBlock(descendant) &&
132
+ descendant.type === Constants.PROCEDURES_CALL_BLOCK_TYPE &&
133
+ descendant.getProcCode() === name
134
+ );
135
+ });
136
+ });
137
+ }
138
+
139
+ /**
140
+ * Find and edit all callers with a procCode using a new mutation.
141
+ * @param name Name of procedure (procCode in scratch-blocks).
142
+ * @param workspace The workspace to find callers in.
143
+ * @param mutation New mutation for the callers.
144
+ */
145
+ function mutateCallersAndPrototype(
146
+ name: string,
147
+ workspace: Blockly.WorkspaceSvg,
148
+ mutation: Element
149
+ ) {
150
+ const defineBlock = getDefineBlock(name, workspace);
151
+ const prototypeBlock = getPrototypeBlock(name, workspace);
152
+ if (!(defineBlock && prototypeBlock)) {
153
+ alert("No define block on workspace"); // TODO decide what to do about this.
154
+ return;
155
+ }
156
+
157
+ const callers = getCallers(
158
+ name,
159
+ defineBlock.workspace,
160
+ defineBlock,
161
+ true /* allowRecursive */
162
+ );
163
+ callers.push(prototypeBlock);
164
+ Blockly.Events.setGroup(true);
165
+ callers.forEach((caller) => {
166
+ const oldMutationDom = caller.mutationToDom();
167
+ const oldMutation = oldMutationDom && Blockly.Xml.domToText(oldMutationDom);
168
+ caller.domToMutation(mutation);
169
+ const newMutationDom = caller.mutationToDom();
170
+ const newMutation = newMutationDom && Blockly.Xml.domToText(newMutationDom);
171
+ if (oldMutation !== newMutation) {
172
+ Blockly.Events.fire(
173
+ new (Blockly.Events.get(Blockly.Events.BLOCK_CHANGE))(
174
+ caller,
175
+ "mutation",
176
+ null,
177
+ oldMutation,
178
+ newMutation
179
+ )
180
+ );
181
+ }
182
+ });
183
+ Blockly.Events.setGroup(false);
184
+ }
185
+
186
+ /**
187
+ * Find the definition block for the named procedure.
188
+ * @param procCode The identifier of the procedure.
189
+ * @param workspace The workspace to search.
190
+ * @return The procedure definition block, or undefined if not found.
191
+ */
192
+ function getDefineBlock(
193
+ procCode: string,
194
+ workspace: Blockly.WorkspaceSvg
195
+ ): Blockly.BlockSvg | undefined {
196
+ // Assume that a procedure definition is a top block.
197
+ return workspace.getTopBlocks(false).find((block) => {
198
+ if (block.type === Constants.PROCEDURES_DEFINITION_BLOCK_TYPE) {
199
+ const prototypeBlock = block
200
+ .getInput("custom_block")
201
+ .connection.targetBlock() as Blockly.BlockSvg;
202
+ return (
203
+ isProcedureBlock(prototypeBlock) &&
204
+ prototypeBlock.getProcCode() === procCode
205
+ );
206
+ }
207
+
208
+ return false;
209
+ });
210
+ }
211
+
212
+ /**
213
+ * Find the prototype block for the named procedure.
214
+ * @param procCode The identifier of the procedure.
215
+ * @param workspace The workspace to search.
216
+ * @return The procedure prototype block, or undefined if not found.
217
+ */
218
+ function getPrototypeBlock(
219
+ procCode: string,
220
+ workspace: Blockly.WorkspaceSvg
221
+ ): Blockly.BlockSvg | undefined {
222
+ const defineBlock = getDefineBlock(procCode, workspace);
223
+ if (defineBlock) {
224
+ return defineBlock
225
+ .getInput("custom_block")
226
+ .connection.targetBlock() as Blockly.BlockSvg;
227
+ }
228
+ return undefined;
229
+ }
230
+
231
+ /**
232
+ * Create a mutation for a brand new custom procedure.
233
+ * @return The mutation for a new custom procedure
234
+ */
235
+ function newProcedureMutation(): Element {
236
+ const mutationText = `
237
+ <xml>
238
+ <mutation
239
+ proccode="${Blockly.Msg["PROCEDURE_DEFAULT_NAME"]}"
240
+ argumentids="[]"
241
+ argumentnames="[]"
242
+ argumentdefaults="[]"
243
+ warp="false">
244
+ </mutation>
245
+ </xml>`;
246
+ return Blockly.utils.xml.textToDom(mutationText).firstElementChild;
247
+ }
248
+
249
+ /**
250
+ * Callback to create a new procedure custom command block.
251
+ * @param workspace The workspace to create the new procedure on.
252
+ */
253
+ function createProcedureDefCallback(workspace: Blockly.WorkspaceSvg) {
254
+ ScratchProcedures.externalProcedureDefCallback(
255
+ newProcedureMutation(),
256
+ createProcedureCallbackFactory(workspace)
257
+ );
258
+ }
259
+
260
+ /**
261
+ * Callback factory for adding a new custom procedure from a mutation.
262
+ * @param workspace The workspace to create the new procedure on.
263
+ * @return callback for creating the new custom procedure.
264
+ */
265
+ function createProcedureCallbackFactory(
266
+ workspace: Blockly.WorkspaceSvg
267
+ ): (mutation?: Element) => void {
268
+ return (mutation?: Element) => {
269
+ if (!mutation) return;
270
+
271
+ const blockText = `
272
+ <xml>
273
+ <block type="procedures_definition">
274
+ <statement name="custom_block">
275
+ <shadow type="procedures_prototype">
276
+ ${Blockly.Xml.domToText(mutation)}
277
+ </shadow>
278
+ </statement>
279
+ </block>
280
+ </xml>`;
281
+ const blockDom = Blockly.utils.xml.textToDom(blockText).firstElementChild;
282
+ Blockly.Events.setGroup(true);
283
+ const block = Blockly.Xml.domToBlock(
284
+ blockDom,
285
+ workspace
286
+ ) as Blockly.BlockSvg;
287
+ Blockly.renderManagement.finishQueuedRenders().then(() => {
288
+ // To convert from pixel units to workspace units
289
+ const scale = workspace.scale;
290
+ // Position the block so that it is at the top left of the visible
291
+ // workspace, padded from the edge by 30 units. Position in the top right
292
+ // if RTL.
293
+ let posX = -workspace.scrollX;
294
+ if (workspace.RTL) {
295
+ posX += workspace.getMetrics().contentWidth - 30;
296
+ } else {
297
+ posX += 30;
298
+ }
299
+ block.moveBy(posX / scale, (-workspace.scrollY + 30) / scale);
300
+ block.scheduleSnapAndBump();
301
+ Blockly.Events.setGroup(false);
302
+ });
303
+ };
304
+ }
305
+
306
+ /**
307
+ * Callback to open the modal for editing custom procedures.
308
+ * @param block The block that was right-clicked.
309
+ */
310
+ function editProcedureCallback(block: Blockly.BlockSvg) {
311
+ // Edit can come from one of three block types (call, define, prototype)
312
+ // Normalize by setting the block to the prototype block for the procedure.
313
+ let prototypeBlock: Blockly.BlockSvg;
314
+ if (block.type === Constants.PROCEDURES_DEFINITION_BLOCK_TYPE) {
315
+ const input = block.getInput("custom_block");
316
+ if (!input) {
317
+ alert("Bad input"); // TODO: Decide what to do about this.
318
+ return;
319
+ }
320
+ const conn = input.connection;
321
+ if (!conn) {
322
+ alert("Bad connection"); // TODO: Decide what to do about this.
323
+ return;
324
+ }
325
+ const innerBlock = conn.targetBlock();
326
+ if (
327
+ !innerBlock ||
328
+ innerBlock.type !== Constants.PROCEDURES_PROTOTYPE_BLOCK_TYPE
329
+ ) {
330
+ alert("Bad inner block"); // TODO: Decide what to do about this.
331
+ return;
332
+ }
333
+ prototypeBlock = innerBlock as Blockly.BlockSvg;
334
+ } else if (
335
+ block.type === Constants.PROCEDURES_CALL_BLOCK_TYPE &&
336
+ isProcedureBlock(block)
337
+ ) {
338
+ // This is a call block, find the prototype corresponding to the procCode.
339
+ // Make sure to search the correct workspace, call block can be in flyout.
340
+ const workspaceToSearch = block.workspace.isFlyout
341
+ ? block.workspace.targetWorkspace
342
+ : block.workspace;
343
+ prototypeBlock = getPrototypeBlock(block.getProcCode(), workspaceToSearch);
344
+ } else {
345
+ prototypeBlock = block;
346
+ }
347
+ // Block now refers to the procedure prototype block, it is safe to proceed.
348
+ ScratchProcedures.externalProcedureDefCallback(
349
+ prototypeBlock.mutationToDom(),
350
+ editProcedureCallbackFactory(prototypeBlock)
351
+ );
352
+ }
353
+
354
+ /**
355
+ * Callback factory for editing an existing custom procedure.
356
+ * @param block The procedure prototype block being edited.
357
+ * @return Callback for editing the custom procedure.
358
+ */
359
+ function editProcedureCallbackFactory(
360
+ block: Blockly.BlockSvg
361
+ ): (mutation?: Element) => void {
362
+ return (mutation?: Element) => {
363
+ if (mutation && isProcedureBlock(block)) {
364
+ mutateCallersAndPrototype(block.getProcCode(), block.workspace, mutation);
365
+ }
366
+ };
367
+ }
368
+
369
+ /**
370
+ * Make a context menu option for editing a custom procedure.
371
+ * This appears in the context menu for procedure definitions and procedure
372
+ * calls.
373
+ * @param block The block where the right-click originated.
374
+ * @return A menu option, containing text, enabled, and a callback.
375
+ */
376
+ function makeEditOption(
377
+ block: Blockly.BlockSvg
378
+ ): Blockly.ContextMenuRegistry.ContextMenuOption {
379
+ return {
380
+ enabled: true,
381
+ text: Blockly.Msg.EDIT_PROCEDURE,
382
+ callback: () => {
383
+ editProcedureCallback(block);
384
+ },
385
+ scope: block,
386
+ weight: 7,
387
+ };
388
+ }
389
+
390
+ /**
391
+ * Callback to try to delete a custom block definitions.
392
+ * @param procCode The identifier of the procedure to delete.
393
+ * @param definitionRoot The root block of the stack that defines the custom
394
+ * procedure.
395
+ * @return True if the custom procedure was deleted, false otherwise.
396
+ */
397
+ function deleteProcedureDefCallback(
398
+ procCode: string,
399
+ definitionRoot: Blockly.BlockSvg
400
+ ): boolean {
401
+ const callers = getCallers(
402
+ procCode,
403
+ definitionRoot.workspace,
404
+ definitionRoot,
405
+ false /* allowRecursive */
406
+ );
407
+ if (callers.length > 0) {
408
+ return false;
409
+ }
410
+
411
+ const workspace = definitionRoot.workspace;
412
+ // Bypass the checkAndDelete provided by the procedure block mixin
413
+ Blockly.BlockSvg.prototype.checkAndDelete.call(definitionRoot);
414
+ return true;
415
+ }
416
+
417
+ /**
418
+ * Returns whether the given block is a procedure block and narrows its type.
419
+ *
420
+ * @param block The block to check.
421
+ * @returns True if the block is a procedure block, otherwise false.
422
+ */
423
+ export function isProcedureBlock(
424
+ block: Blockly.BlockSvg
425
+ ): block is ProcedureBlock {
426
+ return (
427
+ block.type === Constants.PROCEDURES_CALL_BLOCK_TYPE ||
428
+ block.type === Constants.PROCEDURES_DECLARATION_BLOCK_TYPE ||
429
+ block.type === Constants.PROCEDURES_PROTOTYPE_BLOCK_TYPE
430
+ );
431
+ }
432
+
433
+ /**
434
+ * Interface for procedure blocks, which have the getProcCode method added
435
+ * through an extension.
436
+ */
437
+ interface ProcedureBlock extends Blockly.BlockSvg {
438
+ getProcCode(): string;
439
+ }
440
+
441
+ /**
442
+ * Type for a callback function invoked after a procedure is modified.
443
+ */
444
+ type ProcedureDefCallback = (
445
+ mutation: Element,
446
+ postEditCallback: (mutation?: Element) => void
447
+ ) => void;
448
+
449
+ const ScratchProcedures: {
450
+ externalProcedureDefCallback: ProcedureDefCallback | undefined;
451
+ createProcedureDefCallback: typeof createProcedureDefCallback;
452
+ deleteProcedureDefCallback: typeof deleteProcedureDefCallback;
453
+ getProceduresCategory: typeof getProceduresCategory;
454
+ makeEditOption: typeof makeEditOption;
455
+ } = {
456
+ externalProcedureDefCallback: undefined,
457
+ createProcedureDefCallback,
458
+ deleteProcedureDefCallback,
459
+ getProceduresCategory,
460
+ makeEditOption,
461
+ };
462
+ export { ScratchProcedures };
@@ -0,0 +1,51 @@
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";
9
+ import { RecyclableBlockFlyoutInflater as BlocklyRecyclableBlockFlyoutInflater } from "@blockly/continuous-toolbox";
10
+
11
+ /**
12
+ * A block inflater that caches and reuses blocks to improve performance.
13
+ */
14
+ export class RecyclableBlockFlyoutInflater extends BlocklyRecyclableBlockFlyoutInflater {
15
+ /**
16
+ * Creates a block on the flyout workspace from the given block definition.
17
+ *
18
+ * @param state A JSON representation of a block to load.
19
+ * @param flyoutWorkspace The flyout's workspace.
20
+ * @returns The newly created block.
21
+ */
22
+ load(
23
+ state: Blockly.utils.toolbox.BlockInfo,
24
+ flyoutWorkspace: Blockly.WorkspaceSvg
25
+ ): Blockly.FlyoutItem {
26
+ const flyoutItem = super.load(state, flyoutWorkspace);
27
+ const block = flyoutItem.getElement();
28
+ if ("checkboxInFlyout" in block && block.checkboxInFlyout) {
29
+ block.moveBy(
30
+ (flyoutWorkspace.RTL ? -1 : 1) *
31
+ (CheckboxBubble.CHECKBOX_SIZE + CheckboxBubble.CHECKBOX_MARGIN),
32
+ 0
33
+ );
34
+ }
35
+
36
+ return flyoutItem;
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Registers the recyclable block flyout inflater, replacing the standard
42
+ * block flyout inflater.
43
+ */
44
+ export function registerRecyclableBlockFlyoutInflater() {
45
+ Blockly.registry.unregister(Blockly.registry.Type.FLYOUT_INFLATER, "block");
46
+ Blockly.registry.register(
47
+ Blockly.registry.Type.FLYOUT_INFLATER,
48
+ "block",
49
+ RecyclableBlockFlyoutInflater
50
+ );
51
+ }
@@ -7,7 +7,7 @@
7
7
  import * as Blockly from "blockly/core";
8
8
 
9
9
  export class BowlerHat extends Blockly.blockRendering.Hat {
10
- constructor(constants) {
10
+ constructor(constants: Blockly.blockRendering.ConstantProvider) {
11
11
  super(constants);
12
12
  // Calculated dynamically by computeBounds_().
13
13
  this.width = 0;