scratch-blocks 2.0.0-spork.2 → 2.0.0-spork.3

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 (81) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/main.js +1 -1
  3. package/package.json +1 -1
  4. package/src/{block_reporting.js → block_reporting.ts} +7 -5
  5. package/src/blocks/{colour.js → colour.ts} +6 -6
  6. package/src/blocks/{control.js → control.ts} +21 -54
  7. package/src/blocks/{data.js → data.ts} +134 -142
  8. package/src/blocks/{event.js → event.ts} +12 -33
  9. package/src/blocks/{looks.js → looks.ts} +24 -73
  10. package/src/blocks/{math.js → math.ts} +6 -11
  11. package/src/blocks/{matrix.js → matrix.ts} +2 -3
  12. package/src/blocks/{motion.js → motion.ts} +23 -70
  13. package/src/blocks/{note.js → note.ts} +2 -3
  14. package/src/blocks/{operators.js → operators.ts} +18 -55
  15. package/src/blocks/{procedures.js → procedures.ts} +418 -269
  16. package/src/blocks/{sensing.js → sensing.ts} +21 -61
  17. package/src/blocks/{sound.js → sound.ts} +9 -28
  18. package/src/blocks/{text.js → text.ts} +1 -2
  19. package/src/blocks/{vertical_extensions.js → vertical_extensions.ts} +63 -100
  20. package/src/checkable_continuous_flyout.js +2 -2
  21. package/src/{checkbox_bubble.js → checkbox_bubble.ts} +36 -53
  22. package/src/{colours.js → colours.ts} +11 -4
  23. package/src/{constants.js → constants.ts} +13 -0
  24. package/src/{context_menu_items.js → context_menu_items.ts} +18 -12
  25. package/src/{data_category.js → data_category.ts} +216 -150
  26. package/src/events/{events_block_comment_base.js → events_block_comment_base.ts} +23 -4
  27. package/src/events/{events_block_comment_change.js → events_block_comment_change.ts} +29 -5
  28. package/src/events/{events_block_comment_collapse.js → events_block_comment_collapse.ts} +24 -6
  29. package/src/events/{events_block_comment_create.js → events_block_comment_create.ts} +36 -10
  30. package/src/events/{events_block_comment_delete.js → events_block_comment_delete.ts} +6 -2
  31. package/src/events/{events_block_comment_move.js → events_block_comment_move.ts} +36 -6
  32. package/src/events/events_block_comment_resize.ts +88 -0
  33. package/src/events/events_block_drag_end.ts +49 -0
  34. package/src/events/events_block_drag_outside.ts +44 -0
  35. package/src/events/{events_scratch_variable_create.js → events_scratch_variable_create.ts} +28 -15
  36. package/src/fields/{field_colour_slider.js → field_colour_slider.ts} +117 -106
  37. package/src/fields/{field_matrix.js → field_matrix.ts} +189 -215
  38. package/src/fields/{field_note.js → field_note.ts} +227 -286
  39. package/src/fields/{field_textinput_removable.js → field_textinput_removable.ts} +17 -20
  40. package/src/fields/{field_variable_getter.js → field_variable_getter.ts} +28 -17
  41. package/src/fields/{field_vertical_separator.js → field_vertical_separator.ts} +14 -30
  42. package/src/fields/{field_angle.js → scratch_field_angle.ts} +124 -80
  43. package/src/fields/{field_dropdown.js → scratch_field_dropdown.ts} +9 -7
  44. package/src/fields/{field_number.js → scratch_field_number.ts} +60 -55
  45. package/src/fields/{field_variable.js → scratch_field_variable.ts} +46 -27
  46. package/src/{flyout_checkbox_icon.js → flyout_checkbox_icon.ts} +15 -19
  47. package/src/{glows.js → glows.ts} +29 -18
  48. package/src/index.ts +59 -60
  49. package/src/procedures.ts +462 -0
  50. package/src/{recyclable_block_flyout_inflater.js → recyclable_block_flyout_inflater.ts} +35 -35
  51. package/src/renderer/{bowler_hat.js → bowler_hat.ts} +1 -1
  52. package/src/renderer/{constants.js → constants.ts} +26 -12
  53. package/src/renderer/{drawer.js → drawer.ts} +8 -3
  54. package/src/renderer/{path_object.js → path_object.ts} +2 -2
  55. package/src/renderer/{render_info.js → render_info.ts} +19 -7
  56. package/src/renderer/renderer.ts +76 -0
  57. package/src/{scratch_block_paster.js → scratch_block_paster.ts} +9 -7
  58. package/src/scratch_blocks_utils.ts +39 -0
  59. package/src/{scratch_comment_icon.js → scratch_comment_icon.ts} +43 -26
  60. package/src/scratch_connection_checker.ts +44 -0
  61. package/src/{scratch_continuous_category.js → scratch_continuous_category.ts} +20 -13
  62. package/src/{scratch_continuous_toolbox.js → scratch_continuous_toolbox.ts} +20 -18
  63. package/src/{scratch_dragger.js → scratch_dragger.ts} +97 -28
  64. package/src/{scratch_variable_map.js → scratch_variable_map.ts} +4 -1
  65. package/src/scratch_variable_model.ts +30 -0
  66. package/src/{shadows.js → shadows.ts} +8 -4
  67. package/src/{status_indicator_label.js → status_indicator_label.ts} +24 -36
  68. package/src/{status_indicator_label_flyout_inflater.js → status_indicator_label_flyout_inflater.ts} +9 -7
  69. package/src/{variables.js → variables.ts} +153 -123
  70. package/tsconfig.json +5 -0
  71. package/src/categories.js +0 -15
  72. package/src/events/events_block_comment_resize.js +0 -52
  73. package/src/events/events_block_drag_end.js +0 -33
  74. package/src/events/events_block_drag_outside.js +0 -30
  75. package/src/procedures.js +0 -425
  76. package/src/renderer/renderer.js +0 -74
  77. package/src/scratch_blocks_utils.js +0 -148
  78. package/src/scratch_connection_checker.js +0 -29
  79. package/src/scratch_variable_model.js +0 -24
  80. /package/src/{css.js → css.ts} +0 -0
  81. /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,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);
@@ -1,148 +0,0 @@
1
- /**
2
- * @license
3
- * Visual Blocks Editor
4
- *
5
- * Copyright 2018 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 methods for Scratch Blocks but not Blockly.
23
- * @author fenichel@google.com (Rachel Fenichel)
24
- */
25
- import * as Blockly from "blockly/core";
26
-
27
- /**
28
- * Re-assign obscured shadow blocks new IDs to prevent collisions
29
- * Scratch specific to help the VM handle deleting obscured shadows.
30
- * @param {Blockly.Block} block the root block to be processed.
31
- * @package
32
- */
33
- export function changeObscuredShadowIds(block) {
34
- var blocks = block.getDescendants(false);
35
- for (var i = blocks.length - 1; i >= 0; i--) {
36
- var descendant = blocks[i];
37
- for (var j = 0; j < descendant.inputList.length; j++) {
38
- var connection = descendant.inputList[j].connection;
39
- if (connection) {
40
- var shadowDom = connection.getShadowDom();
41
- if (shadowDom) {
42
- shadowDom.setAttribute("id", Blockly.utils.genUid());
43
- connection.setShadowDom(shadowDom);
44
- }
45
- }
46
- }
47
- }
48
- }
49
-
50
- /**
51
- * Compare strings with natural number sorting.
52
- * @param {string} str1 First input.
53
- * @param {string} str2 Second input.
54
- * @return {number} -1, 0, or 1 to signify greater than, equality, or less than.
55
- */
56
- export function compareStrings(str1, str2) {
57
- return str1.localeCompare(str2, [], {
58
- sensitivity: "base",
59
- numeric: true,
60
- });
61
- }
62
-
63
- /**
64
- * Creates a callback function for a click on the "duplicate" context menu
65
- * option in Scratch Blocks. The block is duplicated and attached to the mouse,
66
- * which acts as though it were pressed and mid-drag. Clicking the mouse
67
- * releases the new dragging block.
68
- * @param {!Blockly.BlockSvg} oldBlock The block that will be duplicated.
69
- * @param {!Event} event Event that caused the context menu to open.
70
- * @return {Function} A callback function that duplicates the block and starts a
71
- * drag.
72
- * @package
73
- */
74
- export function duplicateAndDragCallback(oldBlock, event) {
75
- var isMouseEvent =
76
- Blockly.Touch.getTouchIdentifierFromEvent(event) === "mouse";
77
- return function (e) {
78
- // Give the context menu a chance to close.
79
- setTimeout(function () {
80
- var ws = oldBlock.workspace;
81
- var svgRootOld = oldBlock.getSvgRoot();
82
- if (!svgRootOld) {
83
- throw new Error("oldBlock is not rendered.");
84
- }
85
-
86
- // Create the new block by cloning the block in the flyout (via XML).
87
- var xml = Blockly.Xml.blockToDom(oldBlock);
88
- // The target workspace would normally resize during domToBlock, which
89
- // will lead to weird jumps.
90
- // Resizing will be enabled when the drag ends.
91
- ws.setResizesEnabled(false);
92
-
93
- // Disable events and manually emit events after the block has been
94
- // positioned and has had its shadow IDs fixed (Scratch-specific).
95
- Blockly.Events.disable();
96
- try {
97
- // Using domToBlock instead of domToWorkspace means that the new block
98
- // will be placed at position (0, 0) in main workspace units.
99
- var newBlock = Blockly.Xml.domToBlock(xml, ws);
100
-
101
- // Scratch-specific: Give shadow dom new IDs to prevent duplicating on paste
102
- changeObscuredShadowIds(newBlock);
103
-
104
- var svgRootNew = newBlock.getSvgRoot();
105
- if (!svgRootNew) {
106
- throw new Error("newBlock is not rendered.");
107
- }
108
-
109
- // The position of the old block in workspace coordinates.
110
- var oldBlockPosWs = oldBlock.getRelativeToSurfaceXY();
111
-
112
- // Place the new block as the same position as the old block.
113
- // TODO: Offset by the difference between the mouse position and the upper
114
- // left corner of the block.
115
- newBlock.moveBy(oldBlockPosWs.x, oldBlockPosWs.y);
116
- if (!isMouseEvent) {
117
- var offsetX = ws.RTL ? -100 : 100;
118
- var offsetY = 100;
119
- newBlock.moveBy(offsetX, offsetY); // Just offset the block for touch.
120
- }
121
- } finally {
122
- Blockly.Events.enable();
123
- }
124
- if (Blockly.Events.isEnabled()) {
125
- Blockly.Events.fire(new Blockly.Events.BlockCreate(newBlock));
126
- }
127
-
128
- if (isMouseEvent) {
129
- // e is not a real mouseEvent/touchEvent/pointerEvent. It's an event
130
- // created by the context menu and has the coordinates of the mouse
131
- // click that opened the context menu.
132
- var fakeEvent = {
133
- clientX: event.clientX,
134
- clientY: event.clientY,
135
- type: "mousedown",
136
- preventDefault: function () {
137
- e.preventDefault();
138
- },
139
- stopPropagation: function () {
140
- e.stopPropagation();
141
- },
142
- target: e.target,
143
- };
144
- ws.startDragWithFakeEvent(fakeEvent, newBlock);
145
- }
146
- }, 0);
147
- };
148
- }
@@ -1,29 +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
-
9
- class ScratchConnectionChecker extends Blockly.ConnectionChecker {
10
- // This check prevents dragging a block into the slot occupied by the
11
- // procedure caller example block in a procedure definition block.
12
- doDragChecks(a, b, distance) {
13
- if (
14
- b.getSourceBlock().type === "procedures_definition" &&
15
- b.getParentInput()?.name === "custom_block"
16
- ) {
17
- return false;
18
- }
19
-
20
- return super.doDragChecks(a, b, distance);
21
- }
22
- }
23
-
24
- Blockly.registry.register(
25
- Blockly.registry.Type.CONNECTION_CHECKER,
26
- Blockly.registry.DEFAULT,
27
- ScratchConnectionChecker,
28
- true
29
- );
@@ -1,24 +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
-
9
- class ScratchVariableModel extends Blockly.VariableModel {
10
- constructor(workspace, name, type, id, isLocal, isCloud) {
11
- super(workspace, name, type, id);
12
- // isLocal and isCloud may not be passed when creating broadcast message
13
- // variables, which conveniently are neither local nor cloud.
14
- this.isLocal = !!isLocal;
15
- this.isCloud = !!isCloud;
16
- }
17
- }
18
-
19
- Blockly.registry.register(
20
- Blockly.registry.Type.VARIABLE_MODEL,
21
- Blockly.registry.DEFAULT,
22
- ScratchVariableModel,
23
- true
24
- );
File without changes