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.
- package/CHANGELOG.md +7 -0
- package/dist/main.js +1 -1
- package/package.json +1 -1
- 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.js +2 -2
- package/src/{checkbox_bubble.js → checkbox_bubble.ts} +36 -53
- 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/{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 +59 -60
- package/src/procedures.ts +462 -0
- package/src/{recyclable_block_flyout_inflater.js → recyclable_block_flyout_inflater.ts} +35 -35
- 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.js → scratch_continuous_toolbox.ts} +20 -18
- 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} +9 -7
- package/src/{variables.js → variables.ts} +153 -123
- package/tsconfig.json +5 -0
- package/src/categories.js +0 -15
- 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/renderer/renderer.js +0 -74
- package/src/scratch_blocks_utils.js +0 -148
- package/src/scratch_connection_checker.js +0 -29
- package/src/scratch_variable_model.js +0 -24
- /package/src/{css.js → css.ts} +0 -0
- /package/{continuous-toolbox.d.ts → types/continuous-toolbox.d.ts} +0 -0
package/src/index.ts
CHANGED
|
@@ -5,94 +5,93 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import * as Blockly from "blockly/core";
|
|
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
|
|
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
29
|
ContinuousToolbox,
|
|
30
30
|
ContinuousFlyout,
|
|
31
31
|
ContinuousMetrics,
|
|
32
32
|
} from "@blockly/continuous-toolbox";
|
|
33
33
|
import { CheckableContinuousFlyout } from "./checkable_continuous_flyout.js";
|
|
34
|
-
import { buildGlowFilter, glowStack } from "./glows
|
|
35
|
-
import { ScratchContinuousToolbox } from "./scratch_continuous_toolbox
|
|
36
|
-
import "./scratch_comment_icon
|
|
37
|
-
import "./scratch_dragger
|
|
38
|
-
import "./scratch_variable_map
|
|
39
|
-
import "./scratch_variable_model
|
|
40
|
-
import "./scratch_connection_checker
|
|
41
|
-
import "./flyout_checkbox_icon
|
|
42
|
-
import "./events/events_block_comment_change
|
|
43
|
-
import "./events/events_block_comment_collapse
|
|
44
|
-
import "./events/events_block_comment_create
|
|
45
|
-
import "./events/events_block_comment_delete
|
|
46
|
-
import "./events/events_block_comment_move
|
|
47
|
-
import "./events/events_block_comment_resize
|
|
48
|
-
import "./events/events_scratch_variable_create
|
|
49
|
-
import { buildShadowFilter } from "./shadows
|
|
50
|
-
import {
|
|
34
|
+
import { buildGlowFilter, glowStack } from "./glows";
|
|
35
|
+
import { ScratchContinuousToolbox } from "./scratch_continuous_toolbox";
|
|
36
|
+
import "./scratch_comment_icon";
|
|
37
|
+
import "./scratch_dragger";
|
|
38
|
+
import "./scratch_variable_map";
|
|
39
|
+
import "./scratch_variable_model";
|
|
40
|
+
import "./scratch_connection_checker";
|
|
41
|
+
import "./flyout_checkbox_icon";
|
|
42
|
+
import "./events/events_block_comment_change";
|
|
43
|
+
import "./events/events_block_comment_collapse";
|
|
44
|
+
import "./events/events_block_comment_create";
|
|
45
|
+
import "./events/events_block_comment_delete";
|
|
46
|
+
import "./events/events_block_comment_move";
|
|
47
|
+
import "./events/events_block_comment_resize";
|
|
48
|
+
import "./events/events_scratch_variable_create";
|
|
49
|
+
import { buildShadowFilter } from "./shadows";
|
|
50
|
+
import { registerScratchFieldAngle } from "./fields/scratch_field_angle";
|
|
51
51
|
import {
|
|
52
52
|
registerFieldColourSlider,
|
|
53
53
|
FieldColourSlider,
|
|
54
|
-
} from "./fields/field_colour_slider
|
|
55
|
-
import {
|
|
56
|
-
import { registerFieldMatrix } from "./fields/field_matrix
|
|
57
|
-
import { registerFieldNote, FieldNote } from "./fields/field_note
|
|
58
|
-
import {
|
|
59
|
-
import { registerFieldTextInputRemovable } from "./fields/field_textinput_removable
|
|
60
|
-
import { registerFieldVariableGetter } from "./fields/field_variable_getter
|
|
61
|
-
import {
|
|
62
|
-
import { registerFieldVerticalSeparator } from "./fields/field_vertical_separator
|
|
63
|
-
import { registerRecyclableBlockFlyoutInflater } from "./recyclable_block_flyout_inflater
|
|
64
|
-
import { registerScratchBlockPaster } from "./scratch_block_paster
|
|
65
|
-
import { registerStatusIndicatorLabelFlyoutInflater } from "./status_indicator_label_flyout_inflater
|
|
66
|
-
import { registerScratchContinuousCategory } from "./scratch_continuous_category
|
|
54
|
+
} from "./fields/field_colour_slider";
|
|
55
|
+
import { registerScratchFieldDropdown } from "./fields/scratch_field_dropdown";
|
|
56
|
+
import { registerFieldMatrix } from "./fields/field_matrix";
|
|
57
|
+
import { registerFieldNote, FieldNote } from "./fields/field_note";
|
|
58
|
+
import { registerScratchFieldNumber } from "./fields/scratch_field_number";
|
|
59
|
+
import { registerFieldTextInputRemovable } from "./fields/field_textinput_removable";
|
|
60
|
+
import { registerFieldVariableGetter } from "./fields/field_variable_getter";
|
|
61
|
+
import { registerScratchFieldVariable } from "./fields/scratch_field_variable";
|
|
62
|
+
import { registerFieldVerticalSeparator } from "./fields/field_vertical_separator";
|
|
63
|
+
import { registerRecyclableBlockFlyoutInflater } from "./recyclable_block_flyout_inflater";
|
|
64
|
+
import { registerScratchBlockPaster } from "./scratch_block_paster";
|
|
65
|
+
import { registerStatusIndicatorLabelFlyoutInflater } from "./status_indicator_label_flyout_inflater";
|
|
66
|
+
import { registerScratchContinuousCategory } from "./scratch_continuous_category";
|
|
67
67
|
|
|
68
68
|
export * from "blockly/core";
|
|
69
|
-
export * from "./block_reporting
|
|
70
|
-
export * from "./
|
|
71
|
-
export * from "./procedures.js";
|
|
69
|
+
export * from "./block_reporting";
|
|
70
|
+
export * from "./procedures";
|
|
72
71
|
export * from "../msg/scratch_msgs.js";
|
|
73
|
-
export * from "./constants
|
|
72
|
+
export * from "./constants";
|
|
74
73
|
export { glowStack };
|
|
75
74
|
export { scratchBlocksUtils };
|
|
76
75
|
export { CheckableContinuousFlyout };
|
|
77
76
|
export { ScratchVariables };
|
|
78
77
|
export { contextMenuItems };
|
|
79
78
|
export { FieldColourSlider, FieldNote };
|
|
80
|
-
export { CheckboxBubble } from "./checkbox_bubble
|
|
79
|
+
export { CheckboxBubble } from "./checkbox_bubble";
|
|
81
80
|
export {
|
|
82
81
|
StatusIndicatorLabel,
|
|
83
82
|
StatusButtonState,
|
|
84
|
-
} from "./status_indicator_label
|
|
83
|
+
} from "./status_indicator_label";
|
|
85
84
|
|
|
86
85
|
export function inject(container: Element, options: Blockly.BlocklyOptions) {
|
|
87
|
-
|
|
86
|
+
registerScratchFieldAngle();
|
|
88
87
|
registerFieldColourSlider();
|
|
89
|
-
|
|
88
|
+
registerScratchFieldDropdown();
|
|
90
89
|
registerFieldMatrix();
|
|
91
90
|
registerFieldNote();
|
|
92
|
-
|
|
91
|
+
registerScratchFieldNumber();
|
|
93
92
|
registerFieldTextInputRemovable();
|
|
94
93
|
registerFieldVariableGetter();
|
|
95
|
-
|
|
94
|
+
registerScratchFieldVariable();
|
|
96
95
|
registerFieldVerticalSeparator();
|
|
97
96
|
registerRecyclableBlockFlyoutInflater();
|
|
98
97
|
registerScratchBlockPaster();
|
|
@@ -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 };
|