scratch-blocks 0.1.0-prerelease.20211005172512

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 (243) hide show
  1. package/LICENSE +177 -0
  2. package/README.md +23 -0
  3. package/TRADEMARK +1 -0
  4. package/blockly_compressed_horizontal.js +2253 -0
  5. package/blockly_compressed_vertical.js +2281 -0
  6. package/blockly_uncompressed_horizontal.js +1210 -0
  7. package/blockly_uncompressed_vertical.js +1210 -0
  8. package/blocks_common/colour.js +61 -0
  9. package/blocks_common/math.js +154 -0
  10. package/blocks_common/matrix.js +54 -0
  11. package/blocks_common/note.js +57 -0
  12. package/blocks_common/text.js +56 -0
  13. package/blocks_compressed.js +36 -0
  14. package/blocks_compressed_horizontal.js +58 -0
  15. package/blocks_compressed_vertical.js +207 -0
  16. package/blocks_horizontal/control.js +207 -0
  17. package/blocks_horizontal/default_toolbox.js +139 -0
  18. package/blocks_horizontal/event.js +185 -0
  19. package/blocks_horizontal/wedo.js +316 -0
  20. package/blocks_vertical/control.js +531 -0
  21. package/blocks_vertical/data.js +670 -0
  22. package/blocks_vertical/default_toolbox.js +564 -0
  23. package/blocks_vertical/event.js +328 -0
  24. package/blocks_vertical/extensions.js +294 -0
  25. package/blocks_vertical/looks.js +589 -0
  26. package/blocks_vertical/motion.js +584 -0
  27. package/blocks_vertical/operators.js +470 -0
  28. package/blocks_vertical/procedures.js +971 -0
  29. package/blocks_vertical/sensing.js +539 -0
  30. package/blocks_vertical/sound.js +245 -0
  31. package/blocks_vertical/vertical_extensions.js +264 -0
  32. package/build/gen_blocks.js +1 -0
  33. package/build/test_expect.js +1 -0
  34. package/build/test_input.js +1 -0
  35. package/build.py +630 -0
  36. package/cleanup.sh +101 -0
  37. package/core/block.js +1811 -0
  38. package/core/block_animations.js +107 -0
  39. package/core/block_drag_surface.js +299 -0
  40. package/core/block_dragger.js +421 -0
  41. package/core/block_events.js +531 -0
  42. package/core/block_render_svg_horizontal.js +890 -0
  43. package/core/block_render_svg_vertical.js +1732 -0
  44. package/core/block_svg.js +1314 -0
  45. package/core/blockly.js +622 -0
  46. package/core/blocks.js +37 -0
  47. package/core/bubble.js +664 -0
  48. package/core/bubble_dragger.js +285 -0
  49. package/core/colours.js +147 -0
  50. package/core/comment.js +293 -0
  51. package/core/comment_events.js +539 -0
  52. package/core/connection.js +766 -0
  53. package/core/connection_db.js +300 -0
  54. package/core/constants.js +387 -0
  55. package/core/contextmenu.js +519 -0
  56. package/core/css.js +1350 -0
  57. package/core/data_category.js +490 -0
  58. package/core/dragged_connection_manager.js +260 -0
  59. package/core/dropdowndiv.js +408 -0
  60. package/core/events.js +429 -0
  61. package/core/events_abstract.js +113 -0
  62. package/core/extensions.js +450 -0
  63. package/core/field.js +807 -0
  64. package/core/field_angle.js +398 -0
  65. package/core/field_checkbox.js +133 -0
  66. package/core/field_colour.js +253 -0
  67. package/core/field_colour_slider.js +386 -0
  68. package/core/field_date.js +353 -0
  69. package/core/field_dropdown.js +483 -0
  70. package/core/field_iconmenu.js +307 -0
  71. package/core/field_image.js +193 -0
  72. package/core/field_label.js +136 -0
  73. package/core/field_label_serializable.js +125 -0
  74. package/core/field_matrix.js +566 -0
  75. package/core/field_note.js +850 -0
  76. package/core/field_number.js +366 -0
  77. package/core/field_numberdropdown.js +77 -0
  78. package/core/field_textdropdown.js +164 -0
  79. package/core/field_textinput.js +675 -0
  80. package/core/field_textinput_removable.js +105 -0
  81. package/core/field_variable.js +385 -0
  82. package/core/field_variable_getter.js +185 -0
  83. package/core/field_vertical_separator.js +161 -0
  84. package/core/flyout_base.js +923 -0
  85. package/core/flyout_button.js +322 -0
  86. package/core/flyout_dragger.js +83 -0
  87. package/core/flyout_extension_category_header.js +159 -0
  88. package/core/flyout_horizontal.js +475 -0
  89. package/core/flyout_vertical.js +770 -0
  90. package/core/generator.js +426 -0
  91. package/core/gesture.js +1011 -0
  92. package/core/grid.js +227 -0
  93. package/core/icon.js +205 -0
  94. package/core/inject.js +491 -0
  95. package/core/input.js +285 -0
  96. package/core/insertion_marker_manager.js +678 -0
  97. package/core/msg.js +62 -0
  98. package/core/mutator.js +426 -0
  99. package/core/names.js +198 -0
  100. package/core/options.js +244 -0
  101. package/core/procedures.js +577 -0
  102. package/core/rendered_connection.js +417 -0
  103. package/core/scratch_block_comment.js +653 -0
  104. package/core/scratch_blocks_utils.js +244 -0
  105. package/core/scratch_bubble.js +696 -0
  106. package/core/scratch_events.js +131 -0
  107. package/core/scratch_msgs.js +85 -0
  108. package/core/scrollbar.js +875 -0
  109. package/core/toolbox.js +801 -0
  110. package/core/tooltip.js +337 -0
  111. package/core/touch.js +226 -0
  112. package/core/trashcan.js +343 -0
  113. package/core/ui_events.js +91 -0
  114. package/core/ui_menu_utils.js +68 -0
  115. package/core/utils.js +948 -0
  116. package/core/variable_events.js +259 -0
  117. package/core/variable_map.js +415 -0
  118. package/core/variable_model.js +116 -0
  119. package/core/variables.js +674 -0
  120. package/core/warning.js +199 -0
  121. package/core/widgetdiv.js +344 -0
  122. package/core/workspace.js +673 -0
  123. package/core/workspace_audio.js +170 -0
  124. package/core/workspace_comment.js +432 -0
  125. package/core/workspace_comment_render_svg.js +723 -0
  126. package/core/workspace_comment_svg.js +611 -0
  127. package/core/workspace_drag_surface_svg.js +195 -0
  128. package/core/workspace_dragger.js +132 -0
  129. package/core/workspace_svg.js +2267 -0
  130. package/core/xml.js +919 -0
  131. package/core/zoom_controls.js +301 -0
  132. package/dist/horizontal.js +222 -0
  133. package/dist/vertical.js +222 -0
  134. package/dist/web/horizontal.js +232 -0
  135. package/dist/web/vertical.js +232 -0
  136. package/i18n/common.py +234 -0
  137. package/i18n/create_messages.py +165 -0
  138. package/i18n/create_scratch_msgs.js +69 -0
  139. package/i18n/dedup_json.py +73 -0
  140. package/i18n/js_to_json.js +46 -0
  141. package/i18n/js_to_json.py +136 -0
  142. package/i18n/json_to_js.js +52 -0
  143. package/i18n/json_to_js.py +185 -0
  144. package/i18n/sync_translations.sh +17 -0
  145. package/i18n/sync_tx_translations.js +121 -0
  146. package/i18n/test_scratch_msgs.js +87 -0
  147. package/i18n/tests.py +47 -0
  148. package/i18n/xliff_to_json.py +232 -0
  149. package/local_build.sh +70 -0
  150. package/media/1x1.gif +0 -0
  151. package/media/click.mp3 +0 -0
  152. package/media/click.ogg +0 -0
  153. package/media/click.wav +0 -0
  154. package/media/comment-arrow-down.svg +10 -0
  155. package/media/comment-arrow-up.svg +10 -0
  156. package/media/delete-x.svg +10 -0
  157. package/media/delete.mp3 +0 -0
  158. package/media/delete.ogg +0 -0
  159. package/media/delete.wav +0 -0
  160. package/media/dropdown-arrow-dark.svg +1 -0
  161. package/media/dropdown-arrow.svg +1 -0
  162. package/media/extensions/microbit-block-icon.svg +130 -0
  163. package/media/extensions/music-block-icon.svg +17 -0
  164. package/media/extensions/pen-block-icon.svg +19 -0
  165. package/media/extensions/wedo2-block-icon.svg +36 -0
  166. package/media/eyedropper.svg +22 -0
  167. package/media/green-flag.svg +17 -0
  168. package/media/handclosed.cur +0 -0
  169. package/media/handdelete.cur +0 -0
  170. package/media/handopen.cur +0 -0
  171. package/media/icons/arrow.svg +12 -0
  172. package/media/icons/arrow_button.svg +1 -0
  173. package/media/icons/control_forever.svg +1 -0
  174. package/media/icons/control_repeat.svg +1 -0
  175. package/media/icons/control_stop.svg +1 -0
  176. package/media/icons/control_wait.svg +1 -0
  177. package/media/icons/event_broadcast_blue.svg +1 -0
  178. package/media/icons/event_broadcast_coral.svg +1 -0
  179. package/media/icons/event_broadcast_green.svg +1 -0
  180. package/media/icons/event_broadcast_magenta.svg +1 -0
  181. package/media/icons/event_broadcast_orange.svg +1 -0
  182. package/media/icons/event_broadcast_purple.svg +1 -0
  183. package/media/icons/event_when-broadcast-received_blue.svg +1 -0
  184. package/media/icons/event_when-broadcast-received_coral.svg +1 -0
  185. package/media/icons/event_when-broadcast-received_green.svg +1 -0
  186. package/media/icons/event_when-broadcast-received_magenta.svg +1 -0
  187. package/media/icons/event_when-broadcast-received_orange.svg +1 -0
  188. package/media/icons/event_when-broadcast-received_purple.svg +1 -0
  189. package/media/icons/event_whenflagclicked.svg +1 -0
  190. package/media/icons/remove.svg +19 -0
  191. package/media/icons/set-led_blue.svg +1 -0
  192. package/media/icons/set-led_coral.svg +1 -0
  193. package/media/icons/set-led_green.svg +1 -0
  194. package/media/icons/set-led_magenta.svg +1 -0
  195. package/media/icons/set-led_mystery.svg +1 -0
  196. package/media/icons/set-led_orange.svg +1 -0
  197. package/media/icons/set-led_purple.svg +1 -0
  198. package/media/icons/set-led_white.svg +1 -0
  199. package/media/icons/set-led_yellow.svg +1 -0
  200. package/media/icons/wedo_motor-clockwise.svg +1 -0
  201. package/media/icons/wedo_motor-counterclockwise.svg +1 -0
  202. package/media/icons/wedo_motor-speed_fast.svg +1 -0
  203. package/media/icons/wedo_motor-speed_med.svg +1 -0
  204. package/media/icons/wedo_motor-speed_slow.svg +1 -0
  205. package/media/icons/wedo_when-distance_close.svg +1 -0
  206. package/media/icons/wedo_when-tilt-backward.svg +1 -0
  207. package/media/icons/wedo_when-tilt-forward.svg +1 -0
  208. package/media/icons/wedo_when-tilt-left.svg +1 -0
  209. package/media/icons/wedo_when-tilt-right.svg +1 -0
  210. package/media/icons/wedo_when-tilt.svg +1 -0
  211. package/media/repeat.svg +18 -0
  212. package/media/rotate-left.svg +1 -0
  213. package/media/rotate-right.svg +1 -0
  214. package/media/sprites.png +0 -0
  215. package/media/sprites.svg +74 -0
  216. package/media/status-not-ready.svg +13 -0
  217. package/media/status-ready.svg +13 -0
  218. package/media/zoom-in.svg +1 -0
  219. package/media/zoom-out.svg +1 -0
  220. package/media/zoom-reset.svg +1 -0
  221. package/msg/js/en.js +290 -0
  222. package/msg/json/en.json +285 -0
  223. package/msg/messages.js +353 -0
  224. package/msg/scratch_msgs.js +21247 -0
  225. package/package.json +52 -0
  226. package/pull_from_blockly.sh +151 -0
  227. package/renovate.json5 +15 -0
  228. package/shim/blockly_compressed_horizontal-blocks_compressed.js +1 -0
  229. package/shim/blockly_compressed_horizontal.Blockly.js +1 -0
  230. package/shim/blockly_compressed_horizontal.goog.js +1 -0
  231. package/shim/blockly_compressed_horizontal.js +1 -0
  232. package/shim/blockly_compressed_vertical-blocks_compressed.js +1 -0
  233. package/shim/blockly_compressed_vertical.Blockly.js +1 -0
  234. package/shim/blockly_compressed_vertical.goog.js +1 -0
  235. package/shim/blockly_compressed_vertical.js +1 -0
  236. package/shim/blocks_compressed_horizontal-blockly_compressed_horizontal-messages.js +1 -0
  237. package/shim/blocks_compressed_horizontal.js +1 -0
  238. package/shim/blocks_compressed_vertical-blockly_compressed_vertical-messages.js +1 -0
  239. package/shim/blocks_compressed_vertical.js +1 -0
  240. package/shim/gh-pages.js +1 -0
  241. package/shim/horizontal.js +1 -0
  242. package/shim/index.js +17 -0
  243. package/shim/vertical.js +1 -0
@@ -0,0 +1,577 @@
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
+ 'use strict';
26
+
27
+ /**
28
+ * @name Blockly.Procedures
29
+ * @namespace
30
+ **/
31
+ goog.provide('Blockly.Procedures');
32
+
33
+ goog.require('Blockly.Blocks');
34
+ goog.require('Blockly.constants');
35
+ goog.require('Blockly.Events.BlockChange');
36
+ goog.require('Blockly.Field');
37
+ goog.require('Blockly.Names');
38
+ goog.require('Blockly.Workspace');
39
+
40
+
41
+ /**
42
+ * Constant to separate procedure names from variables and generated functions
43
+ * when running generators.
44
+ * @deprecated Use Blockly.PROCEDURE_CATEGORY_NAME
45
+ */
46
+ Blockly.Procedures.NAME_TYPE = Blockly.PROCEDURE_CATEGORY_NAME;
47
+
48
+ /**
49
+ * Find all user-created procedure definitions in a workspace.
50
+ * @param {!Blockly.Workspace} root Root workspace.
51
+ * @return {!Array.<!Array.<!Array>>} Pair of arrays, the
52
+ * first contains procedures without return variables, the second with.
53
+ * Each procedure is defined by a three-element list of name, parameter
54
+ * list, and return value boolean.
55
+ */
56
+ Blockly.Procedures.allProcedures = function(root) {
57
+ var blocks = root.getAllBlocks();
58
+ var proceduresReturn = [];
59
+ var proceduresNoReturn = [];
60
+ for (var i = 0; i < blocks.length; i++) {
61
+ if (blocks[i].getProcedureDef) {
62
+ var tuple = blocks[i].getProcedureDef();
63
+ if (tuple) {
64
+ if (tuple[2]) {
65
+ proceduresReturn.push(tuple);
66
+ } else {
67
+ proceduresNoReturn.push(tuple);
68
+ }
69
+ }
70
+ }
71
+ }
72
+ proceduresNoReturn.sort(Blockly.Procedures.procTupleComparator_);
73
+ proceduresReturn.sort(Blockly.Procedures.procTupleComparator_);
74
+ return [proceduresNoReturn, proceduresReturn];
75
+ };
76
+
77
+ /**
78
+ * Find all user-created procedure definition mutations in a workspace.
79
+ * @param {!Blockly.Workspace} root Root workspace.
80
+ * @return {!Array.<Element>} Array of mutation xml elements.
81
+ * @package
82
+ */
83
+ Blockly.Procedures.allProcedureMutations = function(root) {
84
+ var blocks = root.getAllBlocks();
85
+ var mutations = [];
86
+ for (var i = 0; i < blocks.length; i++) {
87
+ if (blocks[i].type == Blockly.PROCEDURES_PROTOTYPE_BLOCK_TYPE) {
88
+ var mutation = blocks[i].mutationToDom(/* opt_generateShadows */ true);
89
+ if (mutation) {
90
+ mutations.push(mutation);
91
+ }
92
+ }
93
+ }
94
+ return mutations;
95
+ };
96
+
97
+ /**
98
+ * Sorts an array of procedure definition mutations alphabetically.
99
+ * (Does not mutate the given array.)
100
+ * @param {!Array.<Element>} mutations Array of mutation xml elements.
101
+ * @return {!Array.<Element>} Sorted array of mutation xml elements.
102
+ * @private
103
+ */
104
+ Blockly.Procedures.sortProcedureMutations_ = function(mutations) {
105
+ var newMutations = mutations.slice();
106
+
107
+ newMutations.sort(function(a, b) {
108
+ var procCodeA = a.getAttribute('proccode');
109
+ var procCodeB = b.getAttribute('proccode');
110
+
111
+ return Blockly.scratchBlocksUtils.compareStrings(procCodeA, procCodeB);
112
+ });
113
+
114
+ return newMutations;
115
+ };
116
+
117
+ /**
118
+ * Comparison function for case-insensitive sorting of the first element of
119
+ * a tuple.
120
+ * @param {!Array} ta First tuple.
121
+ * @param {!Array} tb Second tuple.
122
+ * @return {number} -1, 0, or 1 to signify greater than, equality, or less than.
123
+ * @private
124
+ */
125
+ Blockly.Procedures.procTupleComparator_ = function(ta, tb) {
126
+ return Blockly.scratchBlocksUtils.compareStrings(ta[0], tb[0]);
127
+ };
128
+
129
+ /**
130
+ * Ensure two identically-named procedures don't exist.
131
+ * @param {string} name Proposed procedure name.
132
+ * @param {!Blockly.Block} block Block to disambiguate.
133
+ * @return {string} Non-colliding name.
134
+ */
135
+ Blockly.Procedures.findLegalName = function(name, block) {
136
+ if (block.isInFlyout) {
137
+ // Flyouts can have multiple procedures called 'do something'.
138
+ return name;
139
+ }
140
+ while (!Blockly.Procedures.isLegalName_(name, block.workspace, block)) {
141
+ // Collision with another procedure.
142
+ var r = name.match(/^(.*?)(\d+)$/);
143
+ if (!r) {
144
+ name += '2';
145
+ } else {
146
+ name = r[1] + (parseInt(r[2], 10) + 1);
147
+ }
148
+ }
149
+ return name;
150
+ };
151
+
152
+ /**
153
+ * Does this procedure have a legal name? Illegal names include names of
154
+ * procedures already defined.
155
+ * @param {string} name The questionable name.
156
+ * @param {!Blockly.Workspace} workspace The workspace to scan for collisions.
157
+ * @param {Blockly.Block=} opt_exclude Optional block to exclude from
158
+ * comparisons (one doesn't want to collide with oneself).
159
+ * @return {boolean} True if the name is legal.
160
+ * @private
161
+ */
162
+ Blockly.Procedures.isLegalName_ = function(name, workspace, opt_exclude) {
163
+ return !Blockly.Procedures.isNameUsed(name, workspace, opt_exclude);
164
+ };
165
+
166
+ /**
167
+ * Return if the given name is already a procedure name.
168
+ * @param {string} name The questionable name.
169
+ * @param {!Blockly.Workspace} workspace The workspace to scan for collisions.
170
+ * @param {Blockly.Block=} opt_exclude Optional block to exclude from
171
+ * comparisons (one doesn't want to collide with oneself).
172
+ * @return {boolean} True if the name is used, otherwise return false.
173
+ */
174
+ Blockly.Procedures.isNameUsed = function(name, workspace, opt_exclude) {
175
+ var blocks = workspace.getAllBlocks();
176
+ // Iterate through every block and check the name.
177
+ for (var i = 0; i < blocks.length; i++) {
178
+ if (blocks[i] == opt_exclude) {
179
+ continue;
180
+ }
181
+ if (blocks[i].getProcedureDef) {
182
+ var procName = blocks[i].getProcedureDef();
183
+ if (Blockly.Names.equals(procName[0], name)) {
184
+ return false;
185
+ }
186
+ }
187
+ }
188
+ return true;
189
+ };
190
+
191
+ /**
192
+ * Rename a procedure. Called by the editable field.
193
+ * @param {string} name The proposed new name.
194
+ * @return {string} The accepted name.
195
+ * @this {Blockly.Field}
196
+ */
197
+ Blockly.Procedures.rename = function(name) {
198
+ // Strip leading and trailing whitespace. Beyond this, all names are legal.
199
+ name = name.replace(/^[\s\xa0]+|[\s\xa0]+$/g, '');
200
+
201
+ // Ensure two identically-named procedures don't exist.
202
+ var legalName = Blockly.Procedures.findLegalName(name, this.sourceBlock_);
203
+ var oldName = this.text_;
204
+ if (oldName != name && oldName != legalName) {
205
+ // Rename any callers.
206
+ var blocks = this.sourceBlock_.workspace.getAllBlocks();
207
+ for (var i = 0; i < blocks.length; i++) {
208
+ if (blocks[i].renameProcedure) {
209
+ blocks[i].renameProcedure(oldName, legalName);
210
+ }
211
+ }
212
+ }
213
+ return legalName;
214
+ };
215
+
216
+ /**
217
+ * Construct the blocks required by the flyout for the procedure category.
218
+ * @param {!Blockly.Workspace} workspace The workspace contianing procedures.
219
+ * @return {!Array.<!Element>} Array of XML block elements.
220
+ */
221
+ Blockly.Procedures.flyoutCategory = function(workspace) {
222
+ var xmlList = [];
223
+
224
+ Blockly.Procedures.addCreateButton_(workspace, xmlList);
225
+
226
+ // Create call blocks for each procedure defined in the workspace
227
+ var mutations = Blockly.Procedures.allProcedureMutations(workspace);
228
+ mutations = Blockly.Procedures.sortProcedureMutations_(mutations);
229
+ for (var i = 0; i < mutations.length; i++) {
230
+ var mutation = mutations[i];
231
+ // <block type="procedures_call">
232
+ // <mutation ...></mutation>
233
+ // </block>
234
+ var block = goog.dom.createDom('block');
235
+ block.setAttribute('type', 'procedures_call');
236
+ block.setAttribute('gap', 16);
237
+ block.appendChild(mutation);
238
+ xmlList.push(block);
239
+ }
240
+ return xmlList;
241
+ };
242
+
243
+ /**
244
+ * Create the "Make a Block..." button.
245
+ * @param {!Blockly.Workspace} workspace The workspace contianing procedures.
246
+ * @param {!Array.<!Element>} xmlList Array of XML block elements to add to.
247
+ * @private
248
+ */
249
+ Blockly.Procedures.addCreateButton_ = function(workspace, xmlList) {
250
+ var button = goog.dom.createDom('button');
251
+ var msg = Blockly.Msg.NEW_PROCEDURE;
252
+ var callbackKey = 'CREATE_PROCEDURE';
253
+ var callback = function() {
254
+ Blockly.Procedures.createProcedureDefCallback_(workspace);
255
+ };
256
+ button.setAttribute('text', msg);
257
+ button.setAttribute('callbackKey', callbackKey);
258
+ workspace.registerButtonCallback(callbackKey, callback);
259
+ xmlList.push(button);
260
+ };
261
+
262
+ /**
263
+ * Find all callers of a named procedure.
264
+ * @param {string} name Name of procedure (procCode in scratch-blocks).
265
+ * @param {!Blockly.Workspace} ws The workspace to find callers in.
266
+ * @param {!Blockly.Block} definitionRoot The root of the stack where the
267
+ * procedure is defined.
268
+ * @param {boolean} allowRecursive True if the search should include recursive
269
+ * procedure calls. False if the search should ignore the stack starting
270
+ * with definitionRoot.
271
+ * @return {!Array.<!Blockly.Block>} Array of caller blocks.
272
+ * @package
273
+ */
274
+ Blockly.Procedures.getCallers = function(name, ws, definitionRoot,
275
+ allowRecursive) {
276
+ var allBlocks = [];
277
+ var topBlocks = ws.getTopBlocks();
278
+
279
+ // Start by deciding which stacks to investigate.
280
+ for (var i = 0; i < topBlocks.length; i++) {
281
+ var block = topBlocks[i];
282
+ if (block.id == definitionRoot.id && !allowRecursive) {
283
+ continue;
284
+ }
285
+ allBlocks.push.apply(allBlocks, block.getDescendants(false));
286
+ }
287
+
288
+ var callers = [];
289
+ for (var i = 0; i < allBlocks.length; i++) {
290
+ var block = allBlocks[i];
291
+ if (block.type == Blockly.PROCEDURES_CALL_BLOCK_TYPE ) {
292
+ var procCode = block.getProcCode();
293
+ if (procCode && procCode == name) {
294
+ callers.push(block);
295
+ }
296
+ }
297
+ }
298
+ return callers;
299
+ };
300
+
301
+ /**
302
+ * Find and edit all callers with a procCode using a new mutation.
303
+ * @param {string} name Name of procedure (procCode in scratch-blocks).
304
+ * @param {!Blockly.Workspace} ws The workspace to find callers in.
305
+ * @param {!Element} mutation New mutation for the callers.
306
+ * @package
307
+ */
308
+ Blockly.Procedures.mutateCallersAndPrototype = function(name, ws, mutation) {
309
+ var defineBlock = Blockly.Procedures.getDefineBlock(name, ws);
310
+ var prototypeBlock = Blockly.Procedures.getPrototypeBlock(name, ws);
311
+ if (defineBlock && prototypeBlock) {
312
+ var callers = Blockly.Procedures.getCallers(name,
313
+ defineBlock.workspace, defineBlock, true /* allowRecursive */);
314
+ callers.push(prototypeBlock);
315
+ Blockly.Events.setGroup(true);
316
+ for (var i = 0, caller; caller = callers[i]; i++) {
317
+ var oldMutationDom = caller.mutationToDom();
318
+ var oldMutation = oldMutationDom && Blockly.Xml.domToText(oldMutationDom);
319
+ caller.domToMutation(mutation);
320
+ var newMutationDom = caller.mutationToDom();
321
+ var newMutation = newMutationDom && Blockly.Xml.domToText(newMutationDom);
322
+ if (oldMutation != newMutation) {
323
+ Blockly.Events.fire(new Blockly.Events.BlockChange(
324
+ caller, 'mutation', null, oldMutation, newMutation));
325
+ }
326
+ }
327
+ Blockly.Events.setGroup(false);
328
+ } else {
329
+ alert('No define block on workspace'); // TODO decide what to do about this.
330
+ }
331
+ };
332
+
333
+ /**
334
+ * Find the definition block for the named procedure.
335
+ * @param {string} procCode The identifier of the procedure.
336
+ * @param {!Blockly.Workspace} workspace The workspace to search.
337
+ * @return {Blockly.Block} The procedure definition block, or null not found.
338
+ * @package
339
+ */
340
+ Blockly.Procedures.getDefineBlock = function(procCode, workspace) {
341
+ // Assume that a procedure definition is a top block.
342
+ var blocks = workspace.getTopBlocks(false);
343
+ for (var i = 0; i < blocks.length; i++) {
344
+ if (blocks[i].type == Blockly.PROCEDURES_DEFINITION_BLOCK_TYPE) {
345
+ var prototypeBlock = blocks[i].getInput('custom_block').connection.targetBlock();
346
+ if (prototypeBlock.getProcCode && prototypeBlock.getProcCode() == procCode) {
347
+ return blocks[i];
348
+ }
349
+ }
350
+ }
351
+ return null;
352
+ };
353
+
354
+ /**
355
+ * Find the prototype block for the named procedure.
356
+ * @param {string} procCode The identifier of the procedure.
357
+ * @param {!Blockly.Workspace} workspace The workspace to search.
358
+ * @return {Blockly.Block} The procedure prototype block, or null not found.
359
+ * @package
360
+ */
361
+ Blockly.Procedures.getPrototypeBlock = function(procCode, workspace) {
362
+ var defineBlock = Blockly.Procedures.getDefineBlock(procCode, workspace);
363
+ if (defineBlock) {
364
+ return defineBlock.getInput('custom_block').connection.targetBlock();
365
+ }
366
+ return null;
367
+ };
368
+
369
+ /**
370
+ * Create a mutation for a brand new custom procedure.
371
+ * @return {Element} The mutation for a new custom procedure
372
+ * @package
373
+ */
374
+ Blockly.Procedures.newProcedureMutation = function() {
375
+ var mutationText = '<xml>' +
376
+ '<mutation' +
377
+ ' proccode="' + Blockly.Msg['PROCEDURE_DEFAULT_NAME'] + '"' +
378
+ ' argumentids="[]"' +
379
+ ' argumentnames="[]"' +
380
+ ' argumentdefaults="[]"' +
381
+ ' warp="false">' +
382
+ '</mutation>' +
383
+ '</xml>';
384
+ return Blockly.Xml.textToDom(mutationText).firstChild;
385
+ };
386
+
387
+ /**
388
+ * Callback to create a new procedure custom command block.
389
+ * @param {!Blockly.Workspace} workspace The workspace to create the new procedure on.
390
+ * @private
391
+ */
392
+ Blockly.Procedures.createProcedureDefCallback_ = function(workspace) {
393
+ Blockly.Procedures.externalProcedureDefCallback(
394
+ Blockly.Procedures.newProcedureMutation(),
395
+ Blockly.Procedures.createProcedureCallbackFactory_(workspace)
396
+ );
397
+ };
398
+
399
+ /**
400
+ * Callback factory for adding a new custom procedure from a mutation.
401
+ * @param {!Blockly.Workspace} workspace The workspace to create the new procedure on.
402
+ * @return {function(?Element)} callback for creating the new custom procedure.
403
+ * @private
404
+ */
405
+ Blockly.Procedures.createProcedureCallbackFactory_ = function(workspace) {
406
+ return function(mutation) {
407
+ if (mutation) {
408
+ var blockText = '<xml>' +
409
+ '<block type="procedures_definition">' +
410
+ '<statement name="custom_block">' +
411
+ '<shadow type="procedures_prototype">' +
412
+ Blockly.Xml.domToText(mutation) +
413
+ '</shadow>' +
414
+ '</statement>' +
415
+ '</block>' +
416
+ '</xml>';
417
+ var blockDom = Blockly.Xml.textToDom(blockText).firstChild;
418
+ Blockly.Events.setGroup(true);
419
+ var block = Blockly.Xml.domToBlock(blockDom, workspace);
420
+ var scale = workspace.scale; // To convert from pixel units to workspace units
421
+ // Position the block so that it is at the top left of the visible workspace,
422
+ // padded from the edge by 30 units. Position in the top right if RTL.
423
+ var posX = -workspace.scrollX;
424
+ if (workspace.RTL) {
425
+ posX += workspace.getMetrics().contentWidth - 30;
426
+ } else {
427
+ posX += 30;
428
+ }
429
+ block.moveBy(posX / scale, (-workspace.scrollY + 30) / scale);
430
+ block.scheduleSnapAndBump();
431
+ Blockly.Events.setGroup(false);
432
+ }
433
+ };
434
+ };
435
+
436
+ /**
437
+ * Callback to open the modal for editing custom procedures.
438
+ * @param {!Blockly.Block} block The block that was right-clicked.
439
+ * @private
440
+ */
441
+ Blockly.Procedures.editProcedureCallback_ = function(block) {
442
+ // Edit can come from one of three block types (call, define, prototype)
443
+ // Normalize by setting the block to the prototype block for the procedure.
444
+ if (block.type == Blockly.PROCEDURES_DEFINITION_BLOCK_TYPE) {
445
+ var input = block.getInput('custom_block');
446
+ if (!input) {
447
+ alert('Bad input'); // TODO: Decide what to do about this.
448
+ return;
449
+ }
450
+ var conn = input.connection;
451
+ if (!conn) {
452
+ alert('Bad connection'); // TODO: Decide what to do about this.
453
+ return;
454
+ }
455
+ var innerBlock = conn.targetBlock();
456
+ if (!innerBlock ||
457
+ !innerBlock.type == Blockly.PROCEDURES_PROTOTYPE_BLOCK_TYPE) {
458
+ alert('Bad inner block'); // TODO: Decide what to do about this.
459
+ return;
460
+ }
461
+ block = innerBlock;
462
+ } else if (block.type == Blockly.PROCEDURES_CALL_BLOCK_TYPE) {
463
+ // This is a call block, find the prototype corresponding to the procCode.
464
+ // Make sure to search the correct workspace, call block can be in flyout.
465
+ var workspaceToSearch = block.workspace.isFlyout ?
466
+ block.workspace.targetWorkspace : block.workspace;
467
+ block = Blockly.Procedures.getPrototypeBlock(
468
+ block.getProcCode(), workspaceToSearch);
469
+ }
470
+ // Block now refers to the procedure prototype block, it is safe to proceed.
471
+ Blockly.Procedures.externalProcedureDefCallback(
472
+ block.mutationToDom(),
473
+ Blockly.Procedures.editProcedureCallbackFactory_(block)
474
+ );
475
+ };
476
+
477
+ /**
478
+ * Callback factory for editing an existing custom procedure.
479
+ * @param {!Blockly.Block} block The procedure prototype block being edited.
480
+ * @return {function(?Element)} Callback for editing the custom procedure.
481
+ * @private
482
+ */
483
+ Blockly.Procedures.editProcedureCallbackFactory_ = function(block) {
484
+ return function(mutation) {
485
+ if (mutation) {
486
+ Blockly.Procedures.mutateCallersAndPrototype(block.getProcCode(),
487
+ block.workspace, mutation);
488
+ }
489
+ };
490
+ };
491
+
492
+ /**
493
+ * Callback to create a new procedure custom command block.
494
+ * @public
495
+ */
496
+ Blockly.Procedures.externalProcedureDefCallback = function(/** mutator, callback */) {
497
+ alert('External procedure editor must be override Blockly.Procedures.externalProcedureDefCallback');
498
+ };
499
+
500
+ /**
501
+ * Make a context menu option for editing a custom procedure.
502
+ * This appears in the context menu for procedure definitions and procedure
503
+ * calls.
504
+ * @param {!Blockly.BlockSvg} block The block where the right-click originated.
505
+ * @return {!Object} A menu option, containing text, enabled, and a callback.
506
+ * @package
507
+ */
508
+ Blockly.Procedures.makeEditOption = function(block) {
509
+ var editOption = {
510
+ enabled: true,
511
+ text: Blockly.Msg.EDIT_PROCEDURE,
512
+ callback: function() {
513
+ Blockly.Procedures.editProcedureCallback_(block);
514
+ }
515
+ };
516
+ return editOption;
517
+ };
518
+
519
+ /**
520
+ * Callback to show the procedure definition corresponding to a custom command
521
+ * block.
522
+ * TODO(#1136): Implement.
523
+ * @param {!Blockly.Block} block The block that was right-clicked.
524
+ * @private
525
+ */
526
+ Blockly.Procedures.showProcedureDefCallback_ = function(block) {
527
+ alert('TODO(#1136): implement showing procedure definition (procCode was "' +
528
+ block.procCode_ + '")');
529
+ };
530
+
531
+ /**
532
+ * Make a context menu option for showing the definition for a custom procedure,
533
+ * based on a right-click on a custom command block.
534
+ * @param {!Blockly.BlockSvg} block The block where the right-click originated.
535
+ * @return {!Object} A menu option, containing text, enabled, and a callback.
536
+ * @package
537
+ */
538
+ Blockly.Procedures.makeShowDefinitionOption = function(block) {
539
+ var option = {
540
+ enabled: true,
541
+ text: Blockly.Msg.SHOW_PROCEDURE_DEFINITION,
542
+ callback: function() {
543
+ Blockly.Procedures.showProcedureDefCallback_(block);
544
+ }
545
+ };
546
+ return option;
547
+ };
548
+
549
+ /**
550
+ * Callback to try to delete a custom block definitions.
551
+ * @param {string} procCode The identifier of the procedure to delete.
552
+ * @param {!Blockly.Block} definitionRoot The root block of the stack that
553
+ * defines the custom procedure.
554
+ * @return {boolean} True if the custom procedure was deleted, false otherwise.
555
+ * @package
556
+ */
557
+ Blockly.Procedures.deleteProcedureDefCallback = function(procCode,
558
+ definitionRoot) {
559
+ var callers = Blockly.Procedures.getCallers(procCode,
560
+ definitionRoot.workspace, definitionRoot, false /* allowRecursive */);
561
+ if (callers.length > 0) {
562
+ return false;
563
+ }
564
+
565
+ var workspace = definitionRoot.workspace;
566
+
567
+ // Delete the whole stack.
568
+ Blockly.Events.setGroup(true);
569
+ definitionRoot.dispose();
570
+ Blockly.Events.setGroup(false);
571
+
572
+ // TODO (#1354) Update this function when '_' is removed
573
+ // Refresh toolbox, so caller doesn't appear there anymore
574
+ workspace.refreshToolboxSelection_();
575
+
576
+ return true;
577
+ };