@blockdia/scratch-blocks 0.1.0
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/.nvmrc +1 -0
- package/LICENSE +674 -0
- package/README.md +57 -0
- package/TRADEMARK +1 -0
- package/blockly_compressed_horizontal.js +2268 -0
- package/blockly_compressed_vertical.js +2296 -0
- package/blockly_uncompressed_horizontal.js +1212 -0
- package/blockly_uncompressed_vertical.js +1212 -0
- package/blocks_common/colour.js +61 -0
- package/blocks_common/math.js +159 -0
- package/blocks_common/matrix.js +54 -0
- package/blocks_common/note.js +58 -0
- package/blocks_common/text.js +57 -0
- package/blocks_compressed.js +37 -0
- package/blocks_compressed_horizontal.js +68 -0
- package/blocks_compressed_vertical.js +216 -0
- package/blocks_horizontal/control.js +212 -0
- package/blocks_horizontal/default_toolbox.js +139 -0
- package/blocks_horizontal/event.js +190 -0
- package/blocks_horizontal/wedo.js +325 -0
- package/blocks_vertical/control.js +532 -0
- package/blocks_vertical/data.js +666 -0
- package/blocks_vertical/default_toolbox.js +571 -0
- package/blocks_vertical/event.js +329 -0
- package/blocks_vertical/extensions.js +294 -0
- package/blocks_vertical/looks.js +591 -0
- package/blocks_vertical/motion.js +587 -0
- package/blocks_vertical/operators.js +470 -0
- package/blocks_vertical/procedures.js +1051 -0
- package/blocks_vertical/sensing.js +555 -0
- package/blocks_vertical/sound.js +246 -0
- package/blocks_vertical/vertical_extensions.js +289 -0
- package/build/gen_blocks.js +1 -0
- package/build/test_expect.js +1 -0
- package/build/test_input.js +1 -0
- package/build.py +647 -0
- package/cleanup.sh +101 -0
- package/core/block.js +1837 -0
- package/core/block_animations.js +107 -0
- package/core/block_drag_surface.js +299 -0
- package/core/block_dragger.js +424 -0
- package/core/block_events.js +531 -0
- package/core/block_render_svg_horizontal.js +892 -0
- package/core/block_render_svg_vertical.js +1734 -0
- package/core/block_svg.js +1355 -0
- package/core/blockly.js +620 -0
- package/core/blocks.js +37 -0
- package/core/bubble.js +664 -0
- package/core/bubble_dragger.js +285 -0
- package/core/colours.js +177 -0
- package/core/comment.js +293 -0
- package/core/comment_events.js +539 -0
- package/core/connection.js +776 -0
- package/core/connection_db.js +300 -0
- package/core/constants.js +408 -0
- package/core/contextmenu.js +534 -0
- package/core/css.js +1354 -0
- package/core/data_category.js +490 -0
- package/core/dragged_connection_manager.js +260 -0
- package/core/dropdowndiv.js +408 -0
- package/core/events.js +429 -0
- package/core/events_abstract.js +113 -0
- package/core/extensions.js +450 -0
- package/core/field.js +810 -0
- package/core/field_angle.js +410 -0
- package/core/field_checkbox.js +133 -0
- package/core/field_colour.js +253 -0
- package/core/field_colour_slider.js +387 -0
- package/core/field_date.js +353 -0
- package/core/field_dropdown.js +447 -0
- package/core/field_iconmenu.js +309 -0
- package/core/field_image.js +200 -0
- package/core/field_label.js +136 -0
- package/core/field_label_serializable.js +125 -0
- package/core/field_matrix.js +566 -0
- package/core/field_note.js +850 -0
- package/core/field_number.js +366 -0
- package/core/field_numberdropdown.js +77 -0
- package/core/field_textdropdown.js +164 -0
- package/core/field_textinput.js +675 -0
- package/core/field_textinput_removable.js +105 -0
- package/core/field_variable.js +385 -0
- package/core/field_variable_getter.js +185 -0
- package/core/field_vertical_separator.js +161 -0
- package/core/flyout_base.js +935 -0
- package/core/flyout_button.js +324 -0
- package/core/flyout_dragger.js +83 -0
- package/core/flyout_extension_category_header.js +159 -0
- package/core/flyout_horizontal.js +475 -0
- package/core/flyout_vertical.js +770 -0
- package/core/generator.js +426 -0
- package/core/gesture.js +1010 -0
- package/core/grid.js +227 -0
- package/core/icon.js +205 -0
- package/core/inject.js +496 -0
- package/core/input.js +285 -0
- package/core/insertion_marker_manager.js +678 -0
- package/core/intersection_observer.js +102 -0
- package/core/msg.js +62 -0
- package/core/mutator.js +426 -0
- package/core/names.js +198 -0
- package/core/options.js +244 -0
- package/core/procedures.js +739 -0
- package/core/rendered_connection.js +417 -0
- package/core/scratch_block_comment.js +646 -0
- package/core/scratch_blocks_utils.js +246 -0
- package/core/scratch_bubble.js +699 -0
- package/core/scratch_events.js +131 -0
- package/core/scratch_msgs.js +85 -0
- package/core/scrollbar.js +875 -0
- package/core/toolbox.js +803 -0
- package/core/tooltip.js +337 -0
- package/core/touch.js +226 -0
- package/core/trashcan.js +343 -0
- package/core/ui_events.js +91 -0
- package/core/ui_menu_utils.js +68 -0
- package/core/utils.js +825 -0
- package/core/variable_events.js +259 -0
- package/core/variable_map.js +415 -0
- package/core/variable_model.js +116 -0
- package/core/variables.js +674 -0
- package/core/warning.js +199 -0
- package/core/widgetdiv.js +344 -0
- package/core/workspace.js +673 -0
- package/core/workspace_audio.js +170 -0
- package/core/workspace_comment.js +426 -0
- package/core/workspace_comment_render_svg.js +706 -0
- package/core/workspace_comment_svg.js +609 -0
- package/core/workspace_drag_surface_svg.js +195 -0
- package/core/workspace_dragger.js +132 -0
- package/core/workspace_svg.js +2382 -0
- package/core/xml.js +930 -0
- package/core/zoom_controls.js +301 -0
- package/dist/horizontal.js +222 -0
- package/dist/vertical.js +222 -0
- package/dist/web/horizontal.js +232 -0
- package/dist/web/vertical.js +232 -0
- package/i18n/common.py +234 -0
- package/i18n/create_messages.py +162 -0
- package/i18n/create_scratch_msgs.js +69 -0
- package/i18n/dedup_json.py +73 -0
- package/i18n/js_to_json.js +46 -0
- package/i18n/js_to_json.py +136 -0
- package/i18n/json_to_js.js +52 -0
- package/i18n/json_to_js.py +185 -0
- package/i18n/sync_tx_translations.js +111 -0
- package/i18n/test_scratch_msgs.js +87 -0
- package/i18n/tests.py +47 -0
- package/i18n/xliff_to_json.py +232 -0
- package/local_build.sh +70 -0
- package/media/click.mp3 +0 -0
- package/media/click.ogg +0 -0
- package/media/click.wav +0 -0
- package/media/comment-arrow-down.svg +10 -0
- package/media/comment-arrow-up.svg +10 -0
- package/media/delete-x.svg +10 -0
- package/media/delete.mp3 +0 -0
- package/media/delete.ogg +0 -0
- package/media/delete.wav +0 -0
- package/media/dropdown-arrow-dark.svg +1 -0
- package/media/dropdown-arrow.svg +1 -0
- package/media/extensions/microbit-block-icon.svg +130 -0
- package/media/extensions/music-block-icon.svg +17 -0
- package/media/extensions/pen-block-icon.svg +19 -0
- package/media/extensions/wedo2-block-icon.svg +36 -0
- package/media/eyedropper.svg +22 -0
- package/media/green-flag.svg +17 -0
- package/media/handclosed.cur +0 -0
- package/media/handdelete.cur +0 -0
- package/media/handopen.cur +0 -0
- package/media/icons/arrow.svg +12 -0
- package/media/icons/arrow_button.svg +1 -0
- package/media/icons/control_forever.svg +1 -0
- package/media/icons/control_repeat.svg +1 -0
- package/media/icons/control_stop.svg +1 -0
- package/media/icons/control_wait.svg +1 -0
- package/media/icons/event_broadcast_blue.svg +1 -0
- package/media/icons/event_broadcast_coral.svg +1 -0
- package/media/icons/event_broadcast_green.svg +1 -0
- package/media/icons/event_broadcast_magenta.svg +1 -0
- package/media/icons/event_broadcast_orange.svg +1 -0
- package/media/icons/event_broadcast_purple.svg +1 -0
- package/media/icons/event_when-broadcast-received_blue.svg +1 -0
- package/media/icons/event_when-broadcast-received_coral.svg +1 -0
- package/media/icons/event_when-broadcast-received_green.svg +1 -0
- package/media/icons/event_when-broadcast-received_magenta.svg +1 -0
- package/media/icons/event_when-broadcast-received_orange.svg +1 -0
- package/media/icons/event_when-broadcast-received_purple.svg +1 -0
- package/media/icons/event_whenflagclicked.svg +1 -0
- package/media/icons/remove.svg +19 -0
- package/media/icons/set-led_blue.svg +1 -0
- package/media/icons/set-led_coral.svg +1 -0
- package/media/icons/set-led_green.svg +1 -0
- package/media/icons/set-led_magenta.svg +1 -0
- package/media/icons/set-led_mystery.svg +1 -0
- package/media/icons/set-led_orange.svg +1 -0
- package/media/icons/set-led_purple.svg +1 -0
- package/media/icons/set-led_white.svg +1 -0
- package/media/icons/set-led_yellow.svg +1 -0
- package/media/icons/wedo_motor-clockwise.svg +1 -0
- package/media/icons/wedo_motor-counterclockwise.svg +1 -0
- package/media/icons/wedo_motor-speed_fast.svg +1 -0
- package/media/icons/wedo_motor-speed_med.svg +1 -0
- package/media/icons/wedo_motor-speed_slow.svg +1 -0
- package/media/icons/wedo_when-distance_close.svg +1 -0
- package/media/icons/wedo_when-tilt-backward.svg +1 -0
- package/media/icons/wedo_when-tilt-forward.svg +1 -0
- package/media/icons/wedo_when-tilt-left.svg +1 -0
- package/media/icons/wedo_when-tilt-right.svg +1 -0
- package/media/icons/wedo_when-tilt.svg +1 -0
- package/media/repeat.svg +18 -0
- package/media/rotate-left.svg +1 -0
- package/media/rotate-right.svg +1 -0
- package/media/sprites.png +0 -0
- package/media/status-not-ready.svg +13 -0
- package/media/status-ready.svg +13 -0
- package/media/zoom-in.svg +1 -0
- package/media/zoom-out.svg +1 -0
- package/media/zoom-reset.svg +1 -0
- package/msg/js/en.js +290 -0
- package/msg/json/en.json +285 -0
- package/msg/messages.js +359 -0
- package/msg/scratch_msgs.js +22969 -0
- package/package.json +60 -0
- package/pull_from_blockly.sh +151 -0
- package/renovate.json5 +15 -0
- package/shim/blockly_compressed_horizontal-blocks_compressed.js +1 -0
- package/shim/blockly_compressed_horizontal.Blockly.js +1 -0
- package/shim/blockly_compressed_horizontal.goog.js +1 -0
- package/shim/blockly_compressed_horizontal.js +1 -0
- package/shim/blockly_compressed_vertical-blocks_compressed.js +1 -0
- package/shim/blockly_compressed_vertical.Blockly.js +1 -0
- package/shim/blockly_compressed_vertical.goog.js +1 -0
- package/shim/blockly_compressed_vertical.js +1 -0
- package/shim/blocks_compressed_horizontal-blockly_compressed_horizontal-messages.js +1 -0
- package/shim/blocks_compressed_horizontal.js +1 -0
- package/shim/blocks_compressed_vertical-blockly_compressed_vertical-messages.js +1 -0
- package/shim/blocks_compressed_vertical.js +1 -0
- package/shim/gh-pages.js +1 -0
- package/shim/horizontal.js +1 -0
- package/shim/index.js +17 -0
- package/shim/vertical.js +1 -0
- package/universal-python.js +82 -0
|
@@ -0,0 +1,1051 @@
|
|
|
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 Procedure blocks for Scratch.
|
|
23
|
+
*/
|
|
24
|
+
'use strict';
|
|
25
|
+
|
|
26
|
+
goog.provide('Blockly.ScratchBlocks.ProcedureUtils');
|
|
27
|
+
|
|
28
|
+
goog.require('Blockly.Blocks');
|
|
29
|
+
goog.require('Blockly.Colours');
|
|
30
|
+
goog.require('Blockly.constants');
|
|
31
|
+
goog.require('Blockly.ScratchBlocks.VerticalExtensions');
|
|
32
|
+
|
|
33
|
+
// Serialization and deserialization.
|
|
34
|
+
|
|
35
|
+
Blockly.ScratchBlocks.ProcedureUtils.parseReturnMutation = function(xmlElement) {
|
|
36
|
+
if (xmlElement.hasAttribute('return')) {
|
|
37
|
+
var type = +xmlElement.getAttribute('return');
|
|
38
|
+
if (
|
|
39
|
+
type === Blockly.PROCEDURES_CALL_TYPE_STATEMENT ||
|
|
40
|
+
type === Blockly.PROCEDURES_CALL_TYPE_REPORTER ||
|
|
41
|
+
type === Blockly.PROCEDURES_CALL_TYPE_BOOLEAN
|
|
42
|
+
) {
|
|
43
|
+
return type;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return Blockly.PROCEDURES_CALL_TYPE_STATEMENT;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Create XML to represent the (non-editable) name and arguments of a procedure
|
|
51
|
+
* call block.
|
|
52
|
+
* @return {!Element} XML storage element.
|
|
53
|
+
* @this Blockly.Block
|
|
54
|
+
*/
|
|
55
|
+
Blockly.ScratchBlocks.ProcedureUtils.callerMutationToDom = function() {
|
|
56
|
+
var container = document.createElement('mutation');
|
|
57
|
+
container.setAttribute('proccode', this.procCode_);
|
|
58
|
+
container.setAttribute('argumentids', JSON.stringify(this.argumentIds_));
|
|
59
|
+
container.setAttribute('warp', JSON.stringify(this.warp_));
|
|
60
|
+
if (this.return_ !== Blockly.PROCEDURES_CALL_TYPE_STATEMENT) {
|
|
61
|
+
container.setAttribute('return', this.return_);
|
|
62
|
+
}
|
|
63
|
+
return container;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Parse XML to restore the (non-editable) name and arguments of a procedure
|
|
68
|
+
* call block.
|
|
69
|
+
* @param {!Element} xmlElement XML storage element.
|
|
70
|
+
* @this Blockly.Block
|
|
71
|
+
*/
|
|
72
|
+
Blockly.ScratchBlocks.ProcedureUtils.callerDomToMutation = function(xmlElement) {
|
|
73
|
+
this.procCode_ = xmlElement.getAttribute('proccode');
|
|
74
|
+
this.generateShadows_ =
|
|
75
|
+
JSON.parse(xmlElement.getAttribute('generateshadows'));
|
|
76
|
+
this.argumentIds_ = JSON.parse(xmlElement.getAttribute('argumentids'));
|
|
77
|
+
this.warp_ = JSON.parse(xmlElement.getAttribute('warp'));
|
|
78
|
+
this.return_ = Blockly.ScratchBlocks.ProcedureUtils.parseReturnMutation(xmlElement);
|
|
79
|
+
if (this.return_ !== Blockly.PROCEDURES_CALL_TYPE_STATEMENT) {
|
|
80
|
+
this.workspace.enableProcedureReturns();
|
|
81
|
+
}
|
|
82
|
+
this.updateDisplay_();
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Create XML to represent the (non-editable) name and arguments of a
|
|
87
|
+
* procedures_prototype block or a procedures_declaration block.
|
|
88
|
+
* @param {boolean=} opt_generateShadows Whether to include the generateshadows
|
|
89
|
+
* flag in the generated XML. False if not provided.
|
|
90
|
+
* @return {!Element} XML storage element.
|
|
91
|
+
* @this Blockly.Block
|
|
92
|
+
*/
|
|
93
|
+
Blockly.ScratchBlocks.ProcedureUtils.definitionMutationToDom = function(
|
|
94
|
+
opt_generateShadows) {
|
|
95
|
+
var container = document.createElement('mutation');
|
|
96
|
+
|
|
97
|
+
if (opt_generateShadows) {
|
|
98
|
+
container.setAttribute('generateshadows', true);
|
|
99
|
+
}
|
|
100
|
+
container.setAttribute('proccode', this.procCode_);
|
|
101
|
+
container.setAttribute('argumentids', JSON.stringify(this.argumentIds_));
|
|
102
|
+
container.setAttribute('argumentnames', JSON.stringify(this.displayNames_));
|
|
103
|
+
container.setAttribute('argumentdefaults',
|
|
104
|
+
JSON.stringify(this.argumentDefaults_));
|
|
105
|
+
container.setAttribute('warp', JSON.stringify(this.warp_));
|
|
106
|
+
return container;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Parse XML to restore the (non-editable) name and arguments of a
|
|
111
|
+
* procedures_prototype block or a procedures_declaration block.
|
|
112
|
+
* @param {!Element} xmlElement XML storage element.
|
|
113
|
+
* @this Blockly.Block
|
|
114
|
+
*/
|
|
115
|
+
Blockly.ScratchBlocks.ProcedureUtils.definitionDomToMutation = function(xmlElement) {
|
|
116
|
+
this.procCode_ = xmlElement.getAttribute('proccode');
|
|
117
|
+
this.warp_ = JSON.parse(xmlElement.getAttribute('warp'));
|
|
118
|
+
|
|
119
|
+
var prevArgIds = this.argumentIds_;
|
|
120
|
+
var prevDisplayNames = this.displayNames_;
|
|
121
|
+
|
|
122
|
+
this.argumentIds_ = JSON.parse(xmlElement.getAttribute('argumentids'));
|
|
123
|
+
this.displayNames_ = JSON.parse(xmlElement.getAttribute('argumentnames'));
|
|
124
|
+
this.argumentDefaults_ = JSON.parse(
|
|
125
|
+
xmlElement.getAttribute('argumentdefaults'));
|
|
126
|
+
this.updateDisplay_();
|
|
127
|
+
if (this.updateArgumentReporterNames_) {
|
|
128
|
+
this.updateArgumentReporterNames_(prevArgIds, prevDisplayNames);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// End of serialization and deserialization.
|
|
133
|
+
|
|
134
|
+
// Shared by all three procedure blocks (procedures_declaration,
|
|
135
|
+
// procedures_call, and procedures_prototype).
|
|
136
|
+
/**
|
|
137
|
+
* Returns the name of the procedure this block calls, or the empty string if
|
|
138
|
+
* it has not yet been set.
|
|
139
|
+
* @return {string} Procedure name.
|
|
140
|
+
* @this Blockly.Block
|
|
141
|
+
*/
|
|
142
|
+
Blockly.ScratchBlocks.ProcedureUtils.getProcCode = function() {
|
|
143
|
+
return this.procCode_;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Update the block's structure and appearance to match the internally stored
|
|
148
|
+
* mutation.
|
|
149
|
+
* @private
|
|
150
|
+
* @this Blockly.Block
|
|
151
|
+
*/
|
|
152
|
+
Blockly.ScratchBlocks.ProcedureUtils.updateDisplay_ = function() {
|
|
153
|
+
var wasRendered = this.rendered;
|
|
154
|
+
this.rendered = false;
|
|
155
|
+
|
|
156
|
+
var connectionMap = this.disconnectOldBlocks_();
|
|
157
|
+
this.removeAllInputs_();
|
|
158
|
+
|
|
159
|
+
this.createAllInputs_(connectionMap);
|
|
160
|
+
this.deleteShadows_(connectionMap);
|
|
161
|
+
|
|
162
|
+
if (!wasRendered && this.getReturn) {
|
|
163
|
+
this.setInputsInline(true);
|
|
164
|
+
if (this.getReturn() === Blockly.PROCEDURES_CALL_TYPE_STATEMENT) {
|
|
165
|
+
this.setPreviousStatement(true, null);
|
|
166
|
+
this.setNextStatement(true, null);
|
|
167
|
+
} else {
|
|
168
|
+
if (this.getReturn() === Blockly.PROCEDURES_CALL_TYPE_BOOLEAN) {
|
|
169
|
+
this.setOutput(true, null);
|
|
170
|
+
this.setOutputShape(Blockly.OUTPUT_SHAPE_HEXAGONAL);
|
|
171
|
+
} else {
|
|
172
|
+
this.setOutput(true, Blockly.Procedures.ENFORCE_TYPES ? 'Number' : null);
|
|
173
|
+
this.setOutputShape(Blockly.OUTPUT_SHAPE_ROUND);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
this.rendered = wasRendered;
|
|
179
|
+
if (wasRendered && !this.isInsertionMarker()) {
|
|
180
|
+
this.initSvg();
|
|
181
|
+
this.render();
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Disconnect old blocks from all value inputs on this block, but hold onto them
|
|
187
|
+
* in case they can be reattached later. Also save the shadow DOM if it exists.
|
|
188
|
+
* The result is a map from argument ID to information that was associated with
|
|
189
|
+
* that argument at the beginning of the mutation.
|
|
190
|
+
* @return {!Object.<string, {shadow: Element, block: Blockly.Block}>} An object
|
|
191
|
+
* mapping argument IDs to blocks and shadow DOMs.
|
|
192
|
+
* @private
|
|
193
|
+
* @this Blockly.Block
|
|
194
|
+
*/
|
|
195
|
+
Blockly.ScratchBlocks.ProcedureUtils.disconnectOldBlocks_ = function() {
|
|
196
|
+
// Remove old stuff
|
|
197
|
+
var connectionMap = {};
|
|
198
|
+
for (var i = 0, input; input = this.inputList[i]; i++) {
|
|
199
|
+
if (input.connection) {
|
|
200
|
+
var target = input.connection.targetBlock();
|
|
201
|
+
var saveInfo = {
|
|
202
|
+
shadow: input.connection.getShadowDom(),
|
|
203
|
+
block: target
|
|
204
|
+
};
|
|
205
|
+
connectionMap[input.name] = saveInfo;
|
|
206
|
+
|
|
207
|
+
// Remove the shadow DOM, then disconnect the block. Otherwise a shadow
|
|
208
|
+
// block will respawn instantly, and we'd have to remove it when we remove
|
|
209
|
+
// the input.
|
|
210
|
+
input.connection.setShadowDom(null);
|
|
211
|
+
if (target) {
|
|
212
|
+
input.connection.disconnect();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return connectionMap;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Remove all inputs on the block, including dummy inputs.
|
|
221
|
+
* Assumes no input has shadow DOM set.
|
|
222
|
+
* @private
|
|
223
|
+
* @this Blockly.Block
|
|
224
|
+
*/
|
|
225
|
+
Blockly.ScratchBlocks.ProcedureUtils.removeAllInputs_ = function() {
|
|
226
|
+
// Delete inputs directly instead of with block.removeInput to avoid splicing
|
|
227
|
+
// out of the input list at every index.
|
|
228
|
+
for (var i = 0, input; input = this.inputList[i]; i++) {
|
|
229
|
+
input.dispose();
|
|
230
|
+
}
|
|
231
|
+
this.inputList = [];
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Create all inputs specified by the new procCode, and populate them with
|
|
236
|
+
* shadow blocks or reconnected old blocks as appropriate.
|
|
237
|
+
* @param {!Object.<string, {shadow: Element, block: Blockly.Block}>}
|
|
238
|
+
* connectionMap An object mapping argument IDs to blocks and shadow DOMs.
|
|
239
|
+
* @private
|
|
240
|
+
* @this Blockly.Block
|
|
241
|
+
*/
|
|
242
|
+
Blockly.ScratchBlocks.ProcedureUtils.createAllInputs_ = function(connectionMap) {
|
|
243
|
+
// Split the proc into components, by %n, %b, and %s (ignoring escaped).
|
|
244
|
+
var procComponents = this.procCode_.split(/(?=[^\\]%[nbs])/);
|
|
245
|
+
procComponents = procComponents.map(function(c) {
|
|
246
|
+
return c.trim(); // Strip whitespace.
|
|
247
|
+
});
|
|
248
|
+
// Create arguments and labels as appropriate.
|
|
249
|
+
var argumentCount = 0;
|
|
250
|
+
var hasAnyField = false;
|
|
251
|
+
for (var i = 0, component; component = procComponents[i]; i++) {
|
|
252
|
+
var labelText;
|
|
253
|
+
if (component.substring(0, 1) == '%') {
|
|
254
|
+
var argumentType = component.substring(1, 2);
|
|
255
|
+
if (!(argumentType == 'n' || argumentType == 'b' || argumentType == 's')) {
|
|
256
|
+
throw new Error(
|
|
257
|
+
'Found an custom procedure with an invalid type: ' + argumentType);
|
|
258
|
+
}
|
|
259
|
+
labelText = component.substring(2).trim();
|
|
260
|
+
|
|
261
|
+
var id = this.argumentIds_[argumentCount];
|
|
262
|
+
|
|
263
|
+
var input = this.appendValueInput(id);
|
|
264
|
+
if (argumentType == 'b') {
|
|
265
|
+
input.setCheck('Boolean');
|
|
266
|
+
}
|
|
267
|
+
this.populateArgument_(argumentType, argumentCount, connectionMap, id,
|
|
268
|
+
input);
|
|
269
|
+
hasAnyField = true;
|
|
270
|
+
argumentCount++;
|
|
271
|
+
} else {
|
|
272
|
+
labelText = component.trim();
|
|
273
|
+
}
|
|
274
|
+
labelText = labelText.replace(/\\%/, '%');
|
|
275
|
+
// don't add empty labels which will just waste space
|
|
276
|
+
if (labelText) {
|
|
277
|
+
this.addProcedureLabel_(labelText);
|
|
278
|
+
hasAnyField = true;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
// Custom reporters will crash editor if they have no fields.
|
|
282
|
+
if (!hasAnyField) {
|
|
283
|
+
this.addProcedureLabel_(' ');
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Delete all shadow blocks in the given map.
|
|
289
|
+
* @param {!Object.<string, Blockly.Block>} connectionMap An object mapping
|
|
290
|
+
* argument IDs to the blocks that were connected to those IDs at the
|
|
291
|
+
* beginning of the mutation.
|
|
292
|
+
* @private
|
|
293
|
+
* @this Blockly.Block
|
|
294
|
+
*/
|
|
295
|
+
Blockly.ScratchBlocks.ProcedureUtils.deleteShadows_ = function(connectionMap) {
|
|
296
|
+
// Get rid of all of the old shadow blocks if they aren't connected.
|
|
297
|
+
if (connectionMap) {
|
|
298
|
+
for (var id in connectionMap) {
|
|
299
|
+
var saveInfo = connectionMap[id];
|
|
300
|
+
if (saveInfo) {
|
|
301
|
+
var block = saveInfo['block'];
|
|
302
|
+
if (block && block.isShadow()) {
|
|
303
|
+
block.dispose();
|
|
304
|
+
connectionMap[id] = null;
|
|
305
|
+
// At this point we know which shadow DOMs are about to be orphaned in
|
|
306
|
+
// the VM. What do we do with that information?
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
// End of shared code.
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Add a label field with the given text to a procedures_call or
|
|
316
|
+
* procedures_prototype block.
|
|
317
|
+
* @param {string} text The label text.
|
|
318
|
+
* @private
|
|
319
|
+
*/
|
|
320
|
+
Blockly.ScratchBlocks.ProcedureUtils.addLabelField_ = function(text) {
|
|
321
|
+
this.appendDummyInput().appendField(text);
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Add a label editor with the given text to a procedures_declaration
|
|
326
|
+
* block. Editing the text in the label editor updates the text of the
|
|
327
|
+
* corresponding label fields on function calls.
|
|
328
|
+
* @param {string} text The label text.
|
|
329
|
+
* @private
|
|
330
|
+
*/
|
|
331
|
+
Blockly.ScratchBlocks.ProcedureUtils.addLabelEditor_ = function(text) {
|
|
332
|
+
if (text) {
|
|
333
|
+
this.appendDummyInput(Blockly.utils.genUid()).
|
|
334
|
+
appendField(new Blockly.FieldTextInputRemovable(text));
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Build a DOM node representing a shadow block of the given type.
|
|
340
|
+
* @param {string} type One of 's' (string) or 'n' (number).
|
|
341
|
+
* @return {!Element} The DOM node representing the new shadow block.
|
|
342
|
+
* @private
|
|
343
|
+
* @this Blockly.Block
|
|
344
|
+
*/
|
|
345
|
+
Blockly.ScratchBlocks.ProcedureUtils.buildShadowDom_ = function(type) {
|
|
346
|
+
var shadowDom = goog.dom.createDom('shadow');
|
|
347
|
+
if (type == 'n') {
|
|
348
|
+
var shadowType = 'math_number';
|
|
349
|
+
var fieldName = 'NUM';
|
|
350
|
+
var fieldValue = '1';
|
|
351
|
+
} else {
|
|
352
|
+
var shadowType = 'text';
|
|
353
|
+
var fieldName = 'TEXT';
|
|
354
|
+
var fieldValue = '';
|
|
355
|
+
}
|
|
356
|
+
shadowDom.setAttribute('type', shadowType);
|
|
357
|
+
var fieldDom = goog.dom.createDom('field', null, fieldValue);
|
|
358
|
+
fieldDom.setAttribute('name', fieldName);
|
|
359
|
+
shadowDom.appendChild(fieldDom);
|
|
360
|
+
return shadowDom;
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Create a new shadow block and attach it to the given input.
|
|
365
|
+
* @param {!Blockly.Input} input The value input to attach a block to.
|
|
366
|
+
* @param {string} argumentType One of 'b' (boolean), 's' (string) or
|
|
367
|
+
* 'n' (number).
|
|
368
|
+
* @private
|
|
369
|
+
* @this Blockly.Block
|
|
370
|
+
*/
|
|
371
|
+
Blockly.ScratchBlocks.ProcedureUtils.attachShadow_ = function(input,
|
|
372
|
+
argumentType) {
|
|
373
|
+
if (argumentType == 'n' || argumentType == 's') {
|
|
374
|
+
var blockType = argumentType == 'n' ? 'math_number' : 'text';
|
|
375
|
+
Blockly.Events.disable();
|
|
376
|
+
try {
|
|
377
|
+
var newBlock = this.workspace.newBlock(blockType);
|
|
378
|
+
if (argumentType == 'n') {
|
|
379
|
+
newBlock.setFieldValue('1', 'NUM');
|
|
380
|
+
} else {
|
|
381
|
+
newBlock.setFieldValue('', 'TEXT');
|
|
382
|
+
}
|
|
383
|
+
newBlock.setShadow(true);
|
|
384
|
+
if (!this.isInsertionMarker()) {
|
|
385
|
+
newBlock.initSvg();
|
|
386
|
+
newBlock.render(false);
|
|
387
|
+
}
|
|
388
|
+
} finally {
|
|
389
|
+
Blockly.Events.enable();
|
|
390
|
+
}
|
|
391
|
+
if (Blockly.Events.isEnabled()) {
|
|
392
|
+
Blockly.Events.fire(new Blockly.Events.BlockCreate(newBlock));
|
|
393
|
+
}
|
|
394
|
+
newBlock.outputConnection.connect(input.connection);
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Create a new argument reporter block.
|
|
400
|
+
* @param {string} argumentType One of 'b' (boolean), 's' (string) or
|
|
401
|
+
* 'n' (number).
|
|
402
|
+
* @param {string} displayName The name of the argument as provided by the
|
|
403
|
+
* user, which becomes the text of the label on the argument reporter block.
|
|
404
|
+
* @return {!Blockly.BlockSvg} The newly created argument reporter block.
|
|
405
|
+
* @private
|
|
406
|
+
* @this Blockly.Block
|
|
407
|
+
*/
|
|
408
|
+
Blockly.ScratchBlocks.ProcedureUtils.createArgumentReporter_ = function(
|
|
409
|
+
argumentType, displayName) {
|
|
410
|
+
if (argumentType == 'n' || argumentType == 's') {
|
|
411
|
+
var blockType = 'argument_reporter_string_number';
|
|
412
|
+
} else {
|
|
413
|
+
var blockType = 'argument_reporter_boolean';
|
|
414
|
+
}
|
|
415
|
+
Blockly.Events.disable();
|
|
416
|
+
try {
|
|
417
|
+
var newBlock = this.workspace.newBlock(blockType);
|
|
418
|
+
newBlock.setShadow(true);
|
|
419
|
+
newBlock.setFieldValue(displayName, 'VALUE');
|
|
420
|
+
if (!this.isInsertionMarker()) {
|
|
421
|
+
newBlock.initSvg();
|
|
422
|
+
newBlock.render(false);
|
|
423
|
+
}
|
|
424
|
+
} finally {
|
|
425
|
+
Blockly.Events.enable();
|
|
426
|
+
}
|
|
427
|
+
if (Blockly.Events.isEnabled()) {
|
|
428
|
+
Blockly.Events.fire(new Blockly.Events.BlockCreate(newBlock));
|
|
429
|
+
}
|
|
430
|
+
return newBlock;
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Populate the argument by attaching the correct child block or shadow to the
|
|
435
|
+
* given input.
|
|
436
|
+
* @param {string} type One of 'b' (boolean), 's' (string) or 'n' (number).
|
|
437
|
+
* @param {number} index The index of this argument into the argument id array.
|
|
438
|
+
* @param {!Object.<string, {shadow: Element, block: Blockly.Block}>}
|
|
439
|
+
* connectionMap An object mapping argument IDs to blocks and shadow DOMs.
|
|
440
|
+
* @param {string} id The ID of the input to populate.
|
|
441
|
+
* @param {!Blockly.Input} input The newly created input to populate.
|
|
442
|
+
* @private
|
|
443
|
+
* @this Blockly.Block
|
|
444
|
+
*/
|
|
445
|
+
Blockly.ScratchBlocks.ProcedureUtils.populateArgumentOnCaller_ = function(type,
|
|
446
|
+
index, connectionMap, id, input) {
|
|
447
|
+
var oldBlock = null;
|
|
448
|
+
var oldShadow = null;
|
|
449
|
+
if (connectionMap && (id in connectionMap)) {
|
|
450
|
+
var saveInfo = connectionMap[id];
|
|
451
|
+
oldBlock = saveInfo['block'];
|
|
452
|
+
oldShadow = saveInfo['shadow'];
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
if (connectionMap && oldBlock) {
|
|
456
|
+
// Reattach the old block and shadow DOM.
|
|
457
|
+
connectionMap[input.name] = null;
|
|
458
|
+
oldBlock.outputConnection.connect(input.connection);
|
|
459
|
+
if (type != 'b' && this.generateShadows_) {
|
|
460
|
+
var shadowDom = oldShadow || this.buildShadowDom_(type);
|
|
461
|
+
console.log("setting shadow dom: " + shadowDom);
|
|
462
|
+
input.connection.setShadowDom(shadowDom);
|
|
463
|
+
}
|
|
464
|
+
} else if (this.generateShadows_) {
|
|
465
|
+
this.attachShadow_(input, type);
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Populate the argument by attaching the correct argument reporter to the given
|
|
471
|
+
* input.
|
|
472
|
+
* @param {string} type One of 'b' (boolean), 's' (string) or 'n' (number).
|
|
473
|
+
* @param {number} index The index of this argument into the argument ID and
|
|
474
|
+
* argument display name arrays.
|
|
475
|
+
* @param {!Object.<string, {shadow: Element, block: Blockly.Block}>}
|
|
476
|
+
* connectionMap An object mapping argument IDs to blocks and shadow DOMs.
|
|
477
|
+
* @param {string} id The ID of the input to populate.
|
|
478
|
+
* @param {!Blockly.Input} input The newly created input to populate.
|
|
479
|
+
* @private
|
|
480
|
+
* @this Blockly.Block
|
|
481
|
+
*/
|
|
482
|
+
Blockly.ScratchBlocks.ProcedureUtils.populateArgumentOnPrototype_ = function(
|
|
483
|
+
type, index, connectionMap, id, input) {
|
|
484
|
+
var oldBlock = null;
|
|
485
|
+
if (connectionMap && (id in connectionMap)) {
|
|
486
|
+
var saveInfo = connectionMap[id];
|
|
487
|
+
oldBlock = saveInfo['block'];
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
var oldTypeMatches =
|
|
491
|
+
Blockly.ScratchBlocks.ProcedureUtils.checkOldTypeMatches_(oldBlock, type);
|
|
492
|
+
var displayName = this.displayNames_[index];
|
|
493
|
+
|
|
494
|
+
// Decide which block to attach.
|
|
495
|
+
if (connectionMap && oldBlock && oldTypeMatches) {
|
|
496
|
+
// Update the text if needed. The old argument reporter is the same type,
|
|
497
|
+
// and on the same input, but the argument's display name may have changed.
|
|
498
|
+
var argumentReporter = oldBlock;
|
|
499
|
+
argumentReporter.setFieldValue(displayName, 'VALUE');
|
|
500
|
+
connectionMap[input.name] = null;
|
|
501
|
+
} else {
|
|
502
|
+
var argumentReporter = this.createArgumentReporter_(type, displayName);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// Attach the block.
|
|
506
|
+
input.connection.connect(argumentReporter.outputConnection);
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Populate the argument by attaching the correct argument editor to the given
|
|
511
|
+
* input.
|
|
512
|
+
* @param {string} type One of 'b' (boolean), 's' (string) or 'n' (number).
|
|
513
|
+
* @param {number} index The index of this argument into the argument id and
|
|
514
|
+
* argument display name arrays.
|
|
515
|
+
* @param {!Object.<string, {shadow: Element, block: Blockly.Block}>}
|
|
516
|
+
* connectionMap An object mapping argument IDs to blocks and shadow DOMs.
|
|
517
|
+
* @param {string} id The ID of the input to populate.
|
|
518
|
+
* @param {!Blockly.Input} input The newly created input to populate.
|
|
519
|
+
* @private
|
|
520
|
+
* @this Blockly.Block
|
|
521
|
+
*/
|
|
522
|
+
Blockly.ScratchBlocks.ProcedureUtils.populateArgumentOnDeclaration_ = function(
|
|
523
|
+
type, index, connectionMap, id, input) {
|
|
524
|
+
|
|
525
|
+
var oldBlock = null;
|
|
526
|
+
if (connectionMap && (id in connectionMap)) {
|
|
527
|
+
var saveInfo = connectionMap[id];
|
|
528
|
+
oldBlock = saveInfo['block'];
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// TODO: This always returns false, because it checks for argument reporter
|
|
532
|
+
// blocks instead of argument editor blocks. Create a new version for argument
|
|
533
|
+
// editors.
|
|
534
|
+
var oldTypeMatches =
|
|
535
|
+
Blockly.ScratchBlocks.ProcedureUtils.checkOldTypeMatches_(oldBlock, type);
|
|
536
|
+
var displayName = this.displayNames_[index];
|
|
537
|
+
|
|
538
|
+
// Decide which block to attach.
|
|
539
|
+
if (oldBlock && oldTypeMatches) {
|
|
540
|
+
var argumentEditor = oldBlock;
|
|
541
|
+
oldBlock.setFieldValue(displayName, 'TEXT');
|
|
542
|
+
connectionMap[input.name] = null;
|
|
543
|
+
} else {
|
|
544
|
+
var argumentEditor = this.createArgumentEditor_(type, displayName);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// Attach the block.
|
|
548
|
+
input.connection.connect(argumentEditor.outputConnection);
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Check whether the type of the old block corresponds to the given argument
|
|
553
|
+
* type.
|
|
554
|
+
* @param {Blockly.BlockSvg} oldBlock The old block to check.
|
|
555
|
+
* @param {string} type The argument type. One of 'n', 'n', or 's'.
|
|
556
|
+
* @return {boolean} True if the type matches, false otherwise.
|
|
557
|
+
*/
|
|
558
|
+
Blockly.ScratchBlocks.ProcedureUtils.checkOldTypeMatches_ = function(oldBlock,
|
|
559
|
+
type) {
|
|
560
|
+
if (!oldBlock) {
|
|
561
|
+
return false;
|
|
562
|
+
}
|
|
563
|
+
if ((type == 'n' || type == 's') &&
|
|
564
|
+
oldBlock.type == 'argument_reporter_string_number') {
|
|
565
|
+
return true;
|
|
566
|
+
}
|
|
567
|
+
if (type == 'b' && oldBlock.type == 'argument_reporter_boolean') {
|
|
568
|
+
return true;
|
|
569
|
+
}
|
|
570
|
+
return false;
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Create an argument editor.
|
|
575
|
+
* An argument editor is a shadow block with a single text field, which is used
|
|
576
|
+
* to set the display name of the argument.
|
|
577
|
+
* @param {string} argumentType One of 'b' (boolean), 's' (string) or
|
|
578
|
+
* 'n' (number).
|
|
579
|
+
* @param {string} displayName The display name of this argument, which is the
|
|
580
|
+
* text of the field on the shadow block.
|
|
581
|
+
* @return {!Blockly.BlockSvg} The newly created argument editor block.
|
|
582
|
+
* @private
|
|
583
|
+
* @this Blockly.Block
|
|
584
|
+
*/
|
|
585
|
+
Blockly.ScratchBlocks.ProcedureUtils.createArgumentEditor_ = function(
|
|
586
|
+
argumentType, displayName) {
|
|
587
|
+
Blockly.Events.disable();
|
|
588
|
+
try {
|
|
589
|
+
if (argumentType == 'n' || argumentType == 's') {
|
|
590
|
+
var newBlock = this.workspace.newBlock('argument_editor_string_number');
|
|
591
|
+
} else {
|
|
592
|
+
var newBlock = this.workspace.newBlock('argument_editor_boolean');
|
|
593
|
+
}
|
|
594
|
+
newBlock.setFieldValue(displayName, 'TEXT');
|
|
595
|
+
newBlock.setShadow(true);
|
|
596
|
+
if (!this.isInsertionMarker()) {
|
|
597
|
+
newBlock.initSvg();
|
|
598
|
+
newBlock.render(false);
|
|
599
|
+
}
|
|
600
|
+
} finally {
|
|
601
|
+
Blockly.Events.enable();
|
|
602
|
+
}
|
|
603
|
+
if (Blockly.Events.isEnabled()) {
|
|
604
|
+
Blockly.Events.fire(new Blockly.Events.BlockCreate(newBlock));
|
|
605
|
+
}
|
|
606
|
+
return newBlock;
|
|
607
|
+
};
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Update the serializable information on the block based on the existing inputs
|
|
611
|
+
* and their text.
|
|
612
|
+
*/
|
|
613
|
+
Blockly.ScratchBlocks.ProcedureUtils.updateDeclarationProcCode_ = function() {
|
|
614
|
+
this.procCode_ = '';
|
|
615
|
+
this.displayNames_ = [];
|
|
616
|
+
this.argumentIds_ = [];
|
|
617
|
+
for (var i = 0; i < this.inputList.length; i++) {
|
|
618
|
+
if (i != 0) {
|
|
619
|
+
this.procCode_ += ' ';
|
|
620
|
+
}
|
|
621
|
+
var input = this.inputList[i];
|
|
622
|
+
if (input.type == Blockly.DUMMY_INPUT) {
|
|
623
|
+
this.procCode_ += input.fieldRow[0].getValue();
|
|
624
|
+
} else if (input.type == Blockly.INPUT_VALUE) {
|
|
625
|
+
// Inspect the argument editor.
|
|
626
|
+
var target = input.connection.targetBlock();
|
|
627
|
+
this.displayNames_.push(target.getFieldValue('TEXT'));
|
|
628
|
+
this.argumentIds_.push(input.name);
|
|
629
|
+
if (target.type == 'argument_editor_boolean') {
|
|
630
|
+
this.procCode_ += '%b';
|
|
631
|
+
} else {
|
|
632
|
+
this.procCode_ += '%s';
|
|
633
|
+
}
|
|
634
|
+
} else {
|
|
635
|
+
throw new Error(
|
|
636
|
+
'Unexpected input type on a procedure mutator root: ' + input.type);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* Focus on the last argument editor or label editor on the block.
|
|
643
|
+
* @private
|
|
644
|
+
*/
|
|
645
|
+
Blockly.ScratchBlocks.ProcedureUtils.focusLastEditor_ = function() {
|
|
646
|
+
if (this.inputList.length > 0) {
|
|
647
|
+
var newInput = this.inputList[this.inputList.length - 1];
|
|
648
|
+
if (newInput.type == Blockly.DUMMY_INPUT) {
|
|
649
|
+
newInput.fieldRow[0].showEditor_();
|
|
650
|
+
} else if (newInput.type == Blockly.INPUT_VALUE) {
|
|
651
|
+
// Inspect the argument editor.
|
|
652
|
+
var target = newInput.connection.targetBlock();
|
|
653
|
+
target.getField('TEXT').showEditor_();
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
};
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* Externally-visible function to add a label to the procedure declaration.
|
|
660
|
+
* @public
|
|
661
|
+
*/
|
|
662
|
+
Blockly.ScratchBlocks.ProcedureUtils.addLabelExternal = function() {
|
|
663
|
+
Blockly.WidgetDiv.hide(true);
|
|
664
|
+
this.procCode_ = this.procCode_ + ' label text';
|
|
665
|
+
this.updateDisplay_();
|
|
666
|
+
this.focusLastEditor_();
|
|
667
|
+
};
|
|
668
|
+
|
|
669
|
+
/**
|
|
670
|
+
* Externally-visible function to add a boolean argument to the procedure
|
|
671
|
+
* declaration.
|
|
672
|
+
* @public
|
|
673
|
+
*/
|
|
674
|
+
Blockly.ScratchBlocks.ProcedureUtils.addBooleanExternal = function() {
|
|
675
|
+
Blockly.WidgetDiv.hide(true);
|
|
676
|
+
this.procCode_ = this.procCode_ + ' %b';
|
|
677
|
+
this.displayNames_.push('boolean');
|
|
678
|
+
this.argumentIds_.push(Blockly.utils.genUid());
|
|
679
|
+
this.argumentDefaults_.push('false');
|
|
680
|
+
this.updateDisplay_();
|
|
681
|
+
this.focusLastEditor_();
|
|
682
|
+
};
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Externally-visible function to add a string/number argument to the procedure
|
|
686
|
+
* declaration.
|
|
687
|
+
* @public
|
|
688
|
+
*/
|
|
689
|
+
Blockly.ScratchBlocks.ProcedureUtils.addStringNumberExternal = function() {
|
|
690
|
+
Blockly.WidgetDiv.hide(true);
|
|
691
|
+
this.procCode_ = this.procCode_ + ' %s';
|
|
692
|
+
this.displayNames_.push('number or text');
|
|
693
|
+
this.argumentIds_.push(Blockly.utils.genUid());
|
|
694
|
+
this.argumentDefaults_.push('');
|
|
695
|
+
this.updateDisplay_();
|
|
696
|
+
this.focusLastEditor_();
|
|
697
|
+
};
|
|
698
|
+
|
|
699
|
+
/**
|
|
700
|
+
* Externally-visible function to get the warp on procedure declaration.
|
|
701
|
+
* @return {boolean} The value of the warp_ property.
|
|
702
|
+
* @public
|
|
703
|
+
*/
|
|
704
|
+
Blockly.ScratchBlocks.ProcedureUtils.getWarp = function() {
|
|
705
|
+
return this.warp_;
|
|
706
|
+
};
|
|
707
|
+
|
|
708
|
+
/**
|
|
709
|
+
* Externally-visible function to set the warp on procedure declaration.
|
|
710
|
+
* @param {boolean} warp The value of the warp_ property.
|
|
711
|
+
* @public
|
|
712
|
+
*/
|
|
713
|
+
Blockly.ScratchBlocks.ProcedureUtils.setWarp = function(warp) {
|
|
714
|
+
this.warp_ = warp;
|
|
715
|
+
};
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* @this {BlockSvg}
|
|
719
|
+
* @returns {number} Value of the return_ property. See enum in constants.js
|
|
720
|
+
*/
|
|
721
|
+
Blockly.ScratchBlocks.ProcedureUtils.getReturn = function() {
|
|
722
|
+
return this.return_;
|
|
723
|
+
};
|
|
724
|
+
|
|
725
|
+
/**
|
|
726
|
+
* Callback to remove a field, only for the declaration block.
|
|
727
|
+
* @param {Blockly.Field} field The field being removed.
|
|
728
|
+
* @public
|
|
729
|
+
*/
|
|
730
|
+
Blockly.ScratchBlocks.ProcedureUtils.removeFieldCallback = function(field) {
|
|
731
|
+
// Do not delete if there is only one input
|
|
732
|
+
if (this.inputList.length === 1) {
|
|
733
|
+
return;
|
|
734
|
+
}
|
|
735
|
+
var inputNameToRemove = null;
|
|
736
|
+
for (var n = 0; n < this.inputList.length; n++) {
|
|
737
|
+
var input = this.inputList[n];
|
|
738
|
+
if (input.connection) {
|
|
739
|
+
var target = input.connection.targetBlock();
|
|
740
|
+
if (target.getField(field.name) == field) {
|
|
741
|
+
inputNameToRemove = input.name;
|
|
742
|
+
}
|
|
743
|
+
} else {
|
|
744
|
+
for (var j = 0; j < input.fieldRow.length; j++) {
|
|
745
|
+
if (input.fieldRow[j] == field) {
|
|
746
|
+
inputNameToRemove = input.name;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
if (inputNameToRemove) {
|
|
752
|
+
Blockly.WidgetDiv.hide(true);
|
|
753
|
+
this.removeInput(inputNameToRemove);
|
|
754
|
+
this.onChangeFn();
|
|
755
|
+
this.updateDisplay_();
|
|
756
|
+
}
|
|
757
|
+
};
|
|
758
|
+
|
|
759
|
+
/**
|
|
760
|
+
* Callback to pass removeField up to the declaration block from arguments.
|
|
761
|
+
* @param {Blockly.Field} field The field being removed.
|
|
762
|
+
* @public
|
|
763
|
+
*/
|
|
764
|
+
Blockly.ScratchBlocks.ProcedureUtils.removeArgumentCallback_ = function(
|
|
765
|
+
field) {
|
|
766
|
+
if (this.parentBlock_ && this.parentBlock_.removeFieldCallback) {
|
|
767
|
+
this.parentBlock_.removeFieldCallback(field);
|
|
768
|
+
}
|
|
769
|
+
};
|
|
770
|
+
|
|
771
|
+
/**
|
|
772
|
+
* Update argument reporter field values after an edit to the prototype mutation
|
|
773
|
+
* using previous argument ids and names.
|
|
774
|
+
* Because the argument reporters only store names and not which argument ids they
|
|
775
|
+
* are linked to, it would not be safe to update all argument reporters on the workspace
|
|
776
|
+
* since they may be argument reporters with the same name from a different procedure.
|
|
777
|
+
* Until there is a more explicit way of identifying argument reporter blocks using ids,
|
|
778
|
+
* be conservative and only update argument reporters that are used in the
|
|
779
|
+
* stack below the prototype, ie the definition.
|
|
780
|
+
* @param {!Array<string>} prevArgIds The previous ordering of argument ids.
|
|
781
|
+
* @param {!Array<string>} prevDisplayNames The previous argument names.
|
|
782
|
+
* @this Blockly.Block
|
|
783
|
+
*/
|
|
784
|
+
Blockly.ScratchBlocks.ProcedureUtils.updateArgumentReporterNames_ = function(prevArgIds, prevDisplayNames) {
|
|
785
|
+
var nameChanges = [];
|
|
786
|
+
var argReporters = [];
|
|
787
|
+
var definitionBlock = this.getParent();
|
|
788
|
+
if (!definitionBlock) return;
|
|
789
|
+
|
|
790
|
+
// Create a list of argument reporters that are descendants of the definition stack (see above comment)
|
|
791
|
+
var allBlocks = definitionBlock.getDescendants(false);
|
|
792
|
+
for (var i = 0; i < allBlocks.length; i++) {
|
|
793
|
+
var block = allBlocks[i];
|
|
794
|
+
if ((block.type === 'argument_reporter_string_number' ||
|
|
795
|
+
block.type === 'argument_reporter_boolean') &&
|
|
796
|
+
!block.isShadow()) { // Exclude arg reporters in the prototype block, which are shadows.
|
|
797
|
+
argReporters.push(block);
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
// Create a list of "name changes", including the new name and blocks matching the old name
|
|
802
|
+
// Only search over the current set of argument ids, ignore args that have been removed
|
|
803
|
+
for (var i = 0, id; id = this.argumentIds_[i]; i++) {
|
|
804
|
+
// Find the previous index of this argument id. Could be -1 if it is newly added.
|
|
805
|
+
var prevIndex = prevArgIds.indexOf(id);
|
|
806
|
+
if (prevIndex == -1) continue; // Newly added argument, no corresponding previous argument to update.
|
|
807
|
+
var prevName = prevDisplayNames[prevIndex];
|
|
808
|
+
if (prevName != this.displayNames_[i]) {
|
|
809
|
+
nameChanges.push({
|
|
810
|
+
newName: this.displayNames_[i],
|
|
811
|
+
blocks: argReporters.filter(function(block) {
|
|
812
|
+
return block.getFieldValue('VALUE') == prevName;
|
|
813
|
+
})
|
|
814
|
+
});
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
// Finally update the blocks for each name change.
|
|
819
|
+
// Do this after creating the lists to avoid cycles of renaming.
|
|
820
|
+
for (var j = 0, nameChange; nameChange = nameChanges[j]; j++) {
|
|
821
|
+
for (var k = 0, block; block = nameChange.blocks[k]; k++) {
|
|
822
|
+
block.setFieldValue(nameChange.newName, 'VALUE');
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
};
|
|
826
|
+
|
|
827
|
+
Blockly.Blocks['procedures_definition'] = {
|
|
828
|
+
/**
|
|
829
|
+
* Block for defining a procedure with no return value.
|
|
830
|
+
* @this Blockly.Block
|
|
831
|
+
*/
|
|
832
|
+
init: function() {
|
|
833
|
+
this.jsonInit({
|
|
834
|
+
"message0": Blockly.Msg.PROCEDURES_DEFINITION,
|
|
835
|
+
"args0": [
|
|
836
|
+
{
|
|
837
|
+
"type": "input_statement",
|
|
838
|
+
"name": "custom_block"
|
|
839
|
+
}
|
|
840
|
+
],
|
|
841
|
+
"extensions": ["colours_more", "shape_hat", "procedure_def_contextmenu"]
|
|
842
|
+
});
|
|
843
|
+
}
|
|
844
|
+
};
|
|
845
|
+
|
|
846
|
+
Blockly.Blocks['procedures_call'] = {
|
|
847
|
+
/**
|
|
848
|
+
* Block for calling a procedure with no return value.
|
|
849
|
+
* @this Blockly.Block
|
|
850
|
+
*/
|
|
851
|
+
init: function() {
|
|
852
|
+
this.jsonInit({
|
|
853
|
+
"extensions": ["colours_more", "procedure_call_contextmenu"]
|
|
854
|
+
});
|
|
855
|
+
this.procCode_ = '';
|
|
856
|
+
this.argumentIds_ = [];
|
|
857
|
+
this.warp_ = false;
|
|
858
|
+
this.return_ = Blockly.PROCEDURES_CALL_TYPE_STATEMENT;
|
|
859
|
+
},
|
|
860
|
+
// Shared.
|
|
861
|
+
getProcCode: Blockly.ScratchBlocks.ProcedureUtils.getProcCode,
|
|
862
|
+
removeAllInputs_: Blockly.ScratchBlocks.ProcedureUtils.removeAllInputs_,
|
|
863
|
+
disconnectOldBlocks_: Blockly.ScratchBlocks.ProcedureUtils.disconnectOldBlocks_,
|
|
864
|
+
deleteShadows_: Blockly.ScratchBlocks.ProcedureUtils.deleteShadows_,
|
|
865
|
+
createAllInputs_: Blockly.ScratchBlocks.ProcedureUtils.createAllInputs_,
|
|
866
|
+
updateDisplay_: Blockly.ScratchBlocks.ProcedureUtils.updateDisplay_,
|
|
867
|
+
getReturn: Blockly.ScratchBlocks.ProcedureUtils.getReturn,
|
|
868
|
+
|
|
869
|
+
// Exist on all three blocks, but have different implementations.
|
|
870
|
+
mutationToDom: Blockly.ScratchBlocks.ProcedureUtils.callerMutationToDom,
|
|
871
|
+
domToMutation: Blockly.ScratchBlocks.ProcedureUtils.callerDomToMutation,
|
|
872
|
+
populateArgument_: Blockly.ScratchBlocks.ProcedureUtils.populateArgumentOnCaller_,
|
|
873
|
+
addProcedureLabel_: Blockly.ScratchBlocks.ProcedureUtils.addLabelField_,
|
|
874
|
+
|
|
875
|
+
// Only exists on the external caller.
|
|
876
|
+
attachShadow_: Blockly.ScratchBlocks.ProcedureUtils.attachShadow_,
|
|
877
|
+
buildShadowDom_: Blockly.ScratchBlocks.ProcedureUtils.buildShadowDom_
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
Blockly.Blocks['procedures_prototype'] = {
|
|
881
|
+
/**
|
|
882
|
+
* Block for calling a procedure with no return value, for rendering inside
|
|
883
|
+
* define block.
|
|
884
|
+
* @this Blockly.Block
|
|
885
|
+
*/
|
|
886
|
+
init: function() {
|
|
887
|
+
this.jsonInit({
|
|
888
|
+
"extensions": ["colours_more", "shape_statement"]
|
|
889
|
+
});
|
|
890
|
+
|
|
891
|
+
/* Data known about the procedure. */
|
|
892
|
+
this.procCode_ = '';
|
|
893
|
+
this.displayNames_ = [];
|
|
894
|
+
this.argumentIds_ = [];
|
|
895
|
+
this.argumentDefaults_ = [];
|
|
896
|
+
this.warp_ = false;
|
|
897
|
+
},
|
|
898
|
+
// Shared.
|
|
899
|
+
getProcCode: Blockly.ScratchBlocks.ProcedureUtils.getProcCode,
|
|
900
|
+
removeAllInputs_: Blockly.ScratchBlocks.ProcedureUtils.removeAllInputs_,
|
|
901
|
+
disconnectOldBlocks_: Blockly.ScratchBlocks.ProcedureUtils.disconnectOldBlocks_,
|
|
902
|
+
deleteShadows_: Blockly.ScratchBlocks.ProcedureUtils.deleteShadows_,
|
|
903
|
+
createAllInputs_: Blockly.ScratchBlocks.ProcedureUtils.createAllInputs_,
|
|
904
|
+
updateDisplay_: Blockly.ScratchBlocks.ProcedureUtils.updateDisplay_,
|
|
905
|
+
|
|
906
|
+
// Exist on all three blocks, but have different implementations.
|
|
907
|
+
mutationToDom: Blockly.ScratchBlocks.ProcedureUtils.definitionMutationToDom,
|
|
908
|
+
domToMutation: Blockly.ScratchBlocks.ProcedureUtils.definitionDomToMutation,
|
|
909
|
+
populateArgument_: Blockly.ScratchBlocks.ProcedureUtils.populateArgumentOnPrototype_,
|
|
910
|
+
addProcedureLabel_: Blockly.ScratchBlocks.ProcedureUtils.addLabelField_,
|
|
911
|
+
|
|
912
|
+
// Only exists on procedures_prototype.
|
|
913
|
+
createArgumentReporter_: Blockly.ScratchBlocks.ProcedureUtils.createArgumentReporter_,
|
|
914
|
+
updateArgumentReporterNames_: Blockly.ScratchBlocks.ProcedureUtils.updateArgumentReporterNames_
|
|
915
|
+
};
|
|
916
|
+
|
|
917
|
+
Blockly.Blocks['procedures_declaration'] = {
|
|
918
|
+
/**
|
|
919
|
+
* The root block in the procedure declaration editor.
|
|
920
|
+
* @this Blockly.Block
|
|
921
|
+
*/
|
|
922
|
+
init: function() {
|
|
923
|
+
this.jsonInit({
|
|
924
|
+
"extensions": ["colours_more", "shape_statement"]
|
|
925
|
+
});
|
|
926
|
+
/* Data known about the procedure. */
|
|
927
|
+
this.procCode_ = '';
|
|
928
|
+
this.displayNames_ = [];
|
|
929
|
+
this.argumentIds_ = [];
|
|
930
|
+
this.argumentDefaults_ = [];
|
|
931
|
+
this.warp_ = false;
|
|
932
|
+
},
|
|
933
|
+
// Shared.
|
|
934
|
+
getProcCode: Blockly.ScratchBlocks.ProcedureUtils.getProcCode,
|
|
935
|
+
removeAllInputs_: Blockly.ScratchBlocks.ProcedureUtils.removeAllInputs_,
|
|
936
|
+
disconnectOldBlocks_: Blockly.ScratchBlocks.ProcedureUtils.disconnectOldBlocks_,
|
|
937
|
+
deleteShadows_: Blockly.ScratchBlocks.ProcedureUtils.deleteShadows_,
|
|
938
|
+
createAllInputs_: Blockly.ScratchBlocks.ProcedureUtils.createAllInputs_,
|
|
939
|
+
updateDisplay_: Blockly.ScratchBlocks.ProcedureUtils.updateDisplay_,
|
|
940
|
+
|
|
941
|
+
// Exist on all three blocks, but have different implementations.
|
|
942
|
+
mutationToDom: Blockly.ScratchBlocks.ProcedureUtils.definitionMutationToDom,
|
|
943
|
+
domToMutation: Blockly.ScratchBlocks.ProcedureUtils.definitionDomToMutation,
|
|
944
|
+
populateArgument_: Blockly.ScratchBlocks.ProcedureUtils.populateArgumentOnDeclaration_,
|
|
945
|
+
addProcedureLabel_: Blockly.ScratchBlocks.ProcedureUtils.addLabelEditor_,
|
|
946
|
+
|
|
947
|
+
// Exist on declaration and arguments editors, with different implementations.
|
|
948
|
+
removeFieldCallback: Blockly.ScratchBlocks.ProcedureUtils.removeFieldCallback,
|
|
949
|
+
|
|
950
|
+
// Only exist on procedures_declaration.
|
|
951
|
+
createArgumentEditor_: Blockly.ScratchBlocks.ProcedureUtils.createArgumentEditor_,
|
|
952
|
+
focusLastEditor_: Blockly.ScratchBlocks.ProcedureUtils.focusLastEditor_,
|
|
953
|
+
getWarp: Blockly.ScratchBlocks.ProcedureUtils.getWarp,
|
|
954
|
+
setWarp: Blockly.ScratchBlocks.ProcedureUtils.setWarp,
|
|
955
|
+
addLabelExternal: Blockly.ScratchBlocks.ProcedureUtils.addLabelExternal,
|
|
956
|
+
addBooleanExternal: Blockly.ScratchBlocks.ProcedureUtils.addBooleanExternal,
|
|
957
|
+
addStringNumberExternal: Blockly.ScratchBlocks.ProcedureUtils.addStringNumberExternal,
|
|
958
|
+
onChangeFn: Blockly.ScratchBlocks.ProcedureUtils.updateDeclarationProcCode_
|
|
959
|
+
};
|
|
960
|
+
|
|
961
|
+
Blockly.Blocks['argument_reporter_boolean'] = {
|
|
962
|
+
init: function() {
|
|
963
|
+
this.jsonInit({ "message0": " %1",
|
|
964
|
+
"args0": [
|
|
965
|
+
{
|
|
966
|
+
"type": "field_label_serializable",
|
|
967
|
+
"name": "VALUE",
|
|
968
|
+
"text": ""
|
|
969
|
+
}
|
|
970
|
+
],
|
|
971
|
+
"extensions": ["colours_more", "output_boolean"]
|
|
972
|
+
});
|
|
973
|
+
}
|
|
974
|
+
};
|
|
975
|
+
|
|
976
|
+
Blockly.Blocks['argument_reporter_string_number'] = {
|
|
977
|
+
init: function() {
|
|
978
|
+
this.jsonInit({ "message0": " %1",
|
|
979
|
+
"args0": [
|
|
980
|
+
{
|
|
981
|
+
"type": "field_label_serializable",
|
|
982
|
+
"name": "VALUE",
|
|
983
|
+
"text": ""
|
|
984
|
+
}
|
|
985
|
+
],
|
|
986
|
+
"extensions": ["colours_more", "output_number", "output_string"]
|
|
987
|
+
});
|
|
988
|
+
}
|
|
989
|
+
};
|
|
990
|
+
|
|
991
|
+
Blockly.Blocks['argument_editor_boolean'] = {
|
|
992
|
+
init: function() {
|
|
993
|
+
this.jsonInit({ "message0": " %1",
|
|
994
|
+
"args0": [
|
|
995
|
+
{
|
|
996
|
+
"type": "field_input_removable",
|
|
997
|
+
"name": "TEXT",
|
|
998
|
+
"text": "foo"
|
|
999
|
+
}
|
|
1000
|
+
],
|
|
1001
|
+
"colour": Blockly.Colours.textField,
|
|
1002
|
+
"colourSecondary": Blockly.Colours.textField,
|
|
1003
|
+
"colourTertiary": Blockly.Colours.textField,
|
|
1004
|
+
"colourQuaternary": Blockly.Colours.textField,
|
|
1005
|
+
"extensions": ["output_boolean"]
|
|
1006
|
+
});
|
|
1007
|
+
},
|
|
1008
|
+
// Exist on declaration and arguments editors, with different implementations.
|
|
1009
|
+
removeFieldCallback: Blockly.ScratchBlocks.ProcedureUtils.removeArgumentCallback_
|
|
1010
|
+
};
|
|
1011
|
+
|
|
1012
|
+
Blockly.Blocks['argument_editor_string_number'] = {
|
|
1013
|
+
init: function() {
|
|
1014
|
+
this.jsonInit({ "message0": " %1",
|
|
1015
|
+
"args0": [
|
|
1016
|
+
{
|
|
1017
|
+
"type": "field_input_removable",
|
|
1018
|
+
"name": "TEXT",
|
|
1019
|
+
"text": "foo"
|
|
1020
|
+
}
|
|
1021
|
+
],
|
|
1022
|
+
"colour": Blockly.Colours.textField,
|
|
1023
|
+
"colourSecondary": Blockly.Colours.textField,
|
|
1024
|
+
"colourTertiary": Blockly.Colours.textField,
|
|
1025
|
+
"colourQuaternary": Blockly.Colours.textField,
|
|
1026
|
+
"extensions": ["output_number", "output_string"]
|
|
1027
|
+
});
|
|
1028
|
+
},
|
|
1029
|
+
// Exist on declaration and arguments editors, with different implementations.
|
|
1030
|
+
removeFieldCallback: Blockly.ScratchBlocks.ProcedureUtils.removeArgumentCallback_
|
|
1031
|
+
};
|
|
1032
|
+
|
|
1033
|
+
Blockly.Blocks['procedures_return'] = {
|
|
1034
|
+
/**
|
|
1035
|
+
* Point towards drop-down menu.
|
|
1036
|
+
* @this Blockly.Block
|
|
1037
|
+
*/
|
|
1038
|
+
init: function() {
|
|
1039
|
+
this.jsonInit({
|
|
1040
|
+
"message0": Blockly.Msg.PROCEDURES_RETURN,
|
|
1041
|
+
"args0": [
|
|
1042
|
+
{
|
|
1043
|
+
"type": "input_value",
|
|
1044
|
+
"name": "VALUE"
|
|
1045
|
+
}
|
|
1046
|
+
],
|
|
1047
|
+
"extensions": ["colours_more", "shape_end"]
|
|
1048
|
+
});
|
|
1049
|
+
this.workspace.enableProcedureReturns();
|
|
1050
|
+
}
|
|
1051
|
+
};
|