blockly 7.20211209.4 → 8.0.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 (262) hide show
  1. package/blockly.d.ts +18963 -18432
  2. package/blockly.min.js +852 -844
  3. package/blockly_compressed.js +669 -664
  4. package/blockly_compressed.js.map +1 -1
  5. package/blocks/blocks.js +47 -0
  6. package/blocks/colour.js +13 -3
  7. package/blocks/lists.js +22 -13
  8. package/blocks/logic.js +13 -3
  9. package/blocks/loops.js +24 -11
  10. package/blocks/math.js +12 -3
  11. package/blocks/procedures.js +41 -27
  12. package/blocks/text.js +22 -13
  13. package/blocks/variables.js +14 -3
  14. package/blocks/variables_dynamic.js +13 -3
  15. package/blocks_compressed.js +146 -141
  16. package/blocks_compressed.js.map +1 -1
  17. package/core/block.js +1869 -1814
  18. package/core/block_drag_surface.js +201 -200
  19. package/core/block_dragger.js +377 -373
  20. package/core/block_svg.js +1593 -1479
  21. package/core/blockly.js +8 -22
  22. package/core/blocks.js +9 -2
  23. package/core/browser_events.js +22 -5
  24. package/core/bubble.js +841 -797
  25. package/core/bubble_dragger.js +213 -206
  26. package/core/bump_objects.js +2 -2
  27. package/core/clipboard.js +9 -9
  28. package/core/comment.js +353 -332
  29. package/core/common.js +46 -17
  30. package/core/component_manager.js +181 -174
  31. package/core/config.js +87 -0
  32. package/core/connection.js +595 -584
  33. package/core/connection_checker.js +242 -244
  34. package/core/connection_db.js +235 -230
  35. package/core/contextmenu.js +9 -6
  36. package/core/contextmenu_items.js +1 -2
  37. package/core/contextmenu_registry.js +93 -89
  38. package/core/css.js +474 -474
  39. package/core/delete_area.js +45 -42
  40. package/core/drag_target.js +57 -56
  41. package/core/dropdowndiv.js +153 -163
  42. package/core/events/events.js +2 -2
  43. package/core/events/events_abstract.js +89 -77
  44. package/core/events/events_block_base.js +37 -36
  45. package/core/events/events_block_change.js +130 -124
  46. package/core/events/events_block_create.js +73 -71
  47. package/core/events/events_block_delete.js +84 -82
  48. package/core/events/events_block_drag.js +50 -49
  49. package/core/events/events_block_move.js +147 -140
  50. package/core/events/events_bubble_open.js +51 -50
  51. package/core/events/events_click.js +48 -44
  52. package/core/events/events_comment_base.js +72 -69
  53. package/core/events/events_comment_change.js +63 -61
  54. package/core/events/events_comment_create.js +44 -42
  55. package/core/events/events_comment_delete.js +42 -40
  56. package/core/events/events_comment_move.js +106 -104
  57. package/core/events/events_marker_move.js +65 -64
  58. package/core/events/events_selected.js +46 -45
  59. package/core/events/events_theme_change.js +36 -35
  60. package/core/events/events_toolbox_item_select.js +46 -45
  61. package/core/events/events_trashcan_open.js +37 -36
  62. package/core/events/events_ui.js +47 -46
  63. package/core/events/events_ui_base.js +30 -29
  64. package/core/events/events_var_base.js +37 -36
  65. package/core/events/events_var_create.js +50 -48
  66. package/core/events/events_var_delete.js +50 -48
  67. package/core/events/events_var_rename.js +51 -49
  68. package/core/events/events_viewport.js +66 -65
  69. package/core/events/utils.js +29 -14
  70. package/core/events/workspace_events.js +49 -55
  71. package/core/extensions.js +4 -3
  72. package/core/field.js +1061 -997
  73. package/core/field_angle.js +462 -442
  74. package/core/field_checkbox.js +194 -182
  75. package/core/field_colour.js +519 -505
  76. package/core/field_dropdown.js +617 -598
  77. package/core/field_image.js +229 -220
  78. package/core/field_label.js +102 -91
  79. package/core/field_label_serializable.js +42 -41
  80. package/core/field_multilineinput.js +372 -358
  81. package/core/field_number.js +272 -253
  82. package/core/field_textinput.js +499 -467
  83. package/core/field_variable.js +458 -420
  84. package/core/flyout_base.js +1005 -952
  85. package/core/flyout_button.js +277 -260
  86. package/core/flyout_horizontal.js +304 -302
  87. package/core/flyout_metrics_manager.js +64 -64
  88. package/core/flyout_vertical.js +306 -300
  89. package/core/generator.js +459 -446
  90. package/core/gesture.js +829 -813
  91. package/core/grid.js +166 -163
  92. package/core/icon.js +168 -159
  93. package/core/inject.js +7 -5
  94. package/core/input.js +257 -248
  95. package/core/insertion_marker_manager.js +655 -624
  96. package/core/internal_constants.js +0 -129
  97. package/core/keyboard_nav/ast_node.js +605 -596
  98. package/core/keyboard_nav/basic_cursor.js +166 -165
  99. package/core/keyboard_nav/cursor.js +99 -97
  100. package/core/keyboard_nav/marker.js +83 -79
  101. package/core/keyboard_nav/tab_navigate_cursor.js +18 -23
  102. package/core/marker_manager.js +153 -141
  103. package/core/menu.js +377 -372
  104. package/core/menuitem.js +223 -217
  105. package/core/metrics_manager.js +403 -390
  106. package/core/mutator.js +468 -437
  107. package/core/names.js +229 -188
  108. package/core/options.js +290 -284
  109. package/core/procedures.js +29 -17
  110. package/core/registry.js +19 -16
  111. package/core/rendered_connection.js +482 -463
  112. package/core/renderers/common/block_rendering.js +9 -3
  113. package/core/renderers/common/constants.js +1119 -1112
  114. package/core/renderers/common/debug.js +14 -0
  115. package/core/renderers/common/debugger.js +338 -316
  116. package/core/renderers/common/drawer.js +380 -370
  117. package/core/renderers/common/i_path_object.js +2 -2
  118. package/core/renderers/common/info.js +626 -618
  119. package/core/renderers/common/marker_svg.js +579 -541
  120. package/core/renderers/common/path_object.js +203 -200
  121. package/core/renderers/common/renderer.js +220 -218
  122. package/core/renderers/geras/constants.js +36 -36
  123. package/core/renderers/geras/drawer.js +155 -147
  124. package/core/renderers/geras/highlight_constants.js +244 -238
  125. package/core/renderers/geras/highlighter.js +231 -179
  126. package/core/renderers/geras/info.js +392 -369
  127. package/core/renderers/geras/measurables/inline_input.js +25 -19
  128. package/core/renderers/geras/measurables/statement_input.js +23 -17
  129. package/core/renderers/geras/path_object.js +106 -121
  130. package/core/renderers/geras/renderer.js +96 -98
  131. package/core/renderers/measurables/base.js +30 -18
  132. package/core/renderers/measurables/bottom_row.js +83 -80
  133. package/core/renderers/measurables/connection.js +22 -15
  134. package/core/renderers/measurables/external_value_input.js +35 -22
  135. package/core/renderers/measurables/field.js +35 -20
  136. package/core/renderers/measurables/hat.js +18 -13
  137. package/core/renderers/measurables/icon.js +24 -17
  138. package/core/renderers/measurables/in_row_spacer.js +15 -13
  139. package/core/renderers/measurables/inline_input.js +43 -33
  140. package/core/renderers/measurables/input_connection.js +41 -28
  141. package/core/renderers/measurables/input_row.js +50 -44
  142. package/core/renderers/measurables/jagged_edge.js +14 -12
  143. package/core/renderers/measurables/next_connection.js +16 -14
  144. package/core/renderers/measurables/output_connection.js +26 -20
  145. package/core/renderers/measurables/previous_connection.js +16 -15
  146. package/core/renderers/measurables/round_corner.js +20 -18
  147. package/core/renderers/measurables/row.js +184 -168
  148. package/core/renderers/measurables/spacer_row.js +38 -23
  149. package/core/renderers/measurables/square_corner.js +18 -16
  150. package/core/renderers/measurables/statement_input.js +23 -20
  151. package/core/renderers/measurables/top_row.js +88 -85
  152. package/core/renderers/minimalist/constants.js +8 -7
  153. package/core/renderers/minimalist/drawer.js +11 -10
  154. package/core/renderers/minimalist/info.js +18 -18
  155. package/core/renderers/minimalist/renderer.js +40 -39
  156. package/core/renderers/thrasos/info.js +258 -248
  157. package/core/renderers/thrasos/renderer.js +20 -20
  158. package/core/renderers/zelos/constants.js +898 -873
  159. package/core/renderers/zelos/drawer.js +186 -169
  160. package/core/renderers/zelos/info.js +502 -479
  161. package/core/renderers/zelos/marker_svg.js +129 -115
  162. package/core/renderers/zelos/measurables/bottom_row.js +31 -30
  163. package/core/renderers/zelos/measurables/inputs.js +22 -21
  164. package/core/renderers/zelos/measurables/row_elements.js +14 -13
  165. package/core/renderers/zelos/measurables/top_row.js +34 -33
  166. package/core/renderers/zelos/path_object.js +181 -180
  167. package/core/renderers/zelos/renderer.js +91 -92
  168. package/core/scrollbar.js +759 -713
  169. package/core/scrollbar_pair.js +250 -245
  170. package/core/serialization/blocks.js +19 -9
  171. package/core/serialization/workspaces.js +3 -2
  172. package/core/shortcut_registry.js +286 -277
  173. package/core/sprites.js +31 -0
  174. package/core/theme.js +135 -141
  175. package/core/theme_manager.js +147 -143
  176. package/core/toolbox/category.js +602 -576
  177. package/core/toolbox/collapsible_category.js +226 -227
  178. package/core/toolbox/separator.js +70 -61
  179. package/core/toolbox/toolbox.js +934 -927
  180. package/core/toolbox/toolbox_item.js +115 -99
  181. package/core/tooltip.js +108 -35
  182. package/core/touch.js +8 -3
  183. package/core/touch_gesture.js +254 -251
  184. package/core/trashcan.js +606 -595
  185. package/core/utils/coordinate.js +97 -95
  186. package/core/utils/dom.js +2 -2
  187. package/core/utils/global.js +2 -0
  188. package/core/utils/rect.js +41 -37
  189. package/core/utils/sentinel.js +25 -0
  190. package/core/utils/size.js +30 -27
  191. package/core/utils/svg.js +18 -16
  192. package/core/variable_map.js +325 -341
  193. package/core/variable_model.js +55 -54
  194. package/core/variables.js +9 -2
  195. package/core/variables_dynamic.js +3 -1
  196. package/core/warning.js +126 -120
  197. package/core/widgetdiv.js +4 -4
  198. package/core/workspace.js +685 -664
  199. package/core/workspace_audio.js +124 -118
  200. package/core/workspace_comment.js +308 -298
  201. package/core/workspace_comment_svg.js +1029 -951
  202. package/core/workspace_drag_surface_svg.js +147 -140
  203. package/core/workspace_dragger.js +70 -71
  204. package/core/workspace_svg.js +2322 -2297
  205. package/core/xml.js +30 -20
  206. package/core/zoom_controls.js +431 -439
  207. package/dart_compressed.js +40 -43
  208. package/dart_compressed.js.map +1 -1
  209. package/generators/dart/colour.js +56 -64
  210. package/generators/dart/lists.js +61 -50
  211. package/generators/dart/math.js +160 -148
  212. package/generators/dart/text.js +83 -61
  213. package/generators/javascript/colour.js +37 -34
  214. package/generators/javascript/lists.js +50 -43
  215. package/generators/javascript/math.js +123 -139
  216. package/generators/javascript/text.js +67 -81
  217. package/generators/lua/colour.js +25 -23
  218. package/generators/lua/lists.js +97 -69
  219. package/generators/lua/logic.js +1 -2
  220. package/generators/lua/math.js +182 -144
  221. package/generators/lua/text.js +116 -99
  222. package/generators/php/colour.js +38 -32
  223. package/generators/php/lists.js +109 -89
  224. package/generators/php/math.js +90 -81
  225. package/generators/php/text.js +63 -61
  226. package/generators/python/colour.js +18 -18
  227. package/generators/python/lists.js +38 -30
  228. package/generators/python/loops.js +12 -8
  229. package/generators/python/math.js +104 -106
  230. package/generators/python/text.js +34 -30
  231. package/javascript_compressed.js +37 -39
  232. package/javascript_compressed.js.map +1 -1
  233. package/lua_compressed.js +39 -42
  234. package/lua_compressed.js.map +1 -1
  235. package/msg/az.js +2 -2
  236. package/msg/be.js +4 -4
  237. package/msg/cs.js +15 -15
  238. package/msg/de.js +1 -1
  239. package/msg/diq.js +1 -1
  240. package/msg/eo.js +1 -1
  241. package/msg/es.js +1 -1
  242. package/msg/fa.js +1 -1
  243. package/msg/fr.js +4 -4
  244. package/msg/he.js +1 -1
  245. package/msg/hr.js +2 -2
  246. package/msg/hy.js +2 -2
  247. package/msg/id.js +12 -12
  248. package/msg/inh.js +14 -14
  249. package/msg/ja.js +7 -7
  250. package/msg/lv.js +29 -29
  251. package/msg/pa.js +3 -3
  252. package/msg/smn.js +436 -0
  253. package/msg/te.js +1 -1
  254. package/msg/yue.js +1 -1
  255. package/msg/zh-hans.js +3 -3
  256. package/msg/zh-hant.js +3 -3
  257. package/package.json +7 -6
  258. package/php_compressed.js +38 -42
  259. package/php_compressed.js.map +1 -1
  260. package/python_compressed.js +26 -25
  261. package/python_compressed.js.map +1 -1
  262. package/blocks/all.js +0 -23
@@ -19,10 +19,10 @@ const blockAnimations = goog.require('Blockly.blockAnimations');
19
19
  const common = goog.require('Blockly.common');
20
20
  const constants = goog.require('Blockly.constants');
21
21
  const eventUtils = goog.require('Blockly.Events.utils');
22
- const internalConstants = goog.require('Blockly.internalConstants');
23
22
  /* eslint-disable-next-line no-unused-vars */
24
23
  const {BlockSvg} = goog.requireType('Blockly.BlockSvg');
25
24
  const {ComponentManager} = goog.require('Blockly.ComponentManager');
25
+ const {config} = goog.require('Blockly.config');
26
26
  const {ConnectionType} = goog.require('Blockly.ConnectionType');
27
27
  /* eslint-disable-next-line no-unused-vars */
28
28
  const {Coordinate} = goog.requireType('Blockly.utils.Coordinate');
@@ -36,724 +36,755 @@ const {RenderedConnection} = goog.requireType('Blockly.RenderedConnection');
36
36
  const {WorkspaceSvg} = goog.requireType('Blockly.WorkspaceSvg');
37
37
 
38
38
 
39
+ /**
40
+ * An error message to throw if the block created by createMarkerBlock_ is
41
+ * missing any components.
42
+ * @type {string}
43
+ * @const
44
+ */
45
+ const DUPLICATE_BLOCK_ERROR = 'The insertion marker ' +
46
+ 'manager tried to create a marker but the result is missing %1. If ' +
47
+ 'you are using a mutator, make sure your domToMutation method is ' +
48
+ 'properly defined.';
49
+
50
+
39
51
  /**
40
52
  * Class that controls updates to connections during drags. It is primarily
41
53
  * responsible for finding the closest eligible connection and highlighting or
42
54
  * unhighlighting it as needed during a drag.
43
- * @param {!BlockSvg} block The top block in the stack being dragged.
44
- * @constructor
45
55
  * @alias Blockly.InsertionMarkerManager
46
56
  */
47
- const InsertionMarkerManager = function(block) {
48
- common.setSelected(block);
57
+ class InsertionMarkerManager {
58
+ /**
59
+ * @param {!BlockSvg} block The top block in the stack being dragged.
60
+ */
61
+ constructor(block) {
62
+ common.setSelected(block);
63
+
64
+ /**
65
+ * The top block in the stack being dragged.
66
+ * Does not change during a drag.
67
+ * @type {!BlockSvg}
68
+ * @private
69
+ */
70
+ this.topBlock_ = block;
71
+
72
+ /**
73
+ * The workspace on which these connections are being dragged.
74
+ * Does not change during a drag.
75
+ * @type {!WorkspaceSvg}
76
+ * @private
77
+ */
78
+ this.workspace_ = block.workspace;
79
+
80
+ /**
81
+ * The last connection on the stack, if it's not the last connection on the
82
+ * first block.
83
+ * Set in initAvailableConnections, if at all.
84
+ * @type {RenderedConnection}
85
+ * @private
86
+ */
87
+ this.lastOnStack_ = null;
88
+
89
+ /**
90
+ * The insertion marker corresponding to the last block in the stack, if
91
+ * that's not the same as the first block in the stack.
92
+ * Set in initAvailableConnections, if at all
93
+ * @type {BlockSvg}
94
+ * @private
95
+ */
96
+ this.lastMarker_ = null;
97
+
98
+ /**
99
+ * The insertion marker that shows up between blocks to show where a block
100
+ * would go if dropped immediately.
101
+ * @type {BlockSvg}
102
+ * @private
103
+ */
104
+ this.firstMarker_ = this.createMarkerBlock_(this.topBlock_);
105
+
106
+ /**
107
+ * The connection that this block would connect to if released immediately.
108
+ * Updated on every mouse move.
109
+ * This is not on any of the blocks that are being dragged.
110
+ * @type {RenderedConnection}
111
+ * @private
112
+ */
113
+ this.closestConnection_ = null;
114
+
115
+ /**
116
+ * The connection that would connect to this.closestConnection_ if this
117
+ * block were released immediately. Updated on every mouse move. This is on
118
+ * the top block that is being dragged or the last block in the dragging
119
+ * stack.
120
+ * @type {RenderedConnection}
121
+ * @private
122
+ */
123
+ this.localConnection_ = null;
124
+
125
+ /**
126
+ * Whether the block would be deleted if it were dropped immediately.
127
+ * Updated on every mouse move.
128
+ * @type {boolean}
129
+ * @private
130
+ */
131
+ this.wouldDeleteBlock_ = false;
132
+
133
+ /**
134
+ * Connection on the insertion marker block that corresponds to
135
+ * this.localConnection_ on the currently dragged block.
136
+ * @type {RenderedConnection}
137
+ * @private
138
+ */
139
+ this.markerConnection_ = null;
140
+
141
+ /**
142
+ * The block that currently has an input being highlighted, or null.
143
+ * @type {BlockSvg}
144
+ * @private
145
+ */
146
+ this.highlightedBlock_ = null;
147
+
148
+ /**
149
+ * The block being faded to indicate replacement, or null.
150
+ * @type {BlockSvg}
151
+ * @private
152
+ */
153
+ this.fadedBlock_ = null;
154
+
155
+ /**
156
+ * The connections on the dragging blocks that are available to connect to
157
+ * other blocks. This includes all open connections on the top block, as
158
+ * well as the last connection on the block stack. Does not change during a
159
+ * drag.
160
+ * @type {!Array<!RenderedConnection>}
161
+ * @private
162
+ */
163
+ this.availableConnections_ = this.initAvailableConnections_();
164
+ }
49
165
 
50
166
  /**
51
- * The top block in the stack being dragged.
52
- * Does not change during a drag.
53
- * @type {!BlockSvg}
54
- * @private
167
+ * Sever all links from this object.
168
+ * @package
55
169
  */
56
- this.topBlock_ = block;
170
+ dispose() {
171
+ this.availableConnections_.length = 0;
172
+
173
+ eventUtils.disable();
174
+ try {
175
+ if (this.firstMarker_) {
176
+ this.firstMarker_.dispose();
177
+ }
178
+ if (this.lastMarker_) {
179
+ this.lastMarker_.dispose();
180
+ }
181
+ } finally {
182
+ eventUtils.enable();
183
+ }
184
+ }
57
185
 
58
186
  /**
59
- * The workspace on which these connections are being dragged.
60
- * Does not change during a drag.
61
- * @type {!WorkspaceSvg}
62
- * @private
187
+ * Update the available connections for the top block. These connections can
188
+ * change if a block is unplugged and the stack is healed.
189
+ * @package
63
190
  */
64
- this.workspace_ = block.workspace;
191
+ updateAvailableConnections() {
192
+ this.availableConnections_ = this.initAvailableConnections_();
193
+ }
65
194
 
66
195
  /**
67
- * The last connection on the stack, if it's not the last connection on the
68
- * first block.
69
- * Set in initAvailableConnections, if at all.
70
- * @type {RenderedConnection}
71
- * @private
196
+ * Return whether the block would be deleted if dropped immediately, based on
197
+ * information from the most recent move event.
198
+ * @return {boolean} True if the block would be deleted if dropped
199
+ * immediately.
200
+ * @package
72
201
  */
73
- this.lastOnStack_ = null;
202
+ wouldDeleteBlock() {
203
+ return this.wouldDeleteBlock_;
204
+ }
74
205
 
75
206
  /**
76
- * The insertion marker corresponding to the last block in the stack, if
77
- * that's not the same as the first block in the stack.
78
- * Set in initAvailableConnections, if at all
79
- * @type {BlockSvg}
80
- * @private
207
+ * Return whether the block would be connected if dropped immediately, based
208
+ * on information from the most recent move event.
209
+ * @return {boolean} True if the block would be connected if dropped
210
+ * immediately.
211
+ * @package
81
212
  */
82
- this.lastMarker_ = null;
213
+ wouldConnectBlock() {
214
+ return !!this.closestConnection_;
215
+ }
83
216
 
84
217
  /**
85
- * The insertion marker that shows up between blocks to show where a block
86
- * would go if dropped immediately.
87
- * @type {BlockSvg}
88
- * @private
218
+ * Connect to the closest connection and render the results.
219
+ * This should be called at the end of a drag.
220
+ * @package
89
221
  */
90
- this.firstMarker_ = this.createMarkerBlock_(this.topBlock_);
222
+ applyConnections() {
223
+ if (this.closestConnection_) {
224
+ // Don't fire events for insertion markers.
225
+ eventUtils.disable();
226
+ this.hidePreview_();
227
+ eventUtils.enable();
228
+ // Connect two blocks together.
229
+ this.localConnection_.connect(this.closestConnection_);
230
+ if (this.topBlock_.rendered) {
231
+ // Trigger a connection animation.
232
+ // Determine which connection is inferior (lower in the source stack).
233
+ const inferiorConnection = this.localConnection_.isSuperior() ?
234
+ this.closestConnection_ :
235
+ this.localConnection_;
236
+ blockAnimations.connectionUiEffect(inferiorConnection.getSourceBlock());
237
+ // Bring the just-edited stack to the front.
238
+ const rootBlock = this.topBlock_.getRootBlock();
239
+ rootBlock.bringToFront();
240
+ }
241
+ }
242
+ }
91
243
 
92
244
  /**
93
- * The connection that this block would connect to if released immediately.
94
- * Updated on every mouse move.
95
- * This is not on any of the blocks that are being dragged.
96
- * @type {RenderedConnection}
97
- * @private
245
+ * Update connections based on the most recent move location.
246
+ * @param {!Coordinate} dxy Position relative to drag start,
247
+ * in workspace units.
248
+ * @param {?IDragTarget} dragTarget The drag target that the block is
249
+ * currently over.
250
+ * @package
98
251
  */
99
- this.closestConnection_ = null;
252
+ update(dxy, dragTarget) {
253
+ const candidate = this.getCandidate_(dxy);
254
+
255
+ this.wouldDeleteBlock_ = this.shouldDelete_(candidate, dragTarget);
256
+
257
+ const shouldUpdate =
258
+ this.wouldDeleteBlock_ || this.shouldUpdatePreviews_(candidate, dxy);
259
+
260
+ if (shouldUpdate) {
261
+ // Don't fire events for insertion marker creation or movement.
262
+ eventUtils.disable();
263
+ this.maybeHidePreview_(candidate);
264
+ this.maybeShowPreview_(candidate);
265
+ eventUtils.enable();
266
+ }
267
+ }
100
268
 
101
269
  /**
102
- * The connection that would connect to this.closestConnection_ if this block
103
- * were released immediately.
104
- * Updated on every mouse move.
105
- * This is on the top block that is being dragged or the last block in the
106
- * dragging stack.
107
- * @type {RenderedConnection}
270
+ * Create an insertion marker that represents the given block.
271
+ * @param {!BlockSvg} sourceBlock The block that the insertion marker
272
+ * will represent.
273
+ * @return {!BlockSvg} The insertion marker that represents the given
274
+ * block.
108
275
  * @private
109
276
  */
110
- this.localConnection_ = null;
277
+ createMarkerBlock_(sourceBlock) {
278
+ const imType = sourceBlock.type;
279
+
280
+ eventUtils.disable();
281
+ let result;
282
+ try {
283
+ result = this.workspace_.newBlock(imType);
284
+ result.setInsertionMarker(true);
285
+ if (sourceBlock.saveExtraState) {
286
+ const state = sourceBlock.saveExtraState();
287
+ if (state) {
288
+ result.loadExtraState(state);
289
+ }
290
+ } else if (sourceBlock.mutationToDom) {
291
+ const oldMutationDom = sourceBlock.mutationToDom();
292
+ if (oldMutationDom) {
293
+ result.domToMutation(oldMutationDom);
294
+ }
295
+ }
296
+ // Copy field values from the other block. These values may impact the
297
+ // rendered size of the insertion marker. Note that we do not care about
298
+ // child blocks here.
299
+ for (let i = 0; i < sourceBlock.inputList.length; i++) {
300
+ const sourceInput = sourceBlock.inputList[i];
301
+ if (sourceInput.name === constants.COLLAPSED_INPUT_NAME) {
302
+ continue; // Ignore the collapsed input.
303
+ }
304
+ const resultInput = result.inputList[i];
305
+ if (!resultInput) {
306
+ throw new Error(DUPLICATE_BLOCK_ERROR.replace('%1', 'an input'));
307
+ }
308
+ for (let j = 0; j < sourceInput.fieldRow.length; j++) {
309
+ const sourceField = sourceInput.fieldRow[j];
310
+ const resultField = resultInput.fieldRow[j];
311
+ if (!resultField) {
312
+ throw new Error(DUPLICATE_BLOCK_ERROR.replace('%1', 'a field'));
313
+ }
314
+ resultField.setValue(sourceField.getValue());
315
+ }
316
+ }
317
+
318
+ result.setCollapsed(sourceBlock.isCollapsed());
319
+ result.setInputsInline(sourceBlock.getInputsInline());
320
+
321
+ result.initSvg();
322
+ result.getSvgRoot().setAttribute('visibility', 'hidden');
323
+ } finally {
324
+ eventUtils.enable();
325
+ }
326
+
327
+ return result;
328
+ }
111
329
 
112
330
  /**
113
- * Whether the block would be deleted if it were dropped immediately.
114
- * Updated on every mouse move.
115
- * @type {boolean}
331
+ * Populate the list of available connections on this block stack. This
332
+ * should only be called once, at the beginning of a drag. If the stack has
333
+ * more than one block, this function will populate lastOnStack_ and create
334
+ * the corresponding insertion marker.
335
+ * @return {!Array<!RenderedConnection>} A list of available
336
+ * connections.
116
337
  * @private
117
338
  */
118
- this.wouldDeleteBlock_ = false;
339
+ initAvailableConnections_() {
340
+ const available = this.topBlock_.getConnections_(false);
341
+ // Also check the last connection on this stack
342
+ const lastOnStack = this.topBlock_.lastConnectionInStack(true);
343
+ if (lastOnStack && lastOnStack !== this.topBlock_.nextConnection) {
344
+ available.push(lastOnStack);
345
+ this.lastOnStack_ = lastOnStack;
346
+ if (this.lastMarker_) {
347
+ eventUtils.disable();
348
+ try {
349
+ this.lastMarker_.dispose();
350
+ } finally {
351
+ eventUtils.enable();
352
+ }
353
+ }
354
+ this.lastMarker_ = this.createMarkerBlock_(lastOnStack.getSourceBlock());
355
+ }
356
+ return available;
357
+ }
119
358
 
120
359
  /**
121
- * Connection on the insertion marker block that corresponds to
122
- * this.localConnection_ on the currently dragged block.
123
- * @type {RenderedConnection}
360
+ * Whether the previews (insertion marker and replacement marker) should be
361
+ * updated based on the closest candidate and the current drag distance.
362
+ * @param {!Object} candidate An object containing a local connection, a
363
+ * closest connection, and a radius. Returned by getCandidate_.
364
+ * @param {!Coordinate} dxy Position relative to drag start,
365
+ * in workspace units.
366
+ * @return {boolean} Whether the preview should be updated.
124
367
  * @private
125
368
  */
126
- this.markerConnection_ = null;
369
+ shouldUpdatePreviews_(candidate, dxy) {
370
+ const candidateLocal = candidate.local;
371
+ const candidateClosest = candidate.closest;
372
+ const radius = candidate.radius;
373
+
374
+ // Found a connection!
375
+ if (candidateLocal && candidateClosest) {
376
+ // We're already showing an insertion marker.
377
+ // Decide whether the new connection has higher priority.
378
+ if (this.localConnection_ && this.closestConnection_) {
379
+ // The connection was the same as the current connection.
380
+ if (this.closestConnection_ === candidateClosest &&
381
+ this.localConnection_ === candidateLocal) {
382
+ return false;
383
+ }
384
+ const xDiff =
385
+ this.localConnection_.x + dxy.x - this.closestConnection_.x;
386
+ const yDiff =
387
+ this.localConnection_.y + dxy.y - this.closestConnection_.y;
388
+ const curDistance = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
389
+ // Slightly prefer the existing preview over a new preview.
390
+ return !(
391
+ candidateClosest &&
392
+ radius > curDistance - config.currentConnectionPreference);
393
+ } else if (!this.localConnection_ && !this.closestConnection_) {
394
+ // We weren't showing a preview before, but we should now.
395
+ return true;
396
+ } else {
397
+ console.error(
398
+ 'Only one of localConnection_ and closestConnection_ was set.');
399
+ }
400
+ } else { // No connection found.
401
+ // Only need to update if we were showing a preview before.
402
+ return !!(this.localConnection_ && this.closestConnection_);
403
+ }
404
+
405
+ console.error(
406
+ 'Returning true from shouldUpdatePreviews, but it\'s not clear why.');
407
+ return true;
408
+ }
127
409
 
128
410
  /**
129
- * The block that currently has an input being highlighted, or null.
130
- * @type {BlockSvg}
411
+ * Find the nearest valid connection, which may be the same as the current
412
+ * closest connection.
413
+ * @param {!Coordinate} dxy Position relative to drag start,
414
+ * in workspace units.
415
+ * @return {!Object} An object containing a local connection, a closest
416
+ * connection, and a radius.
131
417
  * @private
132
418
  */
133
- this.highlightedBlock_ = null;
419
+ getCandidate_(dxy) {
420
+ let radius = this.getStartRadius_();
421
+ let candidateClosest = null;
422
+ let candidateLocal = null;
423
+
424
+ // It's possible that a block has added or removed connections during a
425
+ // drag, (e.g. in a drag/move event handler), so let's update the available
426
+ // connections. Note that this will be called on every move while dragging,
427
+ // so it might cause slowness, especially if the block stack is large. If
428
+ // so, maybe it could be made more efficient. Also note that we won't update
429
+ // the connections if we've already connected the insertion marker to a
430
+ // block.
431
+ if (!this.markerConnection_ || !this.markerConnection_.isConnected()) {
432
+ this.updateAvailableConnections();
433
+ }
434
+
435
+ for (let i = 0; i < this.availableConnections_.length; i++) {
436
+ const myConnection = this.availableConnections_[i];
437
+ const neighbour = myConnection.closest(radius, dxy);
438
+ if (neighbour.connection) {
439
+ candidateClosest = neighbour.connection;
440
+ candidateLocal = myConnection;
441
+ radius = neighbour.radius;
442
+ }
443
+ }
444
+ return {closest: candidateClosest, local: candidateLocal, radius: radius};
445
+ }
134
446
 
135
447
  /**
136
- * The block being faded to indicate replacement, or null.
137
- * @type {BlockSvg}
448
+ * Decide the radius at which to start searching for the closest connection.
449
+ * @return {number} The radius at which to start the search for the closest
450
+ * connection.
138
451
  * @private
139
452
  */
140
- this.fadedBlock_ = null;
453
+ getStartRadius_() {
454
+ // If there is already a connection highlighted,
455
+ // increase the radius we check for making new connections.
456
+ // Why? When a connection is highlighted, blocks move around when the
457
+ // insertion marker is created, which could cause the connection became out
458
+ // of range. By increasing radiusConnection when a connection already
459
+ // exists, we never "lose" the connection from the offset.
460
+ if (this.closestConnection_ && this.localConnection_) {
461
+ return config.connectingSnapRadius;
462
+ }
463
+ return config.snapRadius;
464
+ }
141
465
 
142
466
  /**
143
- * The connections on the dragging blocks that are available to connect to
144
- * other blocks. This includes all open connections on the top block, as well
145
- * as the last connection on the block stack.
146
- * Does not change during a drag.
147
- * @type {!Array<!RenderedConnection>}
467
+ * Whether ending the drag would delete the block.
468
+ * @param {!Object} candidate An object containing a local connection, a
469
+ * closest
470
+ * connection, and a radius.
471
+ * @param {?IDragTarget} dragTarget The drag target that the block is
472
+ * currently over.
473
+ * @return {boolean} Whether dropping the block immediately would delete the
474
+ * block.
148
475
  * @private
149
476
  */
150
- this.availableConnections_ = this.initAvailableConnections_();
151
- };
152
-
153
- /**
154
- * An enum describing different kinds of previews the InsertionMarkerManager
155
- * could display.
156
- * @enum {number}
157
- */
158
- InsertionMarkerManager.PREVIEW_TYPE = {
159
- INSERTION_MARKER: 0,
160
- INPUT_OUTLINE: 1,
161
- REPLACEMENT_FADE: 2,
162
- };
163
-
164
- /**
165
- * An error message to throw if the block created by createMarkerBlock_ is
166
- * missing any components.
167
- * @type {string}
168
- * @const
169
- */
170
- InsertionMarkerManager.DUPLICATE_BLOCK_ERROR = 'The insertion marker ' +
171
- 'manager tried to create a marker but the result is missing %1. If ' +
172
- 'you are using a mutator, make sure your domToMutation method is ' +
173
- 'properly defined.';
174
-
175
- /**
176
- * Sever all links from this object.
177
- * @package
178
- */
179
- InsertionMarkerManager.prototype.dispose = function() {
180
- this.availableConnections_.length = 0;
181
-
182
- eventUtils.disable();
183
- try {
184
- if (this.firstMarker_) {
185
- this.firstMarker_.dispose();
186
- }
187
- if (this.lastMarker_) {
188
- this.lastMarker_.dispose();
477
+ shouldDelete_(candidate, dragTarget) {
478
+ if (dragTarget) {
479
+ const componentManager = this.workspace_.getComponentManager();
480
+ const isDeleteArea = componentManager.hasCapability(
481
+ dragTarget.id, ComponentManager.Capability.DELETE_AREA);
482
+ if (isDeleteArea) {
483
+ return (
484
+ /** @type {!IDeleteArea} */ (dragTarget))
485
+ .wouldDelete(this.topBlock_, candidate && !!candidate.closest);
486
+ }
189
487
  }
190
- } finally {
191
- eventUtils.enable();
488
+ return false;
192
489
  }
193
- };
194
490
 
195
- /**
196
- * Update the available connections for the top block. These connections can
197
- * change if a block is unplugged and the stack is healed.
198
- * @package
199
- */
200
- InsertionMarkerManager.prototype.updateAvailableConnections = function() {
201
- this.availableConnections_ = this.initAvailableConnections_();
202
- };
203
-
204
- /**
205
- * Return whether the block would be deleted if dropped immediately, based on
206
- * information from the most recent move event.
207
- * @return {boolean} True if the block would be deleted if dropped immediately.
208
- * @package
209
- */
210
- InsertionMarkerManager.prototype.wouldDeleteBlock = function() {
211
- return this.wouldDeleteBlock_;
212
- };
491
+ /**
492
+ * Show an insertion marker or replacement highlighting during a drag, if
493
+ * needed.
494
+ * At the beginning of this function, this.localConnection_ and
495
+ * this.closestConnection_ should both be null.
496
+ * @param {!Object} candidate An object containing a local connection, a
497
+ * closest connection, and a radius.
498
+ * @private
499
+ */
500
+ maybeShowPreview_(candidate) {
501
+ // Nope, don't add a marker.
502
+ if (this.wouldDeleteBlock_) {
503
+ return;
504
+ }
505
+ const closest = candidate.closest;
506
+ const local = candidate.local;
213
507
 
214
- /**
215
- * Return whether the block would be connected if dropped immediately, based on
216
- * information from the most recent move event.
217
- * @return {boolean} True if the block would be connected if dropped
218
- * immediately.
219
- * @package
220
- */
221
- InsertionMarkerManager.prototype.wouldConnectBlock = function() {
222
- return !!this.closestConnection_;
223
- };
508
+ // Nothing to connect to.
509
+ if (!closest) {
510
+ return;
511
+ }
224
512
 
225
- /**
226
- * Connect to the closest connection and render the results.
227
- * This should be called at the end of a drag.
228
- * @package
229
- */
230
- InsertionMarkerManager.prototype.applyConnections = function() {
231
- if (this.closestConnection_) {
232
- // Don't fire events for insertion markers.
233
- eventUtils.disable();
234
- this.hidePreview_();
235
- eventUtils.enable();
236
- // Connect two blocks together.
237
- this.localConnection_.connect(this.closestConnection_);
238
- if (this.topBlock_.rendered) {
239
- // Trigger a connection animation.
240
- // Determine which connection is inferior (lower in the source stack).
241
- const inferiorConnection = this.localConnection_.isSuperior() ?
242
- this.closestConnection_ :
243
- this.localConnection_;
244
- blockAnimations.connectionUiEffect(inferiorConnection.getSourceBlock());
245
- // Bring the just-edited stack to the front.
246
- const rootBlock = this.topBlock_.getRootBlock();
247
- rootBlock.bringToFront();
513
+ // Something went wrong and we're trying to connect to an invalid
514
+ // connection.
515
+ if (closest === this.closestConnection_ ||
516
+ closest.getSourceBlock().isInsertionMarker()) {
517
+ console.log('Trying to connect to an insertion marker');
518
+ return;
248
519
  }
520
+ // Add an insertion marker or replacement marker.
521
+ this.closestConnection_ = closest;
522
+ this.localConnection_ = local;
523
+ this.showPreview_();
249
524
  }
250
- };
251
-
252
- /**
253
- * Update connections based on the most recent move location.
254
- * @param {!Coordinate} dxy Position relative to drag start,
255
- * in workspace units.
256
- * @param {?IDragTarget} dragTarget The drag target that the block is
257
- * currently over.
258
- * @package
259
- */
260
- InsertionMarkerManager.prototype.update = function(dxy, dragTarget) {
261
- const candidate = this.getCandidate_(dxy);
262
-
263
- this.wouldDeleteBlock_ = this.shouldDelete_(candidate, dragTarget);
264
525
 
265
- const shouldUpdate =
266
- this.wouldDeleteBlock_ || this.shouldUpdatePreviews_(candidate, dxy);
526
+ /**
527
+ * A preview should be shown. This function figures out if it should be a
528
+ * block highlight or an insertion marker, and shows the appropriate one.
529
+ * @private
530
+ */
531
+ showPreview_() {
532
+ const closest = this.closestConnection_;
533
+ const renderer = this.workspace_.getRenderer();
534
+ const method = renderer.getConnectionPreviewMethod(
535
+ /** @type {!RenderedConnection} */ (closest),
536
+ /** @type {!RenderedConnection} */ (this.localConnection_),
537
+ this.topBlock_);
538
+
539
+ switch (method) {
540
+ case InsertionMarkerManager.PREVIEW_TYPE.INPUT_OUTLINE:
541
+ this.showInsertionInputOutline_();
542
+ break;
543
+ case InsertionMarkerManager.PREVIEW_TYPE.INSERTION_MARKER:
544
+ this.showInsertionMarker_();
545
+ break;
546
+ case InsertionMarkerManager.PREVIEW_TYPE.REPLACEMENT_FADE:
547
+ this.showReplacementFade_();
548
+ break;
549
+ }
267
550
 
268
- if (shouldUpdate) {
269
- // Don't fire events for insertion marker creation or movement.
270
- eventUtils.disable();
271
- this.maybeHidePreview_(candidate);
272
- this.maybeShowPreview_(candidate);
273
- eventUtils.enable();
551
+ // Optionally highlight the actual connection, as a nod to previous
552
+ // behaviour.
553
+ if (closest && renderer.shouldHighlightConnection(closest)) {
554
+ closest.highlight();
555
+ }
274
556
  }
275
- };
276
557
 
277
- /**
278
- * Create an insertion marker that represents the given block.
279
- * @param {!BlockSvg} sourceBlock The block that the insertion marker
280
- * will represent.
281
- * @return {!BlockSvg} The insertion marker that represents the given
282
- * block.
283
- * @private
284
- */
285
- InsertionMarkerManager.prototype.createMarkerBlock_ = function(sourceBlock) {
286
- const imType = sourceBlock.type;
287
-
288
- eventUtils.disable();
289
- let result;
290
- try {
291
- result = this.workspace_.newBlock(imType);
292
- result.setInsertionMarker(true);
293
- if (sourceBlock.saveExtraState) {
294
- const state = sourceBlock.saveExtraState();
295
- if (state) {
296
- result.loadExtraState(state);
297
- }
298
- } else if (sourceBlock.mutationToDom) {
299
- const oldMutationDom = sourceBlock.mutationToDom();
300
- if (oldMutationDom) {
301
- result.domToMutation(oldMutationDom);
302
- }
303
- }
304
- // Copy field values from the other block. These values may impact the
305
- // rendered size of the insertion marker. Note that we do not care about
306
- // child blocks here.
307
- for (let i = 0; i < sourceBlock.inputList.length; i++) {
308
- const sourceInput = sourceBlock.inputList[i];
309
- if (sourceInput.name === constants.COLLAPSED_INPUT_NAME) {
310
- continue; // Ignore the collapsed input.
311
- }
312
- const resultInput = result.inputList[i];
313
- if (!resultInput) {
314
- throw new Error(InsertionMarkerManager.DUPLICATE_BLOCK_ERROR.replace(
315
- '%1', 'an input'));
316
- }
317
- for (let j = 0; j < sourceInput.fieldRow.length; j++) {
318
- const sourceField = sourceInput.fieldRow[j];
319
- const resultField = resultInput.fieldRow[j];
320
- if (!resultField) {
321
- throw new Error(InsertionMarkerManager.DUPLICATE_BLOCK_ERROR.replace(
322
- '%1', 'a field'));
323
- }
324
- resultField.setValue(sourceField.getValue());
558
+ /**
559
+ * Show an insertion marker or replacement highlighting during a drag, if
560
+ * needed.
561
+ * At the end of this function, this.localConnection_ and
562
+ * this.closestConnection_ should both be null.
563
+ * @param {!Object} candidate An object containing a local connection, a
564
+ * closest connection, and a radius.
565
+ * @private
566
+ */
567
+ maybeHidePreview_(candidate) {
568
+ // If there's no new preview, remove the old one but don't bother deleting
569
+ // it. We might need it later, and this saves disposing of it and recreating
570
+ // it.
571
+ if (!candidate.closest) {
572
+ this.hidePreview_();
573
+ } else {
574
+ // If there's a new preview and there was an preview before, and either
575
+ // connection has changed, remove the old preview.
576
+ const hadPreview = this.closestConnection_ && this.localConnection_;
577
+ const closestChanged = this.closestConnection_ !== candidate.closest;
578
+ const localChanged = this.localConnection_ !== candidate.local;
579
+
580
+ // Also hide if we had a preview before but now we're going to delete
581
+ // instead.
582
+ if (hadPreview &&
583
+ (closestChanged || localChanged || this.wouldDeleteBlock_)) {
584
+ this.hidePreview_();
325
585
  }
326
586
  }
327
587
 
328
- result.setCollapsed(sourceBlock.isCollapsed());
329
- result.setInputsInline(sourceBlock.getInputsInline());
330
-
331
- result.initSvg();
332
- result.getSvgRoot().setAttribute('visibility', 'hidden');
333
- } finally {
334
- eventUtils.enable();
588
+ // Either way, clear out old state.
589
+ this.markerConnection_ = null;
590
+ this.closestConnection_ = null;
591
+ this.localConnection_ = null;
335
592
  }
336
593
 
337
- return result;
338
- };
339
-
340
- /**
341
- * Populate the list of available connections on this block stack. This should
342
- * only be called once, at the beginning of a drag.
343
- * If the stack has more than one block, this function will populate
344
- * lastOnStack_ and create the corresponding insertion marker.
345
- * @return {!Array<!RenderedConnection>} A list of available
346
- * connections.
347
- * @private
348
- */
349
- InsertionMarkerManager.prototype.initAvailableConnections_ = function() {
350
- const available = this.topBlock_.getConnections_(false);
351
- // Also check the last connection on this stack
352
- const lastOnStack = this.topBlock_.lastConnectionInStack(true);
353
- if (lastOnStack && lastOnStack !== this.topBlock_.nextConnection) {
354
- available.push(lastOnStack);
355
- this.lastOnStack_ = lastOnStack;
356
- if (this.lastMarker_) {
357
- eventUtils.disable();
358
- try {
359
- this.lastMarker_.dispose();
360
- } finally {
361
- eventUtils.enable();
362
- }
594
+ /**
595
+ * A preview should be hidden. This function figures out if it is a block
596
+ * highlight or an insertion marker, and hides the appropriate one.
597
+ * @private
598
+ */
599
+ hidePreview_() {
600
+ if (this.closestConnection_ && this.closestConnection_.targetBlock() &&
601
+ this.workspace_.getRenderer().shouldHighlightConnection(
602
+ this.closestConnection_)) {
603
+ this.closestConnection_.unhighlight();
363
604
  }
364
- this.lastMarker_ = this.createMarkerBlock_(lastOnStack.getSourceBlock());
365
- }
366
- return available;
367
- };
368
-
369
- /**
370
- * Whether the previews (insertion marker and replacement marker) should be
371
- * updated based on the closest candidate and the current drag distance.
372
- * @param {!Object} candidate An object containing a local connection, a closest
373
- * connection, and a radius. Returned by getCandidate_.
374
- * @param {!Coordinate} dxy Position relative to drag start,
375
- * in workspace units.
376
- * @return {boolean} Whether the preview should be updated.
377
- * @private
378
- */
379
- InsertionMarkerManager.prototype.shouldUpdatePreviews_ = function(
380
- candidate, dxy) {
381
- const candidateLocal = candidate.local;
382
- const candidateClosest = candidate.closest;
383
- const radius = candidate.radius;
384
-
385
- // Found a connection!
386
- if (candidateLocal && candidateClosest) {
387
- // We're already showing an insertion marker.
388
- // Decide whether the new connection has higher priority.
389
- if (this.localConnection_ && this.closestConnection_) {
390
- // The connection was the same as the current connection.
391
- if (this.closestConnection_ === candidateClosest &&
392
- this.localConnection_ === candidateLocal) {
393
- return false;
394
- }
395
- const xDiff = this.localConnection_.x + dxy.x - this.closestConnection_.x;
396
- const yDiff = this.localConnection_.y + dxy.y - this.closestConnection_.y;
397
- const curDistance = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
398
- // Slightly prefer the existing preview over a new preview.
399
- return !(
400
- candidateClosest &&
401
- radius >
402
- curDistance - internalConstants.CURRENT_CONNECTION_PREFERENCE);
403
- } else if (!this.localConnection_ && !this.closestConnection_) {
404
- // We weren't showing a preview before, but we should now.
405
- return true;
406
- } else {
407
- console.error(
408
- 'Only one of localConnection_ and closestConnection_ was set.');
605
+ if (this.fadedBlock_) {
606
+ this.hideReplacementFade_();
607
+ } else if (this.highlightedBlock_) {
608
+ this.hideInsertionInputOutline_();
609
+ } else if (this.markerConnection_) {
610
+ this.hideInsertionMarker_();
409
611
  }
410
- } else { // No connection found.
411
- // Only need to update if we were showing a preview before.
412
- return !!(this.localConnection_ && this.closestConnection_);
413
612
  }
414
613
 
415
- console.error(
416
- 'Returning true from shouldUpdatePreviews, but it\'s not clear why.');
417
- return true;
418
- };
614
+ /**
615
+ * Shows an insertion marker connected to the appropriate blocks (based on
616
+ * manager state).
617
+ * @private
618
+ */
619
+ showInsertionMarker_() {
620
+ const local = this.localConnection_;
621
+ const closest = this.closestConnection_;
622
+
623
+ const isLastInStack = this.lastOnStack_ && local === this.lastOnStack_;
624
+ let imBlock = isLastInStack ? this.lastMarker_ : this.firstMarker_;
625
+ let imConn;
626
+ try {
627
+ imConn = imBlock.getMatchingConnection(local.getSourceBlock(), local);
628
+ } catch (e) {
629
+ // It's possible that the number of connections on the local block has
630
+ // changed since the insertion marker was originally created. Let's
631
+ // recreate the insertion marker and try again. In theory we could
632
+ // probably recreate the marker block (e.g. in getCandidate_), which is
633
+ // called more often during the drag, but creating a block that often
634
+ // might be too slow, so we only do it if necessary.
635
+ this.firstMarker_ = this.createMarkerBlock_(this.topBlock_);
636
+ imBlock = isLastInStack ? this.lastMarker_ : this.firstMarker_;
637
+ imConn = imBlock.getMatchingConnection(local.getSourceBlock(), local);
638
+ }
419
639
 
420
- /**
421
- * Find the nearest valid connection, which may be the same as the current
422
- * closest connection.
423
- * @param {!Coordinate} dxy Position relative to drag start,
424
- * in workspace units.
425
- * @return {!Object} An object containing a local connection, a closest
426
- * connection, and a radius.
427
- * @private
428
- */
429
- InsertionMarkerManager.prototype.getCandidate_ = function(dxy) {
430
- let radius = this.getStartRadius_();
431
- let candidateClosest = null;
432
- let candidateLocal = null;
433
-
434
- for (let i = 0; i < this.availableConnections_.length; i++) {
435
- const myConnection = this.availableConnections_[i];
436
- const neighbour = myConnection.closest(radius, dxy);
437
- if (neighbour.connection) {
438
- candidateClosest = neighbour.connection;
439
- candidateLocal = myConnection;
440
- radius = neighbour.radius;
640
+ if (imConn === this.markerConnection_) {
641
+ throw Error(
642
+ 'Made it to showInsertionMarker_ even though the marker isn\'t ' +
643
+ 'changing');
441
644
  }
442
- }
443
- return {closest: candidateClosest, local: candidateLocal, radius: radius};
444
- };
445
645
 
446
- /**
447
- * Decide the radius at which to start searching for the closest connection.
448
- * @return {number} The radius at which to start the search for the closest
449
- * connection.
450
- * @private
451
- */
452
- InsertionMarkerManager.prototype.getStartRadius_ = function() {
453
- // If there is already a connection highlighted,
454
- // increase the radius we check for making new connections.
455
- // Why? When a connection is highlighted, blocks move around when the
456
- // insertion marker is created, which could cause the connection became out of
457
- // range. By increasing radiusConnection when a connection already exists, we
458
- // never "lose" the connection from the offset.
459
- if (this.closestConnection_ && this.localConnection_) {
460
- return internalConstants.CONNECTING_SNAP_RADIUS;
461
- }
462
- return internalConstants.SNAP_RADIUS;
463
- };
646
+ // Render disconnected from everything else so that we have a valid
647
+ // connection location.
648
+ imBlock.render();
649
+ imBlock.rendered = true;
650
+ imBlock.getSvgRoot().setAttribute('visibility', 'visible');
464
651
 
465
- /**
466
- * Whether ending the drag would delete the block.
467
- * @param {!Object} candidate An object containing a local connection, a closest
468
- * connection, and a radius.
469
- * @param {?IDragTarget} dragTarget The drag target that the block is
470
- * currently over.
471
- * @return {boolean} Whether dropping the block immediately would delete the
472
- * block.
473
- * @private
474
- */
475
- InsertionMarkerManager.prototype.shouldDelete_ = function(
476
- candidate, dragTarget) {
477
- if (dragTarget) {
478
- const componentManager = this.workspace_.getComponentManager();
479
- const isDeleteArea = componentManager.hasCapability(
480
- dragTarget.id, ComponentManager.Capability.DELETE_AREA);
481
- if (isDeleteArea) {
482
- return (
483
- /** @type {!IDeleteArea} */ (dragTarget))
484
- .wouldDelete(this.topBlock_, candidate && !!candidate.closest);
652
+ if (imConn && closest) {
653
+ // Position so that the existing block doesn't move.
654
+ imBlock.positionNearConnection(imConn, closest);
655
+ }
656
+ if (closest) {
657
+ // Connect() also renders the insertion marker.
658
+ imConn.connect(closest);
485
659
  }
486
- }
487
- return false;
488
- };
489
-
490
- /**
491
- * Show an insertion marker or replacement highlighting during a drag, if
492
- * needed.
493
- * At the beginning of this function, this.localConnection_ and
494
- * this.closestConnection_ should both be null.
495
- * @param {!Object} candidate An object containing a local connection, a closest
496
- * connection, and a radius.
497
- * @private
498
- */
499
- InsertionMarkerManager.prototype.maybeShowPreview_ = function(candidate) {
500
- // Nope, don't add a marker.
501
- if (this.wouldDeleteBlock_) {
502
- return;
503
- }
504
- const closest = candidate.closest;
505
- const local = candidate.local;
506
660
 
507
- // Nothing to connect to.
508
- if (!closest) {
509
- return;
661
+ this.markerConnection_ = imConn;
510
662
  }
511
663
 
512
- // Something went wrong and we're trying to connect to an invalid connection.
513
- if (closest === this.closestConnection_ ||
514
- closest.getSourceBlock().isInsertionMarker()) {
515
- console.log('Trying to connect to an insertion marker');
516
- return;
517
- }
518
- // Add an insertion marker or replacement marker.
519
- this.closestConnection_ = closest;
520
- this.localConnection_ = local;
521
- this.showPreview_();
522
- };
664
+ /**
665
+ * Disconnects and hides the current insertion marker. Should return the
666
+ * blocks to their original state.
667
+ * @private
668
+ */
669
+ hideInsertionMarker_() {
670
+ if (!this.markerConnection_) {
671
+ console.log('No insertion marker connection to disconnect');
672
+ return;
673
+ }
523
674
 
524
- /**
525
- * A preview should be shown. This function figures out if it should be a block
526
- * highlight or an insertion marker, and shows the appropriate one.
527
- * @private
528
- */
529
- InsertionMarkerManager.prototype.showPreview_ = function() {
530
- const closest = this.closestConnection_;
531
- const renderer = this.workspace_.getRenderer();
532
- const method = renderer.getConnectionPreviewMethod(
533
- /** @type {!RenderedConnection} */ (closest),
534
- /** @type {!RenderedConnection} */ (this.localConnection_),
535
- this.topBlock_);
536
-
537
- switch (method) {
538
- case InsertionMarkerManager.PREVIEW_TYPE.INPUT_OUTLINE:
539
- this.showInsertionInputOutline_();
540
- break;
541
- case InsertionMarkerManager.PREVIEW_TYPE.INSERTION_MARKER:
542
- this.showInsertionMarker_();
543
- break;
544
- case InsertionMarkerManager.PREVIEW_TYPE.REPLACEMENT_FADE:
545
- this.showReplacementFade_();
546
- break;
547
- }
675
+ const imConn = this.markerConnection_;
676
+ const imBlock = imConn.getSourceBlock();
677
+ const markerNext = imBlock.nextConnection;
678
+ const markerPrev = imBlock.previousConnection;
679
+ const markerOutput = imBlock.outputConnection;
680
+
681
+ const isFirstInStatementStack =
682
+ (imConn === markerNext && !(markerPrev && markerPrev.targetConnection));
683
+
684
+ const isFirstInOutputStack = imConn.type === ConnectionType.INPUT_VALUE &&
685
+ !(markerOutput && markerOutput.targetConnection);
686
+ // The insertion marker is the first block in a stack. Unplug won't do
687
+ // anything in that case. Instead, unplug the following block.
688
+ if (isFirstInStatementStack || isFirstInOutputStack) {
689
+ imConn.targetBlock().unplug(false);
690
+ } else if (
691
+ imConn.type === ConnectionType.NEXT_STATEMENT &&
692
+ imConn !== markerNext) {
693
+ // Inside of a C-block, first statement connection.
694
+ const innerConnection = imConn.targetConnection;
695
+ innerConnection.getSourceBlock().unplug(false);
696
+
697
+ const previousBlockNextConnection =
698
+ markerPrev ? markerPrev.targetConnection : null;
699
+
700
+ imBlock.unplug(true);
701
+ if (previousBlockNextConnection) {
702
+ previousBlockNextConnection.connect(innerConnection);
703
+ }
704
+ } else {
705
+ imBlock.unplug(true /* healStack */);
706
+ }
548
707
 
549
- // Optionally highlight the actual connection, as a nod to previous behaviour.
550
- if (closest && renderer.shouldHighlightConnection(closest)) {
551
- closest.highlight();
552
- }
553
- };
708
+ if (imConn.targetConnection) {
709
+ throw Error(
710
+ 'markerConnection_ still connected at the end of ' +
711
+ 'disconnectInsertionMarker');
712
+ }
554
713
 
555
- /**
556
- * Show an insertion marker or replacement highlighting during a drag, if
557
- * needed.
558
- * At the end of this function, this.localConnection_ and
559
- * this.closestConnection_ should both be null.
560
- * @param {!Object} candidate An object containing a local connection, a closest
561
- * connection, and a radius.
562
- * @private
563
- */
564
- InsertionMarkerManager.prototype.maybeHidePreview_ = function(candidate) {
565
- // If there's no new preview, remove the old one but don't bother deleting it.
566
- // We might need it later, and this saves disposing of it and recreating it.
567
- if (!candidate.closest) {
568
- this.hidePreview_();
569
- } else {
570
- // If there's a new preview and there was an preview before, and either
571
- // connection has changed, remove the old preview.
572
- const hadPreview = this.closestConnection_ && this.localConnection_;
573
- const closestChanged = this.closestConnection_ !== candidate.closest;
574
- const localChanged = this.localConnection_ !== candidate.local;
575
-
576
- // Also hide if we had a preview before but now we're going to delete
577
- // instead.
578
- if (hadPreview &&
579
- (closestChanged || localChanged || this.wouldDeleteBlock_)) {
580
- this.hidePreview_();
714
+ this.markerConnection_ = null;
715
+ const svg = imBlock.getSvgRoot();
716
+ if (svg) {
717
+ svg.setAttribute('visibility', 'hidden');
581
718
  }
582
719
  }
583
720
 
584
- // Either way, clear out old state.
585
- this.markerConnection_ = null;
586
- this.closestConnection_ = null;
587
- this.localConnection_ = null;
588
- };
589
-
590
- /**
591
- * A preview should be hidden. This function figures out if it is a block
592
- * highlight or an insertion marker, and hides the appropriate one.
593
- * @private
594
- */
595
- InsertionMarkerManager.prototype.hidePreview_ = function() {
596
- if (this.closestConnection_ && this.closestConnection_.targetBlock() &&
597
- this.workspace_.getRenderer().shouldHighlightConnection(
598
- this.closestConnection_)) {
599
- this.closestConnection_.unhighlight();
600
- }
601
- if (this.fadedBlock_) {
602
- this.hideReplacementFade_();
603
- } else if (this.highlightedBlock_) {
604
- this.hideInsertionInputOutline_();
605
- } else if (this.markerConnection_) {
606
- this.hideInsertionMarker_();
721
+ /**
722
+ * Shows an outline around the input the closest connection belongs to.
723
+ * @private
724
+ */
725
+ showInsertionInputOutline_() {
726
+ const closest = this.closestConnection_;
727
+ this.highlightedBlock_ = closest.getSourceBlock();
728
+ this.highlightedBlock_.highlightShapeForInput(closest, true);
607
729
  }
608
- };
609
730
 
610
- /**
611
- * Shows an insertion marker connected to the appropriate blocks (based on
612
- * manager state).
613
- * @private
614
- */
615
- InsertionMarkerManager.prototype.showInsertionMarker_ = function() {
616
- const local = this.localConnection_;
617
- const closest = this.closestConnection_;
618
-
619
- const isLastInStack = this.lastOnStack_ && local === this.lastOnStack_;
620
- const imBlock = isLastInStack ? this.lastMarker_ : this.firstMarker_;
621
- const imConn = imBlock.getMatchingConnection(local.getSourceBlock(), local);
622
-
623
- if (imConn === this.markerConnection_) {
624
- throw Error(
625
- 'Made it to showInsertionMarker_ even though the marker isn\'t ' +
626
- 'changing');
731
+ /**
732
+ * Hides any visible input outlines.
733
+ * @private
734
+ */
735
+ hideInsertionInputOutline_() {
736
+ this.highlightedBlock_.highlightShapeForInput(
737
+ this.closestConnection_, false);
738
+ this.highlightedBlock_ = null;
627
739
  }
628
740
 
629
- // Render disconnected from everything else so that we have a valid
630
- // connection location.
631
- imBlock.render();
632
- imBlock.rendered = true;
633
- imBlock.getSvgRoot().setAttribute('visibility', 'visible');
634
-
635
- if (imConn && closest) {
636
- // Position so that the existing block doesn't move.
637
- imBlock.positionNearConnection(imConn, closest);
638
- }
639
- if (closest) {
640
- // Connect() also renders the insertion marker.
641
- imConn.connect(closest);
741
+ /**
742
+ * Shows a replacement fade affect on the closest connection's target block
743
+ * (the block that is currently connected to it).
744
+ * @private
745
+ */
746
+ showReplacementFade_() {
747
+ this.fadedBlock_ = this.closestConnection_.targetBlock();
748
+ this.fadedBlock_.fadeForReplacement(true);
642
749
  }
643
750
 
644
- this.markerConnection_ = imConn;
645
- };
646
-
647
- /**
648
- * Disconnects and hides the current insertion marker. Should return the blocks
649
- * to their original state.
650
- * @private
651
- */
652
- InsertionMarkerManager.prototype.hideInsertionMarker_ = function() {
653
- if (!this.markerConnection_) {
654
- console.log('No insertion marker connection to disconnect');
655
- return;
751
+ /**
752
+ * Hides/Removes any visible fade affects.
753
+ * @private
754
+ */
755
+ hideReplacementFade_() {
756
+ this.fadedBlock_.fadeForReplacement(false);
757
+ this.fadedBlock_ = null;
656
758
  }
657
759
 
658
- const imConn = this.markerConnection_;
659
- const imBlock = imConn.getSourceBlock();
660
- const markerNext = imBlock.nextConnection;
661
- const markerPrev = imBlock.previousConnection;
662
- const markerOutput = imBlock.outputConnection;
663
-
664
- const isFirstInStatementStack =
665
- (imConn === markerNext && !(markerPrev && markerPrev.targetConnection));
666
-
667
- const isFirstInOutputStack = imConn.type === ConnectionType.INPUT_VALUE &&
668
- !(markerOutput && markerOutput.targetConnection);
669
- // The insertion marker is the first block in a stack. Unplug won't do
670
- // anything in that case. Instead, unplug the following block.
671
- if (isFirstInStatementStack || isFirstInOutputStack) {
672
- imConn.targetBlock().unplug(false);
673
- } else if (
674
- imConn.type === ConnectionType.NEXT_STATEMENT && imConn !== markerNext) {
675
- // Inside of a C-block, first statement connection.
676
- const innerConnection = imConn.targetConnection;
677
- innerConnection.getSourceBlock().unplug(false);
678
-
679
- const previousBlockNextConnection =
680
- markerPrev ? markerPrev.targetConnection : null;
681
-
682
- imBlock.unplug(true);
683
- if (previousBlockNextConnection) {
684
- previousBlockNextConnection.connect(innerConnection);
760
+ /**
761
+ * Get a list of the insertion markers that currently exist. Drags have 0, 1,
762
+ * or 2 insertion markers.
763
+ * @return {!Array<!BlockSvg>} A possibly empty list of insertion
764
+ * marker blocks.
765
+ * @package
766
+ */
767
+ getInsertionMarkers() {
768
+ const result = [];
769
+ if (this.firstMarker_) {
770
+ result.push(this.firstMarker_);
685
771
  }
686
- } else {
687
- imBlock.unplug(true /* healStack */);
688
- }
689
-
690
- if (imConn.targetConnection) {
691
- throw Error(
692
- 'markerConnection_ still connected at the end of ' +
693
- 'disconnectInsertionMarker');
694
- }
695
-
696
- this.markerConnection_ = null;
697
- const svg = imBlock.getSvgRoot();
698
- if (svg) {
699
- svg.setAttribute('visibility', 'hidden');
772
+ if (this.lastMarker_) {
773
+ result.push(this.lastMarker_);
774
+ }
775
+ return result;
700
776
  }
701
- };
777
+ }
702
778
 
703
779
  /**
704
- * Shows an outline around the input the closest connection belongs to.
705
- * @private
706
- */
707
- InsertionMarkerManager.prototype.showInsertionInputOutline_ = function() {
708
- const closest = this.closestConnection_;
709
- this.highlightedBlock_ = closest.getSourceBlock();
710
- this.highlightedBlock_.highlightShapeForInput(closest, true);
711
- };
712
-
713
- /**
714
- * Hides any visible input outlines.
715
- * @private
716
- */
717
- InsertionMarkerManager.prototype.hideInsertionInputOutline_ = function() {
718
- this.highlightedBlock_.highlightShapeForInput(this.closestConnection_, false);
719
- this.highlightedBlock_ = null;
720
- };
721
-
722
- /**
723
- * Shows a replacement fade affect on the closest connection's target block
724
- * (the block that is currently connected to it).
725
- * @private
726
- */
727
- InsertionMarkerManager.prototype.showReplacementFade_ = function() {
728
- this.fadedBlock_ = this.closestConnection_.targetBlock();
729
- this.fadedBlock_.fadeForReplacement(true);
730
- };
731
-
732
- /**
733
- * Hides/Removes any visible fade affects.
734
- * @private
735
- */
736
- InsertionMarkerManager.prototype.hideReplacementFade_ = function() {
737
- this.fadedBlock_.fadeForReplacement(false);
738
- this.fadedBlock_ = null;
739
- };
740
-
741
- /**
742
- * Get a list of the insertion markers that currently exist. Drags have 0, 1,
743
- * or 2 insertion markers.
744
- * @return {!Array<!BlockSvg>} A possibly empty list of insertion
745
- * marker blocks.
746
- * @package
780
+ * An enum describing different kinds of previews the InsertionMarkerManager
781
+ * could display.
782
+ * @enum {number}
747
783
  */
748
- InsertionMarkerManager.prototype.getInsertionMarkers = function() {
749
- const result = [];
750
- if (this.firstMarker_) {
751
- result.push(this.firstMarker_);
752
- }
753
- if (this.lastMarker_) {
754
- result.push(this.lastMarker_);
755
- }
756
- return result;
784
+ InsertionMarkerManager.PREVIEW_TYPE = {
785
+ INSERTION_MARKER: 0,
786
+ INPUT_OUTLINE: 1,
787
+ REPLACEMENT_FADE: 2,
757
788
  };
758
789
 
759
790
  exports.InsertionMarkerManager = InsertionMarkerManager;