scratch-blocks 0.1.0-prerelease.20211005172512
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/LICENSE +177 -0
- package/README.md +23 -0
- package/TRADEMARK +1 -0
- package/blockly_compressed_horizontal.js +2253 -0
- package/blockly_compressed_vertical.js +2281 -0
- package/blockly_uncompressed_horizontal.js +1210 -0
- package/blockly_uncompressed_vertical.js +1210 -0
- package/blocks_common/colour.js +61 -0
- package/blocks_common/math.js +154 -0
- package/blocks_common/matrix.js +54 -0
- package/blocks_common/note.js +57 -0
- package/blocks_common/text.js +56 -0
- package/blocks_compressed.js +36 -0
- package/blocks_compressed_horizontal.js +58 -0
- package/blocks_compressed_vertical.js +207 -0
- package/blocks_horizontal/control.js +207 -0
- package/blocks_horizontal/default_toolbox.js +139 -0
- package/blocks_horizontal/event.js +185 -0
- package/blocks_horizontal/wedo.js +316 -0
- package/blocks_vertical/control.js +531 -0
- package/blocks_vertical/data.js +670 -0
- package/blocks_vertical/default_toolbox.js +564 -0
- package/blocks_vertical/event.js +328 -0
- package/blocks_vertical/extensions.js +294 -0
- package/blocks_vertical/looks.js +589 -0
- package/blocks_vertical/motion.js +584 -0
- package/blocks_vertical/operators.js +470 -0
- package/blocks_vertical/procedures.js +971 -0
- package/blocks_vertical/sensing.js +539 -0
- package/blocks_vertical/sound.js +245 -0
- package/blocks_vertical/vertical_extensions.js +264 -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 +630 -0
- package/cleanup.sh +101 -0
- package/core/block.js +1811 -0
- package/core/block_animations.js +107 -0
- package/core/block_drag_surface.js +299 -0
- package/core/block_dragger.js +421 -0
- package/core/block_events.js +531 -0
- package/core/block_render_svg_horizontal.js +890 -0
- package/core/block_render_svg_vertical.js +1732 -0
- package/core/block_svg.js +1314 -0
- package/core/blockly.js +622 -0
- package/core/blocks.js +37 -0
- package/core/bubble.js +664 -0
- package/core/bubble_dragger.js +285 -0
- package/core/colours.js +147 -0
- package/core/comment.js +293 -0
- package/core/comment_events.js +539 -0
- package/core/connection.js +766 -0
- package/core/connection_db.js +300 -0
- package/core/constants.js +387 -0
- package/core/contextmenu.js +519 -0
- package/core/css.js +1350 -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 +807 -0
- package/core/field_angle.js +398 -0
- package/core/field_checkbox.js +133 -0
- package/core/field_colour.js +253 -0
- package/core/field_colour_slider.js +386 -0
- package/core/field_date.js +353 -0
- package/core/field_dropdown.js +483 -0
- package/core/field_iconmenu.js +307 -0
- package/core/field_image.js +193 -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 +923 -0
- package/core/flyout_button.js +322 -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 +1011 -0
- package/core/grid.js +227 -0
- package/core/icon.js +205 -0
- package/core/inject.js +491 -0
- package/core/input.js +285 -0
- package/core/insertion_marker_manager.js +678 -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 +577 -0
- package/core/rendered_connection.js +417 -0
- package/core/scratch_block_comment.js +653 -0
- package/core/scratch_blocks_utils.js +244 -0
- package/core/scratch_bubble.js +696 -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 +801 -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 +948 -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 +432 -0
- package/core/workspace_comment_render_svg.js +723 -0
- package/core/workspace_comment_svg.js +611 -0
- package/core/workspace_drag_surface_svg.js +195 -0
- package/core/workspace_dragger.js +132 -0
- package/core/workspace_svg.js +2267 -0
- package/core/xml.js +919 -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 +165 -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_translations.sh +17 -0
- package/i18n/sync_tx_translations.js +121 -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/1x1.gif +0 -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/sprites.svg +74 -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 +353 -0
- package/msg/scratch_msgs.js +21247 -0
- package/package.json +52 -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/core/msg.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Visual Blocks Editor
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2013 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 Empty name space for the Message singleton.
|
|
23
|
+
* @author scr@google.com (Sheridan Rawlins)
|
|
24
|
+
*/
|
|
25
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Name space for the Msg singleton.
|
|
29
|
+
* Msg gets populated in the message files.
|
|
30
|
+
*/
|
|
31
|
+
goog.provide('Blockly.Msg');
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Back up original getMsg function.
|
|
36
|
+
* @type {!Function}
|
|
37
|
+
*/
|
|
38
|
+
goog.getMsgOrig = goog.getMsg;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Gets a localized message.
|
|
42
|
+
* Overrides the default Closure function to check for a Blockly.Msg first.
|
|
43
|
+
* Used infrequently, only known case is TODAY button in date picker.
|
|
44
|
+
* @param {string} str Translatable string, places holders in the form {$foo}.
|
|
45
|
+
* @param {Object<string, string>=} opt_values Maps place holder name to value.
|
|
46
|
+
* @return {string} message with placeholders filled.
|
|
47
|
+
* @suppress {duplicate}
|
|
48
|
+
*/
|
|
49
|
+
goog.getMsg = function(str, opt_values) {
|
|
50
|
+
var key = goog.getMsg.blocklyMsgMap[str];
|
|
51
|
+
if (key) {
|
|
52
|
+
str = Blockly.Msg[key];
|
|
53
|
+
}
|
|
54
|
+
return goog.getMsgOrig(str, opt_values);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Mapping of Closure messages to Blockly.Msg names.
|
|
59
|
+
*/
|
|
60
|
+
goog.getMsg.blocklyMsgMap = {
|
|
61
|
+
'Today': 'TODAY'
|
|
62
|
+
};
|
package/core/mutator.js
ADDED
|
@@ -0,0 +1,426 @@
|
|
|
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 mutator dialog. A mutator allows the
|
|
23
|
+
* user to change the shape of a block using a nested blocks editor.
|
|
24
|
+
* @author fraser@google.com (Neil Fraser)
|
|
25
|
+
*/
|
|
26
|
+
'use strict';
|
|
27
|
+
|
|
28
|
+
goog.provide('Blockly.Mutator');
|
|
29
|
+
|
|
30
|
+
goog.require('Blockly.Bubble');
|
|
31
|
+
goog.require('Blockly.Events.BlockChange');
|
|
32
|
+
goog.require('Blockly.Events.Ui');
|
|
33
|
+
goog.require('Blockly.Icon');
|
|
34
|
+
goog.require('Blockly.WorkspaceSvg');
|
|
35
|
+
goog.require('goog.dom');
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Class for a mutator dialog.
|
|
40
|
+
* @param {!Array.<string>} quarkNames List of names of sub-blocks for flyout.
|
|
41
|
+
* @extends {Blockly.Icon}
|
|
42
|
+
* @constructor
|
|
43
|
+
*/
|
|
44
|
+
Blockly.Mutator = function(quarkNames) {
|
|
45
|
+
Blockly.Mutator.superClass_.constructor.call(this, null);
|
|
46
|
+
this.quarkNames_ = quarkNames;
|
|
47
|
+
};
|
|
48
|
+
goog.inherits(Blockly.Mutator, Blockly.Icon);
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Width of workspace.
|
|
52
|
+
* @private
|
|
53
|
+
*/
|
|
54
|
+
Blockly.Mutator.prototype.workspaceWidth_ = 0;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Height of workspace.
|
|
58
|
+
* @private
|
|
59
|
+
*/
|
|
60
|
+
Blockly.Mutator.prototype.workspaceHeight_ = 0;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Draw the mutator icon.
|
|
64
|
+
* @param {!Element} group The icon group.
|
|
65
|
+
* @private
|
|
66
|
+
*/
|
|
67
|
+
Blockly.Mutator.prototype.drawIcon_ = function(group) {
|
|
68
|
+
// Square with rounded corners.
|
|
69
|
+
Blockly.utils.createSvgElement('rect',
|
|
70
|
+
{
|
|
71
|
+
'class': 'blocklyIconShape',
|
|
72
|
+
'rx': '4',
|
|
73
|
+
'ry': '4',
|
|
74
|
+
'height': '16',
|
|
75
|
+
'width': '16'
|
|
76
|
+
},
|
|
77
|
+
group);
|
|
78
|
+
// Gear teeth.
|
|
79
|
+
Blockly.utils.createSvgElement('path',
|
|
80
|
+
{
|
|
81
|
+
'class': 'blocklyIconSymbol',
|
|
82
|
+
'd': 'm4.203,7.296 0,1.368 -0.92,0.677 -0.11,0.41 0.9,1.559 0.41,' +
|
|
83
|
+
'0.11 1.043,-0.457 1.187,0.683 0.127,1.134 0.3,0.3 1.8,0 0.3,' +
|
|
84
|
+
'-0.299 0.127,-1.138 1.185,-0.682 1.046,0.458 0.409,-0.11 0.9,' +
|
|
85
|
+
'-1.559 -0.11,-0.41 -0.92,-0.677 0,-1.366 0.92,-0.677 0.11,' +
|
|
86
|
+
'-0.41 -0.9,-1.559 -0.409,-0.109 -1.046,0.458 -1.185,-0.682 ' +
|
|
87
|
+
'-0.127,-1.138 -0.3,-0.299 -1.8,0 -0.3,0.3 -0.126,1.135 -1.187,' +
|
|
88
|
+
'0.682 -1.043,-0.457 -0.41,0.11 -0.899,1.559 0.108,0.409z'
|
|
89
|
+
},
|
|
90
|
+
group);
|
|
91
|
+
// Axle hole.
|
|
92
|
+
Blockly.utils.createSvgElement(
|
|
93
|
+
'circle',
|
|
94
|
+
{
|
|
95
|
+
'class': 'blocklyIconShape',
|
|
96
|
+
'r': '2.7',
|
|
97
|
+
'cx': '8',
|
|
98
|
+
'cy': '8'
|
|
99
|
+
},
|
|
100
|
+
group);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Clicking on the icon toggles if the mutator bubble is visible.
|
|
105
|
+
* Disable if block is uneditable.
|
|
106
|
+
* @param {!Event} e Mouse click event.
|
|
107
|
+
* @private
|
|
108
|
+
* @override
|
|
109
|
+
*/
|
|
110
|
+
Blockly.Mutator.prototype.iconClick_ = function(e) {
|
|
111
|
+
if (this.block_.isEditable()) {
|
|
112
|
+
Blockly.Icon.prototype.iconClick_.call(this, e);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Create the editor for the mutator's bubble.
|
|
118
|
+
* @return {!Element} The top-level node of the editor.
|
|
119
|
+
* @private
|
|
120
|
+
*/
|
|
121
|
+
Blockly.Mutator.prototype.createEditor_ = function() {
|
|
122
|
+
/* Create the editor. Here's the markup that will be generated:
|
|
123
|
+
<svg>
|
|
124
|
+
[Workspace]
|
|
125
|
+
</svg>
|
|
126
|
+
*/
|
|
127
|
+
this.svgDialog_ = Blockly.utils.createSvgElement('svg',
|
|
128
|
+
{'x': Blockly.Bubble.BORDER_WIDTH, 'y': Blockly.Bubble.BORDER_WIDTH},
|
|
129
|
+
null);
|
|
130
|
+
// Convert the list of names into a list of XML objects for the flyout.
|
|
131
|
+
if (this.quarkNames_.length) {
|
|
132
|
+
var quarkXml = goog.dom.createDom('xml');
|
|
133
|
+
for (var i = 0, quarkName; quarkName = this.quarkNames_[i]; i++) {
|
|
134
|
+
quarkXml.appendChild(goog.dom.createDom('block', {'type': quarkName}));
|
|
135
|
+
}
|
|
136
|
+
} else {
|
|
137
|
+
var quarkXml = null;
|
|
138
|
+
}
|
|
139
|
+
var workspaceOptions = {
|
|
140
|
+
languageTree: quarkXml,
|
|
141
|
+
parentWorkspace: this.block_.workspace,
|
|
142
|
+
pathToMedia: this.block_.workspace.options.pathToMedia,
|
|
143
|
+
RTL: this.block_.RTL,
|
|
144
|
+
toolboxPosition: this.block_.RTL ? Blockly.TOOLBOX_AT_RIGHT :
|
|
145
|
+
Blockly.TOOLBOX_AT_LEFT,
|
|
146
|
+
horizontalLayout: false,
|
|
147
|
+
getMetrics: this.getFlyoutMetrics_.bind(this),
|
|
148
|
+
setMetrics: null
|
|
149
|
+
};
|
|
150
|
+
this.workspace_ = new Blockly.WorkspaceSvg(workspaceOptions, this.block_.workspace.dragSurface);
|
|
151
|
+
this.workspace_.isMutator = true;
|
|
152
|
+
|
|
153
|
+
// Mutator flyouts go inside the mutator workspace's <g> rather than in
|
|
154
|
+
// a top level svg. Instead of handling scale themselves, mutators
|
|
155
|
+
// inherit scale from the parent workspace.
|
|
156
|
+
// To fix this, scale needs to be applied at a different level in the dom.
|
|
157
|
+
var flyoutSvg = this.workspace_.addFlyout_('g');
|
|
158
|
+
var background = this.workspace_.createDom('blocklyMutatorBackground');
|
|
159
|
+
|
|
160
|
+
// Insert the flyout after the <rect> but before the block canvas so that
|
|
161
|
+
// the flyout is underneath in z-order. This makes blocks layering during
|
|
162
|
+
// dragging work properly.
|
|
163
|
+
background.insertBefore(flyoutSvg, this.workspace_.svgBlockCanvas_);
|
|
164
|
+
this.svgDialog_.appendChild(background);
|
|
165
|
+
|
|
166
|
+
return this.svgDialog_;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Add or remove the UI indicating if this icon may be clicked or not.
|
|
171
|
+
*/
|
|
172
|
+
Blockly.Mutator.prototype.updateEditable = function() {
|
|
173
|
+
if (!this.block_.isInFlyout) {
|
|
174
|
+
if (this.block_.isEditable()) {
|
|
175
|
+
if (this.iconGroup_) {
|
|
176
|
+
Blockly.utils.removeClass(
|
|
177
|
+
/** @type {!Element} */ (this.iconGroup_),
|
|
178
|
+
'blocklyIconGroupReadonly');
|
|
179
|
+
}
|
|
180
|
+
} else {
|
|
181
|
+
// Close any mutator bubble. Icon is not clickable.
|
|
182
|
+
this.setVisible(false);
|
|
183
|
+
if (this.iconGroup_) {
|
|
184
|
+
Blockly.utils.addClass(
|
|
185
|
+
/** @type {!Element} */ (this.iconGroup_),
|
|
186
|
+
'blocklyIconGroupReadonly');
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
// Default behaviour for an icon.
|
|
191
|
+
Blockly.Icon.prototype.updateEditable.call(this);
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Callback function triggered when the bubble has resized.
|
|
196
|
+
* Resize the workspace accordingly.
|
|
197
|
+
* @private
|
|
198
|
+
*/
|
|
199
|
+
Blockly.Mutator.prototype.resizeBubble_ = function() {
|
|
200
|
+
var doubleBorderWidth = 2 * Blockly.Bubble.BORDER_WIDTH;
|
|
201
|
+
var workspaceSize = this.workspace_.getCanvas().getBBox();
|
|
202
|
+
var width;
|
|
203
|
+
if (this.block_.RTL) {
|
|
204
|
+
width = -workspaceSize.x;
|
|
205
|
+
} else {
|
|
206
|
+
width = workspaceSize.width + workspaceSize.x;
|
|
207
|
+
}
|
|
208
|
+
var height = workspaceSize.height + doubleBorderWidth * 3;
|
|
209
|
+
if (this.workspace_.flyout_) {
|
|
210
|
+
var flyoutMetrics = this.workspace_.flyout_.getMetrics_();
|
|
211
|
+
height = Math.max(height, flyoutMetrics.contentHeight + 20);
|
|
212
|
+
}
|
|
213
|
+
width += doubleBorderWidth * 3;
|
|
214
|
+
// Only resize if the size difference is significant. Eliminates shuddering.
|
|
215
|
+
if (Math.abs(this.workspaceWidth_ - width) > doubleBorderWidth ||
|
|
216
|
+
Math.abs(this.workspaceHeight_ - height) > doubleBorderWidth) {
|
|
217
|
+
// Record some layout information for getFlyoutMetrics_.
|
|
218
|
+
this.workspaceWidth_ = width;
|
|
219
|
+
this.workspaceHeight_ = height;
|
|
220
|
+
// Resize the bubble.
|
|
221
|
+
this.bubble_.setBubbleSize(
|
|
222
|
+
width + doubleBorderWidth, height + doubleBorderWidth);
|
|
223
|
+
this.svgDialog_.setAttribute('width', this.workspaceWidth_);
|
|
224
|
+
this.svgDialog_.setAttribute('height', this.workspaceHeight_);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (this.block_.RTL) {
|
|
228
|
+
// Scroll the workspace to always left-align.
|
|
229
|
+
var translation = 'translate(' + this.workspaceWidth_ + ',0)';
|
|
230
|
+
this.workspace_.getCanvas().setAttribute('transform', translation);
|
|
231
|
+
}
|
|
232
|
+
this.workspace_.resize();
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Show or hide the mutator bubble.
|
|
237
|
+
* @param {boolean} visible True if the bubble should be visible.
|
|
238
|
+
*/
|
|
239
|
+
Blockly.Mutator.prototype.setVisible = function(visible) {
|
|
240
|
+
if (visible == this.isVisible()) {
|
|
241
|
+
// No change.
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
Blockly.Events.fire(
|
|
245
|
+
new Blockly.Events.Ui(this.block_, 'mutatorOpen', !visible, visible));
|
|
246
|
+
if (visible) {
|
|
247
|
+
// Create the bubble.
|
|
248
|
+
this.bubble_ = new Blockly.Bubble(
|
|
249
|
+
/** @type {!Blockly.WorkspaceSvg} */ (this.block_.workspace),
|
|
250
|
+
this.createEditor_(), this.block_.svgPath_, this.iconXY_, null, null);
|
|
251
|
+
var tree = this.workspace_.options.languageTree;
|
|
252
|
+
if (tree) {
|
|
253
|
+
this.workspace_.flyout_.init(this.workspace_);
|
|
254
|
+
this.workspace_.flyout_.show(tree.childNodes);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
this.rootBlock_ = this.block_.decompose(this.workspace_);
|
|
258
|
+
var blocks = this.rootBlock_.getDescendants(false);
|
|
259
|
+
for (var i = 0, child; child = blocks[i]; i++) {
|
|
260
|
+
child.render();
|
|
261
|
+
}
|
|
262
|
+
// The root block should not be dragable or deletable.
|
|
263
|
+
this.rootBlock_.setMovable(false);
|
|
264
|
+
this.rootBlock_.setDeletable(false);
|
|
265
|
+
if (this.workspace_.flyout_) {
|
|
266
|
+
var margin = this.workspace_.flyout_.CORNER_RADIUS * 2;
|
|
267
|
+
var x = this.workspace_.flyout_.width_ + margin;
|
|
268
|
+
} else {
|
|
269
|
+
var margin = 16;
|
|
270
|
+
var x = margin;
|
|
271
|
+
}
|
|
272
|
+
if (this.block_.RTL) {
|
|
273
|
+
x = -x;
|
|
274
|
+
}
|
|
275
|
+
this.rootBlock_.moveBy(x, margin);
|
|
276
|
+
// Save the initial connections, then listen for further changes.
|
|
277
|
+
if (this.block_.saveConnections) {
|
|
278
|
+
var thisMutator = this;
|
|
279
|
+
this.block_.saveConnections(this.rootBlock_);
|
|
280
|
+
this.sourceListener_ = function() {
|
|
281
|
+
thisMutator.block_.saveConnections(thisMutator.rootBlock_);
|
|
282
|
+
};
|
|
283
|
+
this.block_.workspace.addChangeListener(this.sourceListener_);
|
|
284
|
+
}
|
|
285
|
+
this.resizeBubble_();
|
|
286
|
+
// When the mutator's workspace changes, update the source block.
|
|
287
|
+
this.workspace_.addChangeListener(this.workspaceChanged_.bind(this));
|
|
288
|
+
this.updateColour();
|
|
289
|
+
} else {
|
|
290
|
+
// Dispose of the bubble.
|
|
291
|
+
this.svgDialog_ = null;
|
|
292
|
+
this.workspace_.dispose();
|
|
293
|
+
this.workspace_ = null;
|
|
294
|
+
this.rootBlock_ = null;
|
|
295
|
+
this.bubble_.dispose();
|
|
296
|
+
this.bubble_ = null;
|
|
297
|
+
this.workspaceWidth_ = 0;
|
|
298
|
+
this.workspaceHeight_ = 0;
|
|
299
|
+
if (this.sourceListener_) {
|
|
300
|
+
this.block_.workspace.removeChangeListener(this.sourceListener_);
|
|
301
|
+
this.sourceListener_ = null;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Update the source block when the mutator's blocks are changed.
|
|
308
|
+
* Bump down any block that's too high.
|
|
309
|
+
* Fired whenever a change is made to the mutator's workspace.
|
|
310
|
+
* @private
|
|
311
|
+
*/
|
|
312
|
+
Blockly.Mutator.prototype.workspaceChanged_ = function() {
|
|
313
|
+
if (!this.workspace_.isDragging()) {
|
|
314
|
+
var blocks = this.workspace_.getTopBlocks(false);
|
|
315
|
+
var MARGIN = 20;
|
|
316
|
+
for (var b = 0, block; block = blocks[b]; b++) {
|
|
317
|
+
var blockXY = block.getRelativeToSurfaceXY();
|
|
318
|
+
var blockHW = block.getHeightWidth();
|
|
319
|
+
if (blockXY.y + blockHW.height < MARGIN) {
|
|
320
|
+
// Bump any block that's above the top back inside.
|
|
321
|
+
block.moveBy(0, MARGIN - blockHW.height - blockXY.y);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// When the mutator's workspace changes, update the source block.
|
|
327
|
+
if (this.rootBlock_.workspace == this.workspace_) {
|
|
328
|
+
Blockly.Events.setGroup(true);
|
|
329
|
+
var block = this.block_;
|
|
330
|
+
var oldMutationDom = block.mutationToDom();
|
|
331
|
+
var oldMutation = oldMutationDom && Blockly.Xml.domToText(oldMutationDom);
|
|
332
|
+
// Switch off rendering while the source block is rebuilt.
|
|
333
|
+
var savedRendered = block.rendered;
|
|
334
|
+
block.rendered = false;
|
|
335
|
+
// Allow the source block to rebuild itself.
|
|
336
|
+
block.compose(this.rootBlock_);
|
|
337
|
+
// Restore rendering and show the changes.
|
|
338
|
+
block.rendered = savedRendered;
|
|
339
|
+
// Mutation may have added some elements that need initializing.
|
|
340
|
+
block.initSvg();
|
|
341
|
+
var newMutationDom = block.mutationToDom();
|
|
342
|
+
var newMutation = newMutationDom && Blockly.Xml.domToText(newMutationDom);
|
|
343
|
+
if (oldMutation != newMutation) {
|
|
344
|
+
Blockly.Events.fire(new Blockly.Events.BlockChange(
|
|
345
|
+
block, 'mutation', null, oldMutation, newMutation));
|
|
346
|
+
// Ensure that any bump is part of this mutation's event group.
|
|
347
|
+
var group = Blockly.Events.getGroup();
|
|
348
|
+
setTimeout(function() {
|
|
349
|
+
Blockly.Events.setGroup(group);
|
|
350
|
+
block.bumpNeighbours_();
|
|
351
|
+
Blockly.Events.setGroup(false);
|
|
352
|
+
}, Blockly.BUMP_DELAY);
|
|
353
|
+
}
|
|
354
|
+
if (block.rendered) {
|
|
355
|
+
block.render();
|
|
356
|
+
}
|
|
357
|
+
// Don't update the bubble until the drag has ended, to avoid moving blocks
|
|
358
|
+
// under the cursor.
|
|
359
|
+
if (!this.workspace_.isDragging()) {
|
|
360
|
+
this.resizeBubble_();
|
|
361
|
+
}
|
|
362
|
+
Blockly.Events.setGroup(false);
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Return an object with all the metrics required to size scrollbars for the
|
|
368
|
+
* mutator flyout. The following properties are computed:
|
|
369
|
+
* .viewHeight: Height of the visible rectangle,
|
|
370
|
+
* .viewWidth: Width of the visible rectangle,
|
|
371
|
+
* .absoluteTop: Top-edge of view.
|
|
372
|
+
* .absoluteLeft: Left-edge of view.
|
|
373
|
+
* @return {!Object} Contains size and position metrics of mutator dialog's
|
|
374
|
+
* workspace.
|
|
375
|
+
* @private
|
|
376
|
+
*/
|
|
377
|
+
Blockly.Mutator.prototype.getFlyoutMetrics_ = function() {
|
|
378
|
+
return {
|
|
379
|
+
viewHeight: this.workspaceHeight_,
|
|
380
|
+
viewWidth: this.workspaceWidth_,
|
|
381
|
+
absoluteTop: 0,
|
|
382
|
+
absoluteLeft: 0
|
|
383
|
+
};
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Dispose of this mutator.
|
|
388
|
+
*/
|
|
389
|
+
Blockly.Mutator.prototype.dispose = function() {
|
|
390
|
+
this.block_.mutator = null;
|
|
391
|
+
Blockly.Icon.prototype.dispose.call(this);
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Reconnect an block to a mutated input.
|
|
396
|
+
* @param {Blockly.Connection} connectionChild Connection on child block.
|
|
397
|
+
* @param {!Blockly.Block} block Parent block.
|
|
398
|
+
* @param {string} inputName Name of input on parent block.
|
|
399
|
+
* @return {boolean} True iff a reconnection was made, false otherwise.
|
|
400
|
+
*/
|
|
401
|
+
Blockly.Mutator.reconnect = function(connectionChild, block, inputName) {
|
|
402
|
+
if (!connectionChild || !connectionChild.getSourceBlock().workspace) {
|
|
403
|
+
return false; // No connection or block has been deleted.
|
|
404
|
+
}
|
|
405
|
+
var connectionParent = block.getInput(inputName).connection;
|
|
406
|
+
var currentParent = connectionChild.targetBlock();
|
|
407
|
+
if ((!currentParent || currentParent == block) &&
|
|
408
|
+
connectionParent.targetConnection != connectionChild) {
|
|
409
|
+
if (connectionParent.isConnected()) {
|
|
410
|
+
// There's already something connected here. Get rid of it.
|
|
411
|
+
connectionParent.disconnect();
|
|
412
|
+
}
|
|
413
|
+
connectionParent.connect(connectionChild);
|
|
414
|
+
return true;
|
|
415
|
+
}
|
|
416
|
+
return false;
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
// Export symbols that would otherwise be renamed by Closure compiler.
|
|
420
|
+
if (!goog.global['Blockly']) {
|
|
421
|
+
goog.global['Blockly'] = {};
|
|
422
|
+
}
|
|
423
|
+
if (!goog.global['Blockly']['Mutator']) {
|
|
424
|
+
goog.global['Blockly']['Mutator'] = {};
|
|
425
|
+
}
|
|
426
|
+
goog.global['Blockly']['Mutator']['reconnect'] = Blockly.Mutator.reconnect;
|
package/core/names.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
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 Utility functions for handling variables and procedure names.
|
|
23
|
+
* @author fraser@google.com (Neil Fraser)
|
|
24
|
+
*/
|
|
25
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
goog.provide('Blockly.Names');
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Class for a database of entity names (variables, functions, etc).
|
|
32
|
+
* @param {string} reservedWords A comma-separated string of words that are
|
|
33
|
+
* illegal for use as names in a language (e.g. 'new,if,this,...').
|
|
34
|
+
* @param {string=} opt_variablePrefix Some languages need a '$' or a namespace
|
|
35
|
+
* before all variable names.
|
|
36
|
+
* @constructor
|
|
37
|
+
*/
|
|
38
|
+
Blockly.Names = function(reservedWords, opt_variablePrefix) {
|
|
39
|
+
this.variablePrefix_ = opt_variablePrefix || '';
|
|
40
|
+
this.reservedDict_ = Object.create(null);
|
|
41
|
+
if (reservedWords) {
|
|
42
|
+
var splitWords = reservedWords.split(',');
|
|
43
|
+
for (var i = 0; i < splitWords.length; i++) {
|
|
44
|
+
this.reservedDict_[splitWords[i]] = true;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
this.reset();
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Constant to separate developer variable names from user-defined variable
|
|
52
|
+
* names when running generators.
|
|
53
|
+
* A developer variable will be declared as a global in the generated code, but
|
|
54
|
+
* will never be shown to the user in the workspace or stored in the variable
|
|
55
|
+
* map.
|
|
56
|
+
*/
|
|
57
|
+
Blockly.Names.DEVELOPER_VARIABLE_TYPE = 'DEVELOPER_VARIABLE';
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* When JavaScript (or most other languages) is generated, variable 'foo' and
|
|
61
|
+
* procedure 'foo' would collide. However, Blockly has no such problems since
|
|
62
|
+
* variable get 'foo' and procedure call 'foo' are unambiguous.
|
|
63
|
+
* Therefore, Blockly keeps a separate type name to disambiguate.
|
|
64
|
+
* getName('foo', 'variable') -> 'foo'
|
|
65
|
+
* getName('foo', 'procedure') -> 'foo2'
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Empty the database and start from scratch. The reserved words are kept.
|
|
70
|
+
*/
|
|
71
|
+
Blockly.Names.prototype.reset = function() {
|
|
72
|
+
this.db_ = Object.create(null);
|
|
73
|
+
this.dbReverse_ = Object.create(null);
|
|
74
|
+
this.variableMap_ = null;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Set the variable map that maps from variable name to variable object.
|
|
79
|
+
* @param {!Blockly.VariableMap} map The map to track.
|
|
80
|
+
* @package
|
|
81
|
+
*/
|
|
82
|
+
Blockly.Names.prototype.setVariableMap = function(map) {
|
|
83
|
+
this.variableMap_ = map;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Get the name for a user-defined variable, based on its ID.
|
|
88
|
+
* This should only be used for variables of type Blockly.Variables.NAME_TYPE.
|
|
89
|
+
* @param {string} id The ID to look up in the variable map.
|
|
90
|
+
* @return {?string} The name of the referenced variable, or null if there was
|
|
91
|
+
* no variable map or the variable was not found in the map.
|
|
92
|
+
* @private
|
|
93
|
+
*/
|
|
94
|
+
Blockly.Names.prototype.getNameForUserVariable_ = function(id) {
|
|
95
|
+
if (!this.variableMap_) {
|
|
96
|
+
console.log('Deprecated call to Blockly.Names.prototype.getName without ' +
|
|
97
|
+
'defining a variable map. To fix, add the folowing code in your ' +
|
|
98
|
+
'generator\'s init() function:\n' +
|
|
99
|
+
'Blockly.YourGeneratorName.variableDB_.setVariableMap(' +
|
|
100
|
+
'workspace.getVariableMap());');
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
var variable = this.variableMap_.getVariableById(id);
|
|
104
|
+
if (variable) {
|
|
105
|
+
return variable.name;
|
|
106
|
+
} else {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Convert a Blockly entity name to a legal exportable entity name.
|
|
113
|
+
* @param {string} name The Blockly entity name (no constraints).
|
|
114
|
+
* @param {string} type The type of entity in Blockly
|
|
115
|
+
* ('VARIABLE', 'PROCEDURE', 'BUILTIN', etc...).
|
|
116
|
+
* @return {string} An entity name that is legal in the exported language.
|
|
117
|
+
*/
|
|
118
|
+
Blockly.Names.prototype.getName = function(name, type) {
|
|
119
|
+
if (type == Blockly.Variables.NAME_TYPE) {
|
|
120
|
+
var varName = this.getNameForUserVariable_(name);
|
|
121
|
+
if (varName) {
|
|
122
|
+
name = varName;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
var normalized = name.toLowerCase() + '_' + type;
|
|
126
|
+
|
|
127
|
+
var isVarType = type == Blockly.Variables.NAME_TYPE ||
|
|
128
|
+
type == Blockly.Names.DEVELOPER_VARIABLE_TYPE;
|
|
129
|
+
|
|
130
|
+
var prefix = isVarType ? this.variablePrefix_ : '';
|
|
131
|
+
if (normalized in this.db_) {
|
|
132
|
+
return prefix + this.db_[normalized];
|
|
133
|
+
}
|
|
134
|
+
var safeName = this.getDistinctName(name, type);
|
|
135
|
+
this.db_[normalized] = safeName.substr(prefix.length);
|
|
136
|
+
return safeName;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Convert a Blockly entity name to a legal exportable entity name.
|
|
141
|
+
* Ensure that this is a new name not overlapping any previously defined name.
|
|
142
|
+
* Also check against list of reserved words for the current language and
|
|
143
|
+
* ensure name doesn't collide.
|
|
144
|
+
* @param {string} name The Blockly entity name (no constraints).
|
|
145
|
+
* @param {string} type The type of entity in Blockly
|
|
146
|
+
* ('VARIABLE', 'PROCEDURE', 'BUILTIN', etc...).
|
|
147
|
+
* @return {string} An entity name that is legal in the exported language.
|
|
148
|
+
*/
|
|
149
|
+
Blockly.Names.prototype.getDistinctName = function(name, type) {
|
|
150
|
+
var safeName = this.safeName_(name);
|
|
151
|
+
var i = '';
|
|
152
|
+
while (this.dbReverse_[safeName + i] ||
|
|
153
|
+
(safeName + i) in this.reservedDict_) {
|
|
154
|
+
// Collision with existing name. Create a unique name.
|
|
155
|
+
i = i ? i + 1 : 2;
|
|
156
|
+
}
|
|
157
|
+
safeName += i;
|
|
158
|
+
this.dbReverse_[safeName] = true;
|
|
159
|
+
var isVarType = type == Blockly.Variables.NAME_TYPE ||
|
|
160
|
+
type == Blockly.Names.DEVELOPER_VARIABLE_TYPE;
|
|
161
|
+
var prefix = isVarType ? this.variablePrefix_ : '';
|
|
162
|
+
return prefix + safeName;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Given a proposed entity name, generate a name that conforms to the
|
|
167
|
+
* [_A-Za-z][_A-Za-z0-9]* format that most languages consider legal for
|
|
168
|
+
* variables.
|
|
169
|
+
* @param {string} name Potentially illegal entity name.
|
|
170
|
+
* @return {string} Safe entity name.
|
|
171
|
+
* @private
|
|
172
|
+
*/
|
|
173
|
+
Blockly.Names.prototype.safeName_ = function(name) {
|
|
174
|
+
if (!name) {
|
|
175
|
+
name = 'unnamed';
|
|
176
|
+
} else {
|
|
177
|
+
// Unfortunately names in non-latin characters will look like
|
|
178
|
+
// _E9_9F_B3_E4_B9_90 which is pretty meaningless.
|
|
179
|
+
// https://github.com/google/blockly/issues/1654
|
|
180
|
+
name = encodeURI(name.replace(/ /g, '_')).replace(/[^\w]/g, '_');
|
|
181
|
+
// Most languages don't allow names with leading numbers.
|
|
182
|
+
if ('0123456789'.indexOf(name[0]) != -1) {
|
|
183
|
+
name = 'my_' + name;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return name;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Do the given two entity names refer to the same entity?
|
|
191
|
+
* Blockly names are case-insensitive.
|
|
192
|
+
* @param {string} name1 First name.
|
|
193
|
+
* @param {string} name2 Second name.
|
|
194
|
+
* @return {boolean} True if names are the same.
|
|
195
|
+
*/
|
|
196
|
+
Blockly.Names.equals = function(name1, name2) {
|
|
197
|
+
return name1.toLowerCase() == name2.toLowerCase();
|
|
198
|
+
};
|