@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.
Files changed (243) hide show
  1. package/.nvmrc +1 -0
  2. package/LICENSE +674 -0
  3. package/README.md +57 -0
  4. package/TRADEMARK +1 -0
  5. package/blockly_compressed_horizontal.js +2268 -0
  6. package/blockly_compressed_vertical.js +2296 -0
  7. package/blockly_uncompressed_horizontal.js +1212 -0
  8. package/blockly_uncompressed_vertical.js +1212 -0
  9. package/blocks_common/colour.js +61 -0
  10. package/blocks_common/math.js +159 -0
  11. package/blocks_common/matrix.js +54 -0
  12. package/blocks_common/note.js +58 -0
  13. package/blocks_common/text.js +57 -0
  14. package/blocks_compressed.js +37 -0
  15. package/blocks_compressed_horizontal.js +68 -0
  16. package/blocks_compressed_vertical.js +216 -0
  17. package/blocks_horizontal/control.js +212 -0
  18. package/blocks_horizontal/default_toolbox.js +139 -0
  19. package/blocks_horizontal/event.js +190 -0
  20. package/blocks_horizontal/wedo.js +325 -0
  21. package/blocks_vertical/control.js +532 -0
  22. package/blocks_vertical/data.js +666 -0
  23. package/blocks_vertical/default_toolbox.js +571 -0
  24. package/blocks_vertical/event.js +329 -0
  25. package/blocks_vertical/extensions.js +294 -0
  26. package/blocks_vertical/looks.js +591 -0
  27. package/blocks_vertical/motion.js +587 -0
  28. package/blocks_vertical/operators.js +470 -0
  29. package/blocks_vertical/procedures.js +1051 -0
  30. package/blocks_vertical/sensing.js +555 -0
  31. package/blocks_vertical/sound.js +246 -0
  32. package/blocks_vertical/vertical_extensions.js +289 -0
  33. package/build/gen_blocks.js +1 -0
  34. package/build/test_expect.js +1 -0
  35. package/build/test_input.js +1 -0
  36. package/build.py +647 -0
  37. package/cleanup.sh +101 -0
  38. package/core/block.js +1837 -0
  39. package/core/block_animations.js +107 -0
  40. package/core/block_drag_surface.js +299 -0
  41. package/core/block_dragger.js +424 -0
  42. package/core/block_events.js +531 -0
  43. package/core/block_render_svg_horizontal.js +892 -0
  44. package/core/block_render_svg_vertical.js +1734 -0
  45. package/core/block_svg.js +1355 -0
  46. package/core/blockly.js +620 -0
  47. package/core/blocks.js +37 -0
  48. package/core/bubble.js +664 -0
  49. package/core/bubble_dragger.js +285 -0
  50. package/core/colours.js +177 -0
  51. package/core/comment.js +293 -0
  52. package/core/comment_events.js +539 -0
  53. package/core/connection.js +776 -0
  54. package/core/connection_db.js +300 -0
  55. package/core/constants.js +408 -0
  56. package/core/contextmenu.js +534 -0
  57. package/core/css.js +1354 -0
  58. package/core/data_category.js +490 -0
  59. package/core/dragged_connection_manager.js +260 -0
  60. package/core/dropdowndiv.js +408 -0
  61. package/core/events.js +429 -0
  62. package/core/events_abstract.js +113 -0
  63. package/core/extensions.js +450 -0
  64. package/core/field.js +810 -0
  65. package/core/field_angle.js +410 -0
  66. package/core/field_checkbox.js +133 -0
  67. package/core/field_colour.js +253 -0
  68. package/core/field_colour_slider.js +387 -0
  69. package/core/field_date.js +353 -0
  70. package/core/field_dropdown.js +447 -0
  71. package/core/field_iconmenu.js +309 -0
  72. package/core/field_image.js +200 -0
  73. package/core/field_label.js +136 -0
  74. package/core/field_label_serializable.js +125 -0
  75. package/core/field_matrix.js +566 -0
  76. package/core/field_note.js +850 -0
  77. package/core/field_number.js +366 -0
  78. package/core/field_numberdropdown.js +77 -0
  79. package/core/field_textdropdown.js +164 -0
  80. package/core/field_textinput.js +675 -0
  81. package/core/field_textinput_removable.js +105 -0
  82. package/core/field_variable.js +385 -0
  83. package/core/field_variable_getter.js +185 -0
  84. package/core/field_vertical_separator.js +161 -0
  85. package/core/flyout_base.js +935 -0
  86. package/core/flyout_button.js +324 -0
  87. package/core/flyout_dragger.js +83 -0
  88. package/core/flyout_extension_category_header.js +159 -0
  89. package/core/flyout_horizontal.js +475 -0
  90. package/core/flyout_vertical.js +770 -0
  91. package/core/generator.js +426 -0
  92. package/core/gesture.js +1010 -0
  93. package/core/grid.js +227 -0
  94. package/core/icon.js +205 -0
  95. package/core/inject.js +496 -0
  96. package/core/input.js +285 -0
  97. package/core/insertion_marker_manager.js +678 -0
  98. package/core/intersection_observer.js +102 -0
  99. package/core/msg.js +62 -0
  100. package/core/mutator.js +426 -0
  101. package/core/names.js +198 -0
  102. package/core/options.js +244 -0
  103. package/core/procedures.js +739 -0
  104. package/core/rendered_connection.js +417 -0
  105. package/core/scratch_block_comment.js +646 -0
  106. package/core/scratch_blocks_utils.js +246 -0
  107. package/core/scratch_bubble.js +699 -0
  108. package/core/scratch_events.js +131 -0
  109. package/core/scratch_msgs.js +85 -0
  110. package/core/scrollbar.js +875 -0
  111. package/core/toolbox.js +803 -0
  112. package/core/tooltip.js +337 -0
  113. package/core/touch.js +226 -0
  114. package/core/trashcan.js +343 -0
  115. package/core/ui_events.js +91 -0
  116. package/core/ui_menu_utils.js +68 -0
  117. package/core/utils.js +825 -0
  118. package/core/variable_events.js +259 -0
  119. package/core/variable_map.js +415 -0
  120. package/core/variable_model.js +116 -0
  121. package/core/variables.js +674 -0
  122. package/core/warning.js +199 -0
  123. package/core/widgetdiv.js +344 -0
  124. package/core/workspace.js +673 -0
  125. package/core/workspace_audio.js +170 -0
  126. package/core/workspace_comment.js +426 -0
  127. package/core/workspace_comment_render_svg.js +706 -0
  128. package/core/workspace_comment_svg.js +609 -0
  129. package/core/workspace_drag_surface_svg.js +195 -0
  130. package/core/workspace_dragger.js +132 -0
  131. package/core/workspace_svg.js +2382 -0
  132. package/core/xml.js +930 -0
  133. package/core/zoom_controls.js +301 -0
  134. package/dist/horizontal.js +222 -0
  135. package/dist/vertical.js +222 -0
  136. package/dist/web/horizontal.js +232 -0
  137. package/dist/web/vertical.js +232 -0
  138. package/i18n/common.py +234 -0
  139. package/i18n/create_messages.py +162 -0
  140. package/i18n/create_scratch_msgs.js +69 -0
  141. package/i18n/dedup_json.py +73 -0
  142. package/i18n/js_to_json.js +46 -0
  143. package/i18n/js_to_json.py +136 -0
  144. package/i18n/json_to_js.js +52 -0
  145. package/i18n/json_to_js.py +185 -0
  146. package/i18n/sync_tx_translations.js +111 -0
  147. package/i18n/test_scratch_msgs.js +87 -0
  148. package/i18n/tests.py +47 -0
  149. package/i18n/xliff_to_json.py +232 -0
  150. package/local_build.sh +70 -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/status-not-ready.svg +13 -0
  216. package/media/status-ready.svg +13 -0
  217. package/media/zoom-in.svg +1 -0
  218. package/media/zoom-out.svg +1 -0
  219. package/media/zoom-reset.svg +1 -0
  220. package/msg/js/en.js +290 -0
  221. package/msg/json/en.json +285 -0
  222. package/msg/messages.js +359 -0
  223. package/msg/scratch_msgs.js +22969 -0
  224. package/package.json +60 -0
  225. package/pull_from_blockly.sh +151 -0
  226. package/renovate.json5 +15 -0
  227. package/shim/blockly_compressed_horizontal-blocks_compressed.js +1 -0
  228. package/shim/blockly_compressed_horizontal.Blockly.js +1 -0
  229. package/shim/blockly_compressed_horizontal.goog.js +1 -0
  230. package/shim/blockly_compressed_horizontal.js +1 -0
  231. package/shim/blockly_compressed_vertical-blocks_compressed.js +1 -0
  232. package/shim/blockly_compressed_vertical.Blockly.js +1 -0
  233. package/shim/blockly_compressed_vertical.goog.js +1 -0
  234. package/shim/blockly_compressed_vertical.js +1 -0
  235. package/shim/blocks_compressed_horizontal-blockly_compressed_horizontal-messages.js +1 -0
  236. package/shim/blocks_compressed_horizontal.js +1 -0
  237. package/shim/blocks_compressed_vertical-blockly_compressed_vertical-messages.js +1 -0
  238. package/shim/blocks_compressed_vertical.js +1 -0
  239. package/shim/gh-pages.js +1 -0
  240. package/shim/horizontal.js +1 -0
  241. package/shim/index.js +17 -0
  242. package/shim/vertical.js +1 -0
  243. package/universal-python.js +82 -0
@@ -0,0 +1,739 @@
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].cloneNode(false);
231
+ var procCode = mutation.getAttribute('proccode');
232
+ var returnType = Blockly.Procedures.getProcedureReturnType(procCode, workspace);
233
+ if (returnType !== Blockly.PROCEDURES_CALL_TYPE_STATEMENT) {
234
+ mutation.setAttribute('return', returnType);
235
+ }
236
+ // <block type="procedures_call">
237
+ // <mutation ...></mutation>
238
+ // </block>
239
+ var block = goog.dom.createDom('block');
240
+ block.setAttribute('type', 'procedures_call');
241
+ block.setAttribute('gap', 12);
242
+ block.appendChild(mutation);
243
+ xmlList.push(block);
244
+ }
245
+
246
+ var showReturn = (
247
+ Blockly.Procedures.DEFAULT_ENABLE_RETURNS ?
248
+ mutations.length > 0 :
249
+ workspace.procedureReturnsEnabled
250
+ );
251
+ if (showReturn) {
252
+ var returnBlock = goog.dom.createDom('block');
253
+ returnBlock.setAttribute('type', Blockly.PROCEDURES_RETURN_BLOCK_TYPE);
254
+ returnBlock.setAttribute('gap', 12);
255
+ var returnBlockValue = goog.dom.createDom('value');
256
+ returnBlockValue.setAttribute('name', 'VALUE');
257
+ var returnBlockShadow = goog.dom.createDom('shadow');
258
+ returnBlockShadow.setAttribute('type', 'text');
259
+ var returnBlockField = goog.dom.createDom('field');
260
+ returnBlockField.setAttribute('name', 'TEXT');
261
+ returnBlockShadow.appendChild(returnBlockField);
262
+ returnBlockValue.appendChild(returnBlockShadow);
263
+ returnBlock.appendChild(returnBlockValue);
264
+ xmlList.unshift(returnBlock);
265
+
266
+ var returnDocsButton = goog.dom.createDom('button');
267
+ returnDocsButton.setAttribute('callbackkey', 'OPEN_RETURN_DOCS');
268
+ returnDocsButton.setAttribute('text', Blockly.Msg.PROCEDURES_DOCS);
269
+ xmlList.unshift(returnDocsButton);
270
+ }
271
+
272
+ return xmlList;
273
+ };
274
+
275
+ /**
276
+ * Create the "Make a Block..." button.
277
+ * @param {!Blockly.Workspace} workspace The workspace contianing procedures.
278
+ * @param {!Array.<!Element>} xmlList Array of XML block elements to add to.
279
+ * @private
280
+ */
281
+ Blockly.Procedures.addCreateButton_ = function(workspace, xmlList) {
282
+ var button = goog.dom.createDom('button');
283
+ var msg = Blockly.Msg.NEW_PROCEDURE;
284
+ var callbackKey = 'CREATE_PROCEDURE';
285
+ var callback = function() {
286
+ Blockly.Procedures.createProcedureDefCallback_(workspace);
287
+ };
288
+ button.setAttribute('text', msg);
289
+ button.setAttribute('callbackKey', callbackKey);
290
+ workspace.registerButtonCallback(callbackKey, callback);
291
+ xmlList.push(button);
292
+ };
293
+
294
+ /**
295
+ * Find all callers of a named procedure.
296
+ * @param {string} name Name of procedure (procCode in scratch-blocks).
297
+ * @param {!Blockly.Workspace} ws The workspace to find callers in.
298
+ * @param {!Blockly.Block} definitionRoot The root of the stack where the
299
+ * procedure is defined.
300
+ * @param {boolean} allowRecursive True if the search should include recursive
301
+ * procedure calls. False if the search should ignore the stack starting
302
+ * with definitionRoot.
303
+ * @return {!Array.<!Blockly.Block>} Array of caller blocks.
304
+ * @package
305
+ */
306
+ Blockly.Procedures.getCallers = function(name, ws, definitionRoot,
307
+ allowRecursive) {
308
+ var allBlocks = [];
309
+ var topBlocks = ws.getTopBlocks();
310
+
311
+ // Start by deciding which stacks to investigate.
312
+ for (var i = 0; i < topBlocks.length; i++) {
313
+ var block = topBlocks[i];
314
+ if (block.id == definitionRoot.id && !allowRecursive) {
315
+ continue;
316
+ }
317
+ allBlocks.push.apply(allBlocks, block.getDescendants(false));
318
+ }
319
+
320
+ var callers = [];
321
+ for (var i = 0; i < allBlocks.length; i++) {
322
+ var block = allBlocks[i];
323
+ if (block.type == Blockly.PROCEDURES_CALL_BLOCK_TYPE ) {
324
+ var procCode = block.getProcCode();
325
+ if (procCode && procCode == name) {
326
+ callers.push(block);
327
+ }
328
+ }
329
+ }
330
+ return callers;
331
+ };
332
+
333
+ /**
334
+ * Find and edit all callers with a procCode using a new mutation.
335
+ * @param {string} name Name of procedure (procCode in scratch-blocks).
336
+ * @param {!Blockly.Workspace} ws The workspace to find callers in.
337
+ * @param {!Element} mutation New mutation for the callers.
338
+ * @package
339
+ */
340
+ Blockly.Procedures.mutateCallersAndPrototype = function(name, ws, mutation) {
341
+ var defineBlock = Blockly.Procedures.getDefineBlock(name, ws);
342
+ var prototypeBlock = Blockly.Procedures.getPrototypeBlock(name, ws);
343
+ if (defineBlock && prototypeBlock) {
344
+ var callers = Blockly.Procedures.getCallers(name,
345
+ defineBlock.workspace, defineBlock, true /* allowRecursive */);
346
+ callers.push(prototypeBlock);
347
+ Blockly.Events.setGroup(true);
348
+ for (var i = 0, caller; caller = callers[i]; i++) {
349
+ var oldMutationDom = caller.mutationToDom();
350
+ var oldMutation = oldMutationDom && Blockly.Xml.domToText(oldMutationDom);
351
+
352
+ // Preserve the block's existing shape
353
+ var mutationToReplaceWith = mutation.cloneNode(false);
354
+ mutationToReplaceWith.setAttribute('return', oldMutationDom.getAttribute('return'));
355
+ caller.domToMutation(mutationToReplaceWith);
356
+
357
+ var newMutationDom = caller.mutationToDom();
358
+ var newMutation = newMutationDom && Blockly.Xml.domToText(newMutationDom);
359
+ if (oldMutation != newMutation) {
360
+ Blockly.Events.fire(new Blockly.Events.BlockChange(
361
+ caller, 'mutation', null, oldMutation, newMutation));
362
+ }
363
+ }
364
+ Blockly.Events.setGroup(false);
365
+ } else {
366
+ alert('No define block on workspace'); // TODO decide what to do about this.
367
+ }
368
+ };
369
+
370
+ /**
371
+ * Find the definition block for the named procedure.
372
+ * @param {string} procCode The identifier of the procedure.
373
+ * @param {!Blockly.Workspace} workspace The workspace to search.
374
+ * @return {Blockly.Block} The procedure definition block, or null not found.
375
+ * @package
376
+ */
377
+ Blockly.Procedures.getDefineBlock = function(procCode, workspace) {
378
+ // Assume that a procedure definition is a top block.
379
+ var blocks = workspace.getTopBlocks(false);
380
+ for (var i = 0; i < blocks.length; i++) {
381
+ if (blocks[i].type == Blockly.PROCEDURES_DEFINITION_BLOCK_TYPE) {
382
+ var prototypeBlock = blocks[i].getInput('custom_block').connection.targetBlock();
383
+ if (prototypeBlock.getProcCode && prototypeBlock.getProcCode() == procCode) {
384
+ return blocks[i];
385
+ }
386
+ }
387
+ }
388
+ return null;
389
+ };
390
+
391
+ /**
392
+ * Find the prototype block for the named procedure.
393
+ * @param {string} procCode The identifier of the procedure.
394
+ * @param {!Blockly.Workspace} workspace The workspace to search.
395
+ * @return {Blockly.Block} The procedure prototype block, or null not found.
396
+ * @package
397
+ */
398
+ Blockly.Procedures.getPrototypeBlock = function(procCode, workspace) {
399
+ var defineBlock = Blockly.Procedures.getDefineBlock(procCode, workspace);
400
+ if (defineBlock) {
401
+ return defineBlock.getInput('custom_block').connection.targetBlock();
402
+ }
403
+ return null;
404
+ };
405
+
406
+ /**
407
+ * Create a mutation for a brand new custom procedure.
408
+ * @return {Element} The mutation for a new custom procedure
409
+ * @package
410
+ */
411
+ Blockly.Procedures.newProcedureMutation = function() {
412
+ var mutationText = '<xml>' +
413
+ '<mutation' +
414
+ ' proccode="' + Blockly.Msg['PROCEDURE_DEFAULT_NAME'] + '"' +
415
+ ' argumentids="[]"' +
416
+ ' argumentnames="[]"' +
417
+ ' argumentdefaults="[]"' +
418
+ ' warp="false">' +
419
+ '</mutation>' +
420
+ '</xml>';
421
+ return Blockly.Xml.textToDom(mutationText).firstChild;
422
+ };
423
+
424
+ /**
425
+ * Callback to create a new procedure custom command block.
426
+ * @param {!Blockly.Workspace} workspace The workspace to create the new procedure on.
427
+ * @private
428
+ */
429
+ Blockly.Procedures.createProcedureDefCallback_ = function(workspace) {
430
+ Blockly.Procedures.externalProcedureDefCallback(
431
+ Blockly.Procedures.newProcedureMutation(),
432
+ Blockly.Procedures.createProcedureCallbackFactory_(workspace)
433
+ );
434
+ };
435
+
436
+ /**
437
+ * Callback factory for adding a new custom procedure from a mutation.
438
+ * @param {!Blockly.Workspace} workspace The workspace to create the new procedure on.
439
+ * @return {function(?Element)} callback for creating the new custom procedure.
440
+ * @private
441
+ */
442
+ Blockly.Procedures.createProcedureCallbackFactory_ = function(workspace) {
443
+ return function(mutation) {
444
+ if (mutation) {
445
+ var blockText = '<xml>' +
446
+ '<block type="procedures_definition">' +
447
+ '<statement name="custom_block">' +
448
+ '<shadow type="procedures_prototype">' +
449
+ Blockly.Xml.domToText(mutation) +
450
+ '</shadow>' +
451
+ '</statement>' +
452
+ '</block>' +
453
+ '</xml>';
454
+ var blockDom = Blockly.Xml.textToDom(blockText).firstChild;
455
+ Blockly.Events.setGroup(true);
456
+ var block = Blockly.Xml.domToBlock(blockDom, workspace);
457
+ var scale = workspace.scale; // To convert from pixel units to workspace units
458
+ // Position the block so that it is at the top left of the visible workspace,
459
+ // padded from the edge by 30 units. Position in the top right if RTL.
460
+ var posX = -workspace.scrollX;
461
+ if (workspace.RTL) {
462
+ posX += workspace.getMetrics().contentWidth - 30;
463
+ } else {
464
+ posX += 30;
465
+ }
466
+ block.moveBy(posX / scale, (-workspace.scrollY + 30) / scale);
467
+ block.scheduleSnapAndBump();
468
+ Blockly.Events.setGroup(false);
469
+ }
470
+ };
471
+ };
472
+
473
+ /**
474
+ * Callback to open the modal for editing custom procedures.
475
+ * @param {!Blockly.Block} block The block that was right-clicked.
476
+ * @private
477
+ */
478
+ Blockly.Procedures.editProcedureCallback_ = function(block) {
479
+ // Edit can come from one of three block types (call, define, prototype)
480
+ // Normalize by setting the block to the prototype block for the procedure.
481
+ if (block.type == Blockly.PROCEDURES_DEFINITION_BLOCK_TYPE) {
482
+ var input = block.getInput('custom_block');
483
+ if (!input) {
484
+ alert('Bad input'); // TODO: Decide what to do about this.
485
+ return;
486
+ }
487
+ var conn = input.connection;
488
+ if (!conn) {
489
+ alert('Bad connection'); // TODO: Decide what to do about this.
490
+ return;
491
+ }
492
+ var innerBlock = conn.targetBlock();
493
+ if (!innerBlock ||
494
+ !innerBlock.type == Blockly.PROCEDURES_PROTOTYPE_BLOCK_TYPE) {
495
+ alert('Bad inner block'); // TODO: Decide what to do about this.
496
+ return;
497
+ }
498
+ block = innerBlock;
499
+ } else if (block.type == Blockly.PROCEDURES_CALL_BLOCK_TYPE) {
500
+ // This is a call block, find the prototype corresponding to the procCode.
501
+ // Make sure to search the correct workspace, call block can be in flyout.
502
+ var workspaceToSearch = block.workspace.isFlyout ?
503
+ block.workspace.targetWorkspace : block.workspace;
504
+ block = Blockly.Procedures.getPrototypeBlock(
505
+ block.getProcCode(), workspaceToSearch);
506
+ }
507
+ // Block now refers to the procedure prototype block, it is safe to proceed.
508
+ Blockly.Procedures.externalProcedureDefCallback(
509
+ block.mutationToDom(),
510
+ Blockly.Procedures.editProcedureCallbackFactory_(block)
511
+ );
512
+ };
513
+
514
+ /**
515
+ * Callback factory for editing an existing custom procedure.
516
+ * @param {!Blockly.Block} block The procedure prototype block being edited.
517
+ * @return {function(?Element)} Callback for editing the custom procedure.
518
+ * @private
519
+ */
520
+ Blockly.Procedures.editProcedureCallbackFactory_ = function(block) {
521
+ return function(mutation) {
522
+ if (mutation) {
523
+ Blockly.Procedures.mutateCallersAndPrototype(block.getProcCode(),
524
+ block.workspace, mutation);
525
+ }
526
+ };
527
+ };
528
+
529
+ /**
530
+ * Callback to create a new procedure custom command block.
531
+ * @public
532
+ */
533
+ Blockly.Procedures.externalProcedureDefCallback = function(/** mutator, callback */) {
534
+ alert('External procedure editor must be override Blockly.Procedures.externalProcedureDefCallback');
535
+ };
536
+
537
+ /**
538
+ * Make a context menu option for editing a custom procedure.
539
+ * This appears in the context menu for procedure definitions and procedure
540
+ * calls.
541
+ * @param {!Blockly.BlockSvg} block The block where the right-click originated.
542
+ * @return {!Object} A menu option, containing text, enabled, and a callback.
543
+ * @package
544
+ */
545
+ Blockly.Procedures.makeEditOption = function(block) {
546
+ var editOption = {
547
+ enabled: true,
548
+ text: Blockly.Msg.EDIT_PROCEDURE,
549
+ callback: function() {
550
+ Blockly.Procedures.editProcedureCallback_(block);
551
+ }
552
+ };
553
+ return editOption;
554
+ };
555
+
556
+ Blockly.Procedures.makeChangeTypeOption = function(block) {
557
+ var isStatement = block.getReturn() === Blockly.PROCEDURES_CALL_TYPE_STATEMENT;
558
+ var option = {
559
+ enabled: true,
560
+ text: isStatement ? Blockly.Msg.PROCEDURES_TO_REPORTER : Blockly.Msg.PROCEDURES_TO_STATEMENT,
561
+ callback: function() {
562
+ var newType;
563
+ if (isStatement) {
564
+ var procCode = block.getProcCode();
565
+ var workspace = block.workspace;
566
+ var actualReturnType = Blockly.Procedures.getProcedureReturnType(procCode, workspace);
567
+ // If the definition is boolean-shaped, then the reporter should be boolean-shaped,
568
+ // otherwise normal reporter shaped.
569
+ newType = (
570
+ actualReturnType === Blockly.PROCEDURES_CALL_TYPE_BOOLEAN ?
571
+ actualReturnType :
572
+ Blockly.PROCEDURES_CALL_TYPE_REPORTER
573
+ );
574
+ } else {
575
+ newType = Blockly.PROCEDURES_CALL_TYPE_STATEMENT;
576
+ }
577
+
578
+ Blockly.Events.setGroup(true);
579
+ try {
580
+ Blockly.Procedures.changeReturnType(block, newType);
581
+ } finally {
582
+ Blockly.Events.setGroup(false);
583
+ }
584
+ }
585
+ };
586
+ return option;
587
+ };
588
+
589
+ Blockly.Procedures.changeReturnType = function(block, returnType) {
590
+ block.unplug(true);
591
+ var workspace = block.workspace;
592
+ var xml = Blockly.Xml.blockToDom(block);
593
+ var xy = block.getRelativeToSurfaceXY();
594
+ block.dispose();
595
+
596
+ var mutation = xml.querySelector('mutation');
597
+ mutation.setAttribute('return', returnType);
598
+
599
+ var newBlock = Blockly.Xml.domToBlock(xml, workspace);
600
+ newBlock.moveBy(xy.x, xy.y);
601
+ };
602
+
603
+ /**
604
+ * Callback to show the procedure definition corresponding to a custom command
605
+ * block.
606
+ * TODO(#1136): Implement.
607
+ * @param {!Blockly.Block} block The block that was right-clicked.
608
+ * @private
609
+ */
610
+ Blockly.Procedures.showProcedureDefCallback_ = function(block) {
611
+ alert('TODO(#1136): implement showing procedure definition (procCode was "' +
612
+ block.procCode_ + '")');
613
+ };
614
+
615
+ /**
616
+ * Make a context menu option for showing the definition for a custom procedure,
617
+ * based on a right-click on a custom command block.
618
+ * @param {!Blockly.BlockSvg} block The block where the right-click originated.
619
+ * @return {!Object} A menu option, containing text, enabled, and a callback.
620
+ * @package
621
+ */
622
+ Blockly.Procedures.makeShowDefinitionOption = function(block) {
623
+ var option = {
624
+ enabled: true,
625
+ text: Blockly.Msg.SHOW_PROCEDURE_DEFINITION,
626
+ callback: function() {
627
+ Blockly.Procedures.showProcedureDefCallback_(block);
628
+ }
629
+ };
630
+ return option;
631
+ };
632
+
633
+ /**
634
+ * Callback to try to delete a custom block definitions.
635
+ * @param {string} procCode The identifier of the procedure to delete.
636
+ * @param {!Blockly.Block} definitionRoot The root block of the stack that
637
+ * defines the custom procedure.
638
+ * @return {boolean} True if the custom procedure was deleted, false otherwise.
639
+ * @package
640
+ */
641
+ Blockly.Procedures.deleteProcedureDefCallback = function(procCode,
642
+ definitionRoot) {
643
+ var callers = Blockly.Procedures.getCallers(procCode,
644
+ definitionRoot.workspace, definitionRoot, false /* allowRecursive */);
645
+ if (callers.length > 0) {
646
+ return false;
647
+ }
648
+
649
+ var workspace = definitionRoot.workspace;
650
+
651
+ // Delete the whole stack.
652
+ Blockly.Events.setGroup(true);
653
+ definitionRoot.dispose();
654
+ Blockly.Events.setGroup(false);
655
+
656
+ // TODO (#1354) Update this function when '_' is removed
657
+ // Refresh toolbox, so caller doesn't appear there anymore
658
+ workspace.refreshToolboxSelection_();
659
+
660
+ return true;
661
+ };
662
+
663
+ /**
664
+ * If true, the user will be able to manually override the shape of procedure call blocks.
665
+ */
666
+ Blockly.Procedures.USER_CAN_CHANGE_CALL_TYPE = true;
667
+
668
+ /**
669
+ * If false, a round procedure call reporter can be dropped into any input, including boolean ones.
670
+ */
671
+ Blockly.Procedures.ENFORCE_TYPES = false;
672
+
673
+ /**
674
+ * If true, the return block will always be available. If false, either create a block that requires
675
+ * returns or call workspace.enableProcedureReturns() to enable return blocks.
676
+ */
677
+ Blockly.Procedures.DEFAULT_ENABLE_RETURNS = false;
678
+
679
+ /**
680
+ * @param {string} procCode The procedure code
681
+ * @param {Blockly.Workspace} workspace The workspace
682
+ * @returns {number} The type of the return block
683
+ */
684
+ Blockly.Procedures.getProcedureReturnType = function(procCode, workspace) {
685
+ var defineBlock = Blockly.Procedures.getDefineBlock(procCode, workspace);
686
+ if (!defineBlock) {
687
+ return Blockly.PROCEDURES_CALL_TYPE_STATEMENT;
688
+ }
689
+ return Blockly.Procedures.getBlockReturnType(defineBlock);
690
+ };
691
+
692
+ /**
693
+ * @param {Blockly.Workspace} workspace The workspace
694
+ * @returns {Record<string, number>} The return type of each procedure in the workspace.
695
+ */
696
+ Blockly.Procedures.getAllProcedureReturnTypes = function(workspace) {
697
+ var result = Object.create(null);
698
+ var blocks = workspace.getTopBlocks(false);
699
+ for (var i = 0; i < blocks.length; i++) {
700
+ var block = blocks[i];
701
+ if (block.type == Blockly.PROCEDURES_DEFINITION_BLOCK_TYPE && !block.isInsertionMarker()) {
702
+ var procCode = block.getInput('custom_block').connection.targetBlock().getProcCode();
703
+ // To match behavior of getDefineBlock, if multiple instances of this procedure are
704
+ // defined, only use the first one.
705
+ if (!Object.prototype.hasOwnProperty.call(result, procCode)) {
706
+ result[procCode] = Blockly.Procedures.getBlockReturnType(block);
707
+ }
708
+ }
709
+ }
710
+ return result;
711
+ };
712
+
713
+ /**
714
+ * @param {Blockly.Block} block The block
715
+ * @returns {number} The type of the return block
716
+ */
717
+ Blockly.Procedures.getBlockReturnType = function(block) {
718
+ var hasSeenBooleanReturn = false;
719
+ /** @type {Blockly.Block[]} */
720
+ var descendants = block.getDescendants();
721
+ for (var i = 0; i < descendants.length; i++) {
722
+ if (descendants[i].type === Blockly.PROCEDURES_RETURN_BLOCK_TYPE) {
723
+ // The block at i + 1 should be the block inside of the return block.
724
+ // Even if the return block is missing its input, this will still be fine, because the
725
+ // next block should a stacked block which won't be hexagon-shaped.
726
+ if (i + 1 < descendants.length && descendants[i + 1].outputShape_ === Blockly.OUTPUT_SHAPE_HEXAGONAL) {
727
+ // keep searching, because there may be other, non-boolean returns in this function definition.
728
+ hasSeenBooleanReturn = true;
729
+ } else {
730
+ return Blockly.PROCEDURES_CALL_TYPE_REPORTER;
731
+ }
732
+ }
733
+ }
734
+ if (hasSeenBooleanReturn) {
735
+ return Blockly.PROCEDURES_CALL_TYPE_BOOLEAN;
736
+ } else {
737
+ return Blockly.PROCEDURES_CALL_TYPE_STATEMENT;
738
+ }
739
+ };