blockly 7.20211209.2 → 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
package/core/bubble.js CHANGED
@@ -40,902 +40,946 @@ goog.require('Blockly.Workspace');
40
40
 
41
41
  /**
42
42
  * Class for UI bubble.
43
- * @param {!WorkspaceSvg} workspace The workspace on which to draw the
44
- * bubble.
45
- * @param {!Element} content SVG content for the bubble.
46
- * @param {!Element} shape SVG element to avoid eclipsing.
47
- * @param {!Coordinate} anchorXY Absolute position of bubble's
48
- * anchor point.
49
- * @param {?number} bubbleWidth Width of bubble, or null if not resizable.
50
- * @param {?number} bubbleHeight Height of bubble, or null if not resizable.
51
43
  * @implements {IBubble}
52
- * @constructor
53
44
  * @alias Blockly.Bubble
54
45
  */
55
- const Bubble = function(
56
- workspace, content, shape, anchorXY, bubbleWidth, bubbleHeight) {
57
- this.workspace_ = workspace;
58
- this.content_ = content;
59
- this.shape_ = shape;
46
+ const Bubble = class {
47
+ /**
48
+ * @param {!WorkspaceSvg} workspace The workspace on which to draw the
49
+ * bubble.
50
+ * @param {!Element} content SVG content for the bubble.
51
+ * @param {!Element} shape SVG element to avoid eclipsing.
52
+ * @param {!Coordinate} anchorXY Absolute position of bubble's
53
+ * anchor point.
54
+ * @param {?number} bubbleWidth Width of bubble, or null if not resizable.
55
+ * @param {?number} bubbleHeight Height of bubble, or null if not resizable.
56
+ * @struct
57
+ */
58
+ constructor(workspace, content, shape, anchorXY, bubbleWidth, bubbleHeight) {
59
+ this.workspace_ = workspace;
60
+ this.content_ = content;
61
+ this.shape_ = shape;
62
+
63
+ /**
64
+ * Flag to stop incremental rendering during construction.
65
+ * @type {boolean}
66
+ * @private
67
+ */
68
+ this.rendered_ = false;
69
+
70
+ /**
71
+ * The SVG group containing all parts of the bubble.
72
+ * @type {SVGGElement}
73
+ * @private
74
+ */
75
+ this.bubbleGroup_ = null;
76
+
77
+ /**
78
+ * The SVG path for the arrow from the bubble to the icon on the block.
79
+ * @type {SVGPathElement}
80
+ * @private
81
+ */
82
+ this.bubbleArrow_ = null;
83
+
84
+ /**
85
+ * The SVG rect for the main body of the bubble.
86
+ * @type {SVGRectElement}
87
+ * @private
88
+ */
89
+ this.bubbleBack_ = null;
90
+
91
+ /**
92
+ * The SVG group for the resize hash marks on some bubbles.
93
+ * @type {SVGGElement}
94
+ * @private
95
+ */
96
+ this.resizeGroup_ = null;
97
+
98
+ /**
99
+ * Absolute coordinate of anchor point, in workspace coordinates.
100
+ * @type {Coordinate}
101
+ * @private
102
+ */
103
+ this.anchorXY_ = null;
104
+
105
+ /**
106
+ * Relative X coordinate of bubble with respect to the anchor's centre,
107
+ * in workspace units.
108
+ * In RTL mode the initial value is negated.
109
+ * @type {number}
110
+ * @private
111
+ */
112
+ this.relativeLeft_ = 0;
113
+
114
+ /**
115
+ * Relative Y coordinate of bubble with respect to the anchor's centre, in
116
+ * workspace units.
117
+ * @type {number}
118
+ * @private
119
+ */
120
+ this.relativeTop_ = 0;
121
+
122
+ /**
123
+ * Width of bubble, in workspace units.
124
+ * @type {number}
125
+ * @private
126
+ */
127
+ this.width_ = 0;
128
+
129
+ /**
130
+ * Height of bubble, in workspace units.
131
+ * @type {number}
132
+ * @private
133
+ */
134
+ this.height_ = 0;
135
+
136
+ /**
137
+ * Automatically position and reposition the bubble.
138
+ * @type {boolean}
139
+ * @private
140
+ */
141
+ this.autoLayout_ = true;
142
+
143
+ /**
144
+ * Method to call on resize of bubble.
145
+ * @type {?function()}
146
+ * @private
147
+ */
148
+ this.resizeCallback_ = null;
149
+
150
+ /**
151
+ * Method to call on move of bubble.
152
+ * @type {?function()}
153
+ * @private
154
+ */
155
+ this.moveCallback_ = null;
156
+
157
+ /**
158
+ * Mouse down on bubbleBack_ event data.
159
+ * @type {?browserEvents.Data}
160
+ * @private
161
+ */
162
+ this.onMouseDownBubbleWrapper_ = null;
163
+
164
+ /**
165
+ * Mouse down on resizeGroup_ event data.
166
+ * @type {?browserEvents.Data}
167
+ * @private
168
+ */
169
+ this.onMouseDownResizeWrapper_ = null;
170
+
171
+ /**
172
+ * Describes whether this bubble has been disposed of (nodes and event
173
+ * listeners removed from the page) or not.
174
+ * @type {boolean}
175
+ * @package
176
+ */
177
+ this.disposed = false;
178
+
179
+ let angle = Bubble.ARROW_ANGLE;
180
+ if (this.workspace_.RTL) {
181
+ angle = -angle;
182
+ }
183
+ this.arrow_radians_ = math.toRadians(angle);
184
+
185
+ const canvas = workspace.getBubbleCanvas();
186
+ canvas.appendChild(
187
+ this.createDom_(content, !!(bubbleWidth && bubbleHeight)));
188
+
189
+ this.setAnchorLocation(anchorXY);
190
+ if (!bubbleWidth || !bubbleHeight) {
191
+ const bBox = /** @type {SVGLocatable} */ (this.content_).getBBox();
192
+ bubbleWidth = bBox.width + 2 * Bubble.BORDER_WIDTH;
193
+ bubbleHeight = bBox.height + 2 * Bubble.BORDER_WIDTH;
194
+ }
195
+ this.setBubbleSize(bubbleWidth, bubbleHeight);
196
+
197
+ // Render the bubble.
198
+ this.positionBubble_();
199
+ this.renderArrow_();
200
+ this.rendered_ = true;
201
+ }
60
202
 
61
203
  /**
62
- * Method to call on resize of bubble.
63
- * @type {?function()}
204
+ * Create the bubble's DOM.
205
+ * @param {!Element} content SVG content for the bubble.
206
+ * @param {boolean} hasResize Add diagonal resize gripper if true.
207
+ * @return {!SVGElement} The bubble's SVG group.
64
208
  * @private
65
209
  */
66
- this.resizeCallback_ = null;
210
+ createDom_(content, hasResize) {
211
+ /* Create the bubble. Here's the markup that will be generated:
212
+ <g>
213
+ <g filter="url(#blocklyEmbossFilter837493)">
214
+ <path d="... Z" />
215
+ <rect class="blocklyDraggable" rx="8" ry="8" width="180" height="180"/>
216
+ </g>
217
+ <g transform="translate(165, 165)" class="blocklyResizeSE">
218
+ <polygon points="0,15 15,15 15,0"/>
219
+ <line class="blocklyResizeLine" x1="5" y1="14" x2="14" y2="5"/>
220
+ <line class="blocklyResizeLine" x1="10" y1="14" x2="14" y2="10"/>
221
+ </g>
222
+ [...content goes here...]
223
+ </g>
224
+ */
225
+ this.bubbleGroup_ = dom.createSvgElement(Svg.G, {}, null);
226
+ let filter = {
227
+ 'filter': 'url(#' +
228
+ this.workspace_.getRenderer().getConstants().embossFilterId + ')',
229
+ };
230
+ if (userAgent.JAVA_FX) {
231
+ // Multiple reports that JavaFX can't handle filters.
232
+ // https://github.com/google/blockly/issues/99
233
+ filter = {};
234
+ }
235
+ const bubbleEmboss = dom.createSvgElement(Svg.G, filter, this.bubbleGroup_);
236
+ this.bubbleArrow_ = dom.createSvgElement(Svg.PATH, {}, bubbleEmboss);
237
+ this.bubbleBack_ = dom.createSvgElement(
238
+ Svg.RECT, {
239
+ 'class': 'blocklyDraggable',
240
+ 'x': 0,
241
+ 'y': 0,
242
+ 'rx': Bubble.BORDER_WIDTH,
243
+ 'ry': Bubble.BORDER_WIDTH,
244
+ },
245
+ bubbleEmboss);
246
+ if (hasResize) {
247
+ this.resizeGroup_ = dom.createSvgElement(
248
+ Svg.G, {
249
+ 'class': this.workspace_.RTL ? 'blocklyResizeSW' :
250
+ 'blocklyResizeSE',
251
+ },
252
+ this.bubbleGroup_);
253
+ const resizeSize = 2 * Bubble.BORDER_WIDTH;
254
+ dom.createSvgElement(
255
+ Svg.POLYGON,
256
+ {'points': '0,x x,x x,0'.replace(/x/g, resizeSize.toString())},
257
+ this.resizeGroup_);
258
+ dom.createSvgElement(
259
+ Svg.LINE, {
260
+ 'class': 'blocklyResizeLine',
261
+ 'x1': resizeSize / 3,
262
+ 'y1': resizeSize - 1,
263
+ 'x2': resizeSize - 1,
264
+ 'y2': resizeSize / 3,
265
+ },
266
+ this.resizeGroup_);
267
+ dom.createSvgElement(
268
+ Svg.LINE, {
269
+ 'class': 'blocklyResizeLine',
270
+ 'x1': resizeSize * 2 / 3,
271
+ 'y1': resizeSize - 1,
272
+ 'x2': resizeSize - 1,
273
+ 'y2': resizeSize * 2 / 3,
274
+ },
275
+ this.resizeGroup_);
276
+ } else {
277
+ this.resizeGroup_ = null;
278
+ }
279
+
280
+ if (!this.workspace_.options.readOnly) {
281
+ this.onMouseDownBubbleWrapper_ = browserEvents.conditionalBind(
282
+ this.bubbleBack_, 'mousedown', this, this.bubbleMouseDown_);
283
+ if (this.resizeGroup_) {
284
+ this.onMouseDownResizeWrapper_ = browserEvents.conditionalBind(
285
+ this.resizeGroup_, 'mousedown', this, this.resizeMouseDown_);
286
+ }
287
+ }
288
+ this.bubbleGroup_.appendChild(content);
289
+ return this.bubbleGroup_;
290
+ }
67
291
 
68
292
  /**
69
- * Method to call on move of bubble.
70
- * @type {?function()}
71
- * @private
293
+ * Return the root node of the bubble's SVG group.
294
+ * @return {!SVGElement} The root SVG node of the bubble's group.
72
295
  */
73
- this.moveCallback_ = null;
296
+ getSvgRoot() {
297
+ return /** @type {!SVGElement} */ (this.bubbleGroup_);
298
+ }
74
299
 
75
300
  /**
76
- * Mouse down on bubbleBack_ event data.
77
- * @type {?browserEvents.Data}
78
- * @private
301
+ * Expose the block's ID on the bubble's top-level SVG group.
302
+ * @param {string} id ID of block.
79
303
  */
80
- this.onMouseDownBubbleWrapper_ = null;
304
+ setSvgId(id) {
305
+ if (this.bubbleGroup_.dataset) {
306
+ this.bubbleGroup_.dataset['blockId'] = id;
307
+ }
308
+ }
81
309
 
82
310
  /**
83
- * Mouse down on resizeGroup_ event data.
84
- * @type {?browserEvents.Data}
311
+ * Handle a mouse-down on bubble's border.
312
+ * @param {!Event} e Mouse down event.
85
313
  * @private
86
314
  */
87
- this.onMouseDownResizeWrapper_ = null;
315
+ bubbleMouseDown_(e) {
316
+ const gesture = this.workspace_.getGesture(e);
317
+ if (gesture) {
318
+ gesture.handleBubbleStart(e, this);
319
+ }
320
+ }
88
321
 
89
322
  /**
90
- * Describes whether this bubble has been disposed of (nodes and event
91
- * listeners removed from the page) or not.
92
- * @type {boolean}
323
+ * Show the context menu for this bubble.
324
+ * @param {!Event} _e Mouse event.
93
325
  * @package
94
326
  */
95
- this.disposed = false;
96
-
97
- let angle = Bubble.ARROW_ANGLE;
98
- if (this.workspace_.RTL) {
99
- angle = -angle;
327
+ showContextMenu(_e) {
328
+ // NOP on bubbles, but used by the bubble dragger to pass events to
329
+ // workspace comments.
100
330
  }
101
- this.arrow_radians_ = math.toRadians(angle);
102
331
 
103
- const canvas = workspace.getBubbleCanvas();
104
- canvas.appendChild(this.createDom_(content, !!(bubbleWidth && bubbleHeight)));
105
-
106
- this.setAnchorLocation(anchorXY);
107
- if (!bubbleWidth || !bubbleHeight) {
108
- const bBox = /** @type {SVGLocatable} */ (this.content_).getBBox();
109
- bubbleWidth = bBox.width + 2 * Bubble.BORDER_WIDTH;
110
- bubbleHeight = bBox.height + 2 * Bubble.BORDER_WIDTH;
332
+ /**
333
+ * Get whether this bubble is deletable or not.
334
+ * @return {boolean} True if deletable.
335
+ * @package
336
+ */
337
+ isDeletable() {
338
+ return false;
111
339
  }
112
- this.setBubbleSize(bubbleWidth, bubbleHeight);
113
-
114
- // Render the bubble.
115
- this.positionBubble_();
116
- this.renderArrow_();
117
- this.rendered_ = true;
118
- };
119
-
120
- /**
121
- * Width of the border around the bubble.
122
- */
123
- Bubble.BORDER_WIDTH = 6;
124
-
125
- /**
126
- * Determines the thickness of the base of the arrow in relation to the size
127
- * of the bubble. Higher numbers result in thinner arrows.
128
- */
129
- Bubble.ARROW_THICKNESS = 5;
130
-
131
- /**
132
- * The number of degrees that the arrow bends counter-clockwise.
133
- */
134
- Bubble.ARROW_ANGLE = 20;
135
-
136
- /**
137
- * The sharpness of the arrow's bend. Higher numbers result in smoother arrows.
138
- */
139
- Bubble.ARROW_BEND = 4;
140
-
141
- /**
142
- * Distance between arrow point and anchor point.
143
- */
144
- Bubble.ANCHOR_RADIUS = 8;
145
-
146
- /**
147
- * Mouse up event data.
148
- * @type {?browserEvents.Data}
149
- * @private
150
- */
151
- Bubble.onMouseUpWrapper_ = null;
152
340
 
153
- /**
154
- * Mouse move event data.
155
- * @type {?browserEvents.Data}
156
- * @private
157
- */
158
- Bubble.onMouseMoveWrapper_ = null;
159
-
160
- /**
161
- * Stop binding to the global mouseup and mousemove events.
162
- * @private
163
- */
164
- Bubble.unbindDragEvents_ = function() {
165
- if (Bubble.onMouseUpWrapper_) {
166
- browserEvents.unbind(Bubble.onMouseUpWrapper_);
167
- Bubble.onMouseUpWrapper_ = null;
168
- }
169
- if (Bubble.onMouseMoveWrapper_) {
170
- browserEvents.unbind(Bubble.onMouseMoveWrapper_);
171
- Bubble.onMouseMoveWrapper_ = null;
341
+ /**
342
+ * Update the style of this bubble when it is dragged over a delete area.
343
+ * @param {boolean} _enable True if the bubble is about to be deleted, false
344
+ * otherwise.
345
+ */
346
+ setDeleteStyle(_enable) {
347
+ // NOP if bubble is not deletable.
172
348
  }
173
- };
174
349
 
175
- /**
176
- * Handle a mouse-up event while dragging a bubble's border or resize handle.
177
- * @param {!Event} _e Mouse up event.
178
- * @private
179
- */
180
- Bubble.bubbleMouseUp_ = function(_e) {
181
- Touch.clearTouchIdentifier();
182
- Bubble.unbindDragEvents_();
183
- };
184
-
185
- /**
186
- * Flag to stop incremental rendering during construction.
187
- * @private
188
- */
189
- Bubble.prototype.rendered_ = false;
190
-
191
- /**
192
- * Absolute coordinate of anchor point, in workspace coordinates.
193
- * @type {Coordinate}
194
- * @private
195
- */
196
- Bubble.prototype.anchorXY_ = null;
197
-
198
- /**
199
- * Relative X coordinate of bubble with respect to the anchor's centre,
200
- * in workspace units.
201
- * In RTL mode the initial value is negated.
202
- * @private
203
- */
204
- Bubble.prototype.relativeLeft_ = 0;
205
-
206
- /**
207
- * Relative Y coordinate of bubble with respect to the anchor's centre, in
208
- * workspace units.
209
- * @private
210
- */
211
- Bubble.prototype.relativeTop_ = 0;
212
-
213
- /**
214
- * Width of bubble, in workspace units.
215
- * @private
216
- */
217
- Bubble.prototype.width_ = 0;
218
-
219
- /**
220
- * Height of bubble, in workspace units.
221
- * @private
222
- */
223
- Bubble.prototype.height_ = 0;
224
-
225
- /**
226
- * Automatically position and reposition the bubble.
227
- * @private
228
- */
229
- Bubble.prototype.autoLayout_ = true;
230
-
231
- /**
232
- * Create the bubble's DOM.
233
- * @param {!Element} content SVG content for the bubble.
234
- * @param {boolean} hasResize Add diagonal resize gripper if true.
235
- * @return {!SVGElement} The bubble's SVG group.
236
- * @private
237
- */
238
- Bubble.prototype.createDom_ = function(content, hasResize) {
239
- /* Create the bubble. Here's the markup that will be generated:
240
- <g>
241
- <g filter="url(#blocklyEmbossFilter837493)">
242
- <path d="... Z" />
243
- <rect class="blocklyDraggable" rx="8" ry="8" width="180" height="180"/>
244
- </g>
245
- <g transform="translate(165, 165)" class="blocklyResizeSE">
246
- <polygon points="0,15 15,15 15,0"/>
247
- <line class="blocklyResizeLine" x1="5" y1="14" x2="14" y2="5"/>
248
- <line class="blocklyResizeLine" x1="10" y1="14" x2="14" y2="10"/>
249
- </g>
250
- [...content goes here...]
251
- </g>
252
- */
253
- this.bubbleGroup_ = dom.createSvgElement(Svg.G, {}, null);
254
- let filter = {
255
- 'filter': 'url(#' +
256
- this.workspace_.getRenderer().getConstants().embossFilterId + ')',
257
- };
258
- if (userAgent.JAVA_FX) {
259
- // Multiple reports that JavaFX can't handle filters.
260
- // https://github.com/google/blockly/issues/99
261
- filter = {};
262
- }
263
- const bubbleEmboss = dom.createSvgElement(Svg.G, filter, this.bubbleGroup_);
264
- this.bubbleArrow_ = dom.createSvgElement(Svg.PATH, {}, bubbleEmboss);
265
- this.bubbleBack_ = dom.createSvgElement(
266
- Svg.RECT, {
267
- 'class': 'blocklyDraggable',
268
- 'x': 0,
269
- 'y': 0,
270
- 'rx': Bubble.BORDER_WIDTH,
271
- 'ry': Bubble.BORDER_WIDTH,
272
- },
273
- bubbleEmboss);
274
- if (hasResize) {
275
- this.resizeGroup_ = dom.createSvgElement(
276
- Svg.G,
277
- {'class': this.workspace_.RTL ? 'blocklyResizeSW' : 'blocklyResizeSE'},
278
- this.bubbleGroup_);
279
- const resizeSize = 2 * Bubble.BORDER_WIDTH;
280
- dom.createSvgElement(
281
- Svg.POLYGON,
282
- {'points': '0,x x,x x,0'.replace(/x/g, resizeSize.toString())},
283
- this.resizeGroup_);
284
- dom.createSvgElement(
285
- Svg.LINE, {
286
- 'class': 'blocklyResizeLine',
287
- 'x1': resizeSize / 3,
288
- 'y1': resizeSize - 1,
289
- 'x2': resizeSize - 1,
290
- 'y2': resizeSize / 3,
291
- },
292
- this.resizeGroup_);
293
- dom.createSvgElement(
294
- Svg.LINE, {
295
- 'class': 'blocklyResizeLine',
296
- 'x1': resizeSize * 2 / 3,
297
- 'y1': resizeSize - 1,
298
- 'x2': resizeSize - 1,
299
- 'y2': resizeSize * 2 / 3,
300
- },
301
- this.resizeGroup_);
302
- } else {
303
- this.resizeGroup_ = null;
350
+ /**
351
+ * Handle a mouse-down on bubble's resize corner.
352
+ * @param {!Event} e Mouse down event.
353
+ * @private
354
+ */
355
+ resizeMouseDown_(e) {
356
+ this.promote();
357
+ Bubble.unbindDragEvents_();
358
+ if (browserEvents.isRightButton(e)) {
359
+ // No right-click.
360
+ e.stopPropagation();
361
+ return;
362
+ }
363
+ // Left-click (or middle click)
364
+ this.workspace_.startDrag(
365
+ e,
366
+ new Coordinate(
367
+ this.workspace_.RTL ? -this.width_ : this.width_, this.height_));
368
+
369
+ Bubble.onMouseUpWrapper_ = browserEvents.conditionalBind(
370
+ document, 'mouseup', this, Bubble.bubbleMouseUp_);
371
+ Bubble.onMouseMoveWrapper_ = browserEvents.conditionalBind(
372
+ document, 'mousemove', this, this.resizeMouseMove_);
373
+ this.workspace_.hideChaff();
374
+ // This event has been handled. No need to bubble up to the document.
375
+ e.stopPropagation();
304
376
  }
305
377
 
306
- if (!this.workspace_.options.readOnly) {
307
- this.onMouseDownBubbleWrapper_ = browserEvents.conditionalBind(
308
- this.bubbleBack_, 'mousedown', this, this.bubbleMouseDown_);
309
- if (this.resizeGroup_) {
310
- this.onMouseDownResizeWrapper_ = browserEvents.conditionalBind(
311
- this.resizeGroup_, 'mousedown', this, this.resizeMouseDown_);
378
+ /**
379
+ * Resize this bubble to follow the mouse.
380
+ * @param {!Event} e Mouse move event.
381
+ * @private
382
+ */
383
+ resizeMouseMove_(e) {
384
+ this.autoLayout_ = false;
385
+ const newXY = this.workspace_.moveDrag(e);
386
+ this.setBubbleSize(this.workspace_.RTL ? -newXY.x : newXY.x, newXY.y);
387
+ if (this.workspace_.RTL) {
388
+ // RTL requires the bubble to move its left edge.
389
+ this.positionBubble_();
312
390
  }
313
391
  }
314
- this.bubbleGroup_.appendChild(content);
315
- return this.bubbleGroup_;
316
- };
317
392
 
318
- /**
319
- * Return the root node of the bubble's SVG group.
320
- * @return {!SVGElement} The root SVG node of the bubble's group.
321
- */
322
- Bubble.prototype.getSvgRoot = function() {
323
- return this.bubbleGroup_;
324
- };
325
-
326
- /**
327
- * Expose the block's ID on the bubble's top-level SVG group.
328
- * @param {string} id ID of block.
329
- */
330
- Bubble.prototype.setSvgId = function(id) {
331
- if (this.bubbleGroup_.dataset) {
332
- this.bubbleGroup_.dataset['blockId'] = id;
393
+ /**
394
+ * Register a function as a callback event for when the bubble is resized.
395
+ * @param {!Function} callback The function to call on resize.
396
+ */
397
+ registerResizeEvent(callback) {
398
+ this.resizeCallback_ = callback;
333
399
  }
334
- };
335
400
 
336
- /**
337
- * Handle a mouse-down on bubble's border.
338
- * @param {!Event} e Mouse down event.
339
- * @private
340
- */
341
- Bubble.prototype.bubbleMouseDown_ = function(e) {
342
- const gesture = this.workspace_.getGesture(e);
343
- if (gesture) {
344
- gesture.handleBubbleStart(e, this);
401
+ /**
402
+ * Register a function as a callback event for when the bubble is moved.
403
+ * @param {!Function} callback The function to call on move.
404
+ */
405
+ registerMoveEvent(callback) {
406
+ this.moveCallback_ = callback;
345
407
  }
346
- };
347
-
348
- /**
349
- * Show the context menu for this bubble.
350
- * @param {!Event} _e Mouse event.
351
- * @package
352
- */
353
- Bubble.prototype.showContextMenu = function(_e) {
354
- // NOP on bubbles, but used by the bubble dragger to pass events to
355
- // workspace comments.
356
- };
357
-
358
- /**
359
- * Get whether this bubble is deletable or not.
360
- * @return {boolean} True if deletable.
361
- * @package
362
- */
363
- Bubble.prototype.isDeletable = function() {
364
- return false;
365
- };
366
408
 
367
- /**
368
- * Update the style of this bubble when it is dragged over a delete area.
369
- * @param {boolean} _enable True if the bubble is about to be deleted, false
370
- * otherwise.
371
- */
372
- Bubble.prototype.setDeleteStyle = function(_enable) {
373
- // NOP if bubble is not deletable.
374
- };
375
-
376
- /**
377
- * Handle a mouse-down on bubble's resize corner.
378
- * @param {!Event} e Mouse down event.
379
- * @private
380
- */
381
- Bubble.prototype.resizeMouseDown_ = function(e) {
382
- this.promote();
383
- Bubble.unbindDragEvents_();
384
- if (browserEvents.isRightButton(e)) {
385
- // No right-click.
386
- e.stopPropagation();
387
- return;
388
- }
389
- // Left-click (or middle click)
390
- this.workspace_.startDrag(
391
- e,
392
- new Coordinate(
393
- this.workspace_.RTL ? -this.width_ : this.width_, this.height_));
394
-
395
- Bubble.onMouseUpWrapper_ = browserEvents.conditionalBind(
396
- document, 'mouseup', this, Bubble.bubbleMouseUp_);
397
- Bubble.onMouseMoveWrapper_ = browserEvents.conditionalBind(
398
- document, 'mousemove', this, this.resizeMouseMove_);
399
- this.workspace_.hideChaff();
400
- // This event has been handled. No need to bubble up to the document.
401
- e.stopPropagation();
402
- };
403
-
404
- /**
405
- * Resize this bubble to follow the mouse.
406
- * @param {!Event} e Mouse move event.
407
- * @private
408
- */
409
- Bubble.prototype.resizeMouseMove_ = function(e) {
410
- this.autoLayout_ = false;
411
- const newXY = this.workspace_.moveDrag(e);
412
- this.setBubbleSize(this.workspace_.RTL ? -newXY.x : newXY.x, newXY.y);
413
- if (this.workspace_.RTL) {
414
- // RTL requires the bubble to move its left edge.
415
- this.positionBubble_();
409
+ /**
410
+ * Move this bubble to the top of the stack.
411
+ * @return {boolean} Whether or not the bubble has been moved.
412
+ * @package
413
+ */
414
+ promote() {
415
+ const svgGroup = this.bubbleGroup_.parentNode;
416
+ if (svgGroup.lastChild !== this.bubbleGroup_) {
417
+ svgGroup.appendChild(this.bubbleGroup_);
418
+ return true;
419
+ }
420
+ return false;
416
421
  }
417
- };
418
-
419
- /**
420
- * Register a function as a callback event for when the bubble is resized.
421
- * @param {!Function} callback The function to call on resize.
422
- */
423
- Bubble.prototype.registerResizeEvent = function(callback) {
424
- this.resizeCallback_ = callback;
425
- };
426
422
 
427
- /**
428
- * Register a function as a callback event for when the bubble is moved.
429
- * @param {!Function} callback The function to call on move.
430
- */
431
- Bubble.prototype.registerMoveEvent = function(callback) {
432
- this.moveCallback_ = callback;
433
- };
423
+ /**
424
+ * Notification that the anchor has moved.
425
+ * Update the arrow and bubble accordingly.
426
+ * @param {!Coordinate} xy Absolute location.
427
+ */
428
+ setAnchorLocation(xy) {
429
+ this.anchorXY_ = xy;
430
+ if (this.rendered_) {
431
+ this.positionBubble_();
432
+ }
433
+ }
434
434
 
435
- /**
436
- * Move this bubble to the top of the stack.
437
- * @return {boolean} Whether or not the bubble has been moved.
438
- * @package
439
- */
440
- Bubble.prototype.promote = function() {
441
- const svgGroup = this.bubbleGroup_.parentNode;
442
- if (svgGroup.lastChild !== this.bubbleGroup_) {
443
- svgGroup.appendChild(this.bubbleGroup_);
444
- return true;
435
+ /**
436
+ * Position the bubble so that it does not fall off-screen.
437
+ * @private
438
+ */
439
+ layoutBubble_() {
440
+ // Get the metrics in workspace units.
441
+ const viewMetrics =
442
+ this.workspace_.getMetricsManager().getViewMetrics(true);
443
+
444
+ const optimalLeft = this.getOptimalRelativeLeft_(viewMetrics);
445
+ const optimalTop = this.getOptimalRelativeTop_(viewMetrics);
446
+ const bbox = this.shape_.getBBox();
447
+
448
+ const topPosition = {
449
+ x: optimalLeft,
450
+ y: -this.height_ -
451
+ this.workspace_.getRenderer().getConstants().MIN_BLOCK_HEIGHT,
452
+ };
453
+ const startPosition = {x: -this.width_ - 30, y: optimalTop};
454
+ const endPosition = {x: bbox.width, y: optimalTop};
455
+ const bottomPosition = {x: optimalLeft, y: bbox.height};
456
+
457
+ const closerPosition =
458
+ bbox.width < bbox.height ? endPosition : bottomPosition;
459
+ const fartherPosition =
460
+ bbox.width < bbox.height ? bottomPosition : endPosition;
461
+
462
+ const topPositionOverlap = this.getOverlap_(topPosition, viewMetrics);
463
+ const startPositionOverlap = this.getOverlap_(startPosition, viewMetrics);
464
+ const closerPositionOverlap = this.getOverlap_(closerPosition, viewMetrics);
465
+ const fartherPositionOverlap =
466
+ this.getOverlap_(fartherPosition, viewMetrics);
467
+
468
+ // Set the position to whichever position shows the most of the bubble,
469
+ // with tiebreaks going in the order: top > start > close > far.
470
+ const mostOverlap = Math.max(
471
+ topPositionOverlap, startPositionOverlap, closerPositionOverlap,
472
+ fartherPositionOverlap);
473
+ if (topPositionOverlap === mostOverlap) {
474
+ this.relativeLeft_ = topPosition.x;
475
+ this.relativeTop_ = topPosition.y;
476
+ return;
477
+ }
478
+ if (startPositionOverlap === mostOverlap) {
479
+ this.relativeLeft_ = startPosition.x;
480
+ this.relativeTop_ = startPosition.y;
481
+ return;
482
+ }
483
+ if (closerPositionOverlap === mostOverlap) {
484
+ this.relativeLeft_ = closerPosition.x;
485
+ this.relativeTop_ = closerPosition.y;
486
+ return;
487
+ }
488
+ // TODO: I believe relativeLeft_ should actually be called relativeStart_
489
+ // and then the math should be fixed to reflect this. (hopefully it'll
490
+ // make it look simpler)
491
+ this.relativeLeft_ = fartherPosition.x;
492
+ this.relativeTop_ = fartherPosition.y;
445
493
  }
446
- return false;
447
- };
448
494
 
449
- /**
450
- * Notification that the anchor has moved.
451
- * Update the arrow and bubble accordingly.
452
- * @param {!Coordinate} xy Absolute location.
453
- */
454
- Bubble.prototype.setAnchorLocation = function(xy) {
455
- this.anchorXY_ = xy;
456
- if (this.rendered_) {
457
- this.positionBubble_();
495
+ /**
496
+ * Calculate the what percentage of the bubble overlaps with the visible
497
+ * workspace (what percentage of the bubble is visible).
498
+ * @param {!{x: number, y: number}} relativeMin The position of the top-left
499
+ * corner of the bubble relative to the anchor point.
500
+ * @param {!MetricsManager.ContainerRegion} viewMetrics The view metrics
501
+ * of the workspace the bubble will appear in.
502
+ * @return {number} The percentage of the bubble that is visible.
503
+ * @private
504
+ */
505
+ getOverlap_(relativeMin, viewMetrics) {
506
+ // The position of the top-left corner of the bubble in workspace units.
507
+ const bubbleMin = {
508
+ x: this.workspace_.RTL ?
509
+ (this.anchorXY_.x - relativeMin.x - this.width_) :
510
+ (relativeMin.x + this.anchorXY_.x),
511
+ y: relativeMin.y + this.anchorXY_.y,
512
+ };
513
+ // The position of the bottom-right corner of the bubble in workspace units.
514
+ const bubbleMax = {
515
+ x: bubbleMin.x + this.width_,
516
+ y: bubbleMin.y + this.height_,
517
+ };
518
+
519
+ // We could adjust these values to account for the scrollbars, but the
520
+ // bubbles should have been adjusted to not collide with them anyway, so
521
+ // giving the workspace a slightly larger "bounding box" shouldn't affect
522
+ // the calculation.
523
+
524
+ // The position of the top-left corner of the workspace.
525
+ const workspaceMin = {x: viewMetrics.left, y: viewMetrics.top};
526
+ // The position of the bottom-right corner of the workspace.
527
+ const workspaceMax = {
528
+ x: viewMetrics.left + viewMetrics.width,
529
+ y: viewMetrics.top + viewMetrics.height,
530
+ };
531
+
532
+ const overlapWidth = Math.min(bubbleMax.x, workspaceMax.x) -
533
+ Math.max(bubbleMin.x, workspaceMin.x);
534
+ const overlapHeight = Math.min(bubbleMax.y, workspaceMax.y) -
535
+ Math.max(bubbleMin.y, workspaceMin.y);
536
+ return Math.max(
537
+ 0,
538
+ Math.min(
539
+ 1, (overlapWidth * overlapHeight) / (this.width_ * this.height_)));
458
540
  }
459
- };
460
541
 
461
- /**
462
- * Position the bubble so that it does not fall off-screen.
463
- * @private
464
- */
465
- Bubble.prototype.layoutBubble_ = function() {
466
- // Get the metrics in workspace units.
467
- const viewMetrics = this.workspace_.getMetricsManager().getViewMetrics(true);
468
-
469
- const optimalLeft = this.getOptimalRelativeLeft_(viewMetrics);
470
- const optimalTop = this.getOptimalRelativeTop_(viewMetrics);
471
- const bbox = this.shape_.getBBox();
472
-
473
- const topPosition = {
474
- x: optimalLeft,
475
- y: -this.height_ -
476
- this.workspace_.getRenderer().getConstants().MIN_BLOCK_HEIGHT,
477
- };
478
- const startPosition = {x: -this.width_ - 30, y: optimalTop};
479
- const endPosition = {x: bbox.width, y: optimalTop};
480
- const bottomPosition = {x: optimalLeft, y: bbox.height};
481
-
482
- const closerPosition =
483
- bbox.width < bbox.height ? endPosition : bottomPosition;
484
- const fartherPosition =
485
- bbox.width < bbox.height ? bottomPosition : endPosition;
486
-
487
- const topPositionOverlap = this.getOverlap_(topPosition, viewMetrics);
488
- const startPositionOverlap = this.getOverlap_(startPosition, viewMetrics);
489
- const closerPositionOverlap = this.getOverlap_(closerPosition, viewMetrics);
490
- const fartherPositionOverlap = this.getOverlap_(fartherPosition, viewMetrics);
491
-
492
- // Set the position to whichever position shows the most of the bubble,
493
- // with tiebreaks going in the order: top > start > close > far.
494
- const mostOverlap = Math.max(
495
- topPositionOverlap, startPositionOverlap, closerPositionOverlap,
496
- fartherPositionOverlap);
497
- if (topPositionOverlap === mostOverlap) {
498
- this.relativeLeft_ = topPosition.x;
499
- this.relativeTop_ = topPosition.y;
500
- return;
501
- }
502
- if (startPositionOverlap === mostOverlap) {
503
- this.relativeLeft_ = startPosition.x;
504
- this.relativeTop_ = startPosition.y;
505
- return;
506
- }
507
- if (closerPositionOverlap === mostOverlap) {
508
- this.relativeLeft_ = closerPosition.x;
509
- this.relativeTop_ = closerPosition.y;
510
- return;
511
- }
512
- // TODO: I believe relativeLeft_ should actually be called relativeStart_
513
- // and then the math should be fixed to reflect this. (hopefully it'll
514
- // make it look simpler)
515
- this.relativeLeft_ = fartherPosition.x;
516
- this.relativeTop_ = fartherPosition.y;
517
- };
542
+ /**
543
+ * Calculate what the optimal horizontal position of the top-left corner of
544
+ * the bubble is (relative to the anchor point) so that the most area of the
545
+ * bubble is shown.
546
+ * @param {!MetricsManager.ContainerRegion} viewMetrics The view metrics
547
+ * of the workspace the bubble will appear in.
548
+ * @return {number} The optimal horizontal position of the top-left corner
549
+ * of the bubble.
550
+ * @private
551
+ */
552
+ getOptimalRelativeLeft_(viewMetrics) {
553
+ let relativeLeft = -this.width_ / 4;
518
554
 
519
- /**
520
- * Calculate the what percentage of the bubble overlaps with the visible
521
- * workspace (what percentage of the bubble is visible).
522
- * @param {!{x: number, y: number}} relativeMin The position of the top-left
523
- * corner of the bubble relative to the anchor point.
524
- * @param {!MetricsManager.ContainerRegion} viewMetrics The view metrics
525
- * of the workspace the bubble will appear in.
526
- * @return {number} The percentage of the bubble that is visible.
527
- * @private
528
- */
529
- Bubble.prototype.getOverlap_ = function(relativeMin, viewMetrics) {
530
- // The position of the top-left corner of the bubble in workspace units.
531
- const bubbleMin = {
532
- x: this.workspace_.RTL ? (this.anchorXY_.x - relativeMin.x - this.width_) :
533
- (relativeMin.x + this.anchorXY_.x),
534
- y: relativeMin.y + this.anchorXY_.y,
535
- };
536
- // The position of the bottom-right corner of the bubble in workspace units.
537
- const bubbleMax = {
538
- x: bubbleMin.x + this.width_,
539
- y: bubbleMin.y + this.height_,
540
- };
541
-
542
- // We could adjust these values to account for the scrollbars, but the
543
- // bubbles should have been adjusted to not collide with them anyway, so
544
- // giving the workspace a slightly larger "bounding box" shouldn't affect the
545
- // calculation.
546
-
547
- // The position of the top-left corner of the workspace.
548
- const workspaceMin = {x: viewMetrics.left, y: viewMetrics.top};
549
- // The position of the bottom-right corner of the workspace.
550
- const workspaceMax = {
551
- x: viewMetrics.left + viewMetrics.width,
552
- y: viewMetrics.top + viewMetrics.height,
553
- };
554
-
555
- const overlapWidth = Math.min(bubbleMax.x, workspaceMax.x) -
556
- Math.max(bubbleMin.x, workspaceMin.x);
557
- const overlapHeight = Math.min(bubbleMax.y, workspaceMax.y) -
558
- Math.max(bubbleMin.y, workspaceMin.y);
559
- return Math.max(
560
- 0,
561
- Math.min(
562
- 1, (overlapWidth * overlapHeight) / (this.width_ * this.height_)));
563
- };
555
+ // No amount of sliding left or right will give us a better overlap.
556
+ if (this.width_ > viewMetrics.width) {
557
+ return relativeLeft;
558
+ }
564
559
 
565
- /**
566
- * Calculate what the optimal horizontal position of the top-left corner of the
567
- * bubble is (relative to the anchor point) so that the most area of the
568
- * bubble is shown.
569
- * @param {!MetricsManager.ContainerRegion} viewMetrics The view metrics
570
- * of the workspace the bubble will appear in.
571
- * @return {number} The optimal horizontal position of the top-left corner
572
- * of the bubble.
573
- * @private
574
- */
575
- Bubble.prototype.getOptimalRelativeLeft_ = function(viewMetrics) {
576
- let relativeLeft = -this.width_ / 4;
560
+ if (this.workspace_.RTL) {
561
+ // Bubble coordinates are flipped in RTL.
562
+ const bubbleRight = this.anchorXY_.x - relativeLeft;
563
+ const bubbleLeft = bubbleRight - this.width_;
564
+
565
+ const workspaceRight = viewMetrics.left + viewMetrics.width;
566
+ const workspaceLeft = viewMetrics.left +
567
+ // Thickness in workspace units.
568
+ (Scrollbar.scrollbarThickness / this.workspace_.scale);
569
+
570
+ if (bubbleLeft < workspaceLeft) {
571
+ // Slide the bubble right until it is onscreen.
572
+ relativeLeft = -(workspaceLeft - this.anchorXY_.x + this.width_);
573
+ } else if (bubbleRight > workspaceRight) {
574
+ // Slide the bubble left until it is onscreen.
575
+ relativeLeft = -(workspaceRight - this.anchorXY_.x);
576
+ }
577
+ } else {
578
+ const bubbleLeft = relativeLeft + this.anchorXY_.x;
579
+ const bubbleRight = bubbleLeft + this.width_;
580
+
581
+ const workspaceLeft = viewMetrics.left;
582
+ const workspaceRight = viewMetrics.left + viewMetrics.width -
583
+ // Thickness in workspace units.
584
+ (Scrollbar.scrollbarThickness / this.workspace_.scale);
585
+
586
+ if (bubbleLeft < workspaceLeft) {
587
+ // Slide the bubble right until it is onscreen.
588
+ relativeLeft = workspaceLeft - this.anchorXY_.x;
589
+ } else if (bubbleRight > workspaceRight) {
590
+ // Slide the bubble left until it is onscreen.
591
+ relativeLeft = workspaceRight - this.anchorXY_.x - this.width_;
592
+ }
593
+ }
577
594
 
578
- // No amount of sliding left or right will give us a better overlap.
579
- if (this.width_ > viewMetrics.width) {
580
595
  return relativeLeft;
581
596
  }
582
597
 
583
- if (this.workspace_.RTL) {
584
- // Bubble coordinates are flipped in RTL.
585
- const bubbleRight = this.anchorXY_.x - relativeLeft;
586
- const bubbleLeft = bubbleRight - this.width_;
587
-
588
- const workspaceRight = viewMetrics.left + viewMetrics.width;
589
- const workspaceLeft = viewMetrics.left +
590
- // Thickness in workspace units.
591
- (Scrollbar.scrollbarThickness / this.workspace_.scale);
598
+ /**
599
+ * Calculate what the optimal vertical position of the top-left corner of
600
+ * the bubble is (relative to the anchor point) so that the most area of the
601
+ * bubble is shown.
602
+ * @param {!MetricsManager.ContainerRegion} viewMetrics The view metrics
603
+ * of the workspace the bubble will appear in.
604
+ * @return {number} The optimal vertical position of the top-left corner
605
+ * of the bubble.
606
+ * @private
607
+ */
608
+ getOptimalRelativeTop_(viewMetrics) {
609
+ let relativeTop = -this.height_ / 4;
592
610
 
593
- if (bubbleLeft < workspaceLeft) {
594
- // Slide the bubble right until it is onscreen.
595
- relativeLeft = -(workspaceLeft - this.anchorXY_.x + this.width_);
596
- } else if (bubbleRight > workspaceRight) {
597
- // Slide the bubble left until it is onscreen.
598
- relativeLeft = -(workspaceRight - this.anchorXY_.x);
611
+ // No amount of sliding up or down will give us a better overlap.
612
+ if (this.height_ > viewMetrics.height) {
613
+ return relativeTop;
599
614
  }
600
- } else {
601
- const bubbleLeft = relativeLeft + this.anchorXY_.x;
602
- const bubbleRight = bubbleLeft + this.width_;
603
615
 
604
- const workspaceLeft = viewMetrics.left;
605
- const workspaceRight = viewMetrics.left + viewMetrics.width -
616
+ const bubbleTop = this.anchorXY_.y + relativeTop;
617
+ const bubbleBottom = bubbleTop + this.height_;
618
+ const workspaceTop = viewMetrics.top;
619
+ const workspaceBottom = viewMetrics.top + viewMetrics.height -
606
620
  // Thickness in workspace units.
607
621
  (Scrollbar.scrollbarThickness / this.workspace_.scale);
608
622
 
609
- if (bubbleLeft < workspaceLeft) {
610
- // Slide the bubble right until it is onscreen.
611
- relativeLeft = workspaceLeft - this.anchorXY_.x;
612
- } else if (bubbleRight > workspaceRight) {
613
- // Slide the bubble left until it is onscreen.
614
- relativeLeft = workspaceRight - this.anchorXY_.x - this.width_;
623
+ const anchorY = this.anchorXY_.y;
624
+ if (bubbleTop < workspaceTop) {
625
+ // Slide the bubble down until it is onscreen.
626
+ relativeTop = workspaceTop - anchorY;
627
+ } else if (bubbleBottom > workspaceBottom) {
628
+ // Slide the bubble up until it is onscreen.
629
+ relativeTop = workspaceBottom - anchorY - this.height_;
615
630
  }
616
- }
617
631
 
618
- return relativeLeft;
619
- };
632
+ return relativeTop;
633
+ }
620
634
 
621
- /**
622
- * Calculate what the optimal vertical position of the top-left corner of
623
- * the bubble is (relative to the anchor point) so that the most area of the
624
- * bubble is shown.
625
- * @param {!MetricsManager.ContainerRegion} viewMetrics The view metrics
626
- * of the workspace the bubble will appear in.
627
- * @return {number} The optimal vertical position of the top-left corner
628
- * of the bubble.
629
- * @private
630
- */
631
- Bubble.prototype.getOptimalRelativeTop_ = function(viewMetrics) {
632
- let relativeTop = -this.height_ / 4;
635
+ /**
636
+ * Move the bubble to a location relative to the anchor's centre.
637
+ * @private
638
+ */
639
+ positionBubble_() {
640
+ let left = this.anchorXY_.x;
641
+ if (this.workspace_.RTL) {
642
+ left -= this.relativeLeft_ + this.width_;
643
+ } else {
644
+ left += this.relativeLeft_;
645
+ }
646
+ const top = this.relativeTop_ + this.anchorXY_.y;
647
+ this.moveTo(left, top);
648
+ }
633
649
 
634
- // No amount of sliding up or down will give us a better overlap.
635
- if (this.height_ > viewMetrics.height) {
636
- return relativeTop;
650
+ /**
651
+ * Move the bubble group to the specified location in workspace coordinates.
652
+ * @param {number} x The x position to move to.
653
+ * @param {number} y The y position to move to.
654
+ * @package
655
+ */
656
+ moveTo(x, y) {
657
+ this.bubbleGroup_.setAttribute(
658
+ 'transform', 'translate(' + x + ',' + y + ')');
637
659
  }
638
660
 
639
- const bubbleTop = this.anchorXY_.y + relativeTop;
640
- const bubbleBottom = bubbleTop + this.height_;
641
- const workspaceTop = viewMetrics.top;
642
- const workspaceBottom = viewMetrics.top + viewMetrics.height -
643
- // Thickness in workspace units.
644
- (Scrollbar.scrollbarThickness / this.workspace_.scale);
661
+ /**
662
+ * Triggers a move callback if one exists at the end of a drag.
663
+ * @param {boolean} adding True if adding, false if removing.
664
+ * @package
665
+ */
666
+ setDragging(adding) {
667
+ if (!adding && this.moveCallback_) {
668
+ this.moveCallback_();
669
+ }
670
+ }
645
671
 
646
- const anchorY = this.anchorXY_.y;
647
- if (bubbleTop < workspaceTop) {
648
- // Slide the bubble down until it is onscreen.
649
- relativeTop = workspaceTop - anchorY;
650
- } else if (bubbleBottom > workspaceBottom) {
651
- // Slide the bubble up until it is onscreen.
652
- relativeTop = workspaceBottom - anchorY - this.height_;
672
+ /**
673
+ * Get the dimensions of this bubble.
674
+ * @return {!Size} The height and width of the bubble.
675
+ */
676
+ getBubbleSize() {
677
+ return new Size(this.width_, this.height_);
653
678
  }
654
679
 
655
- return relativeTop;
656
- };
680
+ /**
681
+ * Size this bubble.
682
+ * @param {number} width Width of the bubble.
683
+ * @param {number} height Height of the bubble.
684
+ */
685
+ setBubbleSize(width, height) {
686
+ const doubleBorderWidth = 2 * Bubble.BORDER_WIDTH;
687
+ // Minimum size of a bubble.
688
+ width = Math.max(width, doubleBorderWidth + 45);
689
+ height = Math.max(height, doubleBorderWidth + 20);
690
+ this.width_ = width;
691
+ this.height_ = height;
692
+ this.bubbleBack_.setAttribute('width', width);
693
+ this.bubbleBack_.setAttribute('height', height);
694
+ if (this.resizeGroup_) {
695
+ if (this.workspace_.RTL) {
696
+ // Mirror the resize group.
697
+ const resizeSize = 2 * Bubble.BORDER_WIDTH;
698
+ this.resizeGroup_.setAttribute(
699
+ 'transform',
700
+ 'translate(' + resizeSize + ',' + (height - doubleBorderWidth) +
701
+ ') scale(-1 1)');
702
+ } else {
703
+ this.resizeGroup_.setAttribute(
704
+ 'transform',
705
+ 'translate(' + (width - doubleBorderWidth) + ',' +
706
+ (height - doubleBorderWidth) + ')');
707
+ }
708
+ }
709
+ if (this.autoLayout_) {
710
+ this.layoutBubble_();
711
+ }
712
+ this.positionBubble_();
713
+ this.renderArrow_();
657
714
 
658
- /**
659
- * Move the bubble to a location relative to the anchor's centre.
660
- * @private
661
- */
662
- Bubble.prototype.positionBubble_ = function() {
663
- let left = this.anchorXY_.x;
664
- if (this.workspace_.RTL) {
665
- left -= this.relativeLeft_ + this.width_;
666
- } else {
667
- left += this.relativeLeft_;
668
- }
669
- const top = this.relativeTop_ + this.anchorXY_.y;
670
- this.moveTo(left, top);
671
- };
715
+ // Allow the contents to resize.
716
+ if (this.resizeCallback_) {
717
+ this.resizeCallback_();
718
+ }
719
+ }
672
720
 
673
- /**
674
- * Move the bubble group to the specified location in workspace coordinates.
675
- * @param {number} x The x position to move to.
676
- * @param {number} y The y position to move to.
677
- * @package
678
- */
679
- Bubble.prototype.moveTo = function(x, y) {
680
- this.bubbleGroup_.setAttribute('transform', 'translate(' + x + ',' + y + ')');
681
- };
721
+ /**
722
+ * Draw the arrow between the bubble and the origin.
723
+ * @private
724
+ */
725
+ renderArrow_() {
726
+ const steps = [];
727
+ // Find the relative coordinates of the center of the bubble.
728
+ const relBubbleX = this.width_ / 2;
729
+ const relBubbleY = this.height_ / 2;
730
+ // Find the relative coordinates of the center of the anchor.
731
+ let relAnchorX = -this.relativeLeft_;
732
+ let relAnchorY = -this.relativeTop_;
733
+ if (relBubbleX === relAnchorX && relBubbleY === relAnchorY) {
734
+ // Null case. Bubble is directly on top of the anchor.
735
+ // Short circuit this rather than wade through divide by zeros.
736
+ steps.push('M ' + relBubbleX + ',' + relBubbleY);
737
+ } else {
738
+ // Compute the angle of the arrow's line.
739
+ const rise = relAnchorY - relBubbleY;
740
+ let run = relAnchorX - relBubbleX;
741
+ if (this.workspace_.RTL) {
742
+ run *= -1;
743
+ }
744
+ const hypotenuse = Math.sqrt(rise * rise + run * run);
745
+ let angle = Math.acos(run / hypotenuse);
746
+ if (rise < 0) {
747
+ angle = 2 * Math.PI - angle;
748
+ }
749
+ // Compute a line perpendicular to the arrow.
750
+ let rightAngle = angle + Math.PI / 2;
751
+ if (rightAngle > Math.PI * 2) {
752
+ rightAngle -= Math.PI * 2;
753
+ }
754
+ const rightRise = Math.sin(rightAngle);
755
+ const rightRun = Math.cos(rightAngle);
756
+
757
+ // Calculate the thickness of the base of the arrow.
758
+ const bubbleSize = this.getBubbleSize();
759
+ let thickness =
760
+ (bubbleSize.width + bubbleSize.height) / Bubble.ARROW_THICKNESS;
761
+ thickness = Math.min(thickness, bubbleSize.width, bubbleSize.height) / 4;
762
+
763
+ // Back the tip of the arrow off of the anchor.
764
+ const backoffRatio = 1 - Bubble.ANCHOR_RADIUS / hypotenuse;
765
+ relAnchorX = relBubbleX + backoffRatio * run;
766
+ relAnchorY = relBubbleY + backoffRatio * rise;
767
+
768
+ // Coordinates for the base of the arrow.
769
+ const baseX1 = relBubbleX + thickness * rightRun;
770
+ const baseY1 = relBubbleY + thickness * rightRise;
771
+ const baseX2 = relBubbleX - thickness * rightRun;
772
+ const baseY2 = relBubbleY - thickness * rightRise;
773
+
774
+ // Distortion to curve the arrow.
775
+ let swirlAngle = angle + this.arrow_radians_;
776
+ if (swirlAngle > Math.PI * 2) {
777
+ swirlAngle -= Math.PI * 2;
778
+ }
779
+ const swirlRise = Math.sin(swirlAngle) * hypotenuse / Bubble.ARROW_BEND;
780
+ const swirlRun = Math.cos(swirlAngle) * hypotenuse / Bubble.ARROW_BEND;
781
+
782
+ steps.push('M' + baseX1 + ',' + baseY1);
783
+ steps.push(
784
+ 'C' + (baseX1 + swirlRun) + ',' + (baseY1 + swirlRise) + ' ' +
785
+ relAnchorX + ',' + relAnchorY + ' ' + relAnchorX + ',' + relAnchorY);
786
+ steps.push(
787
+ 'C' + relAnchorX + ',' + relAnchorY + ' ' + (baseX2 + swirlRun) +
788
+ ',' + (baseY2 + swirlRise) + ' ' + baseX2 + ',' + baseY2);
789
+ }
790
+ steps.push('z');
791
+ this.bubbleArrow_.setAttribute('d', steps.join(' '));
792
+ }
682
793
 
683
- /**
684
- * Triggers a move callback if one exists at the end of a drag.
685
- * @param {boolean} adding True if adding, false if removing.
686
- * @package
687
- */
688
- Bubble.prototype.setDragging = function(adding) {
689
- if (!adding && this.moveCallback_) {
690
- this.moveCallback_();
794
+ /**
795
+ * Change the colour of a bubble.
796
+ * @param {string} hexColour Hex code of colour.
797
+ */
798
+ setColour(hexColour) {
799
+ this.bubbleBack_.setAttribute('fill', hexColour);
800
+ this.bubbleArrow_.setAttribute('fill', hexColour);
691
801
  }
692
- };
693
802
 
694
- /**
695
- * Get the dimensions of this bubble.
696
- * @return {!Size} The height and width of the bubble.
697
- */
698
- Bubble.prototype.getBubbleSize = function() {
699
- return new Size(this.width_, this.height_);
700
- };
803
+ /**
804
+ * Dispose of this bubble.
805
+ */
806
+ dispose() {
807
+ if (this.onMouseDownBubbleWrapper_) {
808
+ browserEvents.unbind(this.onMouseDownBubbleWrapper_);
809
+ }
810
+ if (this.onMouseDownResizeWrapper_) {
811
+ browserEvents.unbind(this.onMouseDownResizeWrapper_);
812
+ }
813
+ Bubble.unbindDragEvents_();
814
+ dom.removeNode(this.bubbleGroup_);
815
+ this.disposed = true;
816
+ }
701
817
 
702
- /**
703
- * Size this bubble.
704
- * @param {number} width Width of the bubble.
705
- * @param {number} height Height of the bubble.
706
- */
707
- Bubble.prototype.setBubbleSize = function(width, height) {
708
- const doubleBorderWidth = 2 * Bubble.BORDER_WIDTH;
709
- // Minimum size of a bubble.
710
- width = Math.max(width, doubleBorderWidth + 45);
711
- height = Math.max(height, doubleBorderWidth + 20);
712
- this.width_ = width;
713
- this.height_ = height;
714
- this.bubbleBack_.setAttribute('width', width);
715
- this.bubbleBack_.setAttribute('height', height);
716
- if (this.resizeGroup_) {
818
+ /**
819
+ * Move this bubble during a drag, taking into account whether or not there is
820
+ * a drag surface.
821
+ * @param {BlockDragSurfaceSvg} dragSurface The surface that carries
822
+ * rendered items during a drag, or null if no drag surface is in use.
823
+ * @param {!Coordinate} newLoc The location to translate to, in
824
+ * workspace coordinates.
825
+ * @package
826
+ */
827
+ moveDuringDrag(dragSurface, newLoc) {
828
+ if (dragSurface) {
829
+ dragSurface.translateSurface(newLoc.x, newLoc.y);
830
+ } else {
831
+ this.moveTo(newLoc.x, newLoc.y);
832
+ }
717
833
  if (this.workspace_.RTL) {
718
- // Mirror the resize group.
719
- const resizeSize = 2 * Bubble.BORDER_WIDTH;
720
- this.resizeGroup_.setAttribute(
721
- 'transform',
722
- 'translate(' + resizeSize + ',' + (height - doubleBorderWidth) +
723
- ') scale(-1 1)');
834
+ this.relativeLeft_ = this.anchorXY_.x - newLoc.x - this.width_;
724
835
  } else {
725
- this.resizeGroup_.setAttribute(
726
- 'transform',
727
- 'translate(' + (width - doubleBorderWidth) + ',' +
728
- (height - doubleBorderWidth) + ')');
836
+ this.relativeLeft_ = newLoc.x - this.anchorXY_.x;
729
837
  }
838
+ this.relativeTop_ = newLoc.y - this.anchorXY_.y;
839
+ this.renderArrow_();
730
840
  }
731
- if (this.autoLayout_) {
732
- this.layoutBubble_();
841
+
842
+ /**
843
+ * Return the coordinates of the top-left corner of this bubble's body
844
+ * relative to the drawing surface's origin (0,0), in workspace units.
845
+ * @return {!Coordinate} Object with .x and .y properties.
846
+ */
847
+ getRelativeToSurfaceXY() {
848
+ return new Coordinate(
849
+ this.workspace_.RTL ?
850
+ -this.relativeLeft_ + this.anchorXY_.x - this.width_ :
851
+ this.anchorXY_.x + this.relativeLeft_,
852
+ this.anchorXY_.y + this.relativeTop_);
733
853
  }
734
- this.positionBubble_();
735
- this.renderArrow_();
736
854
 
737
- // Allow the contents to resize.
738
- if (this.resizeCallback_) {
739
- this.resizeCallback_();
855
+ /**
856
+ * Set whether auto-layout of this bubble is enabled. The first time a bubble
857
+ * is shown it positions itself to not cover any blocks. Once a user has
858
+ * dragged it to reposition, it renders where the user put it.
859
+ * @param {boolean} enable True if auto-layout should be enabled, false
860
+ * otherwise.
861
+ * @package
862
+ */
863
+ setAutoLayout(enable) {
864
+ this.autoLayout_ = enable;
740
865
  }
741
- };
742
866
 
743
- /**
744
- * Draw the arrow between the bubble and the origin.
745
- * @private
746
- */
747
- Bubble.prototype.renderArrow_ = function() {
748
- const steps = [];
749
- // Find the relative coordinates of the center of the bubble.
750
- const relBubbleX = this.width_ / 2;
751
- const relBubbleY = this.height_ / 2;
752
- // Find the relative coordinates of the center of the anchor.
753
- let relAnchorX = -this.relativeLeft_;
754
- let relAnchorY = -this.relativeTop_;
755
- if (relBubbleX === relAnchorX && relBubbleY === relAnchorY) {
756
- // Null case. Bubble is directly on top of the anchor.
757
- // Short circuit this rather than wade through divide by zeros.
758
- steps.push('M ' + relBubbleX + ',' + relBubbleY);
759
- } else {
760
- // Compute the angle of the arrow's line.
761
- const rise = relAnchorY - relBubbleY;
762
- let run = relAnchorX - relBubbleX;
763
- if (this.workspace_.RTL) {
764
- run *= -1;
765
- }
766
- const hypotenuse = Math.sqrt(rise * rise + run * run);
767
- let angle = Math.acos(run / hypotenuse);
768
- if (rise < 0) {
769
- angle = 2 * Math.PI - angle;
770
- }
771
- // Compute a line perpendicular to the arrow.
772
- let rightAngle = angle + Math.PI / 2;
773
- if (rightAngle > Math.PI * 2) {
774
- rightAngle -= Math.PI * 2;
775
- }
776
- const rightRise = Math.sin(rightAngle);
777
- const rightRun = Math.cos(rightAngle);
778
-
779
- // Calculate the thickness of the base of the arrow.
780
- const bubbleSize = this.getBubbleSize();
781
- let thickness =
782
- (bubbleSize.width + bubbleSize.height) / Bubble.ARROW_THICKNESS;
783
- thickness = Math.min(thickness, bubbleSize.width, bubbleSize.height) / 4;
784
-
785
- // Back the tip of the arrow off of the anchor.
786
- const backoffRatio = 1 - Bubble.ANCHOR_RADIUS / hypotenuse;
787
- relAnchorX = relBubbleX + backoffRatio * run;
788
- relAnchorY = relBubbleY + backoffRatio * rise;
789
-
790
- // Coordinates for the base of the arrow.
791
- const baseX1 = relBubbleX + thickness * rightRun;
792
- const baseY1 = relBubbleY + thickness * rightRise;
793
- const baseX2 = relBubbleX - thickness * rightRun;
794
- const baseY2 = relBubbleY - thickness * rightRise;
795
-
796
- // Distortion to curve the arrow.
797
- let swirlAngle = angle + this.arrow_radians_;
798
- if (swirlAngle > Math.PI * 2) {
799
- swirlAngle -= Math.PI * 2;
800
- }
801
- const swirlRise = Math.sin(swirlAngle) * hypotenuse / Bubble.ARROW_BEND;
802
- const swirlRun = Math.cos(swirlAngle) * hypotenuse / Bubble.ARROW_BEND;
803
-
804
- steps.push('M' + baseX1 + ',' + baseY1);
805
- steps.push(
806
- 'C' + (baseX1 + swirlRun) + ',' + (baseY1 + swirlRise) + ' ' +
807
- relAnchorX + ',' + relAnchorY + ' ' + relAnchorX + ',' + relAnchorY);
808
- steps.push(
809
- 'C' + relAnchorX + ',' + relAnchorY + ' ' + (baseX2 + swirlRun) + ',' +
810
- (baseY2 + swirlRise) + ' ' + baseX2 + ',' + baseY2);
811
- }
812
- steps.push('z');
813
- this.bubbleArrow_.setAttribute('d', steps.join(' '));
867
+ /**
868
+ * Stop binding to the global mouseup and mousemove events.
869
+ * @private
870
+ */
871
+ static unbindDragEvents_() {
872
+ if (Bubble.onMouseUpWrapper_) {
873
+ browserEvents.unbind(Bubble.onMouseUpWrapper_);
874
+ Bubble.onMouseUpWrapper_ = null;
875
+ }
876
+ if (Bubble.onMouseMoveWrapper_) {
877
+ browserEvents.unbind(Bubble.onMouseMoveWrapper_);
878
+ Bubble.onMouseMoveWrapper_ = null;
879
+ }
880
+ }
881
+
882
+ /**
883
+ * Handle a mouse-up event while dragging a bubble's border or resize handle.
884
+ * @param {!Event} _e Mouse up event.
885
+ * @private
886
+ */
887
+ static bubbleMouseUp_(_e) {
888
+ Touch.clearTouchIdentifier();
889
+ Bubble.unbindDragEvents_();
890
+ }
891
+
892
+ /**
893
+ * Create the text for a non editable bubble.
894
+ * @param {string} text The text to display.
895
+ * @return {!SVGTextElement} The top-level node of the text.
896
+ * @package
897
+ */
898
+ static textToDom(text) {
899
+ const paragraph = dom.createSvgElement(
900
+ Svg.TEXT, {
901
+ 'class': 'blocklyText blocklyBubbleText blocklyNoPointerEvents',
902
+ 'y': Bubble.BORDER_WIDTH,
903
+ },
904
+ null);
905
+ const lines = text.split('\n');
906
+ for (let i = 0; i < lines.length; i++) {
907
+ const tspanElement = dom.createSvgElement(
908
+ Svg.TSPAN, {'dy': '1em', 'x': Bubble.BORDER_WIDTH}, paragraph);
909
+ const textNode = document.createTextNode(lines[i]);
910
+ tspanElement.appendChild(textNode);
911
+ }
912
+ return paragraph;
913
+ }
914
+
915
+ /**
916
+ * Creates a bubble that can not be edited.
917
+ * @param {!SVGTextElement} paragraphElement The text element for the non
918
+ * editable bubble.
919
+ * @param {!BlockSvg} block The block that the bubble is attached to.
920
+ * @param {!Coordinate} iconXY The coordinate of the icon.
921
+ * @return {!Bubble} The non editable bubble.
922
+ * @package
923
+ */
924
+ static createNonEditableBubble(paragraphElement, block, iconXY) {
925
+ const bubble = new Bubble(
926
+ /** @type {!WorkspaceSvg} */ (block.workspace), paragraphElement,
927
+ block.pathObject.svgPath,
928
+ /** @type {!Coordinate} */ (iconXY), null, null);
929
+ // Expose this bubble's block's ID on its top-level SVG group.
930
+ bubble.setSvgId(block.id);
931
+ if (block.RTL) {
932
+ // Right-align the paragraph.
933
+ // This cannot be done until the bubble is rendered on screen.
934
+ const maxWidth = paragraphElement.getBBox().width;
935
+ for (let i = 0, textElement;
936
+ (textElement = paragraphElement.childNodes[i]); i++) {
937
+ textElement.setAttribute('text-anchor', 'end');
938
+ textElement.setAttribute('x', maxWidth + Bubble.BORDER_WIDTH);
939
+ }
940
+ }
941
+ return bubble;
942
+ }
814
943
  };
815
944
 
816
945
  /**
817
- * Change the colour of a bubble.
818
- * @param {string} hexColour Hex code of colour.
946
+ * Width of the border around the bubble.
819
947
  */
820
- Bubble.prototype.setColour = function(hexColour) {
821
- this.bubbleBack_.setAttribute('fill', hexColour);
822
- this.bubbleArrow_.setAttribute('fill', hexColour);
823
- };
948
+ Bubble.BORDER_WIDTH = 6;
824
949
 
825
950
  /**
826
- * Dispose of this bubble.
951
+ * Determines the thickness of the base of the arrow in relation to the size
952
+ * of the bubble. Higher numbers result in thinner arrows.
827
953
  */
828
- Bubble.prototype.dispose = function() {
829
- if (this.onMouseDownBubbleWrapper_) {
830
- browserEvents.unbind(this.onMouseDownBubbleWrapper_);
831
- }
832
- if (this.onMouseDownResizeWrapper_) {
833
- browserEvents.unbind(this.onMouseDownResizeWrapper_);
834
- }
835
- Bubble.unbindDragEvents_();
836
- dom.removeNode(this.bubbleGroup_);
837
- this.disposed = true;
838
- };
954
+ Bubble.ARROW_THICKNESS = 5;
839
955
 
840
956
  /**
841
- * Move this bubble during a drag, taking into account whether or not there is
842
- * a drag surface.
843
- * @param {BlockDragSurfaceSvg} dragSurface The surface that carries
844
- * rendered items during a drag, or null if no drag surface is in use.
845
- * @param {!Coordinate} newLoc The location to translate to, in
846
- * workspace coordinates.
847
- * @package
957
+ * The number of degrees that the arrow bends counter-clockwise.
848
958
  */
849
- Bubble.prototype.moveDuringDrag = function(dragSurface, newLoc) {
850
- if (dragSurface) {
851
- dragSurface.translateSurface(newLoc.x, newLoc.y);
852
- } else {
853
- this.moveTo(newLoc.x, newLoc.y);
854
- }
855
- if (this.workspace_.RTL) {
856
- this.relativeLeft_ = this.anchorXY_.x - newLoc.x - this.width_;
857
- } else {
858
- this.relativeLeft_ = newLoc.x - this.anchorXY_.x;
859
- }
860
- this.relativeTop_ = newLoc.y - this.anchorXY_.y;
861
- this.renderArrow_();
862
- };
959
+ Bubble.ARROW_ANGLE = 20;
863
960
 
864
961
  /**
865
- * Return the coordinates of the top-left corner of this bubble's body relative
866
- * to the drawing surface's origin (0,0), in workspace units.
867
- * @return {!Coordinate} Object with .x and .y properties.
962
+ * The sharpness of the arrow's bend. Higher numbers result in smoother arrows.
868
963
  */
869
- Bubble.prototype.getRelativeToSurfaceXY = function() {
870
- return new Coordinate(
871
- this.workspace_.RTL ?
872
- -this.relativeLeft_ + this.anchorXY_.x - this.width_ :
873
- this.anchorXY_.x + this.relativeLeft_,
874
- this.anchorXY_.y + this.relativeTop_);
875
- };
964
+ Bubble.ARROW_BEND = 4;
876
965
 
877
966
  /**
878
- * Set whether auto-layout of this bubble is enabled. The first time a bubble
879
- * is shown it positions itself to not cover any blocks. Once a user has
880
- * dragged it to reposition, it renders where the user put it.
881
- * @param {boolean} enable True if auto-layout should be enabled, false
882
- * otherwise.
883
- * @package
967
+ * Distance between arrow point and anchor point.
884
968
  */
885
- Bubble.prototype.setAutoLayout = function(enable) {
886
- this.autoLayout_ = enable;
887
- };
969
+ Bubble.ANCHOR_RADIUS = 8;
888
970
 
889
971
  /**
890
- * Create the text for a non editable bubble.
891
- * @param {string} text The text to display.
892
- * @return {!SVGTextElement} The top-level node of the text.
893
- * @package
972
+ * Mouse up event data.
973
+ * @type {?browserEvents.Data}
974
+ * @private
894
975
  */
895
- Bubble.textToDom = function(text) {
896
- const paragraph = dom.createSvgElement(
897
- Svg.TEXT, {
898
- 'class': 'blocklyText blocklyBubbleText blocklyNoPointerEvents',
899
- 'y': Bubble.BORDER_WIDTH,
900
- },
901
- null);
902
- const lines = text.split('\n');
903
- for (let i = 0; i < lines.length; i++) {
904
- const tspanElement = dom.createSvgElement(
905
- Svg.TSPAN, {'dy': '1em', 'x': Bubble.BORDER_WIDTH}, paragraph);
906
- const textNode = document.createTextNode(lines[i]);
907
- tspanElement.appendChild(textNode);
908
- }
909
- return paragraph;
910
- };
976
+ Bubble.onMouseUpWrapper_ = null;
911
977
 
912
978
  /**
913
- * Creates a bubble that can not be edited.
914
- * @param {!SVGTextElement} paragraphElement The text element for the non
915
- * editable bubble.
916
- * @param {!BlockSvg} block The block that the bubble is attached to.
917
- * @param {!Coordinate} iconXY The coordinate of the icon.
918
- * @return {!Bubble} The non editable bubble.
919
- * @package
979
+ * Mouse move event data.
980
+ * @type {?browserEvents.Data}
981
+ * @private
920
982
  */
921
- Bubble.createNonEditableBubble = function(paragraphElement, block, iconXY) {
922
- const bubble = new Bubble(
923
- /** @type {!WorkspaceSvg} */ (block.workspace), paragraphElement,
924
- block.pathObject.svgPath,
925
- /** @type {!Coordinate} */ (iconXY), null, null);
926
- // Expose this bubble's block's ID on its top-level SVG group.
927
- bubble.setSvgId(block.id);
928
- if (block.RTL) {
929
- // Right-align the paragraph.
930
- // This cannot be done until the bubble is rendered on screen.
931
- const maxWidth = paragraphElement.getBBox().width;
932
- for (let i = 0, textElement; (textElement = paragraphElement.childNodes[i]);
933
- i++) {
934
- textElement.setAttribute('text-anchor', 'end');
935
- textElement.setAttribute('x', maxWidth + Bubble.BORDER_WIDTH);
936
- }
937
- }
938
- return bubble;
939
- };
983
+ Bubble.onMouseMoveWrapper_ = null;
940
984
 
941
985
  exports.Bubble = Bubble;