@blockdia/scratch-blocks 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (243) hide show
  1. package/.nvmrc +1 -0
  2. package/LICENSE +674 -0
  3. package/README.md +57 -0
  4. package/TRADEMARK +1 -0
  5. package/blockly_compressed_horizontal.js +2268 -0
  6. package/blockly_compressed_vertical.js +2296 -0
  7. package/blockly_uncompressed_horizontal.js +1212 -0
  8. package/blockly_uncompressed_vertical.js +1212 -0
  9. package/blocks_common/colour.js +61 -0
  10. package/blocks_common/math.js +159 -0
  11. package/blocks_common/matrix.js +54 -0
  12. package/blocks_common/note.js +58 -0
  13. package/blocks_common/text.js +57 -0
  14. package/blocks_compressed.js +37 -0
  15. package/blocks_compressed_horizontal.js +68 -0
  16. package/blocks_compressed_vertical.js +216 -0
  17. package/blocks_horizontal/control.js +212 -0
  18. package/blocks_horizontal/default_toolbox.js +139 -0
  19. package/blocks_horizontal/event.js +190 -0
  20. package/blocks_horizontal/wedo.js +325 -0
  21. package/blocks_vertical/control.js +532 -0
  22. package/blocks_vertical/data.js +666 -0
  23. package/blocks_vertical/default_toolbox.js +571 -0
  24. package/blocks_vertical/event.js +329 -0
  25. package/blocks_vertical/extensions.js +294 -0
  26. package/blocks_vertical/looks.js +591 -0
  27. package/blocks_vertical/motion.js +587 -0
  28. package/blocks_vertical/operators.js +470 -0
  29. package/blocks_vertical/procedures.js +1051 -0
  30. package/blocks_vertical/sensing.js +555 -0
  31. package/blocks_vertical/sound.js +246 -0
  32. package/blocks_vertical/vertical_extensions.js +289 -0
  33. package/build/gen_blocks.js +1 -0
  34. package/build/test_expect.js +1 -0
  35. package/build/test_input.js +1 -0
  36. package/build.py +647 -0
  37. package/cleanup.sh +101 -0
  38. package/core/block.js +1837 -0
  39. package/core/block_animations.js +107 -0
  40. package/core/block_drag_surface.js +299 -0
  41. package/core/block_dragger.js +424 -0
  42. package/core/block_events.js +531 -0
  43. package/core/block_render_svg_horizontal.js +892 -0
  44. package/core/block_render_svg_vertical.js +1734 -0
  45. package/core/block_svg.js +1355 -0
  46. package/core/blockly.js +620 -0
  47. package/core/blocks.js +37 -0
  48. package/core/bubble.js +664 -0
  49. package/core/bubble_dragger.js +285 -0
  50. package/core/colours.js +177 -0
  51. package/core/comment.js +293 -0
  52. package/core/comment_events.js +539 -0
  53. package/core/connection.js +776 -0
  54. package/core/connection_db.js +300 -0
  55. package/core/constants.js +408 -0
  56. package/core/contextmenu.js +534 -0
  57. package/core/css.js +1354 -0
  58. package/core/data_category.js +490 -0
  59. package/core/dragged_connection_manager.js +260 -0
  60. package/core/dropdowndiv.js +408 -0
  61. package/core/events.js +429 -0
  62. package/core/events_abstract.js +113 -0
  63. package/core/extensions.js +450 -0
  64. package/core/field.js +810 -0
  65. package/core/field_angle.js +410 -0
  66. package/core/field_checkbox.js +133 -0
  67. package/core/field_colour.js +253 -0
  68. package/core/field_colour_slider.js +387 -0
  69. package/core/field_date.js +353 -0
  70. package/core/field_dropdown.js +447 -0
  71. package/core/field_iconmenu.js +309 -0
  72. package/core/field_image.js +200 -0
  73. package/core/field_label.js +136 -0
  74. package/core/field_label_serializable.js +125 -0
  75. package/core/field_matrix.js +566 -0
  76. package/core/field_note.js +850 -0
  77. package/core/field_number.js +366 -0
  78. package/core/field_numberdropdown.js +77 -0
  79. package/core/field_textdropdown.js +164 -0
  80. package/core/field_textinput.js +675 -0
  81. package/core/field_textinput_removable.js +105 -0
  82. package/core/field_variable.js +385 -0
  83. package/core/field_variable_getter.js +185 -0
  84. package/core/field_vertical_separator.js +161 -0
  85. package/core/flyout_base.js +935 -0
  86. package/core/flyout_button.js +324 -0
  87. package/core/flyout_dragger.js +83 -0
  88. package/core/flyout_extension_category_header.js +159 -0
  89. package/core/flyout_horizontal.js +475 -0
  90. package/core/flyout_vertical.js +770 -0
  91. package/core/generator.js +426 -0
  92. package/core/gesture.js +1010 -0
  93. package/core/grid.js +227 -0
  94. package/core/icon.js +205 -0
  95. package/core/inject.js +496 -0
  96. package/core/input.js +285 -0
  97. package/core/insertion_marker_manager.js +678 -0
  98. package/core/intersection_observer.js +102 -0
  99. package/core/msg.js +62 -0
  100. package/core/mutator.js +426 -0
  101. package/core/names.js +198 -0
  102. package/core/options.js +244 -0
  103. package/core/procedures.js +739 -0
  104. package/core/rendered_connection.js +417 -0
  105. package/core/scratch_block_comment.js +646 -0
  106. package/core/scratch_blocks_utils.js +246 -0
  107. package/core/scratch_bubble.js +699 -0
  108. package/core/scratch_events.js +131 -0
  109. package/core/scratch_msgs.js +85 -0
  110. package/core/scrollbar.js +875 -0
  111. package/core/toolbox.js +803 -0
  112. package/core/tooltip.js +337 -0
  113. package/core/touch.js +226 -0
  114. package/core/trashcan.js +343 -0
  115. package/core/ui_events.js +91 -0
  116. package/core/ui_menu_utils.js +68 -0
  117. package/core/utils.js +825 -0
  118. package/core/variable_events.js +259 -0
  119. package/core/variable_map.js +415 -0
  120. package/core/variable_model.js +116 -0
  121. package/core/variables.js +674 -0
  122. package/core/warning.js +199 -0
  123. package/core/widgetdiv.js +344 -0
  124. package/core/workspace.js +673 -0
  125. package/core/workspace_audio.js +170 -0
  126. package/core/workspace_comment.js +426 -0
  127. package/core/workspace_comment_render_svg.js +706 -0
  128. package/core/workspace_comment_svg.js +609 -0
  129. package/core/workspace_drag_surface_svg.js +195 -0
  130. package/core/workspace_dragger.js +132 -0
  131. package/core/workspace_svg.js +2382 -0
  132. package/core/xml.js +930 -0
  133. package/core/zoom_controls.js +301 -0
  134. package/dist/horizontal.js +222 -0
  135. package/dist/vertical.js +222 -0
  136. package/dist/web/horizontal.js +232 -0
  137. package/dist/web/vertical.js +232 -0
  138. package/i18n/common.py +234 -0
  139. package/i18n/create_messages.py +162 -0
  140. package/i18n/create_scratch_msgs.js +69 -0
  141. package/i18n/dedup_json.py +73 -0
  142. package/i18n/js_to_json.js +46 -0
  143. package/i18n/js_to_json.py +136 -0
  144. package/i18n/json_to_js.js +52 -0
  145. package/i18n/json_to_js.py +185 -0
  146. package/i18n/sync_tx_translations.js +111 -0
  147. package/i18n/test_scratch_msgs.js +87 -0
  148. package/i18n/tests.py +47 -0
  149. package/i18n/xliff_to_json.py +232 -0
  150. package/local_build.sh +70 -0
  151. package/media/click.mp3 +0 -0
  152. package/media/click.ogg +0 -0
  153. package/media/click.wav +0 -0
  154. package/media/comment-arrow-down.svg +10 -0
  155. package/media/comment-arrow-up.svg +10 -0
  156. package/media/delete-x.svg +10 -0
  157. package/media/delete.mp3 +0 -0
  158. package/media/delete.ogg +0 -0
  159. package/media/delete.wav +0 -0
  160. package/media/dropdown-arrow-dark.svg +1 -0
  161. package/media/dropdown-arrow.svg +1 -0
  162. package/media/extensions/microbit-block-icon.svg +130 -0
  163. package/media/extensions/music-block-icon.svg +17 -0
  164. package/media/extensions/pen-block-icon.svg +19 -0
  165. package/media/extensions/wedo2-block-icon.svg +36 -0
  166. package/media/eyedropper.svg +22 -0
  167. package/media/green-flag.svg +17 -0
  168. package/media/handclosed.cur +0 -0
  169. package/media/handdelete.cur +0 -0
  170. package/media/handopen.cur +0 -0
  171. package/media/icons/arrow.svg +12 -0
  172. package/media/icons/arrow_button.svg +1 -0
  173. package/media/icons/control_forever.svg +1 -0
  174. package/media/icons/control_repeat.svg +1 -0
  175. package/media/icons/control_stop.svg +1 -0
  176. package/media/icons/control_wait.svg +1 -0
  177. package/media/icons/event_broadcast_blue.svg +1 -0
  178. package/media/icons/event_broadcast_coral.svg +1 -0
  179. package/media/icons/event_broadcast_green.svg +1 -0
  180. package/media/icons/event_broadcast_magenta.svg +1 -0
  181. package/media/icons/event_broadcast_orange.svg +1 -0
  182. package/media/icons/event_broadcast_purple.svg +1 -0
  183. package/media/icons/event_when-broadcast-received_blue.svg +1 -0
  184. package/media/icons/event_when-broadcast-received_coral.svg +1 -0
  185. package/media/icons/event_when-broadcast-received_green.svg +1 -0
  186. package/media/icons/event_when-broadcast-received_magenta.svg +1 -0
  187. package/media/icons/event_when-broadcast-received_orange.svg +1 -0
  188. package/media/icons/event_when-broadcast-received_purple.svg +1 -0
  189. package/media/icons/event_whenflagclicked.svg +1 -0
  190. package/media/icons/remove.svg +19 -0
  191. package/media/icons/set-led_blue.svg +1 -0
  192. package/media/icons/set-led_coral.svg +1 -0
  193. package/media/icons/set-led_green.svg +1 -0
  194. package/media/icons/set-led_magenta.svg +1 -0
  195. package/media/icons/set-led_mystery.svg +1 -0
  196. package/media/icons/set-led_orange.svg +1 -0
  197. package/media/icons/set-led_purple.svg +1 -0
  198. package/media/icons/set-led_white.svg +1 -0
  199. package/media/icons/set-led_yellow.svg +1 -0
  200. package/media/icons/wedo_motor-clockwise.svg +1 -0
  201. package/media/icons/wedo_motor-counterclockwise.svg +1 -0
  202. package/media/icons/wedo_motor-speed_fast.svg +1 -0
  203. package/media/icons/wedo_motor-speed_med.svg +1 -0
  204. package/media/icons/wedo_motor-speed_slow.svg +1 -0
  205. package/media/icons/wedo_when-distance_close.svg +1 -0
  206. package/media/icons/wedo_when-tilt-backward.svg +1 -0
  207. package/media/icons/wedo_when-tilt-forward.svg +1 -0
  208. package/media/icons/wedo_when-tilt-left.svg +1 -0
  209. package/media/icons/wedo_when-tilt-right.svg +1 -0
  210. package/media/icons/wedo_when-tilt.svg +1 -0
  211. package/media/repeat.svg +18 -0
  212. package/media/rotate-left.svg +1 -0
  213. package/media/rotate-right.svg +1 -0
  214. package/media/sprites.png +0 -0
  215. package/media/status-not-ready.svg +13 -0
  216. package/media/status-ready.svg +13 -0
  217. package/media/zoom-in.svg +1 -0
  218. package/media/zoom-out.svg +1 -0
  219. package/media/zoom-reset.svg +1 -0
  220. package/msg/js/en.js +290 -0
  221. package/msg/json/en.json +285 -0
  222. package/msg/messages.js +359 -0
  223. package/msg/scratch_msgs.js +22969 -0
  224. package/package.json +60 -0
  225. package/pull_from_blockly.sh +151 -0
  226. package/renovate.json5 +15 -0
  227. package/shim/blockly_compressed_horizontal-blocks_compressed.js +1 -0
  228. package/shim/blockly_compressed_horizontal.Blockly.js +1 -0
  229. package/shim/blockly_compressed_horizontal.goog.js +1 -0
  230. package/shim/blockly_compressed_horizontal.js +1 -0
  231. package/shim/blockly_compressed_vertical-blocks_compressed.js +1 -0
  232. package/shim/blockly_compressed_vertical.Blockly.js +1 -0
  233. package/shim/blockly_compressed_vertical.goog.js +1 -0
  234. package/shim/blockly_compressed_vertical.js +1 -0
  235. package/shim/blocks_compressed_horizontal-blockly_compressed_horizontal-messages.js +1 -0
  236. package/shim/blocks_compressed_horizontal.js +1 -0
  237. package/shim/blocks_compressed_vertical-blockly_compressed_vertical-messages.js +1 -0
  238. package/shim/blocks_compressed_vertical.js +1 -0
  239. package/shim/gh-pages.js +1 -0
  240. package/shim/horizontal.js +1 -0
  241. package/shim/index.js +17 -0
  242. package/shim/vertical.js +1 -0
  243. package/universal-python.js +82 -0
@@ -0,0 +1,935 @@
1
+ /**
2
+ * @license
3
+ * Visual Blocks Editor
4
+ *
5
+ * Copyright 2011 Google Inc.
6
+ * https://developers.google.com/blockly/
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ /**
22
+ * @fileoverview Flyout tray containing blocks which may be created.
23
+ * @author fraser@google.com (Neil Fraser)
24
+ */
25
+ 'use strict';
26
+
27
+ goog.provide('Blockly.Flyout');
28
+
29
+ goog.require('Blockly.Block');
30
+ goog.require('Blockly.Comment');
31
+ goog.require('Blockly.Events');
32
+ goog.require('Blockly.Events.BlockCreate');
33
+ goog.require('Blockly.Events.VarCreate');
34
+ goog.require('Blockly.FlyoutButton');
35
+ goog.require('Blockly.FlyoutExtensionCategoryHeader');
36
+ goog.require('Blockly.Gesture');
37
+ goog.require('Blockly.scratchBlocksUtils');
38
+ goog.require('Blockly.Touch');
39
+ goog.require('Blockly.WorkspaceSvg');
40
+ goog.require('goog.dom');
41
+ goog.require('goog.events');
42
+ goog.require('goog.math.Rect');
43
+ goog.require('goog.userAgent');
44
+
45
+
46
+ /**
47
+ * Class for a flyout.
48
+ * @param {!Object} workspaceOptions Dictionary of options for the workspace.
49
+ * @constructor
50
+ */
51
+ Blockly.Flyout = function(workspaceOptions) {
52
+ workspaceOptions.getMetrics = this.getMetrics_.bind(this);
53
+ workspaceOptions.setMetrics = this.setMetrics_.bind(this);
54
+
55
+ /**
56
+ * @type {!Blockly.Workspace}
57
+ * @protected
58
+ */
59
+ this.workspace_ = new Blockly.WorkspaceSvg(workspaceOptions);
60
+ this.workspace_.isFlyout = true;
61
+
62
+ // When we create blocks for this workspace, instead of using the "optional" id
63
+ // make the default `id` the same as the `type` for easier re-use.
64
+ var newBlock = this.workspace_.newBlock;
65
+ this.workspace_.newBlock = function(type, id) {
66
+ // Use `type` if `id` isn't passed. `this` will be workspace.
67
+ return newBlock.call(this, type, id || type);
68
+ };
69
+
70
+ /**
71
+ * Is RTL vs LTR.
72
+ * @type {boolean}
73
+ */
74
+ this.RTL = !!workspaceOptions.RTL;
75
+
76
+ /**
77
+ * Flyout should be laid out horizontally vs vertically.
78
+ * @type {boolean}
79
+ * @private
80
+ */
81
+ this.horizontalLayout_ = workspaceOptions.horizontalLayout;
82
+
83
+ /**
84
+ * Position of the toolbox and flyout relative to the workspace.
85
+ * @type {number}
86
+ * @protected
87
+ */
88
+ this.toolboxPosition_ = workspaceOptions.toolboxPosition;
89
+
90
+ /**
91
+ * Opaque data that can be passed to Blockly.unbindEvent_.
92
+ * @type {!Array.<!Array>}
93
+ * @private
94
+ */
95
+ this.eventWrappers_ = [];
96
+
97
+ /**
98
+ * List of background buttons that lurk behind each block to catch clicks
99
+ * landing in the blocks' lakes and bays.
100
+ * @type {!Array.<!Element>}
101
+ * @private
102
+ */
103
+ this.backgroundButtons_ = [];
104
+
105
+ /**
106
+ * List of visible buttons.
107
+ * @type {!Array.<!Blockly.FlyoutButton>}
108
+ * @protected
109
+ */
110
+ this.buttons_ = [];
111
+
112
+ /**
113
+ * List of event listeners.
114
+ * @type {!Array.<!Array>}
115
+ * @private
116
+ */
117
+ this.listeners_ = [];
118
+
119
+ /**
120
+ * List of blocks that should always be disabled.
121
+ * @type {!Array.<!Blockly.Block>}
122
+ * @private
123
+ */
124
+ this.permanentlyDisabled_ = [];
125
+
126
+ /**
127
+ * The toolbox that this flyout belongs to, or none if tihs is a simple
128
+ * workspace.
129
+ * @type {Blockly.Toolbox}
130
+ * @private
131
+ */
132
+ this.parentToolbox_ = null;
133
+
134
+ /**
135
+ * The target position for the flyout scroll animation in pixels.
136
+ * Is a number while animating, null otherwise.
137
+ * @type {?number}
138
+ * @package
139
+ */
140
+ this.scrollTarget = null;
141
+
142
+ /**
143
+ * A recycle bin for blocks.
144
+ * @type {!Array.<!Blockly.Block>}
145
+ * @private
146
+ */
147
+ this.recycleBlocks_ = [];
148
+
149
+ };
150
+
151
+ /**
152
+ * Does the flyout automatically close when a block is created?
153
+ * @type {boolean}
154
+ */
155
+ Blockly.Flyout.prototype.autoClose = false;
156
+
157
+ /**
158
+ * Whether the flyout is visible.
159
+ * @type {boolean}
160
+ * @private
161
+ */
162
+ Blockly.Flyout.prototype.isVisible_ = false;
163
+
164
+ /**
165
+ * Whether the workspace containing this flyout is visible.
166
+ * @type {boolean}
167
+ * @private
168
+ */
169
+ Blockly.Flyout.prototype.containerVisible_ = true;
170
+
171
+ /**
172
+ * Corner radius of the flyout background.
173
+ * @type {number}
174
+ * @const
175
+ */
176
+ Blockly.Flyout.prototype.CORNER_RADIUS = 0;
177
+
178
+ /**
179
+ * Margin around the edges of the blocks in the flyout.
180
+ * @type {number}
181
+ * @const
182
+ */
183
+ Blockly.Flyout.prototype.MARGIN = 12;
184
+
185
+ // TODO: Move GAP_X and GAP_Y to their appropriate files.
186
+
187
+ /**
188
+ * Gap between items in horizontal flyouts. Can be overridden with the "sep"
189
+ * element.
190
+ * @const {number}
191
+ */
192
+ Blockly.Flyout.prototype.GAP_X = Blockly.Flyout.prototype.MARGIN * 3;
193
+
194
+ /**
195
+ * Gap between items in vertical flyouts. Can be overridden with the "sep"
196
+ * element.
197
+ * @const {number}
198
+ */
199
+ Blockly.Flyout.prototype.GAP_Y = Blockly.Flyout.prototype.MARGIN;
200
+
201
+ /**
202
+ * Top/bottom padding between scrollbar and edge of flyout background.
203
+ * @type {number}
204
+ * @const
205
+ */
206
+ Blockly.Flyout.prototype.SCROLLBAR_PADDING = 2;
207
+
208
+ /**
209
+ * Width of flyout.
210
+ * @type {number}
211
+ * @protected
212
+ */
213
+ Blockly.Flyout.prototype.width_ = 0;
214
+
215
+ /**
216
+ * Height of flyout.
217
+ * @type {number}
218
+ * @protected
219
+ */
220
+ Blockly.Flyout.prototype.height_ = 0;
221
+
222
+ /**
223
+ * Width of flyout contents.
224
+ * @type {number}
225
+ * @private
226
+ */
227
+ Blockly.Flyout.prototype.contentWidth_ = 0;
228
+
229
+ /**
230
+ * Height of flyout contents.
231
+ * @type {number}
232
+ * @private
233
+ */
234
+ Blockly.Flyout.prototype.contentHeight_ = 0;
235
+
236
+ /**
237
+ * Vertical offset of flyout.
238
+ * @type {number}
239
+ * @private
240
+ */
241
+ Blockly.Flyout.prototype.verticalOffset_ = 0;
242
+
243
+ /**
244
+ * Range of a drag angle from a flyout considered "dragging toward workspace".
245
+ * Drags that are within the bounds of this many degrees from the orthogonal
246
+ * line to the flyout edge are considered to be "drags toward the workspace".
247
+ * Example:
248
+ * Flyout Edge Workspace
249
+ * [block] / <-within this angle, drags "toward workspace" |
250
+ * [block] ---- orthogonal to flyout boundary ---- |
251
+ * [block] \ |
252
+ * The angle is given in degrees from the orthogonal.
253
+ *
254
+ * This is used to know when to create a new block and when to scroll the
255
+ * flyout. Setting it to 360 means that all drags create a new block.
256
+ * @type {number}
257
+ * @protected
258
+ */
259
+ Blockly.Flyout.prototype.dragAngleRange_ = 70;
260
+
261
+ /**
262
+ * The fraction of the distance to the scroll target to move the flyout on
263
+ * each animation frame, when auto-scrolling. Values closer to 1.0 will make
264
+ * the scroll animation complete faster. Use 1.0 for no animation.
265
+ * @type {number}
266
+ */
267
+ Blockly.Flyout.prototype.scrollAnimationFraction = 0.3;
268
+
269
+ /**
270
+ * Whether to recycle blocks when refreshing the flyout. When false, do not allow
271
+ * anything to be recycled. The default is to recycle.
272
+ * @type {boolean}
273
+ * @private
274
+ */
275
+ Blockly.Flyout.prototype.recyclingEnabled_ = true;
276
+
277
+ /**
278
+ * Creates the flyout's DOM. Only needs to be called once. The flyout can
279
+ * either exist as its own svg element or be a g element nested inside a
280
+ * separate svg element.
281
+ * @param {string} tagName The type of tag to put the flyout in. This
282
+ * should be <svg> or <g>.
283
+ * @return {!Element} The flyout's SVG group.
284
+ */
285
+ Blockly.Flyout.prototype.createDom = function(tagName) {
286
+ /*
287
+ <svg | g>
288
+ <path class="blocklyFlyoutBackground"/>
289
+ <g class="blocklyFlyout"></g>
290
+ </ svg | g>
291
+ */
292
+ // Setting style to display:none to start. The toolbox and flyout
293
+ // hide/show code will set up proper visibility and size later.
294
+ this.svgGroup_ = Blockly.utils.createSvgElement(tagName,
295
+ {'class': 'blocklyFlyout', 'style': 'display: none'}, null);
296
+ this.svgBackground_ = Blockly.utils.createSvgElement('path',
297
+ {'class': 'blocklyFlyoutBackground'}, this.svgGroup_);
298
+ this.svgGroup_.appendChild(this.workspace_.createDom());
299
+ return this.svgGroup_;
300
+ };
301
+
302
+ /**
303
+ * Initializes the flyout.
304
+ * @param {!Blockly.Workspace} targetWorkspace The workspace in which to create
305
+ * new blocks.
306
+ */
307
+ Blockly.Flyout.prototype.init = function(targetWorkspace) {
308
+ this.targetWorkspace_ = targetWorkspace;
309
+ this.workspace_.targetWorkspace = targetWorkspace;
310
+ // Add scrollbar.
311
+ this.scrollbar_ = new Blockly.Scrollbar(this.workspace_,
312
+ this.horizontalLayout_, false, 'blocklyFlyoutScrollbar');
313
+
314
+ this.position();
315
+
316
+ Array.prototype.push.apply(this.eventWrappers_,
317
+ Blockly.bindEventWithChecks_(this.svgGroup_, 'wheel', this, this.wheel_));
318
+ // Dragging the flyout up and down (or left and right).
319
+ Array.prototype.push.apply(this.eventWrappers_,
320
+ Blockly.bindEventWithChecks_(
321
+ this.svgGroup_, 'mousedown', this, this.onMouseDown_));
322
+
323
+ // A flyout connected to a workspace doesn't have its own current gesture.
324
+ this.workspace_.getGesture =
325
+ this.targetWorkspace_.getGesture.bind(this.targetWorkspace_);
326
+
327
+ // Get variables from the main workspace rather than the target workspace.
328
+ this.workspace_.variableMap_ = this.targetWorkspace_.getVariableMap();
329
+
330
+ this.workspace_.createPotentialVariableMap();
331
+ };
332
+
333
+ /**
334
+ * Dispose of this flyout.
335
+ * Unlink from all DOM elements to prevent memory leaks.
336
+ */
337
+ Blockly.Flyout.prototype.dispose = function() {
338
+ this.hide();
339
+ Blockly.unbindEvent_(this.eventWrappers_);
340
+ if (this.scrollbar_) {
341
+ this.scrollbar_.dispose();
342
+ this.scrollbar_ = null;
343
+ }
344
+ if (this.workspace_) {
345
+ this.workspace_.targetWorkspace = null;
346
+ this.workspace_.dispose();
347
+ this.workspace_ = null;
348
+ }
349
+ if (this.svgGroup_) {
350
+ goog.dom.removeNode(this.svgGroup_);
351
+ this.svgGroup_ = null;
352
+ }
353
+ this.parentToolbox_ = null;
354
+ this.svgBackground_ = null;
355
+ this.targetWorkspace_ = null;
356
+ };
357
+
358
+ /**
359
+ * Set the parent toolbox of this flyout.
360
+ * @param {!Blockly.Toolbox} toolbox The toolbox that owns this flyout.
361
+ */
362
+ Blockly.Flyout.prototype.setParentToolbox = function(toolbox) {
363
+ this.parentToolbox_ = toolbox;
364
+ };
365
+
366
+ /**
367
+ * Get the width of the flyout.
368
+ * @return {number} The width of the flyout.
369
+ */
370
+ Blockly.Flyout.prototype.getWidth = function() {
371
+ return this.DEFAULT_WIDTH;
372
+ };
373
+
374
+ /**
375
+ * Get the height of the flyout.
376
+ * @return {number} The width of the flyout.
377
+ */
378
+ Blockly.Flyout.prototype.getHeight = function() {
379
+ return this.height_;
380
+ };
381
+
382
+ /**
383
+ * Get the workspace inside the flyout.
384
+ * @return {!Blockly.WorkspaceSvg} The workspace inside the flyout.
385
+ * @package
386
+ */
387
+ Blockly.Flyout.prototype.getWorkspace = function() {
388
+ return this.workspace_;
389
+ };
390
+
391
+ /**
392
+ * Is the flyout visible?
393
+ * @return {boolean} True if visible.
394
+ */
395
+ Blockly.Flyout.prototype.isVisible = function() {
396
+ return this.isVisible_;
397
+ };
398
+
399
+ /**
400
+ * Set whether the flyout is visible. A value of true does not necessarily mean
401
+ * that the flyout is shown. It could be hidden because its container is hidden.
402
+ * @param {boolean} visible True if visible.
403
+ */
404
+ Blockly.Flyout.prototype.setVisible = function(visible) {
405
+ var visibilityChanged = (visible != this.isVisible());
406
+
407
+ this.isVisible_ = visible;
408
+ if (visibilityChanged) {
409
+ this.updateDisplay_();
410
+ }
411
+ };
412
+
413
+ /**
414
+ * Set whether this flyout's container is visible.
415
+ * @param {boolean} visible Whether the container is visible.
416
+ */
417
+ Blockly.Flyout.prototype.setContainerVisible = function(visible) {
418
+ var visibilityChanged = (visible != this.containerVisible_);
419
+ this.containerVisible_ = visible;
420
+ if (visibilityChanged) {
421
+ this.updateDisplay_();
422
+ }
423
+ };
424
+
425
+ /**
426
+ * Update the display property of the flyout based whether it thinks it should
427
+ * be visible and whether its containing workspace is visible.
428
+ * @private
429
+ */
430
+ Blockly.Flyout.prototype.updateDisplay_ = function() {
431
+ var show = true;
432
+ if (!this.containerVisible_) {
433
+ show = false;
434
+ } else {
435
+ show = this.isVisible();
436
+ }
437
+ this.svgGroup_.style.display = show ? 'block' : 'none';
438
+ // Update the scrollbar's visiblity too since it should mimic the
439
+ // flyout's visibility.
440
+ this.scrollbar_.setContainerVisible(show);
441
+ };
442
+
443
+ /**
444
+ * Hide and empty the flyout.
445
+ */
446
+ Blockly.Flyout.prototype.hide = function() {
447
+ if (!this.isVisible()) {
448
+ return;
449
+ }
450
+ this.setVisible(false);
451
+ // Delete all the event listeners.
452
+ for (var x = 0, listen; listen = this.listeners_[x]; x++) {
453
+ Blockly.unbindEvent_(listen);
454
+ }
455
+ this.listeners_.length = 0;
456
+ if (this.reflowWrapper_) {
457
+ this.workspace_.removeChangeListener(this.reflowWrapper_);
458
+ this.reflowWrapper_ = null;
459
+ }
460
+ // Do NOT delete the blocks here. Wait until Flyout.show.
461
+ // https://neil.fraser.name/news/2014/08/09/
462
+ };
463
+
464
+ /**
465
+ * Show and populate the flyout.
466
+ * @param {!Array|string} xmlList List of blocks to show.
467
+ * Variables and procedures have a custom set of blocks.
468
+ */
469
+ Blockly.Flyout.prototype.show = function(xmlList) {
470
+ this.workspace_.setResizesEnabled(false);
471
+ this.hide();
472
+ this.clearOldBlocks_();
473
+
474
+ this.setVisible(true);
475
+ // Create the blocks to be shown in this flyout.
476
+ var contents = [];
477
+ var gaps = [];
478
+ this.permanentlyDisabled_.length = 0;
479
+ for (var i = 0, xml; xml = xmlList[i]; i++) {
480
+ // Handle dynamic categories, represented by a name instead of a list of XML.
481
+ // Look up the correct category generation function and call that to get a
482
+ // valid XML list.
483
+ if (typeof xml === 'string') {
484
+ var fnToApply = this.workspace_.targetWorkspace.getToolboxCategoryCallback(
485
+ xmlList[i]);
486
+ var newList = fnToApply(this.workspace_.targetWorkspace);
487
+ // Insert the new list of blocks in the middle of the list.
488
+ // We use splice to insert at index i, and remove a single element
489
+ // (the placeholder string). Because the spread operator (...) is not
490
+ // available, use apply and concat the array.
491
+ xmlList.splice.apply(xmlList, [i, 1].concat(newList));
492
+ xml = xmlList[i];
493
+ }
494
+ if (xml.tagName) {
495
+ var tagName = xml.tagName.toUpperCase();
496
+ var default_gap = this.horizontalLayout_ ? this.GAP_X : this.GAP_Y;
497
+ if (tagName == 'BLOCK') {
498
+
499
+ // We assume that in a flyout, the same block id (or type if missing id) means
500
+ // the same output BlockSVG.
501
+
502
+ // Look for a block that matches the id or type, our createBlock will assign
503
+ // id = type if none existed.
504
+ var id = xml.getAttribute('id') || xml.getAttribute('type');
505
+ var recycled = this.recycleBlocks_.findIndex(function(block) {
506
+ return block.id === id;
507
+ });
508
+
509
+
510
+ // If we found a recycled item, reuse the BlockSVG from last time.
511
+ // Otherwise, convert the XML block to a BlockSVG.
512
+ var curBlock;
513
+ if (recycled > -1) {
514
+ curBlock = this.recycleBlocks_.splice(recycled, 1)[0];
515
+ } else {
516
+ curBlock = Blockly.Xml.domToBlock(xml, this.workspace_);
517
+ }
518
+
519
+ if (curBlock.disabled) {
520
+ // Record blocks that were initially disabled.
521
+ // Do not enable these blocks as a result of capacity filtering.
522
+ this.permanentlyDisabled_.push(curBlock);
523
+ }
524
+ contents.push({type: 'block', block: curBlock});
525
+ var gap = parseInt(xml.getAttribute('gap'), 10);
526
+ gaps.push(isNaN(gap) ? default_gap : gap);
527
+ } else if (xml.tagName.toUpperCase() == 'SEP') {
528
+ // Change the gap between two blocks.
529
+ // <sep gap="36"></sep>
530
+ // The default gap is 24, can be set larger or smaller.
531
+ // This overwrites the gap attribute on the previous block.
532
+ // Note that a deprecated method is to add a gap to a block.
533
+ // <block type="math_arithmetic" gap="8"></block>
534
+ var newGap = parseInt(xml.getAttribute('gap'), 10);
535
+ // Ignore gaps before the first block.
536
+ if (!isNaN(newGap) && gaps.length > 0) {
537
+ gaps[gaps.length - 1] = newGap;
538
+ } else {
539
+ gaps.push(default_gap);
540
+ }
541
+ } else if ((tagName == 'LABEL') && (xml.getAttribute('showStatusButton') == 'true')) {
542
+ var curButton = new Blockly.FlyoutExtensionCategoryHeader(this.workspace_,
543
+ this.targetWorkspace_, xml);
544
+ contents.push({type: 'button', button: curButton});
545
+ gaps.push(default_gap);
546
+ } else if (tagName == 'BUTTON' || tagName == 'LABEL') {
547
+ // Labels behave the same as buttons, but are styled differently.
548
+ var isLabel = tagName == 'LABEL';
549
+ var curButton = new Blockly.FlyoutButton(this.workspace_,
550
+ this.targetWorkspace_, xml, isLabel);
551
+ contents.push({type: 'button', button: curButton});
552
+ gaps.push(default_gap);
553
+ }
554
+ }
555
+ }
556
+
557
+ this.emptyRecycleBlocks_();
558
+
559
+ this.layout_(contents, gaps);
560
+
561
+ // IE 11 is an incompetent browser that fails to fire mouseout events.
562
+ // When the mouse is over the background, deselect all blocks.
563
+ var deselectAll = function() {
564
+ var topBlocks = this.workspace_.getTopBlocks(false);
565
+ for (var i = 0, block; block = topBlocks[i]; i++) {
566
+ block.removeSelect();
567
+ }
568
+ };
569
+
570
+ this.listeners_.push(Blockly.bindEvent_(this.svgBackground_, 'mouseover',
571
+ this, deselectAll));
572
+
573
+ this.workspace_.setResizesEnabled(true);
574
+ this.reflow();
575
+
576
+ // Correctly position the flyout's scrollbar when it opens.
577
+ this.position();
578
+
579
+ this.reflowWrapper_ = this.reflow.bind(this);
580
+ this.workspace_.addChangeListener(this.reflowWrapper_);
581
+
582
+ this.recordCategoryScrollPositions_();
583
+ };
584
+
585
+ /**
586
+ * Empty out the recycled blocks, properly destroying everything.
587
+ * @private
588
+ */
589
+ Blockly.Flyout.prototype.emptyRecycleBlocks_ = function() {
590
+ // Clean out the old recycle bin.
591
+ var oldBlocks = this.recycleBlocks_;
592
+ this.recycleBlocks_ = [];
593
+ for (var i = 0; i < oldBlocks.length; i++) {
594
+ oldBlocks[i].dispose(false, false);
595
+ }
596
+ };
597
+
598
+ /**
599
+ * Store an array of category names, ids, scrollbar positions, and category lengths.
600
+ * This is used when scrolling the flyout to cause a category to be selected.
601
+ * @private
602
+ */
603
+ Blockly.Flyout.prototype.recordCategoryScrollPositions_ = function() {
604
+ this.categoryScrollPositions = [];
605
+ // Record category names and positions using the text label at the top of each one.
606
+ for (var i = 0; i < this.buttons_.length; i++) {
607
+ if (this.buttons_[i].getIsCategoryLabel()) {
608
+ var categoryLabel = this.buttons_[i];
609
+ this.categoryScrollPositions.push({
610
+ categoryName: categoryLabel.getText(),
611
+ position: this.horizontalLayout_ ?
612
+ categoryLabel.getPosition().x : categoryLabel.getPosition().y
613
+ });
614
+ }
615
+ }
616
+ // Record the length of each category, setting the final one to 0.
617
+ var numCategories = this.categoryScrollPositions.length;
618
+ if (numCategories > 0) {
619
+ for (var i = 0; i < numCategories - 1; i++) {
620
+ var currentPos = this.categoryScrollPositions[i].position;
621
+ var nextPos = this.categoryScrollPositions[i + 1].position;
622
+ var length = nextPos - currentPos;
623
+ this.categoryScrollPositions[i].length = length;
624
+ }
625
+ this.categoryScrollPositions[numCategories - 1].length = 0;
626
+ // Record the id of each category.
627
+ for (var i = 0; i < numCategories; i++) {
628
+ var category = this.parentToolbox_.getCategoryByIndex(i);
629
+ if (category && category.id_) {
630
+ this.categoryScrollPositions[i].categoryId = category.id_;
631
+ }
632
+ }
633
+ }
634
+ };
635
+
636
+ /**
637
+ * Select a category using the scroll position.
638
+ * @param {number} pos The scroll position in pixels.
639
+ * @package
640
+ */
641
+ Blockly.Flyout.prototype.selectCategoryByScrollPosition = function(pos) {
642
+ // If we are currently auto-scrolling, due to selecting a category by clicking on it,
643
+ // do not update the category selection.
644
+ if (this.scrollTarget) {
645
+ return;
646
+ }
647
+ var workspacePos = Math.round(pos / this.workspace_.scale);
648
+ // Traverse the array of scroll positions in reverse, so we can select the furthest
649
+ // category that the scroll position is beyond.
650
+ for (var i = this.categoryScrollPositions.length - 1; i >= 0; i--) {
651
+ if (workspacePos >= this.categoryScrollPositions[i].position) {
652
+ this.parentToolbox_.selectCategoryById(this.categoryScrollPositions[i].categoryId);
653
+ return;
654
+ }
655
+ }
656
+ };
657
+
658
+ Blockly.Flyout.prototype.startScrollAnimation = function() {
659
+ this.scrollTime = -1;
660
+ this.scrollStart = this.horizontalLayout_ ? -this.workspace_.scrollX : -this.workspace_.scrollY;
661
+ requestAnimationFrame(this.stepScrollAnimation.bind(this));
662
+ };
663
+
664
+ /**
665
+ * Step the scrolling animation by scrolling a fraction of the way to
666
+ * a scroll target, and request the next frame if necessary.
667
+ * @param {number} time Time in milliseconds
668
+ * @package
669
+ */
670
+ Blockly.Flyout.prototype.stepScrollAnimation = function(time) {
671
+ if (!this.scrollTarget) {
672
+ return;
673
+ }
674
+ if (this.scrollTime === -1) {
675
+ this.scrollTime = time;
676
+ }
677
+ var animationTime = (time - this.scrollTime) / 60 + 1;
678
+ var totalDistance = this.scrollTarget - this.scrollStart;
679
+ var scrollPos = this.scrollTarget - totalDistance * Math.pow(this.scrollAnimationFraction, animationTime);
680
+ var diff = this.scrollTarget - scrollPos;
681
+ if (Math.abs(diff) < 1) {
682
+ this.scrollbar_.set(this.scrollTarget);
683
+ this.scrollTarget = null;
684
+ return;
685
+ }
686
+ this.scrollbar_.set(scrollPos);
687
+
688
+ // Polyfilled by goog.dom.animationFrame.polyfill
689
+ requestAnimationFrame(this.stepScrollAnimation.bind(this));
690
+ };
691
+
692
+ /**
693
+ * Get the scaled scroll position.
694
+ * @return {number} The current scroll position.
695
+ */
696
+ Blockly.Flyout.prototype.getScrollPos = function() {
697
+ var pos = this.horizontalLayout_ ?
698
+ -this.workspace_.scrollX : -this.workspace_.scrollY;
699
+ return pos / this.workspace_.scale;
700
+ };
701
+
702
+ /**
703
+ * Set the scroll position, scaling it.
704
+ * @param {number} pos The scroll position to set.
705
+ */
706
+ Blockly.Flyout.prototype.setScrollPos = function(pos) {
707
+ this.scrollbar_.set(pos * this.workspace_.scale);
708
+ };
709
+
710
+ /**
711
+ * Set whether the flyout can recycle blocks. A value of true allows blocks to be recycled.
712
+ * @param {boolean} recycle True if recycling is possible.
713
+ */
714
+ Blockly.Flyout.prototype.setRecyclingEnabled = function(recycle) {
715
+ this.recyclingEnabled_ = recycle;
716
+ };
717
+
718
+ /**
719
+ * Delete blocks and background buttons from a previous showing of the flyout.
720
+ * @private
721
+ */
722
+ Blockly.Flyout.prototype.clearOldBlocks_ = function() {
723
+ // Delete any blocks from a previous showing.
724
+ var oldBlocks = this.workspace_.getTopBlocks(false);
725
+ for (var i = 0, block; block = oldBlocks[i]; i++) {
726
+ if (block.workspace == this.workspace_) {
727
+ if (this.recyclingEnabled_ &&
728
+ Blockly.scratchBlocksUtils.blockIsRecyclable(block)) {
729
+ this.recycleBlock_(block);
730
+ } else {
731
+ block.dispose(false, false);
732
+ }
733
+ }
734
+ }
735
+ // Delete any background buttons from a previous showing.
736
+ for (var j = 0; j < this.backgroundButtons_.length; j++) {
737
+ var rect = this.backgroundButtons_[j];
738
+ if (rect) goog.dom.removeNode(rect);
739
+ }
740
+ this.backgroundButtons_.length = 0;
741
+
742
+ for (var i = 0, button; button = this.buttons_[i]; i++) {
743
+ button.dispose();
744
+ }
745
+ this.buttons_.length = 0;
746
+
747
+ // Clear potential variables from the previous showing.
748
+ this.workspace_.getPotentialVariableMap().clear();
749
+ };
750
+
751
+ /**
752
+ * Add listeners to a block that has been added to the flyout.
753
+ * @param {!Element} root The root node of the SVG group the block is in.
754
+ * @param {!Blockly.Block} block The block to add listeners for.
755
+ * @param {!Element} rect The invisible rectangle under the block that acts as
756
+ * a button for that block.
757
+ * @private
758
+ */
759
+ Blockly.Flyout.prototype.addBlockListeners_ = function(root, block, rect) {
760
+ this.listeners_.push(Blockly.bindEventWithChecks_(root, 'mousedown', null,
761
+ this.blockMouseDown_(block)));
762
+ this.listeners_.push(Blockly.bindEventWithChecks_(rect, 'mousedown', null,
763
+ this.blockMouseDown_(block)));
764
+ this.listeners_.push(Blockly.bindEvent_(root, 'mouseover', block,
765
+ block.addSelect));
766
+ this.listeners_.push(Blockly.bindEvent_(root, 'mouseout', block,
767
+ block.removeSelect));
768
+ this.listeners_.push(Blockly.bindEvent_(rect, 'mouseover', block,
769
+ block.addSelect));
770
+ this.listeners_.push(Blockly.bindEvent_(rect, 'mouseout', block,
771
+ block.removeSelect));
772
+ };
773
+
774
+ /**
775
+ * Handle a mouse-down on an SVG block in a non-closing flyout.
776
+ * @param {!Blockly.Block} block The flyout block to copy.
777
+ * @return {!Function} Function to call when block is clicked.
778
+ * @private
779
+ */
780
+ Blockly.Flyout.prototype.blockMouseDown_ = function(block) {
781
+ var flyout = this;
782
+ return function(e) {
783
+ var gesture = flyout.targetWorkspace_.getGesture(e);
784
+ if (gesture) {
785
+ gesture.setStartBlock(block);
786
+ gesture.handleFlyoutStart(e, flyout);
787
+ }
788
+ };
789
+ };
790
+
791
+ /**
792
+ * Mouse down on the flyout background. Start a scroll drag.
793
+ * @param {!Event} e Mouse down event.
794
+ * @private
795
+ */
796
+ Blockly.Flyout.prototype.onMouseDown_ = function(e) {
797
+ var gesture = this.targetWorkspace_.getGesture(e);
798
+ if (gesture) {
799
+ gesture.handleFlyoutStart(e, this);
800
+ }
801
+ this.scrollTarget = null;
802
+ };
803
+
804
+ /**
805
+ * Create a copy of this block on the workspace.
806
+ * @param {!Blockly.BlockSvg} originalBlock The block to copy from the flyout.
807
+ * @return {Blockly.BlockSvg} The newly created block, or null if something
808
+ * went wrong with deserialization.
809
+ * @package
810
+ */
811
+ Blockly.Flyout.prototype.createBlock = function(originalBlock) {
812
+ var newBlock = null;
813
+ Blockly.Events.disable();
814
+ var variablesBeforeCreation = this.targetWorkspace_.getAllVariables();
815
+ this.targetWorkspace_.setResizesEnabled(false);
816
+ try {
817
+ newBlock = this.placeNewBlock_(originalBlock);
818
+ // Close the flyout.
819
+ Blockly.hideChaff();
820
+ } finally {
821
+ Blockly.Events.enable();
822
+ }
823
+
824
+ var newVariables = Blockly.Variables.getAddedVariables(this.targetWorkspace_,
825
+ variablesBeforeCreation);
826
+
827
+ if (Blockly.Events.isEnabled()) {
828
+ Blockly.Events.setGroup(true);
829
+ Blockly.Events.fire(new Blockly.Events.Create(newBlock));
830
+ // Fire a VarCreate event for each (if any) new variable created.
831
+ for (var i = 0; i < newVariables.length; i++) {
832
+ var thisVariable = newVariables[i];
833
+ Blockly.Events.fire(new Blockly.Events.VarCreate(thisVariable));
834
+ }
835
+ }
836
+ if (this.autoClose) {
837
+ this.hide();
838
+ }
839
+ return newBlock;
840
+ };
841
+
842
+ /**
843
+ * Reflow blocks and their buttons.
844
+ */
845
+ Blockly.Flyout.prototype.reflow = function() {
846
+ if (this.reflowWrapper_) {
847
+ this.workspace_.removeChangeListener(this.reflowWrapper_);
848
+ }
849
+ var blocks = this.workspace_.getTopBlocks(false);
850
+ this.reflowInternal_(blocks);
851
+ if (this.reflowWrapper_) {
852
+ this.workspace_.addChangeListener(this.reflowWrapper_);
853
+ }
854
+ };
855
+
856
+ /**
857
+ * @return {boolean} True if this flyout may be scrolled with a scrollbar or by
858
+ * dragging.
859
+ * @package
860
+ */
861
+ Blockly.Flyout.prototype.isScrollable = function() {
862
+ return this.scrollbar_ ? this.scrollbar_.isVisible() : false;
863
+ };
864
+
865
+ /**
866
+ * Copy a block from the flyout to the workspace and position it correctly.
867
+ * @param {!Blockly.Block} oldBlock The flyout block to copy.
868
+ * @return {!Blockly.Block} The new block in the main workspace.
869
+ * @private
870
+ */
871
+ Blockly.Flyout.prototype.placeNewBlock_ = function(oldBlock) {
872
+ var targetWorkspace = this.targetWorkspace_;
873
+ var svgRootOld = oldBlock.getSvgRoot();
874
+ if (!svgRootOld) {
875
+ throw 'oldBlock is not rendered.';
876
+ }
877
+
878
+ // Create the new block by cloning the block in the flyout (via XML).
879
+ var xml = Blockly.Xml.blockToDom(oldBlock);
880
+ // The target workspace would normally resize during domToBlock, which will
881
+ // lead to weird jumps. Save it for terminateDrag.
882
+ targetWorkspace.setResizesEnabled(false);
883
+
884
+ // Using domToBlock instead of domToWorkspace means that the new block will be
885
+ // placed at position (0, 0) in main workspace units.
886
+ var block = Blockly.Xml.domToBlock(xml, targetWorkspace);
887
+ var svgRootNew = block.getSvgRoot();
888
+ if (!svgRootNew) {
889
+ throw 'block is not rendered.';
890
+ }
891
+
892
+ // The offset in pixels between the main workspace's origin and the upper left
893
+ // corner of the injection div.
894
+ var mainOffsetPixels = targetWorkspace.getOriginOffsetInPixels();
895
+
896
+ // The offset in pixels between the flyout workspace's origin and the upper
897
+ // left corner of the injection div.
898
+ var flyoutOffsetPixels = this.workspace_.getOriginOffsetInPixels();
899
+
900
+ // The position of the old block in flyout workspace coordinates.
901
+ var oldBlockPosWs = oldBlock.getRelativeToSurfaceXY();
902
+
903
+ // The position of the old block in pixels relative to the flyout
904
+ // workspace's origin.
905
+ var oldBlockPosPixels = oldBlockPosWs.scale(this.workspace_.scale);
906
+
907
+ // The position of the old block in pixels relative to the upper left corner
908
+ // of the injection div.
909
+ var oldBlockOffsetPixels = goog.math.Coordinate.sum(flyoutOffsetPixels,
910
+ oldBlockPosPixels);
911
+
912
+ // The position of the old block in pixels relative to the origin of the
913
+ // main workspace.
914
+ var finalOffsetPixels = goog.math.Coordinate.difference(oldBlockOffsetPixels,
915
+ mainOffsetPixels);
916
+
917
+ // The position of the old block in main workspace coordinates.
918
+ var finalOffsetMainWs = finalOffsetPixels.scale(1 / targetWorkspace.scale);
919
+
920
+ block.moveBy(finalOffsetMainWs.x, finalOffsetMainWs.y);
921
+ return block;
922
+ };
923
+
924
+ /**
925
+ * Put a previously created block into the recycle bin, used during large
926
+ * workspace swaps to limit the number of new dom elements we need to create
927
+ *
928
+ * @param {!Blockly.BlockSvg} block The block to recycle.
929
+ * @private
930
+ */
931
+ Blockly.Flyout.prototype.recycleBlock_ = function(block) {
932
+ var xy = block.getRelativeToSurfaceXY();
933
+ block.moveBy(-xy.x, -xy.y);
934
+ this.recycleBlocks_.push(block);
935
+ };