@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
package/core/xml.js
ADDED
|
@@ -0,0 +1,930 @@
|
|
|
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 XML reader and writer.
|
|
23
|
+
* @author fraser@google.com (Neil Fraser)
|
|
24
|
+
*/
|
|
25
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @name Blockly.Xml
|
|
29
|
+
* @namespace
|
|
30
|
+
**/
|
|
31
|
+
goog.provide('Blockly.Xml');
|
|
32
|
+
|
|
33
|
+
goog.require('Blockly.Events.BlockCreate');
|
|
34
|
+
goog.require('Blockly.Events.VarCreate');
|
|
35
|
+
|
|
36
|
+
goog.require('goog.asserts');
|
|
37
|
+
goog.require('goog.dom');
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Encode a block tree as XML.
|
|
42
|
+
* @param {!Blockly.Workspace} workspace The workspace containing blocks.
|
|
43
|
+
* @param {boolean=} opt_noId True if the encoder should skip the block IDs.
|
|
44
|
+
* @return {!Element} XML document.
|
|
45
|
+
*/
|
|
46
|
+
Blockly.Xml.workspaceToDom = function(workspace, opt_noId) {
|
|
47
|
+
var xml = goog.dom.createDom('xml');
|
|
48
|
+
xml.appendChild(Blockly.Xml.variablesToDom(workspace.getAllVariables()));
|
|
49
|
+
var comments = workspace.getTopComments(true).filter(function(topComment) {
|
|
50
|
+
return topComment instanceof Blockly.WorkspaceComment;
|
|
51
|
+
});
|
|
52
|
+
for (var i = 0, comment; comment = comments[i]; i++) {
|
|
53
|
+
xml.appendChild(comment.toXmlWithXY(opt_noId));
|
|
54
|
+
}
|
|
55
|
+
var blocks = workspace.getTopBlocks(true);
|
|
56
|
+
for (var i = 0, block; block = blocks[i]; i++) {
|
|
57
|
+
xml.appendChild(Blockly.Xml.blockToDomWithXY(block, opt_noId));
|
|
58
|
+
}
|
|
59
|
+
return xml;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Encode a list of variables as XML.
|
|
64
|
+
* @param {!Array.<!Blockly.VariableModel>} variableList List of all variable
|
|
65
|
+
* models.
|
|
66
|
+
* @return {!Element} List of XML elements.
|
|
67
|
+
*/
|
|
68
|
+
Blockly.Xml.variablesToDom = function(variableList) {
|
|
69
|
+
var variables = goog.dom.createDom('variables');
|
|
70
|
+
for (var i = 0, variable; variable = variableList[i]; i++) {
|
|
71
|
+
var element = goog.dom.createDom('variable', null, variable.name);
|
|
72
|
+
element.setAttribute('type', variable.type);
|
|
73
|
+
element.setAttribute('id', variable.getId());
|
|
74
|
+
element.setAttribute('islocal', variable.isLocal);
|
|
75
|
+
element.setAttribute('isCloud', variable.isCloud);
|
|
76
|
+
variables.appendChild(element);
|
|
77
|
+
}
|
|
78
|
+
return variables;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Encode a block subtree as XML with XY coordinates.
|
|
83
|
+
* @param {!Blockly.Block} block The root block to encode.
|
|
84
|
+
* @param {boolean=} opt_noId True if the encoder should skip the block ID.
|
|
85
|
+
* @return {!Element} Tree of XML elements.
|
|
86
|
+
*/
|
|
87
|
+
Blockly.Xml.blockToDomWithXY = function(block, opt_noId) {
|
|
88
|
+
var width; // Not used in LTR.
|
|
89
|
+
if (block.workspace.RTL) {
|
|
90
|
+
width = block.workspace.getWidth();
|
|
91
|
+
}
|
|
92
|
+
var element = Blockly.Xml.blockToDom(block, opt_noId);
|
|
93
|
+
var xy = block.getRelativeToSurfaceXY();
|
|
94
|
+
element.setAttribute('x',
|
|
95
|
+
Math.round(block.workspace.RTL ? width - xy.x : xy.x));
|
|
96
|
+
element.setAttribute('y', Math.round(xy.y));
|
|
97
|
+
return element;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Encode a variable field as XML.
|
|
102
|
+
* @param {!Blockly.FieldVariable} field The field to encode.
|
|
103
|
+
* @return {?Element} XML element, or null if the field did not need to be
|
|
104
|
+
* serialized.
|
|
105
|
+
* @private
|
|
106
|
+
*/
|
|
107
|
+
Blockly.Xml.fieldToDomVariable_ = function(field) {
|
|
108
|
+
var id = field.getValue();
|
|
109
|
+
// The field had not been initialized fully before being serialized.
|
|
110
|
+
// This can happen if a block is created directly through a call to
|
|
111
|
+
// workspace.newBlock instead of from XML.
|
|
112
|
+
// The new block will be serialized for the first time when firing a block
|
|
113
|
+
// creation event.
|
|
114
|
+
if (id == null) {
|
|
115
|
+
field.initModel();
|
|
116
|
+
id = field.getValue();
|
|
117
|
+
}
|
|
118
|
+
// Get the variable directly from the field, instead of doing a lookup. This
|
|
119
|
+
// will work even if the variable has already been deleted. This can happen
|
|
120
|
+
// because the flyout defers deleting blocks until the next time the flyout is
|
|
121
|
+
// opened.
|
|
122
|
+
var variable = field.getVariable();
|
|
123
|
+
|
|
124
|
+
if (!variable) {
|
|
125
|
+
throw Error('Tried to serialize a variable field with no variable.');
|
|
126
|
+
}
|
|
127
|
+
var container = goog.dom.createDom('field', null, variable.name);
|
|
128
|
+
container.setAttribute('name', field.name);
|
|
129
|
+
container.setAttribute('id', variable.getId());
|
|
130
|
+
container.setAttribute('variabletype', variable.type);
|
|
131
|
+
return container;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Encode a field as XML.
|
|
136
|
+
* @param {!Blockly.Field} field The field to encode.
|
|
137
|
+
* @param {!Blockly.Workspace} workspace The workspace that the field is in.
|
|
138
|
+
* @return {?Element} XML element, or null if the field did not need to be
|
|
139
|
+
* serialized.
|
|
140
|
+
* @private
|
|
141
|
+
*/
|
|
142
|
+
Blockly.Xml.fieldToDom_ = function(field) {
|
|
143
|
+
if (field.name && field.SERIALIZABLE) {
|
|
144
|
+
if (field.referencesVariables()) {
|
|
145
|
+
return Blockly.Xml.fieldToDomVariable_(field);
|
|
146
|
+
} else {
|
|
147
|
+
var container = goog.dom.createDom('field', null, field.getValue());
|
|
148
|
+
container.setAttribute('name', field.name);
|
|
149
|
+
return container;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return null;
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Encode all of a block's fields as XML and attach them to the given tree of
|
|
157
|
+
* XML elements.
|
|
158
|
+
* @param {!Blockly.Block} block A block with fields to be encoded.
|
|
159
|
+
* @param {!Element} element The XML element to which the field DOM should be
|
|
160
|
+
* attached.
|
|
161
|
+
* @private
|
|
162
|
+
*/
|
|
163
|
+
Blockly.Xml.allFieldsToDom_ = function(block, element) {
|
|
164
|
+
for (var i = 0, input; input = block.inputList[i]; i++) {
|
|
165
|
+
for (var j = 0, field; field = input.fieldRow[j]; j++) {
|
|
166
|
+
var fieldDom = Blockly.Xml.fieldToDom_(field);
|
|
167
|
+
if (fieldDom) {
|
|
168
|
+
element.appendChild(fieldDom);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Encode a block subtree as XML.
|
|
176
|
+
* @param {!Blockly.Block} block The root block to encode.
|
|
177
|
+
* @param {boolean=} opt_noId True if the encoder should skip the block ID.
|
|
178
|
+
* @return {!Element} Tree of XML elements.
|
|
179
|
+
*/
|
|
180
|
+
Blockly.Xml.blockToDom = function(block, opt_noId) {
|
|
181
|
+
var element = goog.dom.createDom(block.isShadow() ? 'shadow' : 'block');
|
|
182
|
+
element.setAttribute('type', block.type);
|
|
183
|
+
if (!opt_noId) {
|
|
184
|
+
element.setAttribute('id', block.id);
|
|
185
|
+
}
|
|
186
|
+
if (block.mutationToDom) {
|
|
187
|
+
// Custom data for an advanced block.
|
|
188
|
+
var mutation = block.mutationToDom();
|
|
189
|
+
if (mutation && (mutation.hasChildNodes() || mutation.hasAttributes())) {
|
|
190
|
+
element.appendChild(mutation);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
Blockly.Xml.allFieldsToDom_(block, element);
|
|
195
|
+
|
|
196
|
+
Blockly.Xml.scratchCommentToDom_(block, element);
|
|
197
|
+
|
|
198
|
+
if (block.data) {
|
|
199
|
+
var dataElement = goog.dom.createDom('data', null, block.data);
|
|
200
|
+
element.appendChild(dataElement);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
for (var i = 0, input; input = block.inputList[i]; i++) {
|
|
204
|
+
var container;
|
|
205
|
+
var empty = true;
|
|
206
|
+
if (input.type == Blockly.DUMMY_INPUT) {
|
|
207
|
+
continue;
|
|
208
|
+
} else {
|
|
209
|
+
var childBlock = input.connection.targetBlock();
|
|
210
|
+
if (input.type == Blockly.INPUT_VALUE) {
|
|
211
|
+
container = goog.dom.createDom('value');
|
|
212
|
+
} else if (input.type == Blockly.NEXT_STATEMENT) {
|
|
213
|
+
container = goog.dom.createDom('statement');
|
|
214
|
+
}
|
|
215
|
+
var shadow = input.connection.getShadowDom();
|
|
216
|
+
if (shadow && (!childBlock || !childBlock.isShadow())) {
|
|
217
|
+
var shadowClone = Blockly.Xml.cloneShadow_(shadow);
|
|
218
|
+
// Remove the ID from the shadow dom clone if opt_noId
|
|
219
|
+
// is specified to true.
|
|
220
|
+
if (opt_noId && shadowClone.getAttribute('id')) {
|
|
221
|
+
shadowClone.removeAttribute('id');
|
|
222
|
+
}
|
|
223
|
+
container.appendChild(shadowClone);
|
|
224
|
+
}
|
|
225
|
+
if (childBlock) {
|
|
226
|
+
container.appendChild(Blockly.Xml.blockToDom(childBlock, opt_noId));
|
|
227
|
+
empty = false;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
container.setAttribute('name', input.name);
|
|
231
|
+
if (!empty) {
|
|
232
|
+
element.appendChild(container);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (block.inputsInlineDefault != block.inputsInline) {
|
|
236
|
+
element.setAttribute('inline', block.inputsInline);
|
|
237
|
+
}
|
|
238
|
+
if (block.isCollapsed()) {
|
|
239
|
+
element.setAttribute('collapsed', true);
|
|
240
|
+
}
|
|
241
|
+
if (block.disabled) {
|
|
242
|
+
element.setAttribute('disabled', true);
|
|
243
|
+
}
|
|
244
|
+
if (!block.isDeletable() && !block.isShadow()) {
|
|
245
|
+
element.setAttribute('deletable', false);
|
|
246
|
+
}
|
|
247
|
+
if (!block.isMovable() && !block.isShadow()) {
|
|
248
|
+
element.setAttribute('movable', false);
|
|
249
|
+
}
|
|
250
|
+
if (!block.isEditable()) {
|
|
251
|
+
element.setAttribute('editable', false);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
var nextBlock = block.getNextBlock();
|
|
255
|
+
if (nextBlock) {
|
|
256
|
+
var container = goog.dom.createDom('next', null,
|
|
257
|
+
Blockly.Xml.blockToDom(nextBlock, opt_noId));
|
|
258
|
+
element.appendChild(container);
|
|
259
|
+
}
|
|
260
|
+
var shadow = block.nextConnection && block.nextConnection.getShadowDom();
|
|
261
|
+
if (shadow && (!nextBlock || !nextBlock.isShadow())) {
|
|
262
|
+
container.appendChild(Blockly.Xml.cloneShadow_(shadow));
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return element;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Encode a ScratchBlockComment as XML.
|
|
270
|
+
* @param {!Blockly.ScratchBlockComment} block The block possibly containing
|
|
271
|
+
* a comment to encode.
|
|
272
|
+
* @param {!Element} element The XML element to which the comment should
|
|
273
|
+
* encoding should be attached.
|
|
274
|
+
* @private
|
|
275
|
+
*/
|
|
276
|
+
Blockly.Xml.scratchCommentToDom_ = function(block, element) {
|
|
277
|
+
var commentText = block.getCommentText();
|
|
278
|
+
if (commentText) {
|
|
279
|
+
var commentElement = goog.dom.createDom('comment', null, commentText);
|
|
280
|
+
if (typeof block.comment == 'object') {
|
|
281
|
+
commentElement.setAttribute('id', block.comment.id);
|
|
282
|
+
commentElement.setAttribute('pinned', block.comment.isVisible());
|
|
283
|
+
var hw;
|
|
284
|
+
if (block.comment instanceof Blockly.ScratchBlockComment) {
|
|
285
|
+
hw = block.comment.getHeightWidth();
|
|
286
|
+
} else {
|
|
287
|
+
hw = block.comment.getBubbleSize();
|
|
288
|
+
}
|
|
289
|
+
commentElement.setAttribute('h', hw.height);
|
|
290
|
+
commentElement.setAttribute('w', hw.width);
|
|
291
|
+
var xy = block.comment.getXY();
|
|
292
|
+
commentElement.setAttribute('x',
|
|
293
|
+
Math.round(block.workspace.RTL ? block.workspace.getWidth() - xy.x - hw.width :
|
|
294
|
+
xy.x));
|
|
295
|
+
commentElement.setAttribute('y', xy.y);
|
|
296
|
+
commentElement.setAttribute('minimized', block.comment.isMinimized());
|
|
297
|
+
|
|
298
|
+
}
|
|
299
|
+
element.appendChild(commentElement);
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Deeply clone the shadow's DOM so that changes don't back-wash to the block.
|
|
305
|
+
* @param {!Element} shadow A tree of XML elements.
|
|
306
|
+
* @return {!Element} A tree of XML elements.
|
|
307
|
+
* @private
|
|
308
|
+
*/
|
|
309
|
+
Blockly.Xml.cloneShadow_ = function(shadow) {
|
|
310
|
+
shadow = shadow.cloneNode(true);
|
|
311
|
+
// Walk the tree looking for whitespace. Don't prune whitespace in a tag.
|
|
312
|
+
var node = shadow;
|
|
313
|
+
var textNode;
|
|
314
|
+
while (node) {
|
|
315
|
+
if (node.firstChild) {
|
|
316
|
+
node = node.firstChild;
|
|
317
|
+
} else {
|
|
318
|
+
while (node && !node.nextSibling) {
|
|
319
|
+
textNode = node;
|
|
320
|
+
node = node.parentNode;
|
|
321
|
+
if (textNode.nodeType == 3 && textNode.data.trim() == '' &&
|
|
322
|
+
node.firstChild != textNode) {
|
|
323
|
+
// Prune whitespace after a tag.
|
|
324
|
+
goog.dom.removeNode(textNode);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (node) {
|
|
328
|
+
textNode = node;
|
|
329
|
+
node = node.nextSibling;
|
|
330
|
+
if (textNode.nodeType == 3 && textNode.data.trim() == '') {
|
|
331
|
+
// Prune whitespace before a tag.
|
|
332
|
+
goog.dom.removeNode(textNode);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return shadow;
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Converts a DOM structure into plain text.
|
|
342
|
+
* Currently the text format is fairly ugly: all one line with no whitespace.
|
|
343
|
+
* @param {!Element} dom A tree of XML elements.
|
|
344
|
+
* @return {string} Text representation.
|
|
345
|
+
*/
|
|
346
|
+
Blockly.Xml.domToText = function(dom) {
|
|
347
|
+
var oSerializer = new XMLSerializer();
|
|
348
|
+
return oSerializer.serializeToString(dom);
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Converts a DOM structure into properly indented text.
|
|
353
|
+
* @param {!Element} dom A tree of XML elements.
|
|
354
|
+
* @return {string} Text representation.
|
|
355
|
+
*/
|
|
356
|
+
Blockly.Xml.domToPrettyText = function(dom) {
|
|
357
|
+
// This function is not guaranteed to be correct for all XML.
|
|
358
|
+
// But it handles the XML that Blockly generates.
|
|
359
|
+
var blob = Blockly.Xml.domToText(dom);
|
|
360
|
+
// Place every open and close tag on its own line.
|
|
361
|
+
var lines = blob.split('<');
|
|
362
|
+
// Indent every line.
|
|
363
|
+
var indent = '';
|
|
364
|
+
for (var i = 1; i < lines.length; i++) {
|
|
365
|
+
var line = lines[i];
|
|
366
|
+
if (line[0] == '/') {
|
|
367
|
+
indent = indent.substring(2);
|
|
368
|
+
}
|
|
369
|
+
lines[i] = indent + '<' + line;
|
|
370
|
+
if (line[0] != '/' && line.slice(-2) != '/>') {
|
|
371
|
+
indent += ' ';
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
// Pull simple tags back together.
|
|
375
|
+
// E.g. <foo></foo>
|
|
376
|
+
var text = lines.join('\n');
|
|
377
|
+
text = text.replace(/(<(\w+)\b[^>]*>[^\n]*)\n *<\/\2>/g, '$1</$2>');
|
|
378
|
+
// Trim leading blank line.
|
|
379
|
+
return text.replace(/^\n/, '');
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Converts plain text into a DOM structure.
|
|
384
|
+
* Throws an error if XML doesn't parse.
|
|
385
|
+
* @param {string} text Text representation.
|
|
386
|
+
* @return {!Element} A tree of XML elements.
|
|
387
|
+
*/
|
|
388
|
+
Blockly.Xml.textToDom = function(text) {
|
|
389
|
+
var oParser = new DOMParser();
|
|
390
|
+
var dom = oParser.parseFromString(text, 'text/xml');
|
|
391
|
+
// The DOM should have one and only one top-level node, an XML tag, and no
|
|
392
|
+
// parse error (some invalid characters cause document to be truncated and
|
|
393
|
+
// a <parsererror> is generated)
|
|
394
|
+
if (!dom || !dom.firstChild ||
|
|
395
|
+
dom.firstChild.nodeName.toLowerCase() != 'xml' ||
|
|
396
|
+
dom.firstChild !== dom.lastChild ||
|
|
397
|
+
dom.getElementsByTagName('parsererror').length) {
|
|
398
|
+
// Matching Scratch, fall back to the lenient HTML parser when the text is
|
|
399
|
+
// not valid XML. Some "interesting" projects put control characters in
|
|
400
|
+
// inputs, which are illegal in XML 1.0 but tolerated by the HTML parser.
|
|
401
|
+
var htmlDom = oParser.parseFromString(text, 'text/html');
|
|
402
|
+
if (htmlDom && htmlDom.body.firstChild &&
|
|
403
|
+
htmlDom.body.firstChild.nodeName.toLowerCase() == 'xml') {
|
|
404
|
+
return htmlDom.body.firstChild;
|
|
405
|
+
}
|
|
406
|
+
// Whatever we got back from the parser is not XML.
|
|
407
|
+
goog.asserts.fail('Blockly.Xml.textToDom did not obtain a valid XML tree.');
|
|
408
|
+
}
|
|
409
|
+
return dom.firstChild;
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Clear the given workspace then decode an XML DOM and
|
|
414
|
+
* create blocks on the workspace.
|
|
415
|
+
* @param {!Element} xml XML DOM.
|
|
416
|
+
* @param {!Blockly.Workspace} workspace The workspace.
|
|
417
|
+
* @return {Array.<string>} An array containing new block ids.
|
|
418
|
+
*/
|
|
419
|
+
Blockly.Xml.clearWorkspaceAndLoadFromXml = function(xml, workspace) {
|
|
420
|
+
workspace.setResizesEnabled(false);
|
|
421
|
+
workspace.setToolboxRefreshEnabled(false);
|
|
422
|
+
workspace.clear();
|
|
423
|
+
var blockIds = Blockly.Xml.domToWorkspace(xml, workspace);
|
|
424
|
+
workspace.setResizesEnabled(true);
|
|
425
|
+
workspace.setToolboxRefreshEnabled(true);
|
|
426
|
+
return blockIds;
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Decode an XML DOM and create blocks on the workspace.
|
|
431
|
+
* @param {!Element} xml XML DOM.
|
|
432
|
+
* @param {!Blockly.Workspace} workspace The workspace.
|
|
433
|
+
* @return {Array.<string>} An array containing new block IDs.
|
|
434
|
+
*/
|
|
435
|
+
Blockly.Xml.domToWorkspace = function(xml, workspace) {
|
|
436
|
+
if (xml instanceof Blockly.Workspace) {
|
|
437
|
+
var swap = xml;
|
|
438
|
+
xml = workspace;
|
|
439
|
+
workspace = swap;
|
|
440
|
+
console.warn('Deprecated call to Blockly.Xml.domToWorkspace, ' +
|
|
441
|
+
'swap the arguments.');
|
|
442
|
+
}
|
|
443
|
+
var width; // Not used in LTR.
|
|
444
|
+
if (workspace.RTL) {
|
|
445
|
+
width = workspace.getWidth();
|
|
446
|
+
}
|
|
447
|
+
var newBlockIds = []; // A list of block IDs added by this call.
|
|
448
|
+
Blockly.Field.startCache();
|
|
449
|
+
// Safari 7.1.3 is known to provide node lists with extra references to
|
|
450
|
+
// children beyond the lists' length. Trust the length, do not use the
|
|
451
|
+
// looping pattern of checking the index for an object.
|
|
452
|
+
var childCount = xml.childNodes.length;
|
|
453
|
+
var existingGroup = Blockly.Events.getGroup();
|
|
454
|
+
if (!existingGroup) {
|
|
455
|
+
Blockly.Events.setGroup(true);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// Disable workspace resizes as an optimization.
|
|
459
|
+
if (workspace.setResizesEnabled) {
|
|
460
|
+
workspace.setResizesEnabled(false);
|
|
461
|
+
}
|
|
462
|
+
var variablesFirst = true;
|
|
463
|
+
try {
|
|
464
|
+
for (var i = 0; i < childCount; i++) {
|
|
465
|
+
var xmlChild = xml.childNodes[i];
|
|
466
|
+
var name = xmlChild.nodeName.toLowerCase();
|
|
467
|
+
if (name == 'block' ||
|
|
468
|
+
(name == 'shadow' && !Blockly.Events.recordUndo)) {
|
|
469
|
+
// Allow top-level shadow blocks if recordUndo is disabled since
|
|
470
|
+
// that means an undo is in progress. Such a block is expected
|
|
471
|
+
// to be moved to a nested destination in the next operation.
|
|
472
|
+
var block = Blockly.Xml.domToBlock(xmlChild, workspace);
|
|
473
|
+
newBlockIds.push(block.id);
|
|
474
|
+
var blockX = xmlChild.hasAttribute('x') ?
|
|
475
|
+
parseInt(xmlChild.getAttribute('x'), 10) : 10;
|
|
476
|
+
var blockY = xmlChild.hasAttribute('y') ?
|
|
477
|
+
parseInt(xmlChild.getAttribute('y'), 10) : 10;
|
|
478
|
+
if (!isNaN(blockX) && !isNaN(blockY)) {
|
|
479
|
+
block.moveBy(workspace.RTL ? width - blockX : blockX, blockY);
|
|
480
|
+
if (block.comment && typeof block.comment === 'object') {
|
|
481
|
+
var commentXY = block.comment.getXY();
|
|
482
|
+
var commentWidth = block.comment.getBubbleSize().width;
|
|
483
|
+
block.comment.moveTo(block.workspace.RTL ? width - commentXY.x - commentWidth : commentXY.x, commentXY.y);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
variablesFirst = false;
|
|
487
|
+
} else if (name == 'shadow') {
|
|
488
|
+
goog.asserts.fail('Shadow block cannot be a top-level block.');
|
|
489
|
+
variablesFirst = false;
|
|
490
|
+
} else if (name == 'comment') {
|
|
491
|
+
if (workspace.rendered) {
|
|
492
|
+
Blockly.WorkspaceCommentSvg.fromXml(xmlChild, workspace, width);
|
|
493
|
+
} else {
|
|
494
|
+
Blockly.WorkspaceComment.fromXml(xmlChild, workspace);
|
|
495
|
+
}
|
|
496
|
+
} else if (name == 'variables') {
|
|
497
|
+
if (variablesFirst) {
|
|
498
|
+
Blockly.Xml.domToVariables(xmlChild, workspace);
|
|
499
|
+
} else {
|
|
500
|
+
throw Error('\'variables\' tag must exist once before block and ' +
|
|
501
|
+
'shadow tag elements in the workspace XML, but it was found in ' +
|
|
502
|
+
'another location.');
|
|
503
|
+
}
|
|
504
|
+
variablesFirst = false;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
} finally {
|
|
508
|
+
if (!existingGroup) {
|
|
509
|
+
Blockly.Events.setGroup(false);
|
|
510
|
+
}
|
|
511
|
+
Blockly.Field.stopCache();
|
|
512
|
+
}
|
|
513
|
+
// Re-enable workspace resizing.
|
|
514
|
+
if (workspace.setResizesEnabled) {
|
|
515
|
+
workspace.setResizesEnabled(true);
|
|
516
|
+
}
|
|
517
|
+
return newBlockIds;
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Decode an XML DOM and create blocks on the workspace. Position the new
|
|
522
|
+
* blocks immediately below prior blocks, aligned by their starting edge.
|
|
523
|
+
* @param {!Element} xml The XML DOM.
|
|
524
|
+
* @param {!Blockly.Workspace} workspace The workspace to add to.
|
|
525
|
+
* @return {Array.<string>} An array containing new block IDs.
|
|
526
|
+
*/
|
|
527
|
+
Blockly.Xml.appendDomToWorkspace = function(xml, workspace) {
|
|
528
|
+
var bbox; // Bounding box of the current blocks.
|
|
529
|
+
// First check if we have a workspaceSvg, otherwise the blocks have no shape
|
|
530
|
+
// and the position does not matter.
|
|
531
|
+
if (workspace.hasOwnProperty('scale')) {
|
|
532
|
+
var savetab = Blockly.BlockSvg.TAB_WIDTH;
|
|
533
|
+
try {
|
|
534
|
+
Blockly.BlockSvg.TAB_WIDTH = 0;
|
|
535
|
+
bbox = workspace.getBlocksBoundingBox();
|
|
536
|
+
} finally {
|
|
537
|
+
Blockly.BlockSvg.TAB_WIDTH = savetab;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
// Load the new blocks into the workspace and get the IDs of the new blocks.
|
|
541
|
+
var newBlockIds = Blockly.Xml.domToWorkspace(xml,workspace);
|
|
542
|
+
if (bbox && bbox.height) { // check if any previous block
|
|
543
|
+
var offsetY = 0; // offset to add to y of the new block
|
|
544
|
+
var offsetX = 0;
|
|
545
|
+
var farY = bbox.y + bbox.height; //bottom position
|
|
546
|
+
var topX = bbox.x; // x of bounding box
|
|
547
|
+
// check position of the new blocks
|
|
548
|
+
var newX = Infinity; // x of top corner
|
|
549
|
+
var newY = Infinity; // y of top corner
|
|
550
|
+
for (var i = 0; i < newBlockIds.length; i++) {
|
|
551
|
+
var blockXY = workspace.getBlockById(newBlockIds[i]).getRelativeToSurfaceXY();
|
|
552
|
+
if (blockXY.y < newY) {
|
|
553
|
+
newY = blockXY.y;
|
|
554
|
+
}
|
|
555
|
+
if (blockXY.x < newX) { //if we align also on x
|
|
556
|
+
newX = blockXY.x;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
offsetY = farY - newY + Blockly.BlockSvg.SEP_SPACE_Y;
|
|
560
|
+
offsetX = topX - newX;
|
|
561
|
+
// move the new blocks to append them at the bottom
|
|
562
|
+
var width; // Not used in LTR.
|
|
563
|
+
if (workspace.RTL) {
|
|
564
|
+
width = workspace.getWidth();
|
|
565
|
+
}
|
|
566
|
+
for (var i = 0; i < newBlockIds.length; i++) {
|
|
567
|
+
var block = workspace.getBlockById(newBlockIds[i]);
|
|
568
|
+
block.moveBy(workspace.RTL ? width - offsetX : offsetX, offsetY);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
return newBlockIds;
|
|
572
|
+
};
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* Decode an XML block tag and create a block (and possibly sub blocks) on the
|
|
576
|
+
* workspace.
|
|
577
|
+
* @param {!Element} xmlBlock XML block element.
|
|
578
|
+
* @param {!Blockly.Workspace} workspace The workspace.
|
|
579
|
+
* @return {!Blockly.Block} The root block created.
|
|
580
|
+
*/
|
|
581
|
+
Blockly.Xml.domToBlock = function(xmlBlock, workspace) {
|
|
582
|
+
if (xmlBlock instanceof Blockly.Workspace) {
|
|
583
|
+
var swap = xmlBlock;
|
|
584
|
+
xmlBlock = workspace;
|
|
585
|
+
workspace = swap;
|
|
586
|
+
console.warn('Deprecated call to Blockly.Xml.domToBlock, ' +
|
|
587
|
+
'swap the arguments.');
|
|
588
|
+
}
|
|
589
|
+
// Create top-level block.
|
|
590
|
+
Blockly.Events.disable();
|
|
591
|
+
var variablesBeforeCreation = workspace.getAllVariables();
|
|
592
|
+
try {
|
|
593
|
+
var topBlock = Blockly.Xml.domToBlockHeadless_(xmlBlock, workspace);
|
|
594
|
+
// Generate list of all blocks.
|
|
595
|
+
var blocks = topBlock.getDescendants(false);
|
|
596
|
+
if (workspace.rendered) {
|
|
597
|
+
// Hide connections to speed up assembly.
|
|
598
|
+
topBlock.setConnectionsHidden(true);
|
|
599
|
+
// Render each block.
|
|
600
|
+
for (var i = blocks.length - 1; i >= 0; i--) {
|
|
601
|
+
blocks[i].initSvg();
|
|
602
|
+
}
|
|
603
|
+
for (var i = blocks.length - 1; i >= 0; i--) {
|
|
604
|
+
blocks[i].render(false);
|
|
605
|
+
}
|
|
606
|
+
// Populating the connection database may be deferred until after the
|
|
607
|
+
// blocks have rendered.
|
|
608
|
+
if (!workspace.isFlyout) {
|
|
609
|
+
setTimeout(function() {
|
|
610
|
+
if (topBlock.workspace) { // Check that the block hasn't been deleted.
|
|
611
|
+
topBlock.setConnectionsHidden(false);
|
|
612
|
+
}
|
|
613
|
+
}, 1);
|
|
614
|
+
}
|
|
615
|
+
topBlock.updateDisabled();
|
|
616
|
+
// Allow the scrollbars to resize and move based on the new contents.
|
|
617
|
+
// TODO(@picklesrus): #387. Remove when domToBlock avoids resizing.
|
|
618
|
+
workspace.resizeContents();
|
|
619
|
+
} else {
|
|
620
|
+
for (var i = blocks.length - 1; i >= 0; i--) {
|
|
621
|
+
blocks[i].initModel();
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
} finally {
|
|
625
|
+
Blockly.Events.enable();
|
|
626
|
+
}
|
|
627
|
+
if (Blockly.Events.isEnabled()) {
|
|
628
|
+
var newVariables = Blockly.Variables.getAddedVariables(workspace,
|
|
629
|
+
variablesBeforeCreation);
|
|
630
|
+
// Fire a VarCreate event for each (if any) new variable created.
|
|
631
|
+
for (var i = 0; i < newVariables.length; i++) {
|
|
632
|
+
var thisVariable = newVariables[i];
|
|
633
|
+
Blockly.Events.fire(new Blockly.Events.VarCreate(thisVariable));
|
|
634
|
+
}
|
|
635
|
+
// Block events come after var events, in case they refer to newly created
|
|
636
|
+
// variables.
|
|
637
|
+
Blockly.Events.fire(new Blockly.Events.BlockCreate(topBlock));
|
|
638
|
+
}
|
|
639
|
+
return topBlock;
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* Decode an XML list of variables and add the variables to the workspace.
|
|
644
|
+
* @param {!Element} xmlVariables List of XML variable elements.
|
|
645
|
+
* @param {!Blockly.Workspace} workspace The workspace to which the variable
|
|
646
|
+
* should be added.
|
|
647
|
+
*/
|
|
648
|
+
Blockly.Xml.domToVariables = function(xmlVariables, workspace) {
|
|
649
|
+
for (var i = 0, xmlChild; xmlChild = xmlVariables.children[i]; i++) {
|
|
650
|
+
var type = xmlChild.getAttribute('type');
|
|
651
|
+
var id = xmlChild.getAttribute('id');
|
|
652
|
+
var isLocal = xmlChild.getAttribute('islocal') == 'true';
|
|
653
|
+
var isCloud = xmlChild.getAttribute('iscloud') == 'true';
|
|
654
|
+
var name = xmlChild.textContent;
|
|
655
|
+
|
|
656
|
+
if (typeof(type) === undefined || type === null) {
|
|
657
|
+
throw Error('Variable with id, ' + id + ' is without a type');
|
|
658
|
+
}
|
|
659
|
+
workspace.createVariable(name, type, id, isLocal, isCloud);
|
|
660
|
+
}
|
|
661
|
+
};
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* Decode an XML block tag and create a block (and possibly sub blocks) on the
|
|
665
|
+
* workspace.
|
|
666
|
+
* @param {!Element} xmlBlock XML block element.
|
|
667
|
+
* @param {!Blockly.Workspace} workspace The workspace.
|
|
668
|
+
* @return {!Blockly.Block} The root block created.
|
|
669
|
+
* @private
|
|
670
|
+
*/
|
|
671
|
+
Blockly.Xml.domToBlockHeadless_ = function(xmlBlock, workspace) {
|
|
672
|
+
var block = null;
|
|
673
|
+
var prototypeName = xmlBlock.getAttribute('type');
|
|
674
|
+
goog.asserts.assert(
|
|
675
|
+
prototypeName, 'Block type unspecified: %s', xmlBlock.outerHTML);
|
|
676
|
+
var id = xmlBlock.getAttribute('id');
|
|
677
|
+
block = workspace.newBlock(prototypeName, id);
|
|
678
|
+
|
|
679
|
+
var blockChild = null;
|
|
680
|
+
for (var i = 0, xmlChild; xmlChild = xmlBlock.childNodes[i]; i++) {
|
|
681
|
+
if (xmlChild.nodeType == 3) {
|
|
682
|
+
// Ignore any text at the <block> level. It's all whitespace anyway.
|
|
683
|
+
continue;
|
|
684
|
+
}
|
|
685
|
+
var input;
|
|
686
|
+
|
|
687
|
+
// Find any enclosed blocks or shadows in this tag.
|
|
688
|
+
var childBlockElement = null;
|
|
689
|
+
var childShadowElement = null;
|
|
690
|
+
for (var j = 0, grandchild; grandchild = xmlChild.childNodes[j]; j++) {
|
|
691
|
+
if (grandchild.nodeType == 1) {
|
|
692
|
+
if (grandchild.nodeName.toLowerCase() == 'block') {
|
|
693
|
+
childBlockElement = /** @type {!Element} */ (grandchild);
|
|
694
|
+
} else if (grandchild.nodeName.toLowerCase() == 'shadow') {
|
|
695
|
+
childShadowElement = /** @type {!Element} */ (grandchild);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
// Use the shadow block if there is no child block.
|
|
700
|
+
if (!childBlockElement && childShadowElement) {
|
|
701
|
+
childBlockElement = childShadowElement;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
var name = xmlChild.getAttribute('name');
|
|
705
|
+
switch (xmlChild.nodeName.toLowerCase()) {
|
|
706
|
+
case 'mutation':
|
|
707
|
+
// Custom data for an advanced block.
|
|
708
|
+
if (block.domToMutation) {
|
|
709
|
+
block.domToMutation(xmlChild);
|
|
710
|
+
if (block.initSvg) {
|
|
711
|
+
// Mutation may have added some elements that need initializing.
|
|
712
|
+
block.initSvg();
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
break;
|
|
716
|
+
case 'comment':
|
|
717
|
+
var commentId = xmlChild.getAttribute('id');
|
|
718
|
+
var bubbleX = parseInt(xmlChild.getAttribute('x'), 10);
|
|
719
|
+
var bubbleY = parseInt(xmlChild.getAttribute('y'), 10);
|
|
720
|
+
var minimized = xmlChild.getAttribute('minimized') || false;
|
|
721
|
+
|
|
722
|
+
// Note bubbleX and bubbleY can be NaN, but the ScratchBlockComment
|
|
723
|
+
// constructor will handle that.
|
|
724
|
+
block.setCommentText(xmlChild.textContent, commentId, bubbleX, bubbleY,
|
|
725
|
+
minimized == 'true');
|
|
726
|
+
|
|
727
|
+
var visible = xmlChild.getAttribute('pinned');
|
|
728
|
+
if (visible && !block.isInFlyout) {
|
|
729
|
+
// Give the renderer a millisecond to render and position the block
|
|
730
|
+
// before positioning the comment bubble.
|
|
731
|
+
setTimeout(function() {
|
|
732
|
+
if (block.comment && block.comment.setVisible) {
|
|
733
|
+
block.comment.setVisible(visible == 'true');
|
|
734
|
+
}
|
|
735
|
+
}, 1);
|
|
736
|
+
}
|
|
737
|
+
var bubbleW = parseInt(xmlChild.getAttribute('w'), 10);
|
|
738
|
+
var bubbleH = parseInt(xmlChild.getAttribute('h'), 10);
|
|
739
|
+
if (!isNaN(bubbleW) && !isNaN(bubbleH) &&
|
|
740
|
+
block.comment && block.comment.setVisible) {
|
|
741
|
+
if (block.comment instanceof Blockly.ScratchBlockComment) {
|
|
742
|
+
block.comment.setSize(bubbleW, bubbleH);
|
|
743
|
+
} else {
|
|
744
|
+
block.comment.setBubbleSize(bubbleW, bubbleH);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
break;
|
|
748
|
+
case 'data':
|
|
749
|
+
block.data = xmlChild.textContent;
|
|
750
|
+
break;
|
|
751
|
+
case 'title':
|
|
752
|
+
// Titles were renamed to field in December 2013.
|
|
753
|
+
// Fall through.
|
|
754
|
+
case 'field':
|
|
755
|
+
Blockly.Xml.domToField_(block, name, xmlChild);
|
|
756
|
+
break;
|
|
757
|
+
case 'value':
|
|
758
|
+
case 'statement':
|
|
759
|
+
input = block.getInput(name);
|
|
760
|
+
if (!input) {
|
|
761
|
+
console.warn('Ignoring non-existent input ' + name + ' in block ' +
|
|
762
|
+
prototypeName);
|
|
763
|
+
break;
|
|
764
|
+
}
|
|
765
|
+
if (childShadowElement) {
|
|
766
|
+
input.connection.setShadowDom(childShadowElement);
|
|
767
|
+
}
|
|
768
|
+
if (childBlockElement) {
|
|
769
|
+
blockChild = Blockly.Xml.domToBlockHeadless_(childBlockElement,
|
|
770
|
+
workspace);
|
|
771
|
+
if (blockChild.outputConnection) {
|
|
772
|
+
input.connection.connect(blockChild.outputConnection);
|
|
773
|
+
} else if (blockChild.previousConnection) {
|
|
774
|
+
input.connection.connect(blockChild.previousConnection);
|
|
775
|
+
} else {
|
|
776
|
+
goog.asserts.fail(
|
|
777
|
+
'Child block does not have output or previous statement.');
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
break;
|
|
781
|
+
case 'next':
|
|
782
|
+
if (childShadowElement && block.nextConnection) {
|
|
783
|
+
block.nextConnection.setShadowDom(childShadowElement);
|
|
784
|
+
}
|
|
785
|
+
if (childBlockElement) {
|
|
786
|
+
goog.asserts.assert(block.nextConnection,
|
|
787
|
+
'Next statement does not exist.');
|
|
788
|
+
// If there is more than one XML 'next' tag.
|
|
789
|
+
goog.asserts.assert(!block.nextConnection.isConnected(),
|
|
790
|
+
'Next statement is already connected.');
|
|
791
|
+
blockChild = Blockly.Xml.domToBlockHeadless_(childBlockElement,
|
|
792
|
+
workspace);
|
|
793
|
+
goog.asserts.assert(blockChild.previousConnection,
|
|
794
|
+
'Next block does not have previous statement.');
|
|
795
|
+
block.nextConnection.connect(blockChild.previousConnection);
|
|
796
|
+
}
|
|
797
|
+
break;
|
|
798
|
+
default:
|
|
799
|
+
// Unknown tag; ignore. Same principle as HTML parsers.
|
|
800
|
+
console.warn('Ignoring unknown tag: ' + xmlChild.nodeName);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
var inline = xmlBlock.getAttribute('inline');
|
|
805
|
+
if (inline) {
|
|
806
|
+
block.setInputsInline(inline == 'true');
|
|
807
|
+
}
|
|
808
|
+
var disabled = xmlBlock.getAttribute('disabled');
|
|
809
|
+
if (disabled) {
|
|
810
|
+
block.setDisabled(disabled == 'true' || disabled == 'disabled');
|
|
811
|
+
}
|
|
812
|
+
var deletable = xmlBlock.getAttribute('deletable');
|
|
813
|
+
if (deletable) {
|
|
814
|
+
block.setDeletable(deletable == 'true');
|
|
815
|
+
}
|
|
816
|
+
var movable = xmlBlock.getAttribute('movable');
|
|
817
|
+
if (movable) {
|
|
818
|
+
block.setMovable(movable == 'true');
|
|
819
|
+
}
|
|
820
|
+
var editable = xmlBlock.getAttribute('editable');
|
|
821
|
+
if (editable) {
|
|
822
|
+
block.setEditable(editable == 'true');
|
|
823
|
+
}
|
|
824
|
+
var collapsed = xmlBlock.getAttribute('collapsed');
|
|
825
|
+
if (collapsed) {
|
|
826
|
+
block.setCollapsed(collapsed == 'true');
|
|
827
|
+
}
|
|
828
|
+
if (xmlBlock.nodeName.toLowerCase() == 'shadow') {
|
|
829
|
+
// Ensure all children are also shadows.
|
|
830
|
+
var children = block.getChildren(false);
|
|
831
|
+
for (var i = 0, child; child = children[i]; i++) {
|
|
832
|
+
goog.asserts.assert(
|
|
833
|
+
child.isShadow(), 'Shadow block not allowed non-shadow child.');
|
|
834
|
+
}
|
|
835
|
+
block.setShadow(true);
|
|
836
|
+
}
|
|
837
|
+
return block;
|
|
838
|
+
};
|
|
839
|
+
|
|
840
|
+
/**
|
|
841
|
+
* Decode an XML variable field tag and set the value of that field.
|
|
842
|
+
* @param {!Blockly.Workspace} workspace The workspace that is currently being
|
|
843
|
+
* deserialized.
|
|
844
|
+
* @param {!Element} xml The field tag to decode.
|
|
845
|
+
* @param {string} text The text content of the XML tag.
|
|
846
|
+
* @param {!Blockly.FieldVariable} field The field on which the value will be
|
|
847
|
+
* set.
|
|
848
|
+
* @private
|
|
849
|
+
*/
|
|
850
|
+
Blockly.Xml.domToFieldVariable_ = function(workspace, xml, text, field) {
|
|
851
|
+
var type = xml.getAttribute('variabletype') || '';
|
|
852
|
+
// TODO (fenichel): Does this need to be explicit or not?
|
|
853
|
+
if (type == '\'\'') {
|
|
854
|
+
type = '';
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
var variable;
|
|
858
|
+
// This check ensures that there is not both a potential variable and a real
|
|
859
|
+
// variable with the same name and type.
|
|
860
|
+
if (!workspace.getPotentialVariableMap() && !workspace.isFlyout &&
|
|
861
|
+
workspace.getFlyout()) {
|
|
862
|
+
var flyoutWs = workspace.getFlyout().getWorkspace();
|
|
863
|
+
variable = Blockly.Variables.realizePotentialVar(text, type, flyoutWs, true);
|
|
864
|
+
}
|
|
865
|
+
if (!variable) {
|
|
866
|
+
variable = Blockly.Variables.getOrCreateVariablePackage(workspace, xml.id,
|
|
867
|
+
text, type);
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
// This should never happen :)
|
|
871
|
+
if (type != null && type !== variable.type) {
|
|
872
|
+
throw Error('Serialized variable type with id \'' +
|
|
873
|
+
variable.getId() + '\' had type ' + variable.type + ', and ' +
|
|
874
|
+
'does not match variable field that references it: ' +
|
|
875
|
+
Blockly.Xml.domToText(xml) + '.');
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
field.setValue(variable.getId());
|
|
879
|
+
};
|
|
880
|
+
|
|
881
|
+
/**
|
|
882
|
+
* Decode an XML field tag and set the value of that field on the given block.
|
|
883
|
+
* @param {!Blockly.Block} block The block that is currently being deserialized.
|
|
884
|
+
* @param {string} fieldName The name of the field on the block.
|
|
885
|
+
* @param {!Element} xml The field tag to decode.
|
|
886
|
+
* @private
|
|
887
|
+
*/
|
|
888
|
+
Blockly.Xml.domToField_ = function(block, fieldName, xml) {
|
|
889
|
+
var field = block.getField(fieldName);
|
|
890
|
+
if (!field) {
|
|
891
|
+
console.warn('Ignoring non-existent field ' + fieldName + ' in block ' +
|
|
892
|
+
block.type);
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
var workspace = block.workspace;
|
|
897
|
+
var text = xml.textContent;
|
|
898
|
+
if (field.referencesVariables()) {
|
|
899
|
+
Blockly.Xml.domToFieldVariable_(workspace, xml, text, field);
|
|
900
|
+
} else {
|
|
901
|
+
field.setValue(text);
|
|
902
|
+
}
|
|
903
|
+
};
|
|
904
|
+
|
|
905
|
+
/**
|
|
906
|
+
* Remove any 'next' block (statements in a stack).
|
|
907
|
+
* @param {!Element} xmlBlock XML block element.
|
|
908
|
+
*/
|
|
909
|
+
Blockly.Xml.deleteNext = function(xmlBlock) {
|
|
910
|
+
for (var i = 0, child; child = xmlBlock.childNodes[i]; i++) {
|
|
911
|
+
if (child.nodeName.toLowerCase() == 'next') {
|
|
912
|
+
xmlBlock.removeChild(child);
|
|
913
|
+
break;
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
};
|
|
917
|
+
|
|
918
|
+
// Export symbols that would otherwise be renamed by Closure compiler.
|
|
919
|
+
if (!goog.global['Blockly']) {
|
|
920
|
+
goog.global['Blockly'] = {};
|
|
921
|
+
}
|
|
922
|
+
if (!goog.global['Blockly']['Xml']) {
|
|
923
|
+
goog.global['Blockly']['Xml'] = {};
|
|
924
|
+
}
|
|
925
|
+
goog.global['Blockly']['Xml']['domToText'] = Blockly.Xml.domToText;
|
|
926
|
+
goog.global['Blockly']['Xml']['domToWorkspace'] = Blockly.Xml.domToWorkspace;
|
|
927
|
+
goog.global['Blockly']['Xml']['textToDom'] = Blockly.Xml.textToDom;
|
|
928
|
+
goog.global['Blockly']['Xml']['workspaceToDom'] = Blockly.Xml.workspaceToDom;
|
|
929
|
+
goog.global['Blockly']['Xml']['clearWorkspaceAndLoadFromXml'] =
|
|
930
|
+
Blockly.Xml.clearWorkspaceAndLoadFromXml;
|