@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,102 @@
1
+ 'use strict';
2
+
3
+ goog.provide('Blockly.IntersectionObserver');
4
+
5
+ Blockly.IntersectionObserver = function(workspace) {
6
+ this.workspace = workspace;
7
+ this.observing = [];
8
+ this.intersectionCheckQueued = false;
9
+ this.checkForIntersections = this.checkForIntersections.bind(this);
10
+ };
11
+
12
+ Blockly.IntersectionObserver.prototype.observe = function(block) {
13
+ var index = this.observing.indexOf(block);
14
+ if (index === -1) {
15
+ this.observing.push(block);
16
+ }
17
+ };
18
+
19
+ Blockly.IntersectionObserver.prototype.unobserve = function(block) {
20
+ var index = this.observing.indexOf(block);
21
+ if (index !== -1) {
22
+ this.observing = this.observing.filter(function(i) {
23
+ return i !== block;
24
+ });
25
+ }
26
+ };
27
+
28
+ Blockly.IntersectionObserver.prototype.dispose = function() {
29
+ this.observing = [];
30
+ this.workspace = null;
31
+ };
32
+
33
+ Blockly.IntersectionObserver.prototype.queueIntersectionCheck = function() {
34
+ if (this.intersectionCheckQueued) {
35
+ return;
36
+ }
37
+ this.intersectionCheckQueued = true;
38
+ // Check for intersections on the next microtick
39
+ // Prefer to use the native method when available, otherwise fallback to a Promise-based polyfill
40
+ if (window.queueMicrotask) {
41
+ window.queueMicrotask(this.checkForIntersections);
42
+ } else {
43
+ // eslint-disable-next-line no-undef
44
+ Promise.resolve().then(this.checkForIntersections);
45
+ }
46
+ };
47
+
48
+ Blockly.IntersectionObserver.prototype.checkForIntersections = function() {
49
+ this.intersectionCheckQueued = false;
50
+
51
+ if (!this.workspace) {
52
+ return;
53
+ }
54
+
55
+ var workspace = this.workspace;
56
+ var workspaceScale = workspace.scale;
57
+ var RTL = workspace.RTL;
58
+ var workspaceHeight = workspace.getParentSvg().height.baseVal.value;
59
+ var workspaceWidth = workspace.getParentSvg().width.baseVal.value;
60
+ if (workspace.isDragSurfaceActive_) {
61
+ var canvasPos = Blockly.utils.getRelativeXY(workspace.workspaceDragSurface_.SVG_);
62
+ } else {
63
+ var canvasPos = Blockly.utils.getRelativeXY(workspace.getCanvas());
64
+ }
65
+
66
+ // Allow blocks to go slightly offscreen so that effects such as glow do not get cut off.
67
+ var margin = 12 * workspaceScale;
68
+
69
+ for (var i = 0; i < this.observing.length; i++) {
70
+ var block = this.observing[i];
71
+ var blockPos = block.getRelativeToSurfaceXY();
72
+ var blockSize = null;
73
+ if (RTL) {
74
+ blockSize = block.getHeightWidth();
75
+ blockPos.x -= blockSize.width;
76
+ blockSize.width *= workspaceScale;
77
+ blockSize.height *= workspaceScale;
78
+ }
79
+ blockPos.x *= workspaceScale;
80
+ blockPos.y *= workspaceScale;
81
+
82
+ var visible = true;
83
+ if (canvasPos.y + blockPos.y - margin > workspaceHeight) {
84
+ visible = false;
85
+ } else if (canvasPos.x + blockPos.x - margin > workspaceWidth) {
86
+ visible = false;
87
+ } else {
88
+ if (!blockSize) {
89
+ blockSize = block.getHeightWidth();
90
+ blockSize.width *= workspaceScale;
91
+ blockSize.height *= workspaceScale;
92
+ }
93
+ if (canvasPos.x + blockPos.x + blockSize.width + margin < 0) {
94
+ visible = false;
95
+ } else if (canvasPos.y + blockPos.y + blockSize.height + margin < 0) {
96
+ visible = false;
97
+ }
98
+ }
99
+
100
+ block.setIntersects(visible);
101
+ }
102
+ };
package/core/msg.js ADDED
@@ -0,0 +1,62 @@
1
+ /**
2
+ * @license
3
+ * Visual Blocks Editor
4
+ *
5
+ * Copyright 2013 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 Empty name space for the Message singleton.
23
+ * @author scr@google.com (Sheridan Rawlins)
24
+ */
25
+ 'use strict';
26
+
27
+ /**
28
+ * Name space for the Msg singleton.
29
+ * Msg gets populated in the message files.
30
+ */
31
+ goog.provide('Blockly.Msg');
32
+
33
+
34
+ /**
35
+ * Back up original getMsg function.
36
+ * @type {!Function}
37
+ */
38
+ goog.getMsgOrig = goog.getMsg;
39
+
40
+ /**
41
+ * Gets a localized message.
42
+ * Overrides the default Closure function to check for a Blockly.Msg first.
43
+ * Used infrequently, only known case is TODAY button in date picker.
44
+ * @param {string} str Translatable string, places holders in the form {$foo}.
45
+ * @param {Object<string, string>=} opt_values Maps place holder name to value.
46
+ * @return {string} message with placeholders filled.
47
+ * @suppress {duplicate}
48
+ */
49
+ goog.getMsg = function(str, opt_values) {
50
+ var key = goog.getMsg.blocklyMsgMap[str];
51
+ if (key) {
52
+ str = Blockly.Msg[key];
53
+ }
54
+ return goog.getMsgOrig(str, opt_values);
55
+ };
56
+
57
+ /**
58
+ * Mapping of Closure messages to Blockly.Msg names.
59
+ */
60
+ goog.getMsg.blocklyMsgMap = {
61
+ 'Today': 'TODAY'
62
+ };
@@ -0,0 +1,426 @@
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 Object representing a mutator dialog. A mutator allows the
23
+ * user to change the shape of a block using a nested blocks editor.
24
+ * @author fraser@google.com (Neil Fraser)
25
+ */
26
+ 'use strict';
27
+
28
+ goog.provide('Blockly.Mutator');
29
+
30
+ goog.require('Blockly.Bubble');
31
+ goog.require('Blockly.Events.BlockChange');
32
+ goog.require('Blockly.Events.Ui');
33
+ goog.require('Blockly.Icon');
34
+ goog.require('Blockly.WorkspaceSvg');
35
+ goog.require('goog.dom');
36
+
37
+
38
+ /**
39
+ * Class for a mutator dialog.
40
+ * @param {!Array.<string>} quarkNames List of names of sub-blocks for flyout.
41
+ * @extends {Blockly.Icon}
42
+ * @constructor
43
+ */
44
+ Blockly.Mutator = function(quarkNames) {
45
+ Blockly.Mutator.superClass_.constructor.call(this, null);
46
+ this.quarkNames_ = quarkNames;
47
+ };
48
+ goog.inherits(Blockly.Mutator, Blockly.Icon);
49
+
50
+ /**
51
+ * Width of workspace.
52
+ * @private
53
+ */
54
+ Blockly.Mutator.prototype.workspaceWidth_ = 0;
55
+
56
+ /**
57
+ * Height of workspace.
58
+ * @private
59
+ */
60
+ Blockly.Mutator.prototype.workspaceHeight_ = 0;
61
+
62
+ /**
63
+ * Draw the mutator icon.
64
+ * @param {!Element} group The icon group.
65
+ * @private
66
+ */
67
+ Blockly.Mutator.prototype.drawIcon_ = function(group) {
68
+ // Square with rounded corners.
69
+ Blockly.utils.createSvgElement('rect',
70
+ {
71
+ 'class': 'blocklyIconShape',
72
+ 'rx': '4',
73
+ 'ry': '4',
74
+ 'height': '16',
75
+ 'width': '16'
76
+ },
77
+ group);
78
+ // Gear teeth.
79
+ Blockly.utils.createSvgElement('path',
80
+ {
81
+ 'class': 'blocklyIconSymbol',
82
+ 'd': 'm4.203,7.296 0,1.368 -0.92,0.677 -0.11,0.41 0.9,1.559 0.41,' +
83
+ '0.11 1.043,-0.457 1.187,0.683 0.127,1.134 0.3,0.3 1.8,0 0.3,' +
84
+ '-0.299 0.127,-1.138 1.185,-0.682 1.046,0.458 0.409,-0.11 0.9,' +
85
+ '-1.559 -0.11,-0.41 -0.92,-0.677 0,-1.366 0.92,-0.677 0.11,' +
86
+ '-0.41 -0.9,-1.559 -0.409,-0.109 -1.046,0.458 -1.185,-0.682 ' +
87
+ '-0.127,-1.138 -0.3,-0.299 -1.8,0 -0.3,0.3 -0.126,1.135 -1.187,' +
88
+ '0.682 -1.043,-0.457 -0.41,0.11 -0.899,1.559 0.108,0.409z'
89
+ },
90
+ group);
91
+ // Axle hole.
92
+ Blockly.utils.createSvgElement(
93
+ 'circle',
94
+ {
95
+ 'class': 'blocklyIconShape',
96
+ 'r': '2.7',
97
+ 'cx': '8',
98
+ 'cy': '8'
99
+ },
100
+ group);
101
+ };
102
+
103
+ /**
104
+ * Clicking on the icon toggles if the mutator bubble is visible.
105
+ * Disable if block is uneditable.
106
+ * @param {!Event} e Mouse click event.
107
+ * @private
108
+ * @override
109
+ */
110
+ Blockly.Mutator.prototype.iconClick_ = function(e) {
111
+ if (this.block_.isEditable()) {
112
+ Blockly.Icon.prototype.iconClick_.call(this, e);
113
+ }
114
+ };
115
+
116
+ /**
117
+ * Create the editor for the mutator's bubble.
118
+ * @return {!Element} The top-level node of the editor.
119
+ * @private
120
+ */
121
+ Blockly.Mutator.prototype.createEditor_ = function() {
122
+ /* Create the editor. Here's the markup that will be generated:
123
+ <svg>
124
+ [Workspace]
125
+ </svg>
126
+ */
127
+ this.svgDialog_ = Blockly.utils.createSvgElement('svg',
128
+ {'x': Blockly.Bubble.BORDER_WIDTH, 'y': Blockly.Bubble.BORDER_WIDTH},
129
+ null);
130
+ // Convert the list of names into a list of XML objects for the flyout.
131
+ if (this.quarkNames_.length) {
132
+ var quarkXml = goog.dom.createDom('xml');
133
+ for (var i = 0, quarkName; quarkName = this.quarkNames_[i]; i++) {
134
+ quarkXml.appendChild(goog.dom.createDom('block', {'type': quarkName}));
135
+ }
136
+ } else {
137
+ var quarkXml = null;
138
+ }
139
+ var workspaceOptions = {
140
+ languageTree: quarkXml,
141
+ parentWorkspace: this.block_.workspace,
142
+ pathToMedia: this.block_.workspace.options.pathToMedia,
143
+ RTL: this.block_.RTL,
144
+ toolboxPosition: this.block_.RTL ? Blockly.TOOLBOX_AT_RIGHT :
145
+ Blockly.TOOLBOX_AT_LEFT,
146
+ horizontalLayout: false,
147
+ getMetrics: this.getFlyoutMetrics_.bind(this),
148
+ setMetrics: null
149
+ };
150
+ this.workspace_ = new Blockly.WorkspaceSvg(workspaceOptions, this.block_.workspace.dragSurface);
151
+ this.workspace_.isMutator = true;
152
+
153
+ // Mutator flyouts go inside the mutator workspace's <g> rather than in
154
+ // a top level svg. Instead of handling scale themselves, mutators
155
+ // inherit scale from the parent workspace.
156
+ // To fix this, scale needs to be applied at a different level in the dom.
157
+ var flyoutSvg = this.workspace_.addFlyout_('g');
158
+ var background = this.workspace_.createDom('blocklyMutatorBackground');
159
+
160
+ // Insert the flyout after the <rect> but before the block canvas so that
161
+ // the flyout is underneath in z-order. This makes blocks layering during
162
+ // dragging work properly.
163
+ background.insertBefore(flyoutSvg, this.workspace_.svgBlockCanvas_);
164
+ this.svgDialog_.appendChild(background);
165
+
166
+ return this.svgDialog_;
167
+ };
168
+
169
+ /**
170
+ * Add or remove the UI indicating if this icon may be clicked or not.
171
+ */
172
+ Blockly.Mutator.prototype.updateEditable = function() {
173
+ if (!this.block_.isInFlyout) {
174
+ if (this.block_.isEditable()) {
175
+ if (this.iconGroup_) {
176
+ Blockly.utils.removeClass(
177
+ /** @type {!Element} */ (this.iconGroup_),
178
+ 'blocklyIconGroupReadonly');
179
+ }
180
+ } else {
181
+ // Close any mutator bubble. Icon is not clickable.
182
+ this.setVisible(false);
183
+ if (this.iconGroup_) {
184
+ Blockly.utils.addClass(
185
+ /** @type {!Element} */ (this.iconGroup_),
186
+ 'blocklyIconGroupReadonly');
187
+ }
188
+ }
189
+ }
190
+ // Default behaviour for an icon.
191
+ Blockly.Icon.prototype.updateEditable.call(this);
192
+ };
193
+
194
+ /**
195
+ * Callback function triggered when the bubble has resized.
196
+ * Resize the workspace accordingly.
197
+ * @private
198
+ */
199
+ Blockly.Mutator.prototype.resizeBubble_ = function() {
200
+ var doubleBorderWidth = 2 * Blockly.Bubble.BORDER_WIDTH;
201
+ var workspaceSize = this.workspace_.getCanvas().getBBox();
202
+ var width;
203
+ if (this.block_.RTL) {
204
+ width = -workspaceSize.x;
205
+ } else {
206
+ width = workspaceSize.width + workspaceSize.x;
207
+ }
208
+ var height = workspaceSize.height + doubleBorderWidth * 3;
209
+ if (this.workspace_.flyout_) {
210
+ var flyoutMetrics = this.workspace_.flyout_.getMetrics_();
211
+ height = Math.max(height, flyoutMetrics.contentHeight + 20);
212
+ }
213
+ width += doubleBorderWidth * 3;
214
+ // Only resize if the size difference is significant. Eliminates shuddering.
215
+ if (Math.abs(this.workspaceWidth_ - width) > doubleBorderWidth ||
216
+ Math.abs(this.workspaceHeight_ - height) > doubleBorderWidth) {
217
+ // Record some layout information for getFlyoutMetrics_.
218
+ this.workspaceWidth_ = width;
219
+ this.workspaceHeight_ = height;
220
+ // Resize the bubble.
221
+ this.bubble_.setBubbleSize(
222
+ width + doubleBorderWidth, height + doubleBorderWidth);
223
+ this.svgDialog_.setAttribute('width', this.workspaceWidth_);
224
+ this.svgDialog_.setAttribute('height', this.workspaceHeight_);
225
+ }
226
+
227
+ if (this.block_.RTL) {
228
+ // Scroll the workspace to always left-align.
229
+ var translation = 'translate(' + this.workspaceWidth_ + ',0)';
230
+ this.workspace_.getCanvas().setAttribute('transform', translation);
231
+ }
232
+ this.workspace_.resize();
233
+ };
234
+
235
+ /**
236
+ * Show or hide the mutator bubble.
237
+ * @param {boolean} visible True if the bubble should be visible.
238
+ */
239
+ Blockly.Mutator.prototype.setVisible = function(visible) {
240
+ if (visible == this.isVisible()) {
241
+ // No change.
242
+ return;
243
+ }
244
+ Blockly.Events.fire(
245
+ new Blockly.Events.Ui(this.block_, 'mutatorOpen', !visible, visible));
246
+ if (visible) {
247
+ // Create the bubble.
248
+ this.bubble_ = new Blockly.Bubble(
249
+ /** @type {!Blockly.WorkspaceSvg} */ (this.block_.workspace),
250
+ this.createEditor_(), this.block_.svgPath_, this.iconXY_, null, null);
251
+ var tree = this.workspace_.options.languageTree;
252
+ if (tree) {
253
+ this.workspace_.flyout_.init(this.workspace_);
254
+ this.workspace_.flyout_.show(tree.childNodes);
255
+ }
256
+
257
+ this.rootBlock_ = this.block_.decompose(this.workspace_);
258
+ var blocks = this.rootBlock_.getDescendants(false);
259
+ for (var i = 0, child; child = blocks[i]; i++) {
260
+ child.render();
261
+ }
262
+ // The root block should not be dragable or deletable.
263
+ this.rootBlock_.setMovable(false);
264
+ this.rootBlock_.setDeletable(false);
265
+ if (this.workspace_.flyout_) {
266
+ var margin = this.workspace_.flyout_.CORNER_RADIUS * 2;
267
+ var x = this.workspace_.flyout_.width_ + margin;
268
+ } else {
269
+ var margin = 16;
270
+ var x = margin;
271
+ }
272
+ if (this.block_.RTL) {
273
+ x = -x;
274
+ }
275
+ this.rootBlock_.moveBy(x, margin);
276
+ // Save the initial connections, then listen for further changes.
277
+ if (this.block_.saveConnections) {
278
+ var thisMutator = this;
279
+ this.block_.saveConnections(this.rootBlock_);
280
+ this.sourceListener_ = function() {
281
+ thisMutator.block_.saveConnections(thisMutator.rootBlock_);
282
+ };
283
+ this.block_.workspace.addChangeListener(this.sourceListener_);
284
+ }
285
+ this.resizeBubble_();
286
+ // When the mutator's workspace changes, update the source block.
287
+ this.workspace_.addChangeListener(this.workspaceChanged_.bind(this));
288
+ this.updateColour();
289
+ } else {
290
+ // Dispose of the bubble.
291
+ this.svgDialog_ = null;
292
+ this.workspace_.dispose();
293
+ this.workspace_ = null;
294
+ this.rootBlock_ = null;
295
+ this.bubble_.dispose();
296
+ this.bubble_ = null;
297
+ this.workspaceWidth_ = 0;
298
+ this.workspaceHeight_ = 0;
299
+ if (this.sourceListener_) {
300
+ this.block_.workspace.removeChangeListener(this.sourceListener_);
301
+ this.sourceListener_ = null;
302
+ }
303
+ }
304
+ };
305
+
306
+ /**
307
+ * Update the source block when the mutator's blocks are changed.
308
+ * Bump down any block that's too high.
309
+ * Fired whenever a change is made to the mutator's workspace.
310
+ * @private
311
+ */
312
+ Blockly.Mutator.prototype.workspaceChanged_ = function() {
313
+ if (!this.workspace_.isDragging()) {
314
+ var blocks = this.workspace_.getTopBlocks(false);
315
+ var MARGIN = 20;
316
+ for (var b = 0, block; block = blocks[b]; b++) {
317
+ var blockXY = block.getRelativeToSurfaceXY();
318
+ var blockHW = block.getHeightWidth();
319
+ if (blockXY.y + blockHW.height < MARGIN) {
320
+ // Bump any block that's above the top back inside.
321
+ block.moveBy(0, MARGIN - blockHW.height - blockXY.y);
322
+ }
323
+ }
324
+ }
325
+
326
+ // When the mutator's workspace changes, update the source block.
327
+ if (this.rootBlock_.workspace == this.workspace_) {
328
+ Blockly.Events.setGroup(true);
329
+ var block = this.block_;
330
+ var oldMutationDom = block.mutationToDom();
331
+ var oldMutation = oldMutationDom && Blockly.Xml.domToText(oldMutationDom);
332
+ // Switch off rendering while the source block is rebuilt.
333
+ var savedRendered = block.rendered;
334
+ block.rendered = false;
335
+ // Allow the source block to rebuild itself.
336
+ block.compose(this.rootBlock_);
337
+ // Restore rendering and show the changes.
338
+ block.rendered = savedRendered;
339
+ // Mutation may have added some elements that need initializing.
340
+ block.initSvg();
341
+ var newMutationDom = block.mutationToDom();
342
+ var newMutation = newMutationDom && Blockly.Xml.domToText(newMutationDom);
343
+ if (oldMutation != newMutation) {
344
+ Blockly.Events.fire(new Blockly.Events.BlockChange(
345
+ block, 'mutation', null, oldMutation, newMutation));
346
+ // Ensure that any bump is part of this mutation's event group.
347
+ var group = Blockly.Events.getGroup();
348
+ setTimeout(function() {
349
+ Blockly.Events.setGroup(group);
350
+ block.bumpNeighbours_();
351
+ Blockly.Events.setGroup(false);
352
+ }, Blockly.BUMP_DELAY);
353
+ }
354
+ if (block.rendered) {
355
+ block.render();
356
+ }
357
+ // Don't update the bubble until the drag has ended, to avoid moving blocks
358
+ // under the cursor.
359
+ if (!this.workspace_.isDragging()) {
360
+ this.resizeBubble_();
361
+ }
362
+ Blockly.Events.setGroup(false);
363
+ }
364
+ };
365
+
366
+ /**
367
+ * Return an object with all the metrics required to size scrollbars for the
368
+ * mutator flyout. The following properties are computed:
369
+ * .viewHeight: Height of the visible rectangle,
370
+ * .viewWidth: Width of the visible rectangle,
371
+ * .absoluteTop: Top-edge of view.
372
+ * .absoluteLeft: Left-edge of view.
373
+ * @return {!Object} Contains size and position metrics of mutator dialog's
374
+ * workspace.
375
+ * @private
376
+ */
377
+ Blockly.Mutator.prototype.getFlyoutMetrics_ = function() {
378
+ return {
379
+ viewHeight: this.workspaceHeight_,
380
+ viewWidth: this.workspaceWidth_,
381
+ absoluteTop: 0,
382
+ absoluteLeft: 0
383
+ };
384
+ };
385
+
386
+ /**
387
+ * Dispose of this mutator.
388
+ */
389
+ Blockly.Mutator.prototype.dispose = function() {
390
+ this.block_.mutator = null;
391
+ Blockly.Icon.prototype.dispose.call(this);
392
+ };
393
+
394
+ /**
395
+ * Reconnect an block to a mutated input.
396
+ * @param {Blockly.Connection} connectionChild Connection on child block.
397
+ * @param {!Blockly.Block} block Parent block.
398
+ * @param {string} inputName Name of input on parent block.
399
+ * @return {boolean} True iff a reconnection was made, false otherwise.
400
+ */
401
+ Blockly.Mutator.reconnect = function(connectionChild, block, inputName) {
402
+ if (!connectionChild || !connectionChild.getSourceBlock().workspace) {
403
+ return false; // No connection or block has been deleted.
404
+ }
405
+ var connectionParent = block.getInput(inputName).connection;
406
+ var currentParent = connectionChild.targetBlock();
407
+ if ((!currentParent || currentParent == block) &&
408
+ connectionParent.targetConnection != connectionChild) {
409
+ if (connectionParent.isConnected()) {
410
+ // There's already something connected here. Get rid of it.
411
+ connectionParent.disconnect();
412
+ }
413
+ connectionParent.connect(connectionChild);
414
+ return true;
415
+ }
416
+ return false;
417
+ };
418
+
419
+ // Export symbols that would otherwise be renamed by Closure compiler.
420
+ if (!goog.global['Blockly']) {
421
+ goog.global['Blockly'] = {};
422
+ }
423
+ if (!goog.global['Blockly']['Mutator']) {
424
+ goog.global['Blockly']['Mutator'] = {};
425
+ }
426
+ goog.global['Blockly']['Mutator']['reconnect'] = Blockly.Mutator.reconnect;