@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,678 @@
1
+ /**
2
+ * @license
3
+ * Visual Blocks Editor
4
+ *
5
+ * Copyright 2017 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 Class that controls updates to connections during drags.
23
+ * @author fenichel@google.com (Rachel Fenichel)
24
+ */
25
+ 'use strict';
26
+
27
+ goog.provide('Blockly.InsertionMarkerManager');
28
+
29
+ goog.require('Blockly.BlockAnimations');
30
+ goog.require('Blockly.Events.BlockMove');
31
+ goog.require('Blockly.RenderedConnection');
32
+
33
+ goog.require('goog.math.Coordinate');
34
+
35
+
36
+ /**
37
+ * Class that controls updates to connections during drags. It is primarily
38
+ * responsible for finding the closest eligible connection and highlighting or
39
+ * unhiglighting it as needed during a drag.
40
+ * @param {!Blockly.BlockSvg} block The top block in the stack being dragged.
41
+ * @constructor
42
+ */
43
+ Blockly.InsertionMarkerManager = function(block) {
44
+ Blockly.selected = block;
45
+
46
+ /**
47
+ * The top block in the stack being dragged.
48
+ * Does not change during a drag.
49
+ * @type {!Blockly.Block}
50
+ * @private
51
+ */
52
+ this.topBlock_ = block;
53
+
54
+ /**
55
+ * The workspace on which these connections are being dragged.
56
+ * Does not change during a drag.
57
+ * @type {!Blockly.WorkspaceSvg}
58
+ * @private
59
+ */
60
+ this.workspace_ = block.workspace;
61
+
62
+ /**
63
+ * The last connection on the stack, if it's not the last connection on the
64
+ * first block.
65
+ * Set in initAvailableConnections, if at all.
66
+ * @type {Blockly.RenderedConnection}
67
+ * @private
68
+ */
69
+ this.lastOnStack_ = null;
70
+
71
+ /**
72
+ * The insertion marker corresponding to the last block in the stack, if
73
+ * that's not the same as the first block in the stack.
74
+ * Set in initAvailableConnections, if at all
75
+ * @type {Blockly.BlockSvg}
76
+ * @private
77
+ */
78
+ this.lastMarker_ = null;
79
+
80
+ /**
81
+ * The insertion marker that shows up between blocks to show where a block
82
+ * would go if dropped immediately.
83
+ * This is the scratch-blocks equivalent of connection highlighting.
84
+ * @type {Blockly.BlockSvg}
85
+ * @private
86
+ */
87
+ this.firstMarker_ = this.createMarkerBlock_(this.topBlock_);
88
+
89
+ /**
90
+ * The connection that this block would connect to if released immediately.
91
+ * Updated on every mouse move.
92
+ * This is not on any of the blocks that are being dragged.
93
+ * @type {Blockly.RenderedConnection}
94
+ * @private
95
+ */
96
+ this.closestConnection_ = null;
97
+
98
+ /**
99
+ * The connection that would connect to this.closestConnection_ if this block
100
+ * were released immediately.
101
+ * Updated on every mouse move.
102
+ * This is on the top block that is being dragged or the last block in the
103
+ * dragging stack.
104
+ * @type {Blockly.RenderedConnection}
105
+ * @private
106
+ */
107
+ this.localConnection_ = null;
108
+
109
+ /**
110
+ * Whether the block would be deleted if it were dropped immediately.
111
+ * Updated on every mouse move.
112
+ * @type {boolean}
113
+ * @private
114
+ */
115
+ this.wouldDeleteBlock_ = false;
116
+
117
+ /**
118
+ * Connection on the insertion marker block that corresponds to
119
+ * this.localConnection_ on the currently dragged block.
120
+ * This is part of the scratch-blocks equivalent of connection highlighting.
121
+ * @type {Blockly.RenderedConnection}
122
+ * @private
123
+ */
124
+ this.markerConnection_ = null;
125
+
126
+ /**
127
+ * Whether we are currently highlighting the block (shadow or real) that would
128
+ * be replaced if the drag were released immediately.
129
+ * @type {boolean}
130
+ * @private
131
+ */
132
+ this.highlightingBlock_ = false;
133
+
134
+ /**
135
+ * The block that is being highlighted for replacement, or null.
136
+ * @type {Blockly.BlockSvg}
137
+ * @private
138
+ */
139
+ this.highlightedBlock_ = null;
140
+
141
+ /**
142
+ * The connections on the dragging blocks that are available to connect to
143
+ * other blocks. This includes all open connections on the top block, as well
144
+ * as the last connection on the block stack.
145
+ * Does not change during a drag.
146
+ * @type {!Array.<!Blockly.RenderedConnection>}
147
+ * @private
148
+ */
149
+ this.availableConnections_ = this.initAvailableConnections_();
150
+ };
151
+
152
+ /**
153
+ * Sever all links from this object.
154
+ * @package
155
+ */
156
+ Blockly.InsertionMarkerManager.prototype.dispose = function() {
157
+ this.topBlock_ = null;
158
+ this.workspace_ = null;
159
+ this.availableConnections_.length = 0;
160
+ this.closestConnection_ = null;
161
+ this.localConnection_ = null;
162
+
163
+ Blockly.Events.disable();
164
+ try {
165
+ if (this.firstMarker_) {
166
+ this.firstMarker_.dispose();
167
+ this.firstMarker_ = null;
168
+ }
169
+ if (this.lastMarker_) {
170
+ this.lastMarker_.dispose();
171
+ this.lastMarker_ = null;
172
+ }
173
+ } finally {
174
+ Blockly.Events.enable();
175
+ }
176
+
177
+ this.highlightedBlock_ = null;
178
+ };
179
+
180
+ /**
181
+ * Return whether the block would be deleted if dropped immediately, based on
182
+ * information from the most recent move event.
183
+ * @return {boolean} true if the block would be deleted if dropped immediately.
184
+ * @package
185
+ */
186
+ Blockly.InsertionMarkerManager.prototype.wouldDeleteBlock = function() {
187
+ return this.wouldDeleteBlock_;
188
+ };
189
+
190
+ /**
191
+ * Return whether the block would be connected if dropped immediately, based on
192
+ * information from the most recent move event.
193
+ * @return {boolean} True if the block would be connected if dropped
194
+ * immediately.
195
+ * @package
196
+ */
197
+ Blockly.InsertionMarkerManager.prototype.wouldConnectBlock = function() {
198
+ return !!this.closestConnection_;
199
+ };
200
+
201
+ /**
202
+ * Connect to the closest connection and render the results.
203
+ * This should be called at the end of a drag.
204
+ * @package
205
+ */
206
+ Blockly.InsertionMarkerManager.prototype.applyConnections = function() {
207
+ if (this.closestConnection_) {
208
+ // Don't fire events for insertion markers.
209
+ Blockly.Events.disable();
210
+ this.hidePreview_();
211
+ Blockly.Events.enable();
212
+ // Connect two blocks together.
213
+ this.localConnection_.connect(this.closestConnection_);
214
+ if (this.topBlock_.rendered) {
215
+ // Trigger a connection animation.
216
+ // Determine which connection is inferior (lower in the source stack).
217
+ var inferiorConnection = this.localConnection_.isSuperior() ?
218
+ this.closestConnection_ : this.localConnection_;
219
+ Blockly.BlockAnimations.connectionUiEffect(
220
+ inferiorConnection.getSourceBlock());
221
+ // Bring the just-edited stack to the front.
222
+ var rootBlock = this.topBlock_.getRootBlock();
223
+ rootBlock.bringToFront();
224
+ }
225
+ }
226
+ };
227
+
228
+ /**
229
+ * Update highlighted connections based on the most recent move location.
230
+ * @param {!goog.math.Coordinate} dxy Position relative to drag start,
231
+ * in workspace units.
232
+ * @param {?number} deleteArea One of {@link Blockly.DELETE_AREA_TRASH},
233
+ * {@link Blockly.DELETE_AREA_TOOLBOX}, or {@link Blockly.DELETE_AREA_NONE}.
234
+ * @package
235
+ */
236
+ Blockly.InsertionMarkerManager.prototype.update = function(dxy, deleteArea) {
237
+ var candidate = this.getCandidate_(dxy);
238
+
239
+ this.wouldDeleteBlock_ = this.shouldDelete_(candidate, deleteArea);
240
+ var shouldUpdate = this.wouldDeleteBlock_ ||
241
+ this.shouldUpdatePreviews_(candidate, dxy);
242
+
243
+ if (shouldUpdate) {
244
+ // Don't fire events for insertion marker creation or movement.
245
+ Blockly.Events.disable();
246
+ this.maybeHidePreview_(candidate);
247
+ this.maybeShowPreview_(candidate);
248
+ Blockly.Events.enable();
249
+ }
250
+ };
251
+
252
+ /**** Begin initialization functions ****/
253
+
254
+ /**
255
+ * Create an insertion marker that represents the given block.
256
+ * @param {!Blockly.BlockSvg} sourceBlock The block that the insertion marker
257
+ * will represent.
258
+ * @return {!Blockly.BlockSvg} The insertion marker that represents the given
259
+ * block.
260
+ * @private
261
+ */
262
+ Blockly.InsertionMarkerManager.prototype.createMarkerBlock_ = function(sourceBlock) {
263
+ var imType = sourceBlock.type;
264
+
265
+ Blockly.Events.disable();
266
+ try {
267
+ var result = this.workspace_.newBlock(imType);
268
+ result.setInsertionMarker(true, sourceBlock.width);
269
+ if (sourceBlock.mutationToDom) {
270
+ var oldMutationDom = sourceBlock.mutationToDom();
271
+ if (oldMutationDom) {
272
+ result.domToMutation(oldMutationDom);
273
+ }
274
+ }
275
+ result.initSvg();
276
+ } finally {
277
+ Blockly.Events.enable();
278
+ }
279
+
280
+ return result;
281
+ };
282
+
283
+ /**
284
+ * Populate the list of available connections on this block stack. This should
285
+ * only be called once, at the beginning of a drag.
286
+ * If the stack has more than one block, this function will populate
287
+ * lastOnStack_ and create the corresponding insertion marker.
288
+ * @return {!Array.<!Blockly.RenderedConnection>} a list of available
289
+ * connections.
290
+ * @private
291
+ */
292
+ Blockly.InsertionMarkerManager.prototype.initAvailableConnections_ = function() {
293
+ var available = this.topBlock_.getConnections_(false);
294
+ // Also check the last connection on this stack
295
+ var lastOnStack = this.topBlock_.lastConnectionInStack();
296
+ if (lastOnStack && lastOnStack != this.topBlock_.nextConnection) {
297
+ available.push(lastOnStack);
298
+ this.lastOnStack_ = lastOnStack;
299
+ this.lastMarker_ = this.createMarkerBlock_(lastOnStack.sourceBlock_);
300
+ }
301
+ return available;
302
+ };
303
+
304
+ /**** End initialization functions ****/
305
+
306
+
307
+ /**
308
+ * Whether the previews (insertion marker and replacement marker) should be
309
+ * updated based on the closest candidate and the current drag distance.
310
+ * @param {!Object} candidate An object containing a local connection, a closest
311
+ * connection, and a radius. Returned by getCandidate_.
312
+ * @param {!goog.math.Coordinate} dxy Position relative to drag start,
313
+ * in workspace units.
314
+ * @return {boolean} whether the preview should be updated.
315
+ * @private
316
+ */
317
+ Blockly.InsertionMarkerManager.prototype.shouldUpdatePreviews_ = function(
318
+ candidate, dxy) {
319
+ var candidateLocal = candidate.local;
320
+ var candidateClosest = candidate.closest;
321
+ var radius = candidate.radius;
322
+
323
+ // Found a connection!
324
+ if (candidateLocal && candidateClosest) {
325
+ if (candidateLocal.type == Blockly.OUTPUT_VALUE) {
326
+ // Always update previews for output connections.
327
+ return true;
328
+ }
329
+ // We're already showing an insertion marker.
330
+ // Decide whether the new connection has higher priority.
331
+ if (this.localConnection_ && this.closestConnection_) {
332
+ // The connection was the same as the current connection.
333
+ if (this.closestConnection_ == candidateClosest) {
334
+ return false;
335
+ }
336
+ var xDiff = this.localConnection_.x_ + dxy.x - this.closestConnection_.x_;
337
+ var yDiff = this.localConnection_.y_ + dxy.y - this.closestConnection_.y_;
338
+ var curDistance = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
339
+ // Slightly prefer the existing preview over a new preview.
340
+ return !(candidateClosest && radius > curDistance -
341
+ Blockly.CURRENT_CONNECTION_PREFERENCE);
342
+ } else if (!this.localConnection_ && !this.closestConnection_) {
343
+ // We weren't showing a preview before, but we should now.
344
+ return true;
345
+ } else {
346
+ console.error('Only one of localConnection_ and closestConnection_ was set.');
347
+ }
348
+ } else { // No connection found.
349
+ // Only need to update if we were showing a preview before.
350
+ return !!(this.localConnection_ && this.closestConnection_);
351
+ }
352
+
353
+ console.error('Returning true from shouldUpdatePreviews, but it\'s not clear why.');
354
+ return true;
355
+ };
356
+
357
+ /**
358
+ * Find the nearest valid connection, which may be the same as the current
359
+ * closest connection.
360
+ * @param {!goog.math.Coordinate} dxy Position relative to drag start,
361
+ * in workspace units.
362
+ * @return {!Object} candidate An object containing a local connection, a closest
363
+ * connection, and a radius.
364
+ */
365
+ Blockly.InsertionMarkerManager.prototype.getCandidate_ = function(dxy) {
366
+ var radius = this.getStartRadius_();
367
+ var candidateClosest = null;
368
+ var candidateLocal = null;
369
+
370
+ for (var i = 0; i < this.availableConnections_.length; i++) {
371
+ var myConnection = this.availableConnections_[i];
372
+ var neighbour = myConnection.closest(radius, dxy);
373
+ if (neighbour.connection) {
374
+ candidateClosest = neighbour.connection;
375
+ candidateLocal = myConnection;
376
+ radius = neighbour.radius;
377
+ }
378
+ }
379
+ return {
380
+ closest: candidateClosest,
381
+ local: candidateLocal,
382
+ radius: radius
383
+ };
384
+ };
385
+
386
+ /**
387
+ * Decide the radius at which to start searching for the closest connection.
388
+ * @return {number} The radius at which to start the search for the closest
389
+ * connection.
390
+ * @private
391
+ */
392
+ Blockly.InsertionMarkerManager.prototype.getStartRadius_ = function() {
393
+ // If there is already a connection highlighted,
394
+ // increase the radius we check for making new connections.
395
+ // Why? When a connection is highlighted, blocks move around when the insertion
396
+ // marker is created, which could cause the connection became out of range.
397
+ // By increasing radiusConnection when a connection already exists,
398
+ // we never "lose" the connection from the offset.
399
+ if (this.closestConnection_ && this.localConnection_) {
400
+ return Blockly.CONNECTING_SNAP_RADIUS;
401
+ }
402
+ return Blockly.SNAP_RADIUS;
403
+ };
404
+
405
+ /**
406
+ * Whether ending the drag would replace a block or insert a block.
407
+ * @return {boolean} True if dropping the block immediately would replace
408
+ * another block. False if dropping the block immediately would result in
409
+ * the block being inserted in a block stack.
410
+ * @private
411
+ */
412
+ Blockly.InsertionMarkerManager.prototype.shouldReplace_ = function() {
413
+ var closest = this.closestConnection_;
414
+ var local = this.localConnection_;
415
+
416
+ // Dragging a block over an existing block in an input should replace the
417
+ // existing block and bump it out.
418
+ if (local.type == Blockly.OUTPUT_VALUE) {
419
+ return true; // Replace.
420
+ }
421
+
422
+ // Connecting to a statement input of c-block is an insertion, even if that
423
+ // c-block is terminal (e.g. forever).
424
+ if (local == local.sourceBlock_.getFirstStatementConnection()) {
425
+ return false; // Insert.
426
+ }
427
+
428
+ // Dragging a terminal block over another (connected) terminal block will
429
+ // replace, not insert.
430
+ var isTerminalBlock = !this.topBlock_.nextConnection;
431
+ var isConnectedTerminal = isTerminalBlock &&
432
+ local.type == Blockly.PREVIOUS_STATEMENT && closest.isConnected();
433
+ if (isConnectedTerminal) {
434
+ return true; // Replace.
435
+ }
436
+
437
+ // Otherwise it's an insertion.
438
+ return false;
439
+ };
440
+
441
+ /**
442
+ * Whether ending the drag would delete the block.
443
+ * @param {!Object} candidate An object containing a local connection, a closest
444
+ * connection, and a radius.
445
+ * @param {?number} deleteArea One of {@link Blockly.DELETE_AREA_TRASH},
446
+ * {@link Blockly.DELETE_AREA_TOOLBOX}, or {@link Blockly.DELETE_AREA_NONE}.
447
+ * @return {boolean} True if dropping the block immediately would replace
448
+ * delete the block. False otherwise.
449
+ * @private
450
+ */
451
+ Blockly.InsertionMarkerManager.prototype.shouldDelete_ = function(candidate,
452
+ deleteArea) {
453
+ // Prefer connecting over dropping into the trash can, but prefer dragging to
454
+ // the toolbox over connecting to other blocks.
455
+ var wouldConnect = candidate && !!candidate.closest &&
456
+ deleteArea != Blockly.DELETE_AREA_TOOLBOX;
457
+ var wouldDelete = !!deleteArea && !this.topBlock_.getParent() &&
458
+ this.topBlock_.isDeletable();
459
+
460
+ return wouldDelete && !wouldConnect;
461
+ };
462
+
463
+ /**** Begin preview visibility functions ****/
464
+
465
+ /**
466
+ * Show an insertion marker or replacement highlighting during a drag, if
467
+ * needed.
468
+ * At the beginning of this function, this.localConnection_ and
469
+ * this.closestConnection_ should both be null.
470
+ * @param {!Object} candidate An object containing a local connection, a closest
471
+ * connection, and a radius.
472
+ * @private
473
+ */
474
+ Blockly.InsertionMarkerManager.prototype.maybeShowPreview_ = function(candidate) {
475
+ // Nope, don't add a marker.
476
+ if (this.wouldDeleteBlock_) {
477
+ return;
478
+ }
479
+ var closest = candidate.closest;
480
+ var local = candidate.local;
481
+
482
+ // Nothing to connect to.
483
+ if (!closest) {
484
+ return;
485
+ }
486
+
487
+ // Something went wrong and we're trying to connect to an invalid connection.
488
+ if (closest == this.closestConnection_ ||
489
+ closest.sourceBlock_.isInsertionMarker()) {
490
+ return;
491
+ }
492
+ // Add an insertion marker or replacement marker.
493
+ this.closestConnection_ = closest;
494
+ this.localConnection_ = local;
495
+ this.showPreview_();
496
+ };
497
+
498
+ /**
499
+ * A preview should be shown. This function figures out if it should be a block
500
+ * highlight or an insertion marker, and shows the appropriate one.
501
+ * @private
502
+ */
503
+ Blockly.InsertionMarkerManager.prototype.showPreview_ = function() {
504
+ if (this.shouldReplace_()) {
505
+ this.highlightBlock_();
506
+ } else { // Should insert
507
+ this.connectMarker_();
508
+ }
509
+ };
510
+
511
+ /**
512
+ * Show an insertion marker or replacement highlighting during a drag, if
513
+ * needed.
514
+ * At the end of this function, this.localConnection_ and
515
+ * this.closestConnection_ should both be null.
516
+ * @param {!Object} candidate An object containing a local connection, a closest
517
+ * connection, and a radius.
518
+ * @private
519
+ */
520
+ Blockly.InsertionMarkerManager.prototype.maybeHidePreview_ = function(candidate) {
521
+ // If there's no new preview, remove the old one but don't bother deleting it.
522
+ // We might need it later, and this saves disposing of it and recreating it.
523
+ if (!candidate.closest) {
524
+ this.hidePreview_();
525
+ }
526
+ // If there's a new preview and there was an preview before, and either
527
+ // connection has changed, remove the old preview.
528
+ var hadPreview = this.closestConnection_ && this.localConnection_;
529
+ var closestChanged = this.closestConnection_ != candidate.closest;
530
+ var localChanged = this.localConnection_ != candidate.local;
531
+
532
+ // Also hide if we had a preview before but now we're going to delete instead.
533
+ if (hadPreview && (closestChanged || localChanged || this.wouldDeleteBlock_)) {
534
+ this.hidePreview_();
535
+ }
536
+
537
+ // Either way, clear out old state.
538
+ this.markerConnection_ = null;
539
+ this.closestConnection_ = null;
540
+ this.localConnection_ = null;
541
+ };
542
+
543
+ /**
544
+ * A preview should be hidden. This function figures out if it is a block
545
+ * highlight or an insertion marker, and hides the appropriate one.
546
+ * @private
547
+ */
548
+ Blockly.InsertionMarkerManager.prototype.hidePreview_ = function() {
549
+ if (this.highlightingBlock_) {
550
+ this.unhighlightBlock_();
551
+ } else if (this.markerConnection_) {
552
+ this.disconnectMarker_();
553
+ }
554
+ };
555
+
556
+ /**** End preview visibility functions ****/
557
+
558
+ /**** Begin block highlighting functions ****/
559
+
560
+ /**
561
+ * Add highlighting showing which block will be replaced.
562
+ * Scratch-specific code, where "highlighting" applies to a block rather than
563
+ * a connection.
564
+ */
565
+ Blockly.InsertionMarkerManager.prototype.highlightBlock_ = function() {
566
+ var closest = this.closestConnection_;
567
+ var local = this.localConnection_;
568
+ if (closest.targetBlock()) {
569
+ this.highlightedBlock_ = closest.targetBlock();
570
+ closest.targetBlock().highlightForReplacement(true);
571
+ } else if(local.type == Blockly.OUTPUT_VALUE) {
572
+ this.highlightedBlock_ = closest.sourceBlock_;
573
+ closest.sourceBlock_.highlightShapeForInput(closest, true);
574
+ }
575
+ this.highlightingBlock_ = true;
576
+ };
577
+
578
+ /**
579
+ * Get rid of the highlighting marking the block that will be replaced.
580
+ * Scratch-specific code, where "highlighting" applies to a block rather than
581
+ * a connection.
582
+ */
583
+ Blockly.InsertionMarkerManager.prototype.unhighlightBlock_ = function() {
584
+ var closest = this.closestConnection_;
585
+ // If there's no block in place, but we're still connecting to a value input,
586
+ // then we must have been highlighting an input shape.
587
+ if (closest.type == Blockly.INPUT_VALUE && !closest.isConnected()) {
588
+ this.highlightedBlock_.highlightShapeForInput(closest, false);
589
+ } else {
590
+ this.highlightedBlock_.highlightForReplacement(false);
591
+ }
592
+ this.highlightedBlock_ = null;
593
+ this.highlightingBlock_ = false;
594
+ };
595
+
596
+ /**** End block highlighting functions ****/
597
+
598
+ /**** Begin insertion marker display functions ****/
599
+
600
+ /**
601
+ * Disconnect the insertion marker block in a manner that returns the stack to
602
+ * original state.
603
+ * @private
604
+ */
605
+ Blockly.InsertionMarkerManager.prototype.disconnectMarker_ = function() {
606
+ if (!this.markerConnection_) {
607
+ console.log('No insertion marker connection to disconnect');
608
+ return;
609
+ }
610
+
611
+ var imConn = this.markerConnection_;
612
+ var imBlock = imConn.sourceBlock_;
613
+ var markerNext = imBlock.nextConnection;
614
+ var markerPrev = imBlock.previousConnection;
615
+
616
+
617
+ // The insertion marker is the first block in a stack, either because it
618
+ // doesn't have a previous connection or because the previous connection is
619
+ // not connected. Unplug won't do anything in that case. Instead, unplug the
620
+ // following block.
621
+ if (imConn == markerNext && !(markerPrev && markerPrev.targetConnection)) {
622
+ imConn.targetBlock().unplug(false);
623
+ }
624
+ // Inside of a C-block, first statement connection.
625
+ else if (imConn.type == Blockly.NEXT_STATEMENT && imConn != markerNext) {
626
+ var innerConnection = imConn.targetConnection;
627
+ innerConnection.sourceBlock_.unplug(false);
628
+
629
+ var previousBlockNextConnection =
630
+ markerPrev ? markerPrev.targetConnection : null;
631
+
632
+ imBlock.unplug(true);
633
+ if (previousBlockNextConnection) {
634
+ previousBlockNextConnection.connect(innerConnection);
635
+ }
636
+ } else {
637
+ imBlock.unplug(true /* healStack */);
638
+ }
639
+
640
+ if (imConn.targetConnection) {
641
+ throw 'markerConnection_ still connected at the end of disconnectInsertionMarker';
642
+ }
643
+
644
+ this.markerConnection_ = null;
645
+ imBlock.getSvgRoot().setAttribute('visibility', 'hidden');
646
+ };
647
+
648
+ /**
649
+ * Add an insertion marker connected to the appropriate blocks.
650
+ * @private
651
+ */
652
+ Blockly.InsertionMarkerManager.prototype.connectMarker_ = function() {
653
+ var local = this.localConnection_;
654
+ var closest = this.closestConnection_;
655
+
656
+ var isLastInStack = this.lastOnStack_ && local == this.lastOnStack_;
657
+ var imBlock = isLastInStack ? this.lastMarker_ : this.firstMarker_;
658
+ var imConn = imBlock.getMatchingConnection(local.sourceBlock_, local);
659
+
660
+ goog.asserts.assert(imConn != this.markerConnection_,
661
+ 'Made it to connectMarker_ even though the marker isn\'t changing');
662
+
663
+ // Render disconnected from everything else so that we have a valid
664
+ // connection location.
665
+ imBlock.render();
666
+ imBlock.rendered = true;
667
+ imBlock.getSvgRoot().setAttribute('visibility', 'visible');
668
+
669
+ // TODO: positionNewBlock should be on Blockly.BlockSvg, not prototype,
670
+ // because it doesn't rely on anything in the block it's called on.
671
+ imBlock.positionNewBlock(imBlock, imConn, closest);
672
+
673
+ // Connect() also renders the insertion marker.
674
+ imConn.connect(closest);
675
+ this.markerConnection_ = imConn;
676
+ };
677
+
678
+ /**** End insertion marker display functions ****/