@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.
- package/.nvmrc +1 -0
- package/LICENSE +674 -0
- package/README.md +57 -0
- package/TRADEMARK +1 -0
- package/blockly_compressed_horizontal.js +2268 -0
- package/blockly_compressed_vertical.js +2296 -0
- package/blockly_uncompressed_horizontal.js +1212 -0
- package/blockly_uncompressed_vertical.js +1212 -0
- package/blocks_common/colour.js +61 -0
- package/blocks_common/math.js +159 -0
- package/blocks_common/matrix.js +54 -0
- package/blocks_common/note.js +58 -0
- package/blocks_common/text.js +57 -0
- package/blocks_compressed.js +37 -0
- package/blocks_compressed_horizontal.js +68 -0
- package/blocks_compressed_vertical.js +216 -0
- package/blocks_horizontal/control.js +212 -0
- package/blocks_horizontal/default_toolbox.js +139 -0
- package/blocks_horizontal/event.js +190 -0
- package/blocks_horizontal/wedo.js +325 -0
- package/blocks_vertical/control.js +532 -0
- package/blocks_vertical/data.js +666 -0
- package/blocks_vertical/default_toolbox.js +571 -0
- package/blocks_vertical/event.js +329 -0
- package/blocks_vertical/extensions.js +294 -0
- package/blocks_vertical/looks.js +591 -0
- package/blocks_vertical/motion.js +587 -0
- package/blocks_vertical/operators.js +470 -0
- package/blocks_vertical/procedures.js +1051 -0
- package/blocks_vertical/sensing.js +555 -0
- package/blocks_vertical/sound.js +246 -0
- package/blocks_vertical/vertical_extensions.js +289 -0
- package/build/gen_blocks.js +1 -0
- package/build/test_expect.js +1 -0
- package/build/test_input.js +1 -0
- package/build.py +647 -0
- package/cleanup.sh +101 -0
- package/core/block.js +1837 -0
- package/core/block_animations.js +107 -0
- package/core/block_drag_surface.js +299 -0
- package/core/block_dragger.js +424 -0
- package/core/block_events.js +531 -0
- package/core/block_render_svg_horizontal.js +892 -0
- package/core/block_render_svg_vertical.js +1734 -0
- package/core/block_svg.js +1355 -0
- package/core/blockly.js +620 -0
- package/core/blocks.js +37 -0
- package/core/bubble.js +664 -0
- package/core/bubble_dragger.js +285 -0
- package/core/colours.js +177 -0
- package/core/comment.js +293 -0
- package/core/comment_events.js +539 -0
- package/core/connection.js +776 -0
- package/core/connection_db.js +300 -0
- package/core/constants.js +408 -0
- package/core/contextmenu.js +534 -0
- package/core/css.js +1354 -0
- package/core/data_category.js +490 -0
- package/core/dragged_connection_manager.js +260 -0
- package/core/dropdowndiv.js +408 -0
- package/core/events.js +429 -0
- package/core/events_abstract.js +113 -0
- package/core/extensions.js +450 -0
- package/core/field.js +810 -0
- package/core/field_angle.js +410 -0
- package/core/field_checkbox.js +133 -0
- package/core/field_colour.js +253 -0
- package/core/field_colour_slider.js +387 -0
- package/core/field_date.js +353 -0
- package/core/field_dropdown.js +447 -0
- package/core/field_iconmenu.js +309 -0
- package/core/field_image.js +200 -0
- package/core/field_label.js +136 -0
- package/core/field_label_serializable.js +125 -0
- package/core/field_matrix.js +566 -0
- package/core/field_note.js +850 -0
- package/core/field_number.js +366 -0
- package/core/field_numberdropdown.js +77 -0
- package/core/field_textdropdown.js +164 -0
- package/core/field_textinput.js +675 -0
- package/core/field_textinput_removable.js +105 -0
- package/core/field_variable.js +385 -0
- package/core/field_variable_getter.js +185 -0
- package/core/field_vertical_separator.js +161 -0
- package/core/flyout_base.js +935 -0
- package/core/flyout_button.js +324 -0
- package/core/flyout_dragger.js +83 -0
- package/core/flyout_extension_category_header.js +159 -0
- package/core/flyout_horizontal.js +475 -0
- package/core/flyout_vertical.js +770 -0
- package/core/generator.js +426 -0
- package/core/gesture.js +1010 -0
- package/core/grid.js +227 -0
- package/core/icon.js +205 -0
- package/core/inject.js +496 -0
- package/core/input.js +285 -0
- package/core/insertion_marker_manager.js +678 -0
- package/core/intersection_observer.js +102 -0
- package/core/msg.js +62 -0
- package/core/mutator.js +426 -0
- package/core/names.js +198 -0
- package/core/options.js +244 -0
- package/core/procedures.js +739 -0
- package/core/rendered_connection.js +417 -0
- package/core/scratch_block_comment.js +646 -0
- package/core/scratch_blocks_utils.js +246 -0
- package/core/scratch_bubble.js +699 -0
- package/core/scratch_events.js +131 -0
- package/core/scratch_msgs.js +85 -0
- package/core/scrollbar.js +875 -0
- package/core/toolbox.js +803 -0
- package/core/tooltip.js +337 -0
- package/core/touch.js +226 -0
- package/core/trashcan.js +343 -0
- package/core/ui_events.js +91 -0
- package/core/ui_menu_utils.js +68 -0
- package/core/utils.js +825 -0
- package/core/variable_events.js +259 -0
- package/core/variable_map.js +415 -0
- package/core/variable_model.js +116 -0
- package/core/variables.js +674 -0
- package/core/warning.js +199 -0
- package/core/widgetdiv.js +344 -0
- package/core/workspace.js +673 -0
- package/core/workspace_audio.js +170 -0
- package/core/workspace_comment.js +426 -0
- package/core/workspace_comment_render_svg.js +706 -0
- package/core/workspace_comment_svg.js +609 -0
- package/core/workspace_drag_surface_svg.js +195 -0
- package/core/workspace_dragger.js +132 -0
- package/core/workspace_svg.js +2382 -0
- package/core/xml.js +930 -0
- package/core/zoom_controls.js +301 -0
- package/dist/horizontal.js +222 -0
- package/dist/vertical.js +222 -0
- package/dist/web/horizontal.js +232 -0
- package/dist/web/vertical.js +232 -0
- package/i18n/common.py +234 -0
- package/i18n/create_messages.py +162 -0
- package/i18n/create_scratch_msgs.js +69 -0
- package/i18n/dedup_json.py +73 -0
- package/i18n/js_to_json.js +46 -0
- package/i18n/js_to_json.py +136 -0
- package/i18n/json_to_js.js +52 -0
- package/i18n/json_to_js.py +185 -0
- package/i18n/sync_tx_translations.js +111 -0
- package/i18n/test_scratch_msgs.js +87 -0
- package/i18n/tests.py +47 -0
- package/i18n/xliff_to_json.py +232 -0
- package/local_build.sh +70 -0
- package/media/click.mp3 +0 -0
- package/media/click.ogg +0 -0
- package/media/click.wav +0 -0
- package/media/comment-arrow-down.svg +10 -0
- package/media/comment-arrow-up.svg +10 -0
- package/media/delete-x.svg +10 -0
- package/media/delete.mp3 +0 -0
- package/media/delete.ogg +0 -0
- package/media/delete.wav +0 -0
- package/media/dropdown-arrow-dark.svg +1 -0
- package/media/dropdown-arrow.svg +1 -0
- package/media/extensions/microbit-block-icon.svg +130 -0
- package/media/extensions/music-block-icon.svg +17 -0
- package/media/extensions/pen-block-icon.svg +19 -0
- package/media/extensions/wedo2-block-icon.svg +36 -0
- package/media/eyedropper.svg +22 -0
- package/media/green-flag.svg +17 -0
- package/media/handclosed.cur +0 -0
- package/media/handdelete.cur +0 -0
- package/media/handopen.cur +0 -0
- package/media/icons/arrow.svg +12 -0
- package/media/icons/arrow_button.svg +1 -0
- package/media/icons/control_forever.svg +1 -0
- package/media/icons/control_repeat.svg +1 -0
- package/media/icons/control_stop.svg +1 -0
- package/media/icons/control_wait.svg +1 -0
- package/media/icons/event_broadcast_blue.svg +1 -0
- package/media/icons/event_broadcast_coral.svg +1 -0
- package/media/icons/event_broadcast_green.svg +1 -0
- package/media/icons/event_broadcast_magenta.svg +1 -0
- package/media/icons/event_broadcast_orange.svg +1 -0
- package/media/icons/event_broadcast_purple.svg +1 -0
- package/media/icons/event_when-broadcast-received_blue.svg +1 -0
- package/media/icons/event_when-broadcast-received_coral.svg +1 -0
- package/media/icons/event_when-broadcast-received_green.svg +1 -0
- package/media/icons/event_when-broadcast-received_magenta.svg +1 -0
- package/media/icons/event_when-broadcast-received_orange.svg +1 -0
- package/media/icons/event_when-broadcast-received_purple.svg +1 -0
- package/media/icons/event_whenflagclicked.svg +1 -0
- package/media/icons/remove.svg +19 -0
- package/media/icons/set-led_blue.svg +1 -0
- package/media/icons/set-led_coral.svg +1 -0
- package/media/icons/set-led_green.svg +1 -0
- package/media/icons/set-led_magenta.svg +1 -0
- package/media/icons/set-led_mystery.svg +1 -0
- package/media/icons/set-led_orange.svg +1 -0
- package/media/icons/set-led_purple.svg +1 -0
- package/media/icons/set-led_white.svg +1 -0
- package/media/icons/set-led_yellow.svg +1 -0
- package/media/icons/wedo_motor-clockwise.svg +1 -0
- package/media/icons/wedo_motor-counterclockwise.svg +1 -0
- package/media/icons/wedo_motor-speed_fast.svg +1 -0
- package/media/icons/wedo_motor-speed_med.svg +1 -0
- package/media/icons/wedo_motor-speed_slow.svg +1 -0
- package/media/icons/wedo_when-distance_close.svg +1 -0
- package/media/icons/wedo_when-tilt-backward.svg +1 -0
- package/media/icons/wedo_when-tilt-forward.svg +1 -0
- package/media/icons/wedo_when-tilt-left.svg +1 -0
- package/media/icons/wedo_when-tilt-right.svg +1 -0
- package/media/icons/wedo_when-tilt.svg +1 -0
- package/media/repeat.svg +18 -0
- package/media/rotate-left.svg +1 -0
- package/media/rotate-right.svg +1 -0
- package/media/sprites.png +0 -0
- package/media/status-not-ready.svg +13 -0
- package/media/status-ready.svg +13 -0
- package/media/zoom-in.svg +1 -0
- package/media/zoom-out.svg +1 -0
- package/media/zoom-reset.svg +1 -0
- package/msg/js/en.js +290 -0
- package/msg/json/en.json +285 -0
- package/msg/messages.js +359 -0
- package/msg/scratch_msgs.js +22969 -0
- package/package.json +60 -0
- package/pull_from_blockly.sh +151 -0
- package/renovate.json5 +15 -0
- package/shim/blockly_compressed_horizontal-blocks_compressed.js +1 -0
- package/shim/blockly_compressed_horizontal.Blockly.js +1 -0
- package/shim/blockly_compressed_horizontal.goog.js +1 -0
- package/shim/blockly_compressed_horizontal.js +1 -0
- package/shim/blockly_compressed_vertical-blocks_compressed.js +1 -0
- package/shim/blockly_compressed_vertical.Blockly.js +1 -0
- package/shim/blockly_compressed_vertical.goog.js +1 -0
- package/shim/blockly_compressed_vertical.js +1 -0
- package/shim/blocks_compressed_horizontal-blockly_compressed_horizontal-messages.js +1 -0
- package/shim/blocks_compressed_horizontal.js +1 -0
- package/shim/blocks_compressed_vertical-blockly_compressed_vertical-messages.js +1 -0
- package/shim/blocks_compressed_vertical.js +1 -0
- package/shim/gh-pages.js +1 -0
- package/shim/horizontal.js +1 -0
- package/shim/index.js +17 -0
- package/shim/vertical.js +1 -0
- package/universal-python.js +82 -0
|
@@ -0,0 +1,673 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Visual Blocks Editor
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2012 Google Inc.
|
|
6
|
+
* https://developers.google.com/blockly/
|
|
7
|
+
*
|
|
8
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9
|
+
* you may not use this file except in compliance with the License.
|
|
10
|
+
* You may obtain a copy of the License at
|
|
11
|
+
*
|
|
12
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
*
|
|
14
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
15
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17
|
+
* See the License for the specific language governing permissions and
|
|
18
|
+
* limitations under the License.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @fileoverview Object representing a workspace.
|
|
23
|
+
* @author fraser@google.com (Neil Fraser)
|
|
24
|
+
*/
|
|
25
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
goog.provide('Blockly.Workspace');
|
|
28
|
+
|
|
29
|
+
goog.require('Blockly.VariableMap');
|
|
30
|
+
goog.require('Blockly.WorkspaceComment');
|
|
31
|
+
goog.require('goog.array');
|
|
32
|
+
goog.require('goog.math');
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Class for a workspace. This is a data structure that contains blocks.
|
|
37
|
+
* There is no UI, and can be created headlessly.
|
|
38
|
+
* @param {!Blockly.Options=} opt_options Dictionary of options.
|
|
39
|
+
* @constructor
|
|
40
|
+
*/
|
|
41
|
+
Blockly.Workspace = function(opt_options) {
|
|
42
|
+
/** @type {string} */
|
|
43
|
+
this.id = Blockly.utils.genUid();
|
|
44
|
+
Blockly.Workspace.WorkspaceDB_[this.id] = this;
|
|
45
|
+
/** @type {!Blockly.Options} */
|
|
46
|
+
this.options = opt_options || {};
|
|
47
|
+
/** @type {boolean} */
|
|
48
|
+
this.RTL = !!this.options.RTL;
|
|
49
|
+
/** @type {boolean} */
|
|
50
|
+
this.horizontalLayout = !!this.options.horizontalLayout;
|
|
51
|
+
/** @type {number} */
|
|
52
|
+
this.toolboxPosition = this.options.toolboxPosition;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @type {!Array.<!Blockly.Block>}
|
|
56
|
+
* @private
|
|
57
|
+
*/
|
|
58
|
+
this.topBlocks_ = [];
|
|
59
|
+
/**
|
|
60
|
+
* @type {!Array.<!Blockly.WorkspaceComment>}
|
|
61
|
+
* @private
|
|
62
|
+
*/
|
|
63
|
+
this.topComments_ = [];
|
|
64
|
+
/**
|
|
65
|
+
* @type {!Object}
|
|
66
|
+
* @private
|
|
67
|
+
*/
|
|
68
|
+
this.commentDB_ = Object.create(null);
|
|
69
|
+
/**
|
|
70
|
+
* @type {!Array.<!Function>}
|
|
71
|
+
* @private
|
|
72
|
+
*/
|
|
73
|
+
this.listeners_ = [];
|
|
74
|
+
|
|
75
|
+
/** @type {!Array.<!Function>} */
|
|
76
|
+
this.tapListeners_ = [];
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @type {!Array.<!Blockly.Events.Abstract>}
|
|
80
|
+
* @protected
|
|
81
|
+
*/
|
|
82
|
+
this.undoStack_ = [];
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @type {!Array.<!Blockly.Events.Abstract>}
|
|
86
|
+
* @protected
|
|
87
|
+
*/
|
|
88
|
+
this.redoStack_ = [];
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @type {!Object}
|
|
92
|
+
* @private
|
|
93
|
+
*/
|
|
94
|
+
this.blockDB_ = Object.create(null);
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @type {!Blockly.VariableMap}
|
|
98
|
+
* A map from variable type to list of variable names. The lists contain all
|
|
99
|
+
* of the named variables in the workspace, including variables
|
|
100
|
+
* that are not currently in use.
|
|
101
|
+
* @private
|
|
102
|
+
*/
|
|
103
|
+
this.variableMap_ = new Blockly.VariableMap(this);
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Blocks in the flyout can refer to variables that don't exist in the main
|
|
107
|
+
* workspace. For instance, the "get item in list" block refers to an "item"
|
|
108
|
+
* variable regardless of whether the variable has been created yet.
|
|
109
|
+
* A FieldVariable must always refer to a Blockly.VariableModel. We reconcile
|
|
110
|
+
* these by tracking "potential" variables in the flyout. These variables
|
|
111
|
+
* become real when references to them are dragged into the main workspace.
|
|
112
|
+
* @type {!Blockly.VariableMap}
|
|
113
|
+
* @private
|
|
114
|
+
*/
|
|
115
|
+
this.potentialVariableMap_ = null;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Returns `true` if the workspace is visible and `false` if it's headless.
|
|
120
|
+
* @type {boolean}
|
|
121
|
+
*/
|
|
122
|
+
Blockly.Workspace.prototype.rendered = false;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Returns `true` if the workspace is currently in the process of a bulk clear.
|
|
126
|
+
* @type {boolean}
|
|
127
|
+
* @package
|
|
128
|
+
*/
|
|
129
|
+
Blockly.Workspace.prototype.isClearing = false;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Maximum number of undo events in stack. `0` turns off undo, `Infinity` sets it to unlimited.
|
|
133
|
+
* @type {number}
|
|
134
|
+
*/
|
|
135
|
+
Blockly.Workspace.prototype.MAX_UNDO = 1024;
|
|
136
|
+
|
|
137
|
+
// TODO (#1354) Update this function when it is fixed upstream
|
|
138
|
+
/**
|
|
139
|
+
* Refresh the toolbox. This is a no-op in a non-rendered workspace,
|
|
140
|
+
* but may be overriden by subclasses.
|
|
141
|
+
* @private
|
|
142
|
+
*/
|
|
143
|
+
Blockly.Workspace.prototype.refreshToolboxSelection_ = function() {
|
|
144
|
+
// No-op. Overriden by subclass.
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Dispose of this workspace.
|
|
149
|
+
* Unlink from all DOM elements to prevent memory leaks.
|
|
150
|
+
*/
|
|
151
|
+
Blockly.Workspace.prototype.dispose = function() {
|
|
152
|
+
this.listeners_.length = 0;
|
|
153
|
+
this.clear();
|
|
154
|
+
// Remove from workspace database.
|
|
155
|
+
delete Blockly.Workspace.WorkspaceDB_[this.id];
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Angle away from the horizontal to sweep for blocks. Order of execution is
|
|
160
|
+
* generally top to bottom, but a small angle changes the scan to give a bit of
|
|
161
|
+
* a left to right bias (reversed in RTL). Units are in degrees.
|
|
162
|
+
* See: http://tvtropes.org/pmwiki/pmwiki.php/Main/DiagonalBilling.
|
|
163
|
+
*/
|
|
164
|
+
Blockly.Workspace.SCAN_ANGLE = 3;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Add a block to the list of top blocks.
|
|
168
|
+
* @param {!Blockly.Block} block Block to add.
|
|
169
|
+
*/
|
|
170
|
+
Blockly.Workspace.prototype.addTopBlock = function(block) {
|
|
171
|
+
this.topBlocks_.push(block);
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Remove a block from the list of top blocks.
|
|
176
|
+
* @param {!Blockly.Block} block Block to remove.
|
|
177
|
+
*/
|
|
178
|
+
Blockly.Workspace.prototype.removeTopBlock = function(block) {
|
|
179
|
+
if (!goog.array.remove(this.topBlocks_, block)) {
|
|
180
|
+
throw 'Block not present in workspace\'s list of top-most blocks.';
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Finds the top-level blocks and returns them. Blocks are optionally sorted
|
|
186
|
+
* by position; top to bottom (with slight LTR or RTL bias).
|
|
187
|
+
* @param {boolean} ordered Sort the list if true.
|
|
188
|
+
* @return {!Array.<!Blockly.Block>} The top-level block objects.
|
|
189
|
+
*/
|
|
190
|
+
Blockly.Workspace.prototype.getTopBlocks = function(ordered) {
|
|
191
|
+
// Copy the topBlocks_ list.
|
|
192
|
+
var blocks = [].concat(this.topBlocks_);
|
|
193
|
+
if (ordered && blocks.length > 1) {
|
|
194
|
+
var offset = Math.sin(goog.math.toRadians(Blockly.Workspace.SCAN_ANGLE));
|
|
195
|
+
if (this.RTL) {
|
|
196
|
+
offset *= -1;
|
|
197
|
+
}
|
|
198
|
+
blocks.sort(function(a, b) {
|
|
199
|
+
var aXY = a.getRelativeToSurfaceXY();
|
|
200
|
+
var bXY = b.getRelativeToSurfaceXY();
|
|
201
|
+
return (aXY.y + offset * aXY.x) - (bXY.y + offset * bXY.x);
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
return blocks;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Add a comment to the list of top comments.
|
|
209
|
+
* @param {!Blockly.WorkspaceComment} comment comment to add.
|
|
210
|
+
* @package
|
|
211
|
+
*/
|
|
212
|
+
Blockly.Workspace.prototype.addTopComment = function(comment) {
|
|
213
|
+
this.topComments_.push(comment);
|
|
214
|
+
|
|
215
|
+
// Note: If the comment database starts to hold block comments, this may need
|
|
216
|
+
// to move to a separate function.
|
|
217
|
+
if (this.commentDB_[comment.id]) {
|
|
218
|
+
console.warn('Overriding an existing comment on this workspace, with id "' +
|
|
219
|
+
comment.id + '"');
|
|
220
|
+
}
|
|
221
|
+
this.commentDB_[comment.id] = comment;
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Remove a comment from the list of top comments.
|
|
226
|
+
* @param {!Blockly.WorkspaceComment} comment comment to remove.
|
|
227
|
+
* @package
|
|
228
|
+
*/
|
|
229
|
+
Blockly.Workspace.prototype.removeTopComment = function(comment) {
|
|
230
|
+
if (!goog.array.remove(this.topComments_, comment)) {
|
|
231
|
+
throw 'Comment not present in workspace\'s list of top-most comments.';
|
|
232
|
+
}
|
|
233
|
+
// Note: If the comment database starts to hold block comments, this may need
|
|
234
|
+
// to move to a separate function.
|
|
235
|
+
delete this.commentDB_[comment.id];
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Finds the top-level comments and returns them. Comments are optionally sorted
|
|
240
|
+
* by position; top to bottom (with slight LTR or RTL bias).
|
|
241
|
+
* @param {boolean} ordered Sort the list if true.
|
|
242
|
+
* @return {!Array.<!Blockly.WorkspaceComment>} The top-level comment objects.
|
|
243
|
+
* @package
|
|
244
|
+
*/
|
|
245
|
+
Blockly.Workspace.prototype.getTopComments = function(ordered) {
|
|
246
|
+
// Copy the topComments_ list.
|
|
247
|
+
var comments = [].concat(this.topComments_);
|
|
248
|
+
if (ordered && comments.length > 1) {
|
|
249
|
+
var offset = Math.sin(goog.math.toRadians(Blockly.Workspace.SCAN_ANGLE));
|
|
250
|
+
if (this.RTL) {
|
|
251
|
+
offset *= -1;
|
|
252
|
+
}
|
|
253
|
+
comments.sort(function(a, b) {
|
|
254
|
+
var aXY = a instanceof Blockly.ScratchBlockComment ? a.getXY() : a.getRelativeToSurfaceXY();
|
|
255
|
+
var bXY = b instanceof Blockly.ScratchBlockComment ? b.getXY() : b.getRelativeToSurfaceXY();
|
|
256
|
+
return (aXY.y + offset * aXY.x) - (bXY.y + offset * bXY.x);
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
return comments;
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Find all blocks in workspace. Blocks are optionally sorted
|
|
264
|
+
* by position; top to bottom (with slight LTR or RTL bias).
|
|
265
|
+
* @param {boolean} ordered Sort the list if true.
|
|
266
|
+
* @return {!Array.<!Blockly.Block>} Array of blocks.
|
|
267
|
+
*/
|
|
268
|
+
Blockly.Workspace.prototype.getAllBlocks = function(ordered) {
|
|
269
|
+
if (ordered) {
|
|
270
|
+
// Slow, but ordered.
|
|
271
|
+
// This gets all levels of descendants because getDescendants
|
|
272
|
+
// is called recuusively. They are added to a new list, not the
|
|
273
|
+
// list that it's iterating over.
|
|
274
|
+
var topBlocks = this.getTopBlocks(true);
|
|
275
|
+
var blocks = [];
|
|
276
|
+
for (var i = 0; i < topBlocks.length; i++) {
|
|
277
|
+
blocks.push.apply(blocks, topBlocks[i].getDescendants(true));
|
|
278
|
+
}
|
|
279
|
+
} else {
|
|
280
|
+
// Fast, but in no particular order.
|
|
281
|
+
// This gets all of levels of descendants by always adding to the
|
|
282
|
+
// list that it's iterating over.
|
|
283
|
+
var blocks = this.getTopBlocks(false);
|
|
284
|
+
for (var i = 0; i < blocks.length; i++) {
|
|
285
|
+
blocks.push.apply(blocks, blocks[i].getChildren(false));
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return blocks;
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Dispose of all blocks and comments in workspace.
|
|
293
|
+
*/
|
|
294
|
+
Blockly.Workspace.prototype.clear = function() {
|
|
295
|
+
this.isClearing = true;
|
|
296
|
+
var existingGroup = Blockly.Events.getGroup();
|
|
297
|
+
if (!existingGroup) {
|
|
298
|
+
Blockly.Events.setGroup(true);
|
|
299
|
+
}
|
|
300
|
+
while (this.topBlocks_.length) {
|
|
301
|
+
this.topBlocks_[0].dispose();
|
|
302
|
+
}
|
|
303
|
+
while (this.topComments_.length) {
|
|
304
|
+
this.topComments_[this.topComments_.length - 1].dispose();
|
|
305
|
+
}
|
|
306
|
+
if (!existingGroup) {
|
|
307
|
+
Blockly.Events.setGroup(false);
|
|
308
|
+
}
|
|
309
|
+
this.variableMap_.clear();
|
|
310
|
+
// Any block with a drop-down or WidgetDiv was disposed.
|
|
311
|
+
if (Blockly.DropDownDiv) {
|
|
312
|
+
Blockly.DropDownDiv.hideWithoutAnimation();
|
|
313
|
+
}
|
|
314
|
+
if (Blockly.WidgetDiv) {
|
|
315
|
+
Blockly.WidgetDiv.hide(true);
|
|
316
|
+
}
|
|
317
|
+
if (this.potentialVariableMap_) {
|
|
318
|
+
this.potentialVariableMap_.clear();
|
|
319
|
+
}
|
|
320
|
+
this.isClearing = false;
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
/* Begin functions that are just pass-throughs to the variable map. */
|
|
324
|
+
/**
|
|
325
|
+
* Rename a variable by updating its name in the variable map. Identify the
|
|
326
|
+
* variable to rename with the given ID.
|
|
327
|
+
* @param {string} id ID of the variable to rename.
|
|
328
|
+
* @param {string} newName New variable name.
|
|
329
|
+
*/
|
|
330
|
+
Blockly.Workspace.prototype.renameVariableById = function(id, newName) {
|
|
331
|
+
this.variableMap_.renameVariableById(id, newName);
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Create a variable with a given name, optional type, and optional ID.
|
|
336
|
+
* @param {!string} name The name of the variable. This must be unique across
|
|
337
|
+
* each variable type.
|
|
338
|
+
* @param {?string} opt_type The type of the variable like 'int' or 'string'.
|
|
339
|
+
* Does not need to be unique. Field_variable can filter variables based on
|
|
340
|
+
* their type. This will default to '' which is a specific type.
|
|
341
|
+
* @param {string=} opt_id The unique ID of the variable. This will default to
|
|
342
|
+
* a UUID.
|
|
343
|
+
* @param {boolean=} opt_isLocal Whether the variable to create is locally scoped.
|
|
344
|
+
* @param {boolean=} opt_isCloud Whether the variable to create is locally scoped.
|
|
345
|
+
* @return {?Blockly.VariableModel} The newly created variable.
|
|
346
|
+
*/
|
|
347
|
+
Blockly.Workspace.prototype.createVariable = function(name, opt_type, opt_id,
|
|
348
|
+
opt_isLocal, opt_isCloud) {
|
|
349
|
+
return this.variableMap_.createVariable(name, opt_type, opt_id, opt_isLocal, opt_isCloud);
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Find all the uses of the given variable, which is identified by ID.
|
|
354
|
+
* @param {string} id ID of the variable to find.
|
|
355
|
+
* @return {!Array.<!Blockly.Block>} Array of block usages.
|
|
356
|
+
*/
|
|
357
|
+
Blockly.Workspace.prototype.getVariableUsesById = function(id) {
|
|
358
|
+
return this.variableMap_.getVariableUsesById(id);
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Delete a variables by the passed in ID and all of its uses from this
|
|
363
|
+
* workspace. May prompt the user for confirmation.
|
|
364
|
+
* @param {string} id ID of variable to delete.
|
|
365
|
+
*/
|
|
366
|
+
Blockly.Workspace.prototype.deleteVariableById = function(id) {
|
|
367
|
+
this.variableMap_.deleteVariableById(id);
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Deletes a variable and all of its uses from this workspace without asking the
|
|
372
|
+
* user for confirmation.
|
|
373
|
+
* @param {!Blockly.VariableModel} variable Variable to delete.
|
|
374
|
+
* @param {!Array.<!Blockly.Block>} uses An array of uses of the variable.
|
|
375
|
+
* @private
|
|
376
|
+
*/
|
|
377
|
+
Blockly.Workspace.prototype.deleteVariableInternal_ = function(variable, uses) {
|
|
378
|
+
this.variableMap_.deleteVariableInternal_(variable, uses);
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Check whether a variable exists with the given name. The check is
|
|
383
|
+
* case-insensitive.
|
|
384
|
+
* @param {string} _name The name to check for.
|
|
385
|
+
* @return {number} The index of the name in the variable list, or -1 if it is
|
|
386
|
+
* not present.
|
|
387
|
+
* @deprecated April 2017
|
|
388
|
+
*/
|
|
389
|
+
|
|
390
|
+
Blockly.Workspace.prototype.variableIndexOf = function(_name) {
|
|
391
|
+
console.warn(
|
|
392
|
+
'Deprecated call to Blockly.Workspace.prototype.variableIndexOf');
|
|
393
|
+
return -1;
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Find the variable by the given name and return it. Return null if it is not
|
|
398
|
+
* found.
|
|
399
|
+
* TODO (#1199): Possibly delete this function.
|
|
400
|
+
* @param {!string} name The name to check for.
|
|
401
|
+
* @param {string=} opt_type The type of the variable. If not provided it
|
|
402
|
+
* defaults to the empty string, which is a specific type.
|
|
403
|
+
* @return {?Blockly.VariableModel} the variable with the given name.
|
|
404
|
+
*/
|
|
405
|
+
Blockly.Workspace.prototype.getVariable = function(name, opt_type) {
|
|
406
|
+
return this.variableMap_.getVariable(name, opt_type);
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Find the variable by the given ID and return it. Return null if it is not
|
|
411
|
+
* found.
|
|
412
|
+
* @param {!string} id The ID to check for.
|
|
413
|
+
* @return {?Blockly.VariableModel} The variable with the given ID.
|
|
414
|
+
*/
|
|
415
|
+
Blockly.Workspace.prototype.getVariableById = function(id) {
|
|
416
|
+
return this.variableMap_.getVariableById(id);
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Find the variable with the specified type. If type is null, return list of
|
|
421
|
+
* variables with empty string type.
|
|
422
|
+
* @param {?string} type Type of the variables to find.
|
|
423
|
+
* @return {Array.<Blockly.VariableModel>} The sought after variables of the
|
|
424
|
+
* passed in type. An empty array if none are found.
|
|
425
|
+
*/
|
|
426
|
+
Blockly.Workspace.prototype.getVariablesOfType = function(type) {
|
|
427
|
+
return this.variableMap_.getVariablesOfType(type);
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Return all variable types.
|
|
432
|
+
* @return {!Array.<string>} List of variable types.
|
|
433
|
+
* @package
|
|
434
|
+
*/
|
|
435
|
+
Blockly.Workspace.prototype.getVariableTypes = function() {
|
|
436
|
+
return this.variableMap_.getVariableTypes();
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Return all variables of all types.
|
|
441
|
+
* @return {!Array.<Blockly.VariableModel>} List of variable models.
|
|
442
|
+
*/
|
|
443
|
+
Blockly.Workspace.prototype.getAllVariables = function() {
|
|
444
|
+
return this.variableMap_.getAllVariables();
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
/* End functions that are just pass-throughs to the variable map. */
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Returns the horizontal offset of the workspace.
|
|
451
|
+
* Intended for LTR/RTL compatibility in XML.
|
|
452
|
+
* Not relevant for a headless workspace.
|
|
453
|
+
* @return {number} Width.
|
|
454
|
+
*/
|
|
455
|
+
Blockly.Workspace.prototype.getWidth = function() {
|
|
456
|
+
return 0;
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Obtain a newly created block.
|
|
461
|
+
* @param {?string} prototypeName Name of the language object containing
|
|
462
|
+
* type-specific functions for this block.
|
|
463
|
+
* @param {string=} opt_id Optional ID. Use this ID if provided, otherwise
|
|
464
|
+
* create a new ID.
|
|
465
|
+
* @return {!Blockly.Block} The created block.
|
|
466
|
+
*/
|
|
467
|
+
Blockly.Workspace.prototype.newBlock = function(prototypeName, opt_id) {
|
|
468
|
+
return new Blockly.Block(this, prototypeName, opt_id);
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Undo or redo the previous action.
|
|
473
|
+
* @param {boolean} redo False if undo, true if redo.
|
|
474
|
+
*/
|
|
475
|
+
Blockly.Workspace.prototype.undo = function(redo) {
|
|
476
|
+
var inputStack = redo ? this.redoStack_ : this.undoStack_;
|
|
477
|
+
var outputStack = redo ? this.undoStack_ : this.redoStack_;
|
|
478
|
+
var inputEvent = inputStack.pop();
|
|
479
|
+
if (!inputEvent) {
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
var events = [inputEvent];
|
|
483
|
+
// Do another undo/redo if the next one is of the same group.
|
|
484
|
+
while (inputStack.length && inputEvent.group &&
|
|
485
|
+
inputEvent.group == inputStack[inputStack.length - 1].group) {
|
|
486
|
+
events.push(inputStack.pop());
|
|
487
|
+
}
|
|
488
|
+
// Push these popped events on the opposite stack.
|
|
489
|
+
for (var i = 0, event; event = events[i]; i++) {
|
|
490
|
+
outputStack.push(event);
|
|
491
|
+
}
|
|
492
|
+
events = Blockly.Events.filter(events, redo);
|
|
493
|
+
Blockly.Events.recordUndo = false;
|
|
494
|
+
if (Blockly.selected) {
|
|
495
|
+
Blockly.Events.disable();
|
|
496
|
+
try {
|
|
497
|
+
Blockly.selected.unselect();
|
|
498
|
+
} finally {
|
|
499
|
+
Blockly.Events.enable();
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
try {
|
|
503
|
+
for (var i = 0, event; event = events[i]; i++) {
|
|
504
|
+
event.run(redo);
|
|
505
|
+
}
|
|
506
|
+
} finally {
|
|
507
|
+
Blockly.Events.recordUndo = true;
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Clear the undo/redo stacks.
|
|
513
|
+
*/
|
|
514
|
+
Blockly.Workspace.prototype.clearUndo = function() {
|
|
515
|
+
this.undoStack_.length = 0;
|
|
516
|
+
this.redoStack_.length = 0;
|
|
517
|
+
// Stop any events already in the firing queue from being undoable.
|
|
518
|
+
Blockly.Events.clearPendingUndo();
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* @return {boolean} whether there are any events in the redo stack.
|
|
523
|
+
* @package
|
|
524
|
+
*/
|
|
525
|
+
Blockly.Workspace.prototype.hasRedoStack = function() {
|
|
526
|
+
return this.redoStack_.length != 0;
|
|
527
|
+
};
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* @return {boolean} whether there are any events in the undo stack.
|
|
531
|
+
* @package
|
|
532
|
+
*/
|
|
533
|
+
Blockly.Workspace.prototype.hasUndoStack = function() {
|
|
534
|
+
return this.undoStack_.length != 0;
|
|
535
|
+
};
|
|
536
|
+
/**
|
|
537
|
+
* When something in this workspace changes, call a function.
|
|
538
|
+
* @param {!Function} func Function to call.
|
|
539
|
+
* @return {!Function} Function that can be passed to
|
|
540
|
+
* removeChangeListener.
|
|
541
|
+
*/
|
|
542
|
+
Blockly.Workspace.prototype.addChangeListener = function(func) {
|
|
543
|
+
this.listeners_.push(func);
|
|
544
|
+
return func;
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Stop listening for this workspace's changes.
|
|
549
|
+
* @param {Function} func Function to stop calling.
|
|
550
|
+
*/
|
|
551
|
+
Blockly.Workspace.prototype.removeChangeListener = function(func) {
|
|
552
|
+
goog.array.remove(this.listeners_, func);
|
|
553
|
+
};
|
|
554
|
+
|
|
555
|
+
/**
|
|
556
|
+
* Fire a change event.
|
|
557
|
+
* @param {!Blockly.Events.Abstract} event Event to fire.
|
|
558
|
+
*/
|
|
559
|
+
Blockly.Workspace.prototype.fireChangeListener = function(event) {
|
|
560
|
+
if (event.recordUndo) {
|
|
561
|
+
this.undoStack_.push(event);
|
|
562
|
+
this.redoStack_.length = 0;
|
|
563
|
+
if (this.undoStack_.length > this.MAX_UNDO) {
|
|
564
|
+
this.undoStack_.unshift();
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
// Copy listeners in case a listener attaches/detaches itself.
|
|
568
|
+
var currentListeners = this.listeners_.slice();
|
|
569
|
+
for (var i = 0, func; func = currentListeners[i]; i++) {
|
|
570
|
+
func(event);
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* Find the block on this workspace with the specified ID.
|
|
576
|
+
* @param {string} id ID of block to find.
|
|
577
|
+
* @return {Blockly.Block} The sought after block or null if not found.
|
|
578
|
+
*/
|
|
579
|
+
Blockly.Workspace.prototype.getBlockById = function(id) {
|
|
580
|
+
var block = this.blockDB_[id];
|
|
581
|
+
if (!block && this.getFlyout() && this.getFlyout().getWorkspace()) {
|
|
582
|
+
block = this.getFlyout().getWorkspace().blockDB_[id];
|
|
583
|
+
}
|
|
584
|
+
return block || null;
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Find the comment on this workspace with the specified ID.
|
|
589
|
+
* @param {string} id ID of comment to find.
|
|
590
|
+
* @return {Blockly.WorkspaceComment} The sought after comment or null if not
|
|
591
|
+
* found.
|
|
592
|
+
* @package
|
|
593
|
+
*/
|
|
594
|
+
Blockly.Workspace.prototype.getCommentById = function(id) {
|
|
595
|
+
return this.commentDB_[id] || null;
|
|
596
|
+
};
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* Getter for the flyout associated with this workspace. This is null in a
|
|
600
|
+
* non-rendered workspace, but may be overriden by subclasses.
|
|
601
|
+
* @return {Blockly.Flyout} The flyout on this workspace.
|
|
602
|
+
*/
|
|
603
|
+
Blockly.Workspace.prototype.getFlyout = function() {
|
|
604
|
+
return null;
|
|
605
|
+
};
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* Checks whether all value and statement inputs in the workspace are filled
|
|
609
|
+
* with blocks.
|
|
610
|
+
* @param {boolean=} opt_shadowBlocksAreFilled An optional argument controlling
|
|
611
|
+
* whether shadow blocks are counted as filled. Defaults to true.
|
|
612
|
+
* @return {boolean} True if all inputs are filled, false otherwise.
|
|
613
|
+
*/
|
|
614
|
+
Blockly.Workspace.prototype.allInputsFilled = function(opt_shadowBlocksAreFilled) {
|
|
615
|
+
var blocks = this.getTopBlocks(false);
|
|
616
|
+
for (var i = 0, block; block = blocks[i]; i++) {
|
|
617
|
+
if (!block.allInputsFilled(opt_shadowBlocksAreFilled)) {
|
|
618
|
+
return false;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
return true;
|
|
622
|
+
};
|
|
623
|
+
|
|
624
|
+
/**
|
|
625
|
+
* Return the variable map that contains "potential" variables. These exist in
|
|
626
|
+
* the flyout but not in the workspace.
|
|
627
|
+
* @return {?Blockly.VariableMap} The potential variable map.
|
|
628
|
+
* @package
|
|
629
|
+
*/
|
|
630
|
+
Blockly.Workspace.prototype.getPotentialVariableMap = function() {
|
|
631
|
+
return this.potentialVariableMap_;
|
|
632
|
+
};
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* Create and store the potential variable map for this workspace.
|
|
636
|
+
* @package
|
|
637
|
+
*/
|
|
638
|
+
Blockly.Workspace.prototype.createPotentialVariableMap = function() {
|
|
639
|
+
this.potentialVariableMap_ = new Blockly.VariableMap(this);
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* Return the map of all variables on the workspace.
|
|
644
|
+
* @return {?Blockly.VariableMap} The variable map.
|
|
645
|
+
* @package
|
|
646
|
+
*/
|
|
647
|
+
Blockly.Workspace.prototype.getVariableMap = function() {
|
|
648
|
+
return this.variableMap_;
|
|
649
|
+
};
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* Database of all workspaces.
|
|
653
|
+
* @private
|
|
654
|
+
*/
|
|
655
|
+
Blockly.Workspace.WorkspaceDB_ = Object.create(null);
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* Find the workspace with the specified ID.
|
|
659
|
+
* @param {string} id ID of workspace to find.
|
|
660
|
+
* @return {Blockly.Workspace} The sought after workspace or null if not found.
|
|
661
|
+
*/
|
|
662
|
+
Blockly.Workspace.getById = function(id) {
|
|
663
|
+
return Blockly.Workspace.WorkspaceDB_[id] || null;
|
|
664
|
+
};
|
|
665
|
+
|
|
666
|
+
// Export symbols that would otherwise be renamed by Closure compiler.
|
|
667
|
+
Blockly.Workspace.prototype['clear'] = Blockly.Workspace.prototype.clear;
|
|
668
|
+
Blockly.Workspace.prototype['clearUndo'] =
|
|
669
|
+
Blockly.Workspace.prototype.clearUndo;
|
|
670
|
+
Blockly.Workspace.prototype['addChangeListener'] =
|
|
671
|
+
Blockly.Workspace.prototype.addChangeListener;
|
|
672
|
+
Blockly.Workspace.prototype['removeChangeListener'] =
|
|
673
|
+
Blockly.Workspace.prototype.removeChangeListener;
|