@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,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Visual Blocks Editor
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2018 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 Classes for all types of variable events.
|
|
23
|
+
* @author fenichel@google.com (Rachel Fenichel)
|
|
24
|
+
*/
|
|
25
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
goog.provide('Blockly.Events.VarBase');
|
|
28
|
+
goog.provide('Blockly.Events.VarCreate');
|
|
29
|
+
goog.provide('Blockly.Events.VarDelete');
|
|
30
|
+
goog.provide('Blockly.Events.VarRename');
|
|
31
|
+
|
|
32
|
+
goog.require('Blockly.Events');
|
|
33
|
+
goog.require('Blockly.Events.Abstract');
|
|
34
|
+
|
|
35
|
+
goog.require('goog.array');
|
|
36
|
+
goog.require('goog.math.Coordinate');
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Abstract class for a variable event.
|
|
41
|
+
* @param {Blockly.VariableModel} variable The variable this event corresponds
|
|
42
|
+
* to.
|
|
43
|
+
* @extends {Blockly.Events.Abstract}
|
|
44
|
+
* @constructor
|
|
45
|
+
*/
|
|
46
|
+
Blockly.Events.VarBase = function(variable) {
|
|
47
|
+
Blockly.Events.VarBase.superClass_.constructor.call(this);
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The variable id for the variable this event pertains to.
|
|
51
|
+
* @type {string}
|
|
52
|
+
*/
|
|
53
|
+
this.varId = variable.getId();
|
|
54
|
+
this.workspaceId = variable.workspace.id;
|
|
55
|
+
};
|
|
56
|
+
goog.inherits(Blockly.Events.VarBase, Blockly.Events.Abstract);
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Encode the event as JSON.
|
|
60
|
+
* @return {!Object} JSON representation.
|
|
61
|
+
*/
|
|
62
|
+
Blockly.Events.VarBase.prototype.toJson = function() {
|
|
63
|
+
var json = Blockly.Events.VarBase.superClass_.toJson.call(this);
|
|
64
|
+
json['varId'] = this.varId;
|
|
65
|
+
return json;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Decode the JSON event.
|
|
70
|
+
* @param {!Object} json JSON representation.
|
|
71
|
+
*/
|
|
72
|
+
Blockly.Events.VarBase.prototype.fromJson = function(json) {
|
|
73
|
+
Blockly.Events.VarBase.superClass_.toJson.call(this);
|
|
74
|
+
this.varId = json['varId'];
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Class for a variable creation event.
|
|
79
|
+
* @param {Blockly.VariableModel} variable The created variable.
|
|
80
|
+
* Null for a blank event.
|
|
81
|
+
* @extends {Blockly.Events.VarBase}
|
|
82
|
+
* @constructor
|
|
83
|
+
*/
|
|
84
|
+
Blockly.Events.VarCreate = function(variable) {
|
|
85
|
+
if (!variable) {
|
|
86
|
+
return; // Blank event to be populated by fromJson.
|
|
87
|
+
}
|
|
88
|
+
Blockly.Events.VarCreate.superClass_.constructor.call(this, variable);
|
|
89
|
+
this.varType = variable.type;
|
|
90
|
+
this.varName = variable.name;
|
|
91
|
+
this.isLocal = variable.isLocal;
|
|
92
|
+
this.isCloud = variable.isCloud;
|
|
93
|
+
};
|
|
94
|
+
goog.inherits(Blockly.Events.VarCreate, Blockly.Events.VarBase);
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Type of this event.
|
|
98
|
+
* @type {string}
|
|
99
|
+
*/
|
|
100
|
+
Blockly.Events.VarCreate.prototype.type = Blockly.Events.VAR_CREATE;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Encode the event as JSON.
|
|
104
|
+
* @return {!Object} JSON representation.
|
|
105
|
+
*/
|
|
106
|
+
Blockly.Events.VarCreate.prototype.toJson = function() {
|
|
107
|
+
var json = Blockly.Events.VarCreate.superClass_.toJson.call(this);
|
|
108
|
+
json['varType'] = this.varType;
|
|
109
|
+
json['varName'] = this.varName;
|
|
110
|
+
json['isLocal'] = this.isLocal;
|
|
111
|
+
json['isCloud'] = this.isCloud;
|
|
112
|
+
return json;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Decode the JSON event.
|
|
117
|
+
* @param {!Object} json JSON representation.
|
|
118
|
+
*/
|
|
119
|
+
Blockly.Events.VarCreate.prototype.fromJson = function(json) {
|
|
120
|
+
Blockly.Events.VarCreate.superClass_.fromJson.call(this, json);
|
|
121
|
+
this.varType = json['varType'];
|
|
122
|
+
this.varName = json['varName'];
|
|
123
|
+
this.isLocal = json['isLocal'];
|
|
124
|
+
this.isCloud = json['isCloud'];
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Run a variable creation event.
|
|
129
|
+
* @param {boolean} forward True if run forward, false if run backward (undo).
|
|
130
|
+
*/
|
|
131
|
+
Blockly.Events.VarCreate.prototype.run = function(forward) {
|
|
132
|
+
var workspace = this.getEventWorkspace_();
|
|
133
|
+
if (forward) {
|
|
134
|
+
workspace.createVariable(this.varName, this.varType, this.varId, this.isLocal, this.isCloud);
|
|
135
|
+
} else {
|
|
136
|
+
workspace.deleteVariableById(this.varId);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Class for a variable deletion event.
|
|
142
|
+
* @param {Blockly.VariableModel} variable The deleted variable.
|
|
143
|
+
* Null for a blank event.
|
|
144
|
+
* @extends {Blockly.Events.VarBase}
|
|
145
|
+
* @constructor
|
|
146
|
+
*/
|
|
147
|
+
Blockly.Events.VarDelete = function(variable) {
|
|
148
|
+
if (!variable) {
|
|
149
|
+
return; // Blank event to be populated by fromJson.
|
|
150
|
+
}
|
|
151
|
+
Blockly.Events.VarDelete.superClass_.constructor.call(this, variable);
|
|
152
|
+
this.varType = variable.type;
|
|
153
|
+
this.varName = variable.name;
|
|
154
|
+
this.isLocal = variable.isLocal;
|
|
155
|
+
this.isCloud = variable.isCloud;
|
|
156
|
+
};
|
|
157
|
+
goog.inherits(Blockly.Events.VarDelete, Blockly.Events.VarBase);
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Type of this event.
|
|
161
|
+
* @type {string}
|
|
162
|
+
*/
|
|
163
|
+
Blockly.Events.VarDelete.prototype.type = Blockly.Events.VAR_DELETE;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Encode the event as JSON.
|
|
167
|
+
* @return {!Object} JSON representation.
|
|
168
|
+
*/
|
|
169
|
+
Blockly.Events.VarDelete.prototype.toJson = function() {
|
|
170
|
+
var json = Blockly.Events.VarDelete.superClass_.toJson.call(this);
|
|
171
|
+
json['varType'] = this.varType;
|
|
172
|
+
json['varName'] = this.varName;
|
|
173
|
+
json['isLocal'] = this.isLocal;
|
|
174
|
+
json['isCloud'] = this.isCloud;
|
|
175
|
+
return json;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Decode the JSON event.
|
|
180
|
+
* @param {!Object} json JSON representation.
|
|
181
|
+
*/
|
|
182
|
+
Blockly.Events.VarDelete.prototype.fromJson = function(json) {
|
|
183
|
+
Blockly.Events.VarDelete.superClass_.fromJson.call(this, json);
|
|
184
|
+
this.varType = json['varType'];
|
|
185
|
+
this.varName = json['varName'];
|
|
186
|
+
this.isLocal = json['isLocal'];
|
|
187
|
+
this.isCloud = json['isCloud'];
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Run a variable deletion event.
|
|
192
|
+
* @param {boolean} forward True if run forward, false if run backward (undo).
|
|
193
|
+
*/
|
|
194
|
+
Blockly.Events.VarDelete.prototype.run = function(forward) {
|
|
195
|
+
var workspace = this.getEventWorkspace_();
|
|
196
|
+
if (forward) {
|
|
197
|
+
workspace.deleteVariableById(this.varId);
|
|
198
|
+
} else {
|
|
199
|
+
workspace.createVariable(this.varName, this.varType, this.varId, this.isLocal, this.isCloud);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Class for a variable rename event.
|
|
205
|
+
* @param {Blockly.VariableModel} variable The renamed variable.
|
|
206
|
+
* Null for a blank event.
|
|
207
|
+
* @param {string} newName The new name the variable will be changed to.
|
|
208
|
+
* @extends {Blockly.Events.VarBase}
|
|
209
|
+
* @constructor
|
|
210
|
+
*/
|
|
211
|
+
Blockly.Events.VarRename = function(variable, newName) {
|
|
212
|
+
if (!variable) {
|
|
213
|
+
return; // Blank event to be populated by fromJson.
|
|
214
|
+
}
|
|
215
|
+
Blockly.Events.VarRename.superClass_.constructor.call(this, variable);
|
|
216
|
+
this.oldName = variable.name;
|
|
217
|
+
this.newName = newName;
|
|
218
|
+
};
|
|
219
|
+
goog.inherits(Blockly.Events.VarRename, Blockly.Events.VarBase);
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Type of this event.
|
|
223
|
+
* @type {string}
|
|
224
|
+
*/
|
|
225
|
+
Blockly.Events.VarRename.prototype.type = Blockly.Events.VAR_RENAME;
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Encode the event as JSON.
|
|
229
|
+
* @return {!Object} JSON representation.
|
|
230
|
+
*/
|
|
231
|
+
Blockly.Events.VarRename.prototype.toJson = function() {
|
|
232
|
+
var json = Blockly.Events.VarRename.superClass_.toJson.call(this);
|
|
233
|
+
json['oldName'] = this.oldName;
|
|
234
|
+
json['newName'] = this.newName;
|
|
235
|
+
return json;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Decode the JSON event.
|
|
240
|
+
* @param {!Object} json JSON representation.
|
|
241
|
+
*/
|
|
242
|
+
Blockly.Events.VarRename.prototype.fromJson = function(json) {
|
|
243
|
+
Blockly.Events.VarRename.superClass_.fromJson.call(this, json);
|
|
244
|
+
this.oldName = json['oldName'];
|
|
245
|
+
this.newName = json['newName'];
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Run a variable rename event.
|
|
250
|
+
* @param {boolean} forward True if run forward, false if run backward (undo).
|
|
251
|
+
*/
|
|
252
|
+
Blockly.Events.VarRename.prototype.run = function(forward) {
|
|
253
|
+
var workspace = this.getEventWorkspace_();
|
|
254
|
+
if (forward) {
|
|
255
|
+
workspace.renameVariableById(this.varId, this.newName);
|
|
256
|
+
} else {
|
|
257
|
+
workspace.renameVariableById(this.varId, this.oldName);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Visual Blocks Editor
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2017 Google Inc.
|
|
6
|
+
* https://developers.google.com/blockly/
|
|
7
|
+
*
|
|
8
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9
|
+
* you may not use this file except in compliance with the License.
|
|
10
|
+
* You may obtain a copy of the License at
|
|
11
|
+
*
|
|
12
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
*
|
|
14
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
15
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17
|
+
* See the License for the specific language governing permissions and
|
|
18
|
+
* limitations under the License.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @fileoverview Object representing a map of variables and their types.
|
|
23
|
+
* @author marisaleung@google.com (Marisa Leung)
|
|
24
|
+
*/
|
|
25
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
goog.provide('Blockly.VariableMap');
|
|
28
|
+
|
|
29
|
+
goog.require('Blockly.Events.VarDelete');
|
|
30
|
+
goog.require('Blockly.Events.VarRename');
|
|
31
|
+
goog.require('Blockly.VariableModel');
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Class for a variable map. This contains a dictionary data structure with
|
|
36
|
+
* variable types as keys and lists of variables as values. The list of
|
|
37
|
+
* variables are the type indicated by the key.
|
|
38
|
+
* @param {!Blockly.Workspace} workspace The workspace this map belongs to.
|
|
39
|
+
* @constructor
|
|
40
|
+
*/
|
|
41
|
+
Blockly.VariableMap = function(workspace) {
|
|
42
|
+
/**
|
|
43
|
+
* A map from variable type to list of variable names. The lists contain all
|
|
44
|
+
* of the named variables in the workspace, including variables
|
|
45
|
+
* that are not currently in use.
|
|
46
|
+
* @type {!Object.<string, !Array.<Blockly.VariableModel>>}
|
|
47
|
+
* @private
|
|
48
|
+
*/
|
|
49
|
+
this.variableMap_ = {};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The workspace this map belongs to.
|
|
53
|
+
* @type {!Blockly.Workspace}
|
|
54
|
+
*/
|
|
55
|
+
this.workspace = workspace;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Clear the variable map.
|
|
60
|
+
*/
|
|
61
|
+
Blockly.VariableMap.prototype.clear = function() {
|
|
62
|
+
this.variableMap_ = new Object(null);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/* Begin functions for renaming variables. */
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Rename the given variable by updating its name in the variable map.
|
|
69
|
+
* @param {!Blockly.VariableModel} variable Variable to rename.
|
|
70
|
+
* @param {string} newName New variable name.
|
|
71
|
+
* @package
|
|
72
|
+
*/
|
|
73
|
+
Blockly.VariableMap.prototype.renameVariable = function(variable, newName) {
|
|
74
|
+
var type = variable.type;
|
|
75
|
+
var conflictVar = this.getVariable(newName, type);
|
|
76
|
+
var blocks = this.workspace.getAllBlocks();
|
|
77
|
+
Blockly.Events.setGroup(true);
|
|
78
|
+
try {
|
|
79
|
+
if (!conflictVar) {
|
|
80
|
+
this.renameVariableAndUses_(variable, newName, blocks);
|
|
81
|
+
} else {
|
|
82
|
+
// We don't want to rename the variable if one with the exact new name
|
|
83
|
+
// already exists.
|
|
84
|
+
console.warn('Unexpected conflict when attempting to rename ' +
|
|
85
|
+
'variable with name: ' + variable.name + ' and id: ' + variable.getId() +
|
|
86
|
+
' to new name: ' + newName + '. A variable with the new name already exists' +
|
|
87
|
+
' and has id: ' + conflictVar.getId());
|
|
88
|
+
|
|
89
|
+
}
|
|
90
|
+
} finally {
|
|
91
|
+
Blockly.Events.setGroup(false);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Rename a variable by updating its name in the variable map. Identify the
|
|
97
|
+
* variable to rename with the given ID.
|
|
98
|
+
* @param {string} id ID of the variable to rename.
|
|
99
|
+
* @param {string} newName New variable name.
|
|
100
|
+
*/
|
|
101
|
+
Blockly.VariableMap.prototype.renameVariableById = function(id, newName) {
|
|
102
|
+
var variable = this.getVariableById(id);
|
|
103
|
+
if (!variable) {
|
|
104
|
+
throw new Error('Tried to rename a variable that didn\'t exist. ID: ' + id);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
this.renameVariable(variable, newName);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Update the name of the given variable and refresh all references to it.
|
|
112
|
+
* The new name must not conflict with any existing variable names.
|
|
113
|
+
* @param {!Blockly.VariableModel} variable Variable to rename.
|
|
114
|
+
* @param {string} newName New variable name.
|
|
115
|
+
* @param {!Array.<!Blockly.Block>} blocks The list of all blocks in the
|
|
116
|
+
* workspace.
|
|
117
|
+
* @private
|
|
118
|
+
*/
|
|
119
|
+
Blockly.VariableMap.prototype.renameVariableAndUses_ = function(variable,
|
|
120
|
+
newName, blocks) {
|
|
121
|
+
Blockly.Events.fire(new Blockly.Events.VarRename(variable, newName));
|
|
122
|
+
variable.name = newName;
|
|
123
|
+
for (var i = 0; i < blocks.length; i++) {
|
|
124
|
+
blocks[i].updateVarName(variable);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Update the name of the given variable to the same name as an existing
|
|
130
|
+
* variable. The two variables are coalesced into a single variable with the ID
|
|
131
|
+
* of the existing variable that was already using newName.
|
|
132
|
+
* Refresh all references to the variable.
|
|
133
|
+
* @param {!Blockly.VariableModel} variable Variable to rename.
|
|
134
|
+
* @param {string} newName New variable name.
|
|
135
|
+
* @param {!Blockly.VariableModel} conflictVar The variable that was already
|
|
136
|
+
* using newName.
|
|
137
|
+
* @param {!Array.<!Blockly.Block>} blocks The list of all blocks in the
|
|
138
|
+
* workspace.
|
|
139
|
+
* @private
|
|
140
|
+
*/
|
|
141
|
+
Blockly.VariableMap.prototype.renameVariableWithConflict_ = function(variable,
|
|
142
|
+
newName, conflictVar, blocks) {
|
|
143
|
+
var type = variable.type;
|
|
144
|
+
var oldCase = conflictVar.name;
|
|
145
|
+
|
|
146
|
+
if (newName != oldCase) {
|
|
147
|
+
// Simple rename to change the case and update references.
|
|
148
|
+
this.renameVariableAndUses_(conflictVar, newName, blocks);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// These blocks now refer to a different variable.
|
|
152
|
+
// These will fire change events.
|
|
153
|
+
for (var i = 0; i < blocks.length; i++) {
|
|
154
|
+
blocks[i].renameVarById(variable.getId(), conflictVar.getId());
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Finally delete the original variable, which is now unreferenced.
|
|
158
|
+
Blockly.Events.fire(new Blockly.Events.VarDelete(variable));
|
|
159
|
+
// And remove it from the list.
|
|
160
|
+
var variableList = this.getVariablesOfType(type);
|
|
161
|
+
var variableIndex = variableList.indexOf(variable);
|
|
162
|
+
this.variableMap_[type].splice(variableIndex, 1);
|
|
163
|
+
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/* End functions for renaming variabless. */
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Create a variable with a given name, optional type, and optional id.
|
|
170
|
+
* @param {!string} name The name of the variable. This must be unique across
|
|
171
|
+
* each variable type.
|
|
172
|
+
* @param {?string} opt_type The type of the variable like 'int' or 'string'.
|
|
173
|
+
* Does not need to be unique. Field_variable can filter variables based on
|
|
174
|
+
* their type. This will default to '' which is a specific type.
|
|
175
|
+
* @param {string=} opt_id The unique ID of the variable. This will default to
|
|
176
|
+
* a UUID.
|
|
177
|
+
* @param {boolean=} opt_isLocal Whether the variable is locally scoped.
|
|
178
|
+
* @param {boolean=} opt_isCloud Whether the variable is a cloud variable.
|
|
179
|
+
* @return {?Blockly.VariableModel} The newly created variable.
|
|
180
|
+
*/
|
|
181
|
+
Blockly.VariableMap.prototype.createVariable = function(name,
|
|
182
|
+
opt_type, opt_id, opt_isLocal, opt_isCloud) {
|
|
183
|
+
var variable = this.getVariable(name, opt_type);
|
|
184
|
+
if (variable) {
|
|
185
|
+
if (opt_id && variable.getId() != opt_id) {
|
|
186
|
+
// There is a variable conflict. Variable conflicts should be eliminated
|
|
187
|
+
// in the scratch-vm, or before we get to this point,
|
|
188
|
+
// so log a warning, because throwing an error crashes projects.
|
|
189
|
+
console.warn('Variable "' + name + '" is already in use and its id is "'
|
|
190
|
+
+ variable.getId() + '" which conflicts with the passed in ' +
|
|
191
|
+
'id, "' + opt_id + '".');
|
|
192
|
+
}
|
|
193
|
+
// The variable already exists and has the same ID.
|
|
194
|
+
return variable;
|
|
195
|
+
}
|
|
196
|
+
if (opt_id) {
|
|
197
|
+
variable = this.getVariableById(opt_id);
|
|
198
|
+
if (variable) {
|
|
199
|
+
console.warn('Variable id, "' + opt_id + '", is already in use.');
|
|
200
|
+
return variable;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
opt_id = opt_id || Blockly.utils.genUid();
|
|
204
|
+
opt_type = opt_type || '';
|
|
205
|
+
|
|
206
|
+
variable = new Blockly.VariableModel(this.workspace, name, opt_type, opt_id,
|
|
207
|
+
opt_isLocal, opt_isCloud);
|
|
208
|
+
// If opt_type is not a key, create a new list.
|
|
209
|
+
if (!this.variableMap_[opt_type]) {
|
|
210
|
+
this.variableMap_[opt_type] = [variable];
|
|
211
|
+
} else {
|
|
212
|
+
// Else append the variable to the preexisting list.
|
|
213
|
+
this.variableMap_[opt_type].push(variable);
|
|
214
|
+
}
|
|
215
|
+
return variable;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
/* Begin functions for variable deletion. */
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Delete a variable.
|
|
222
|
+
* @param {Blockly.VariableModel} variable Variable to delete.
|
|
223
|
+
*/
|
|
224
|
+
Blockly.VariableMap.prototype.deleteVariable = function(variable) {
|
|
225
|
+
var variableList = this.variableMap_[variable.type];
|
|
226
|
+
for (var i = 0, tempVar; tempVar = variableList[i]; i++) {
|
|
227
|
+
if (tempVar.getId() == variable.getId()) {
|
|
228
|
+
variableList.splice(i, 1);
|
|
229
|
+
Blockly.Events.fire(new Blockly.Events.VarDelete(variable));
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Delete a variable and all of its uses from this workspace by the passed
|
|
237
|
+
* in ID. May prompt the user for confirmation.
|
|
238
|
+
* @param {string} id ID of variable to delete.
|
|
239
|
+
*/
|
|
240
|
+
Blockly.VariableMap.prototype.deleteVariableById = function(id) {
|
|
241
|
+
var variable = this.getVariableById(id);
|
|
242
|
+
if (variable) {
|
|
243
|
+
// Check whether this variable is a function parameter before deleting.
|
|
244
|
+
var variableName = variable.name;
|
|
245
|
+
var uses = this.getVariableUsesById(id);
|
|
246
|
+
for (var i = 0, block; block = uses[i]; i++) {
|
|
247
|
+
if (block.type == Blockly.PROCEDURES_DEFINITION_BLOCK_TYPE ||
|
|
248
|
+
block.type == 'procedures_defreturn') {
|
|
249
|
+
var procedureName = block.getFieldValue('NAME');
|
|
250
|
+
var deleteText = Blockly.Msg.CANNOT_DELETE_VARIABLE_PROCEDURE.
|
|
251
|
+
replace('%1', variableName).
|
|
252
|
+
replace('%2', procedureName);
|
|
253
|
+
Blockly.alert(deleteText);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
var map = this;
|
|
259
|
+
if (uses.length > 1) {
|
|
260
|
+
// Confirm before deleting multiple blocks.
|
|
261
|
+
var confirmText = Blockly.Msg.DELETE_VARIABLE_CONFIRMATION.
|
|
262
|
+
replace('%1', String(uses.length)).
|
|
263
|
+
replace('%2', variableName);
|
|
264
|
+
Blockly.confirm(confirmText,
|
|
265
|
+
function(ok) {
|
|
266
|
+
if (ok) {
|
|
267
|
+
map.deleteVariableInternal_(variable, uses);
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
} else {
|
|
271
|
+
// No confirmation necessary for a single block.
|
|
272
|
+
map.deleteVariableInternal_(variable, uses);
|
|
273
|
+
}
|
|
274
|
+
} else {
|
|
275
|
+
console.warn("Can't delete non-existent variable: " + id);
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Deletes a variable and all of its uses from this workspace without asking the
|
|
281
|
+
* user for confirmation.
|
|
282
|
+
* @param {!Blockly.VariableModel} variable Variable to delete.
|
|
283
|
+
* @param {!Array.<!Blockly.Block>} uses An array of uses of the variable.
|
|
284
|
+
* @private
|
|
285
|
+
*/
|
|
286
|
+
Blockly.VariableMap.prototype.deleteVariableInternal_ = function(variable,
|
|
287
|
+
uses) {
|
|
288
|
+
var existingGroup = Blockly.Events.getGroup();
|
|
289
|
+
if (!existingGroup) {
|
|
290
|
+
Blockly.Events.setGroup(true);
|
|
291
|
+
}
|
|
292
|
+
try {
|
|
293
|
+
for (var i = 0; i < uses.length; i++) {
|
|
294
|
+
uses[i].dispose(true, false);
|
|
295
|
+
}
|
|
296
|
+
this.deleteVariable(variable);
|
|
297
|
+
} finally {
|
|
298
|
+
if (!existingGroup) {
|
|
299
|
+
Blockly.Events.setGroup(false);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
/* End functions for variable deletion. */
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Find the variable by the given name and type and return it. Return null if
|
|
308
|
+
* it is not found.
|
|
309
|
+
* @param {string} name The name to check for.
|
|
310
|
+
* @param {string=} opt_type The type of the variable. If not provided it
|
|
311
|
+
* defaults to the empty string, which is a specific type.
|
|
312
|
+
* @return {Blockly.VariableModel} The variable with the given name, or null if
|
|
313
|
+
* it was not found.
|
|
314
|
+
*/
|
|
315
|
+
Blockly.VariableMap.prototype.getVariable = function(name, opt_type) {
|
|
316
|
+
var type = opt_type || '';
|
|
317
|
+
var list = this.variableMap_[type];
|
|
318
|
+
if (list) {
|
|
319
|
+
for (var j = 0, variable; variable = list[j]; j++) {
|
|
320
|
+
if (variable.name == name) {
|
|
321
|
+
return variable;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return null;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Find the variable by the given ID and return it. Return null if it is not
|
|
330
|
+
* found.
|
|
331
|
+
* @param {!string} id The id to check for.
|
|
332
|
+
* @return {?Blockly.VariableModel} The variable with the given id.
|
|
333
|
+
*/
|
|
334
|
+
Blockly.VariableMap.prototype.getVariableById = function(id) {
|
|
335
|
+
var keys = Object.keys(this.variableMap_);
|
|
336
|
+
for (var i = 0; i < keys.length; i++ ) {
|
|
337
|
+
var key = keys[i];
|
|
338
|
+
for (var j = 0, variable; variable = this.variableMap_[key][j]; j++) {
|
|
339
|
+
if (variable.getId() == id) {
|
|
340
|
+
return variable;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return null;
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Get a list containing all of the variables of a specified type. If type is
|
|
349
|
+
* null, return list of variables with empty string type.
|
|
350
|
+
* @param {?string} type Type of the variables to find.
|
|
351
|
+
* @return {!Array.<!Blockly.VariableModel>} The sought after variables of the
|
|
352
|
+
* passed in type. An empty array if none are found.
|
|
353
|
+
*/
|
|
354
|
+
Blockly.VariableMap.prototype.getVariablesOfType = function(type) {
|
|
355
|
+
type = type || '';
|
|
356
|
+
var variable_list = this.variableMap_[type];
|
|
357
|
+
if (variable_list) {
|
|
358
|
+
return variable_list.slice();
|
|
359
|
+
}
|
|
360
|
+
return [];
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Return all variable types. This list always contains the empty string.
|
|
365
|
+
* @return {!Array.<string>} List of variable types.
|
|
366
|
+
* @package
|
|
367
|
+
*/
|
|
368
|
+
Blockly.VariableMap.prototype.getVariableTypes = function() {
|
|
369
|
+
var types = Object.keys(this.variableMap_);
|
|
370
|
+
var hasEmpty = false;
|
|
371
|
+
for (var i = 0; i < types.length; i++) {
|
|
372
|
+
if (types[i] == '') {
|
|
373
|
+
hasEmpty = true;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
if (!hasEmpty) {
|
|
377
|
+
types.push('');
|
|
378
|
+
}
|
|
379
|
+
return types;
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Return all variables of all types.
|
|
384
|
+
* @return {!Array.<!Blockly.VariableModel>} List of variable models.
|
|
385
|
+
*/
|
|
386
|
+
Blockly.VariableMap.prototype.getAllVariables = function() {
|
|
387
|
+
var all_variables = [];
|
|
388
|
+
var keys = Object.keys(this.variableMap_);
|
|
389
|
+
for (var i = 0; i < keys.length; i++ ) {
|
|
390
|
+
all_variables = all_variables.concat(this.variableMap_[keys[i]]);
|
|
391
|
+
}
|
|
392
|
+
return all_variables;
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Find all the uses of a named variable.
|
|
397
|
+
* @param {string} id ID of the variable to find.
|
|
398
|
+
* @return {!Array.<!Blockly.Block>} Array of block usages.
|
|
399
|
+
*/
|
|
400
|
+
Blockly.VariableMap.prototype.getVariableUsesById = function(id) {
|
|
401
|
+
var uses = [];
|
|
402
|
+
var blocks = this.workspace.getAllBlocks();
|
|
403
|
+
// Iterate through every block and check the name.
|
|
404
|
+
for (var i = 0; i < blocks.length; i++) {
|
|
405
|
+
var blockVariables = blocks[i].getVarModels();
|
|
406
|
+
if (blockVariables) {
|
|
407
|
+
for (var j = 0; j < blockVariables.length; j++) {
|
|
408
|
+
if (blockVariables[j].getId() == id) {
|
|
409
|
+
uses.push(blocks[i]);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return uses;
|
|
415
|
+
};
|