blockly 7.20211209.2 → 8.0.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/blockly.d.ts +18963 -18432
- package/blockly.min.js +852 -844
- package/blockly_compressed.js +669 -664
- package/blockly_compressed.js.map +1 -1
- package/blocks/blocks.js +47 -0
- package/blocks/colour.js +13 -3
- package/blocks/lists.js +22 -13
- package/blocks/logic.js +13 -3
- package/blocks/loops.js +24 -11
- package/blocks/math.js +12 -3
- package/blocks/procedures.js +41 -27
- package/blocks/text.js +22 -13
- package/blocks/variables.js +14 -3
- package/blocks/variables_dynamic.js +13 -3
- package/blocks_compressed.js +146 -141
- package/blocks_compressed.js.map +1 -1
- package/core/block.js +1869 -1814
- package/core/block_drag_surface.js +201 -200
- package/core/block_dragger.js +377 -373
- package/core/block_svg.js +1593 -1479
- package/core/blockly.js +8 -22
- package/core/blocks.js +9 -2
- package/core/browser_events.js +22 -5
- package/core/bubble.js +841 -797
- package/core/bubble_dragger.js +213 -206
- package/core/bump_objects.js +2 -2
- package/core/clipboard.js +9 -9
- package/core/comment.js +353 -332
- package/core/common.js +46 -17
- package/core/component_manager.js +181 -174
- package/core/config.js +87 -0
- package/core/connection.js +595 -584
- package/core/connection_checker.js +242 -244
- package/core/connection_db.js +235 -230
- package/core/contextmenu.js +9 -6
- package/core/contextmenu_items.js +1 -2
- package/core/contextmenu_registry.js +93 -89
- package/core/css.js +474 -474
- package/core/delete_area.js +45 -42
- package/core/drag_target.js +57 -56
- package/core/dropdowndiv.js +153 -163
- package/core/events/events.js +2 -2
- package/core/events/events_abstract.js +89 -77
- package/core/events/events_block_base.js +37 -36
- package/core/events/events_block_change.js +130 -124
- package/core/events/events_block_create.js +73 -71
- package/core/events/events_block_delete.js +84 -82
- package/core/events/events_block_drag.js +50 -49
- package/core/events/events_block_move.js +147 -140
- package/core/events/events_bubble_open.js +51 -50
- package/core/events/events_click.js +48 -44
- package/core/events/events_comment_base.js +72 -69
- package/core/events/events_comment_change.js +63 -61
- package/core/events/events_comment_create.js +44 -42
- package/core/events/events_comment_delete.js +42 -40
- package/core/events/events_comment_move.js +106 -104
- package/core/events/events_marker_move.js +65 -64
- package/core/events/events_selected.js +46 -45
- package/core/events/events_theme_change.js +36 -35
- package/core/events/events_toolbox_item_select.js +46 -45
- package/core/events/events_trashcan_open.js +37 -36
- package/core/events/events_ui.js +47 -46
- package/core/events/events_ui_base.js +30 -29
- package/core/events/events_var_base.js +37 -36
- package/core/events/events_var_create.js +50 -48
- package/core/events/events_var_delete.js +50 -48
- package/core/events/events_var_rename.js +51 -49
- package/core/events/events_viewport.js +66 -65
- package/core/events/utils.js +29 -14
- package/core/events/workspace_events.js +49 -55
- package/core/extensions.js +4 -3
- package/core/field.js +1061 -997
- package/core/field_angle.js +462 -442
- package/core/field_checkbox.js +194 -182
- package/core/field_colour.js +519 -505
- package/core/field_dropdown.js +617 -598
- package/core/field_image.js +229 -220
- package/core/field_label.js +102 -91
- package/core/field_label_serializable.js +42 -41
- package/core/field_multilineinput.js +372 -358
- package/core/field_number.js +272 -253
- package/core/field_textinput.js +499 -467
- package/core/field_variable.js +458 -420
- package/core/flyout_base.js +1005 -952
- package/core/flyout_button.js +277 -260
- package/core/flyout_horizontal.js +304 -302
- package/core/flyout_metrics_manager.js +64 -64
- package/core/flyout_vertical.js +306 -300
- package/core/generator.js +459 -446
- package/core/gesture.js +829 -813
- package/core/grid.js +166 -163
- package/core/icon.js +168 -159
- package/core/inject.js +7 -5
- package/core/input.js +257 -248
- package/core/insertion_marker_manager.js +655 -624
- package/core/internal_constants.js +0 -129
- package/core/keyboard_nav/ast_node.js +605 -596
- package/core/keyboard_nav/basic_cursor.js +166 -165
- package/core/keyboard_nav/cursor.js +99 -97
- package/core/keyboard_nav/marker.js +83 -79
- package/core/keyboard_nav/tab_navigate_cursor.js +18 -23
- package/core/marker_manager.js +153 -141
- package/core/menu.js +377 -372
- package/core/menuitem.js +223 -217
- package/core/metrics_manager.js +403 -390
- package/core/mutator.js +468 -437
- package/core/names.js +229 -188
- package/core/options.js +290 -284
- package/core/procedures.js +29 -17
- package/core/registry.js +19 -16
- package/core/rendered_connection.js +482 -463
- package/core/renderers/common/block_rendering.js +9 -3
- package/core/renderers/common/constants.js +1119 -1112
- package/core/renderers/common/debug.js +14 -0
- package/core/renderers/common/debugger.js +338 -316
- package/core/renderers/common/drawer.js +380 -370
- package/core/renderers/common/i_path_object.js +2 -2
- package/core/renderers/common/info.js +626 -618
- package/core/renderers/common/marker_svg.js +579 -541
- package/core/renderers/common/path_object.js +203 -200
- package/core/renderers/common/renderer.js +220 -218
- package/core/renderers/geras/constants.js +36 -36
- package/core/renderers/geras/drawer.js +155 -147
- package/core/renderers/geras/highlight_constants.js +244 -238
- package/core/renderers/geras/highlighter.js +231 -179
- package/core/renderers/geras/info.js +392 -369
- package/core/renderers/geras/measurables/inline_input.js +25 -19
- package/core/renderers/geras/measurables/statement_input.js +23 -17
- package/core/renderers/geras/path_object.js +106 -121
- package/core/renderers/geras/renderer.js +96 -98
- package/core/renderers/measurables/base.js +30 -18
- package/core/renderers/measurables/bottom_row.js +83 -80
- package/core/renderers/measurables/connection.js +22 -15
- package/core/renderers/measurables/external_value_input.js +35 -22
- package/core/renderers/measurables/field.js +35 -20
- package/core/renderers/measurables/hat.js +18 -13
- package/core/renderers/measurables/icon.js +24 -17
- package/core/renderers/measurables/in_row_spacer.js +15 -13
- package/core/renderers/measurables/inline_input.js +43 -33
- package/core/renderers/measurables/input_connection.js +41 -28
- package/core/renderers/measurables/input_row.js +50 -44
- package/core/renderers/measurables/jagged_edge.js +14 -12
- package/core/renderers/measurables/next_connection.js +16 -14
- package/core/renderers/measurables/output_connection.js +26 -20
- package/core/renderers/measurables/previous_connection.js +16 -15
- package/core/renderers/measurables/round_corner.js +20 -18
- package/core/renderers/measurables/row.js +184 -168
- package/core/renderers/measurables/spacer_row.js +38 -23
- package/core/renderers/measurables/square_corner.js +18 -16
- package/core/renderers/measurables/statement_input.js +23 -20
- package/core/renderers/measurables/top_row.js +88 -85
- package/core/renderers/minimalist/constants.js +8 -7
- package/core/renderers/minimalist/drawer.js +11 -10
- package/core/renderers/minimalist/info.js +18 -18
- package/core/renderers/minimalist/renderer.js +40 -39
- package/core/renderers/thrasos/info.js +258 -248
- package/core/renderers/thrasos/renderer.js +20 -20
- package/core/renderers/zelos/constants.js +898 -873
- package/core/renderers/zelos/drawer.js +186 -169
- package/core/renderers/zelos/info.js +502 -479
- package/core/renderers/zelos/marker_svg.js +129 -115
- package/core/renderers/zelos/measurables/bottom_row.js +31 -30
- package/core/renderers/zelos/measurables/inputs.js +22 -21
- package/core/renderers/zelos/measurables/row_elements.js +14 -13
- package/core/renderers/zelos/measurables/top_row.js +34 -33
- package/core/renderers/zelos/path_object.js +181 -180
- package/core/renderers/zelos/renderer.js +91 -92
- package/core/scrollbar.js +759 -713
- package/core/scrollbar_pair.js +250 -245
- package/core/serialization/blocks.js +19 -9
- package/core/serialization/workspaces.js +3 -2
- package/core/shortcut_registry.js +286 -277
- package/core/sprites.js +31 -0
- package/core/theme.js +135 -141
- package/core/theme_manager.js +147 -143
- package/core/toolbox/category.js +602 -576
- package/core/toolbox/collapsible_category.js +226 -227
- package/core/toolbox/separator.js +70 -61
- package/core/toolbox/toolbox.js +934 -927
- package/core/toolbox/toolbox_item.js +115 -99
- package/core/tooltip.js +108 -35
- package/core/touch.js +8 -3
- package/core/touch_gesture.js +254 -251
- package/core/trashcan.js +606 -595
- package/core/utils/coordinate.js +97 -95
- package/core/utils/dom.js +2 -2
- package/core/utils/global.js +2 -0
- package/core/utils/rect.js +41 -37
- package/core/utils/sentinel.js +25 -0
- package/core/utils/size.js +30 -27
- package/core/utils/svg.js +18 -16
- package/core/variable_map.js +325 -341
- package/core/variable_model.js +55 -54
- package/core/variables.js +9 -2
- package/core/variables_dynamic.js +3 -1
- package/core/warning.js +126 -120
- package/core/widgetdiv.js +4 -4
- package/core/workspace.js +685 -664
- package/core/workspace_audio.js +124 -118
- package/core/workspace_comment.js +308 -298
- package/core/workspace_comment_svg.js +1029 -951
- package/core/workspace_drag_surface_svg.js +147 -140
- package/core/workspace_dragger.js +70 -71
- package/core/workspace_svg.js +2322 -2297
- package/core/xml.js +30 -20
- package/core/zoom_controls.js +431 -439
- package/dart_compressed.js +40 -43
- package/dart_compressed.js.map +1 -1
- package/generators/dart/colour.js +56 -64
- package/generators/dart/lists.js +61 -50
- package/generators/dart/math.js +160 -148
- package/generators/dart/text.js +83 -61
- package/generators/javascript/colour.js +37 -34
- package/generators/javascript/lists.js +50 -43
- package/generators/javascript/math.js +123 -139
- package/generators/javascript/text.js +67 -81
- package/generators/lua/colour.js +25 -23
- package/generators/lua/lists.js +97 -69
- package/generators/lua/logic.js +1 -2
- package/generators/lua/math.js +182 -144
- package/generators/lua/text.js +116 -99
- package/generators/php/colour.js +38 -32
- package/generators/php/lists.js +109 -89
- package/generators/php/math.js +90 -81
- package/generators/php/text.js +63 -61
- package/generators/python/colour.js +18 -18
- package/generators/python/lists.js +38 -30
- package/generators/python/loops.js +12 -8
- package/generators/python/math.js +104 -106
- package/generators/python/text.js +34 -30
- package/javascript_compressed.js +37 -39
- package/javascript_compressed.js.map +1 -1
- package/lua_compressed.js +39 -42
- package/lua_compressed.js.map +1 -1
- package/msg/az.js +2 -2
- package/msg/be.js +4 -4
- package/msg/cs.js +15 -15
- package/msg/de.js +1 -1
- package/msg/diq.js +1 -1
- package/msg/eo.js +1 -1
- package/msg/es.js +1 -1
- package/msg/fa.js +1 -1
- package/msg/fr.js +4 -4
- package/msg/he.js +1 -1
- package/msg/hr.js +2 -2
- package/msg/hy.js +2 -2
- package/msg/id.js +12 -12
- package/msg/inh.js +14 -14
- package/msg/ja.js +7 -7
- package/msg/lv.js +29 -29
- package/msg/pa.js +3 -3
- package/msg/smn.js +436 -0
- package/msg/te.js +1 -1
- package/msg/yue.js +1 -1
- package/msg/zh-hans.js +3 -3
- package/msg/zh-hant.js +3 -3
- package/package.json +7 -6
- package/php_compressed.js +38 -42
- package/php_compressed.js.map +1 -1
- package/python_compressed.js +26 -25
- package/python_compressed.js.map +1 -1
- package/blocks/all.js +0 -23
package/core/variable_map.js
CHANGED
|
@@ -37,390 +37,374 @@ goog.require('Blockly.Events.VarRename');
|
|
|
37
37
|
* Class for a variable map. This contains a dictionary data structure with
|
|
38
38
|
* variable types as keys and lists of variables as values. The list of
|
|
39
39
|
* variables are the type indicated by the key.
|
|
40
|
-
* @param {!Workspace} workspace The workspace this map belongs to.
|
|
41
|
-
* @constructor
|
|
42
40
|
* @alias Blockly.VariableMap
|
|
43
41
|
*/
|
|
44
|
-
|
|
42
|
+
class VariableMap {
|
|
45
43
|
/**
|
|
46
|
-
*
|
|
47
|
-
* of the named variables in the workspace, including variables
|
|
48
|
-
* that are not currently in use.
|
|
49
|
-
* @type {!Object<string, !Array<VariableModel>>}
|
|
50
|
-
* @private
|
|
44
|
+
* @param {!Workspace} workspace The workspace this map belongs to.
|
|
51
45
|
*/
|
|
52
|
-
|
|
53
|
-
|
|
46
|
+
constructor(workspace) {
|
|
47
|
+
/**
|
|
48
|
+
* A map from variable type to list of variable names. The lists contain
|
|
49
|
+
* all of the named variables in the workspace, including variables that are
|
|
50
|
+
* not currently in use.
|
|
51
|
+
* @type {!Object<string, !Array<VariableModel>>}
|
|
52
|
+
* @private
|
|
53
|
+
*/
|
|
54
|
+
this.variableMap_ = Object.create(null);
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The workspace this map belongs to.
|
|
58
|
+
* @type {!Workspace}
|
|
59
|
+
*/
|
|
60
|
+
this.workspace = workspace;
|
|
61
|
+
}
|
|
54
62
|
/**
|
|
55
|
-
*
|
|
56
|
-
* @type {!Workspace}
|
|
63
|
+
* Clear the variable map.
|
|
57
64
|
*/
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
this.renameVariableAndUses_(variable, newName, blocks);
|
|
85
|
-
} else {
|
|
86
|
-
this.renameVariableWithConflict_(variable, newName, conflictVar, blocks);
|
|
65
|
+
clear() {
|
|
66
|
+
this.variableMap_ = Object.create(null);
|
|
67
|
+
}
|
|
68
|
+
/* Begin functions for renaming variables. */
|
|
69
|
+
/**
|
|
70
|
+
* Rename the given variable by updating its name in the variable map.
|
|
71
|
+
* @param {!VariableModel} variable Variable to rename.
|
|
72
|
+
* @param {string} newName New variable name.
|
|
73
|
+
* @package
|
|
74
|
+
*/
|
|
75
|
+
renameVariable(variable, newName) {
|
|
76
|
+
const type = variable.type;
|
|
77
|
+
const conflictVar = this.getVariable(newName, type);
|
|
78
|
+
const blocks = this.workspace.getAllBlocks(false);
|
|
79
|
+
eventUtils.setGroup(true);
|
|
80
|
+
try {
|
|
81
|
+
// The IDs may match if the rename is a simple case change (name1 ->
|
|
82
|
+
// Name1).
|
|
83
|
+
if (!conflictVar || conflictVar.getId() === variable.getId()) {
|
|
84
|
+
this.renameVariableAndUses_(variable, newName, blocks);
|
|
85
|
+
} else {
|
|
86
|
+
this.renameVariableWithConflict_(
|
|
87
|
+
variable, newName, conflictVar, blocks);
|
|
88
|
+
}
|
|
89
|
+
} finally {
|
|
90
|
+
eventUtils.setGroup(false);
|
|
87
91
|
}
|
|
88
|
-
} finally {
|
|
89
|
-
eventUtils.setGroup(false);
|
|
90
92
|
}
|
|
91
|
-
|
|
93
|
+
/**
|
|
94
|
+
* Rename a variable by updating its name in the variable map. Identify the
|
|
95
|
+
* variable to rename with the given ID.
|
|
96
|
+
* @param {string} id ID of the variable to rename.
|
|
97
|
+
* @param {string} newName New variable name.
|
|
98
|
+
*/
|
|
99
|
+
renameVariableById(id, newName) {
|
|
100
|
+
const variable = this.getVariableById(id);
|
|
101
|
+
if (!variable) {
|
|
102
|
+
throw Error('Tried to rename a variable that didn\'t exist. ID: ' + id);
|
|
103
|
+
}
|
|
92
104
|
|
|
93
|
-
|
|
94
|
-
* Rename a variable by updating its name in the variable map. Identify the
|
|
95
|
-
* variable to rename with the given ID.
|
|
96
|
-
* @param {string} id ID of the variable to rename.
|
|
97
|
-
* @param {string} newName New variable name.
|
|
98
|
-
*/
|
|
99
|
-
VariableMap.prototype.renameVariableById = function(id, newName) {
|
|
100
|
-
const variable = this.getVariableById(id);
|
|
101
|
-
if (!variable) {
|
|
102
|
-
throw Error('Tried to rename a variable that didn\'t exist. ID: ' + id);
|
|
105
|
+
this.renameVariable(variable, newName);
|
|
103
106
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
new (eventUtils.get(eventUtils.VAR_RENAME))(variable, newName));
|
|
121
|
-
variable.name = newName;
|
|
122
|
-
for (let i = 0; i < blocks.length; i++) {
|
|
123
|
-
blocks[i].updateVarName(variable);
|
|
107
|
+
/**
|
|
108
|
+
* Update the name of the given variable and refresh all references to it.
|
|
109
|
+
* The new name must not conflict with any existing variable names.
|
|
110
|
+
* @param {!VariableModel} variable Variable to rename.
|
|
111
|
+
* @param {string} newName New variable name.
|
|
112
|
+
* @param {!Array<!Block>} blocks The list of all blocks in the
|
|
113
|
+
* workspace.
|
|
114
|
+
* @private
|
|
115
|
+
*/
|
|
116
|
+
renameVariableAndUses_(variable, newName, blocks) {
|
|
117
|
+
eventUtils.fire(
|
|
118
|
+
new (eventUtils.get(eventUtils.VAR_RENAME))(variable, newName));
|
|
119
|
+
variable.name = newName;
|
|
120
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
121
|
+
blocks[i].updateVarName(variable);
|
|
122
|
+
}
|
|
124
123
|
}
|
|
125
|
-
|
|
124
|
+
/**
|
|
125
|
+
* Update the name of the given variable to the same name as an existing
|
|
126
|
+
* variable. The two variables are coalesced into a single variable with the
|
|
127
|
+
* ID of the existing variable that was already using newName. Refresh all
|
|
128
|
+
* references to the variable.
|
|
129
|
+
* @param {!VariableModel} variable Variable to rename.
|
|
130
|
+
* @param {string} newName New variable name.
|
|
131
|
+
* @param {!VariableModel} conflictVar The variable that was already
|
|
132
|
+
* using newName.
|
|
133
|
+
* @param {!Array<!Block>} blocks The list of all blocks in the
|
|
134
|
+
* workspace.
|
|
135
|
+
* @private
|
|
136
|
+
*/
|
|
137
|
+
renameVariableWithConflict_(variable, newName, conflictVar, blocks) {
|
|
138
|
+
const type = variable.type;
|
|
139
|
+
const oldCase = conflictVar.name;
|
|
126
140
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
* Refresh all references to the variable.
|
|
132
|
-
* @param {!VariableModel} variable Variable to rename.
|
|
133
|
-
* @param {string} newName New variable name.
|
|
134
|
-
* @param {!VariableModel} conflictVar The variable that was already
|
|
135
|
-
* using newName.
|
|
136
|
-
* @param {!Array<!Block>} blocks The list of all blocks in the
|
|
137
|
-
* workspace.
|
|
138
|
-
* @private
|
|
139
|
-
*/
|
|
140
|
-
VariableMap.prototype.renameVariableWithConflict_ = function(
|
|
141
|
-
variable, newName, conflictVar, blocks) {
|
|
142
|
-
const type = variable.type;
|
|
143
|
-
const oldCase = conflictVar.name;
|
|
141
|
+
if (newName !== oldCase) {
|
|
142
|
+
// Simple rename to change the case and update references.
|
|
143
|
+
this.renameVariableAndUses_(conflictVar, newName, blocks);
|
|
144
|
+
}
|
|
144
145
|
|
|
145
|
-
|
|
146
|
-
//
|
|
147
|
-
|
|
148
|
-
|
|
146
|
+
// These blocks now refer to a different variable.
|
|
147
|
+
// These will fire change events.
|
|
148
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
149
|
+
blocks[i].renameVarById(variable.getId(), conflictVar.getId());
|
|
150
|
+
}
|
|
149
151
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
152
|
+
// Finally delete the original variable, which is now unreferenced.
|
|
153
|
+
eventUtils.fire(new (eventUtils.get(eventUtils.VAR_DELETE))(variable));
|
|
154
|
+
// And remove it from the list.
|
|
155
|
+
arrayUtils.removeElem(this.variableMap_[type], variable);
|
|
154
156
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
if (opt_id && variable.getId() !== opt_id) {
|
|
179
|
-
throw Error(
|
|
180
|
-
'Variable "' + name + '" is already in use and its id is "' +
|
|
181
|
-
variable.getId() + '" which conflicts with the passed in ' +
|
|
182
|
-
'id, "' + opt_id + '".');
|
|
157
|
+
/* End functions for renaming variables. */
|
|
158
|
+
/**
|
|
159
|
+
* Create a variable with a given name, optional type, and optional ID.
|
|
160
|
+
* @param {string} name The name of the variable. This must be unique across
|
|
161
|
+
* variables and procedures.
|
|
162
|
+
* @param {?string=} opt_type The type of the variable like 'int' or 'string'.
|
|
163
|
+
* Does not need to be unique. Field_variable can filter variables based
|
|
164
|
+
* on their type. This will default to '' which is a specific type.
|
|
165
|
+
* @param {?string=} opt_id The unique ID of the variable. This will default
|
|
166
|
+
* to a UUID.
|
|
167
|
+
* @return {!VariableModel} The newly created variable.
|
|
168
|
+
*/
|
|
169
|
+
createVariable(name, opt_type, opt_id) {
|
|
170
|
+
let variable = this.getVariable(name, opt_type);
|
|
171
|
+
if (variable) {
|
|
172
|
+
if (opt_id && variable.getId() !== opt_id) {
|
|
173
|
+
throw Error(
|
|
174
|
+
'Variable "' + name + '" is already in use and its id is "' +
|
|
175
|
+
variable.getId() + '" which conflicts with the passed in ' +
|
|
176
|
+
'id, "' + opt_id + '".');
|
|
177
|
+
}
|
|
178
|
+
// The variable already exists and has the same ID.
|
|
179
|
+
return variable;
|
|
183
180
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
if (opt_id && this.getVariableById(opt_id)) {
|
|
188
|
-
throw Error('Variable id, "' + opt_id + '", is already in use.');
|
|
189
|
-
}
|
|
190
|
-
const id = opt_id || idGenerator.genUid();
|
|
191
|
-
const type = opt_type || '';
|
|
192
|
-
variable = new VariableModel(this.workspace, name, type, id);
|
|
193
|
-
|
|
194
|
-
const variables = this.variableMap_[type] || [];
|
|
195
|
-
variables.push(variable);
|
|
196
|
-
// Delete the list of variables of this type, and re-add it so that
|
|
197
|
-
// the most recent addition is at the end.
|
|
198
|
-
// This is used so the toolbox's set block is set to the most recent variable.
|
|
199
|
-
delete this.variableMap_[type];
|
|
200
|
-
this.variableMap_[type] = variables;
|
|
201
|
-
|
|
202
|
-
return variable;
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
/* Begin functions for variable deletion. */
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Delete a variable.
|
|
209
|
-
* @param {!VariableModel} variable Variable to delete.
|
|
210
|
-
*/
|
|
211
|
-
VariableMap.prototype.deleteVariable = function(variable) {
|
|
212
|
-
const variableId = variable.getId();
|
|
213
|
-
const variableList = this.variableMap_[variable.type];
|
|
214
|
-
for (let i = 0; i < variableList.length; i++) {
|
|
215
|
-
const tempVar = variableList[i];
|
|
216
|
-
if (tempVar.getId() === variableId) {
|
|
217
|
-
variableList.splice(i, 1);
|
|
218
|
-
eventUtils.fire(new (eventUtils.get(eventUtils.VAR_DELETE))(variable));
|
|
219
|
-
return;
|
|
181
|
+
if (opt_id && this.getVariableById(opt_id)) {
|
|
182
|
+
throw Error('Variable id, "' + opt_id + '", is already in use.');
|
|
220
183
|
}
|
|
221
|
-
|
|
222
|
-
|
|
184
|
+
const id = opt_id || idGenerator.genUid();
|
|
185
|
+
const type = opt_type || '';
|
|
186
|
+
variable = new VariableModel(this.workspace, name, type, id);
|
|
187
|
+
|
|
188
|
+
const variables = this.variableMap_[type] || [];
|
|
189
|
+
variables.push(variable);
|
|
190
|
+
// Delete the list of variables of this type, and re-add it so that
|
|
191
|
+
// the most recent addition is at the end.
|
|
192
|
+
// This is used so the toolbox's set block is set to the most recent
|
|
193
|
+
// variable.
|
|
194
|
+
delete this.variableMap_[type];
|
|
195
|
+
this.variableMap_[type] = variables;
|
|
223
196
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
if (
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
const deleteText = Msg['CANNOT_DELETE_VARIABLE_PROCEDURE']
|
|
240
|
-
.replace('%1', variableName)
|
|
241
|
-
.replace('%2', procedureName);
|
|
242
|
-
dialog.alert(deleteText);
|
|
197
|
+
return variable;
|
|
198
|
+
}
|
|
199
|
+
/* Begin functions for variable deletion. */
|
|
200
|
+
/**
|
|
201
|
+
* Delete a variable.
|
|
202
|
+
* @param {!VariableModel} variable Variable to delete.
|
|
203
|
+
*/
|
|
204
|
+
deleteVariable(variable) {
|
|
205
|
+
const variableId = variable.getId();
|
|
206
|
+
const variableList = this.variableMap_[variable.type];
|
|
207
|
+
for (let i = 0; i < variableList.length; i++) {
|
|
208
|
+
const tempVar = variableList[i];
|
|
209
|
+
if (tempVar.getId() === variableId) {
|
|
210
|
+
variableList.splice(i, 1);
|
|
211
|
+
eventUtils.fire(new (eventUtils.get(eventUtils.VAR_DELETE))(variable));
|
|
243
212
|
return;
|
|
244
213
|
}
|
|
245
214
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Delete a variables by the passed in ID and all of its uses from this
|
|
218
|
+
* workspace. May prompt the user for confirmation.
|
|
219
|
+
* @param {string} id ID of variable to delete.
|
|
220
|
+
*/
|
|
221
|
+
deleteVariableById(id) {
|
|
222
|
+
const variable = this.getVariableById(id);
|
|
223
|
+
if (variable) {
|
|
224
|
+
// Check whether this variable is a function parameter before deleting.
|
|
225
|
+
const variableName = variable.name;
|
|
226
|
+
const uses = this.getVariableUsesById(id);
|
|
227
|
+
for (let i = 0, block; (block = uses[i]); i++) {
|
|
228
|
+
if (block.type === 'procedures_defnoreturn' ||
|
|
229
|
+
block.type === 'procedures_defreturn') {
|
|
230
|
+
const procedureName = String(block.getFieldValue('NAME'));
|
|
231
|
+
const deleteText = Msg['CANNOT_DELETE_VARIABLE_PROCEDURE']
|
|
232
|
+
.replace('%1', variableName)
|
|
233
|
+
.replace('%2', procedureName);
|
|
234
|
+
dialog.alert(deleteText);
|
|
235
|
+
return;
|
|
256
236
|
}
|
|
257
|
-
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const map = this;
|
|
240
|
+
if (uses.length > 1) {
|
|
241
|
+
// Confirm before deleting multiple blocks.
|
|
242
|
+
const confirmText = Msg['DELETE_VARIABLE_CONFIRMATION']
|
|
243
|
+
.replace('%1', String(uses.length))
|
|
244
|
+
.replace('%2', variableName);
|
|
245
|
+
dialog.confirm(confirmText, function(ok) {
|
|
246
|
+
if (ok && variable) {
|
|
247
|
+
map.deleteVariableInternal(variable, uses);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
} else {
|
|
251
|
+
// No confirmation necessary for a single block.
|
|
252
|
+
map.deleteVariableInternal(variable, uses);
|
|
253
|
+
}
|
|
258
254
|
} else {
|
|
259
|
-
|
|
260
|
-
map.deleteVariableInternal(variable, uses);
|
|
255
|
+
console.warn('Can\'t delete non-existent variable: ' + id);
|
|
261
256
|
}
|
|
262
|
-
} else {
|
|
263
|
-
console.warn('Can\'t delete non-existent variable: ' + id);
|
|
264
|
-
}
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Deletes a variable and all of its uses from this workspace without asking the
|
|
269
|
-
* user for confirmation.
|
|
270
|
-
* @param {!VariableModel} variable Variable to delete.
|
|
271
|
-
* @param {!Array<!Block>} uses An array of uses of the variable.
|
|
272
|
-
* @package
|
|
273
|
-
*/
|
|
274
|
-
VariableMap.prototype.deleteVariableInternal = function(variable, uses) {
|
|
275
|
-
const existingGroup = eventUtils.getGroup();
|
|
276
|
-
if (!existingGroup) {
|
|
277
|
-
eventUtils.setGroup(true);
|
|
278
257
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
258
|
+
/**
|
|
259
|
+
* Deletes a variable and all of its uses from this workspace without asking
|
|
260
|
+
* the user for confirmation.
|
|
261
|
+
* @param {!VariableModel} variable Variable to delete.
|
|
262
|
+
* @param {!Array<!Block>} uses An array of uses of the variable.
|
|
263
|
+
* @package
|
|
264
|
+
*/
|
|
265
|
+
deleteVariableInternal(variable, uses) {
|
|
266
|
+
const existingGroup = eventUtils.getGroup();
|
|
285
267
|
if (!existingGroup) {
|
|
286
|
-
eventUtils.setGroup(
|
|
268
|
+
eventUtils.setGroup(true);
|
|
287
269
|
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
* @param {string} name The name to check for.
|
|
297
|
-
* @param {?string=} opt_type The type of the variable. If not provided it
|
|
298
|
-
* defaults to the empty string, which is a specific type.
|
|
299
|
-
* @return {?VariableModel} The variable with the given name, or null if
|
|
300
|
-
* it was not found.
|
|
301
|
-
*/
|
|
302
|
-
VariableMap.prototype.getVariable = function(name, opt_type) {
|
|
303
|
-
const type = opt_type || '';
|
|
304
|
-
const list = this.variableMap_[type];
|
|
305
|
-
if (list) {
|
|
306
|
-
for (let j = 0, variable; (variable = list[j]); j++) {
|
|
307
|
-
if (Names.equals(variable.name, name)) {
|
|
308
|
-
return variable;
|
|
270
|
+
try {
|
|
271
|
+
for (let i = 0; i < uses.length; i++) {
|
|
272
|
+
uses[i].dispose(true);
|
|
273
|
+
}
|
|
274
|
+
this.deleteVariable(variable);
|
|
275
|
+
} finally {
|
|
276
|
+
if (!existingGroup) {
|
|
277
|
+
eventUtils.setGroup(false);
|
|
309
278
|
}
|
|
310
279
|
}
|
|
311
280
|
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
const
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
281
|
+
/* End functions for variable deletion. */
|
|
282
|
+
/**
|
|
283
|
+
* Find the variable by the given name and type and return it. Return null if
|
|
284
|
+
* it is not found.
|
|
285
|
+
* @param {string} name The name to check for.
|
|
286
|
+
* @param {?string=} opt_type The type of the variable. If not provided it
|
|
287
|
+
* defaults to the empty string, which is a specific type.
|
|
288
|
+
* @return {?VariableModel} The variable with the given name, or null if
|
|
289
|
+
* it was not found.
|
|
290
|
+
*/
|
|
291
|
+
getVariable(name, opt_type) {
|
|
292
|
+
const type = opt_type || '';
|
|
293
|
+
const list = this.variableMap_[type];
|
|
294
|
+
if (list) {
|
|
295
|
+
for (let j = 0, variable; (variable = list[j]); j++) {
|
|
296
|
+
if (Names.equals(variable.name, name)) {
|
|
297
|
+
return variable;
|
|
298
|
+
}
|
|
327
299
|
}
|
|
328
300
|
}
|
|
301
|
+
return null;
|
|
329
302
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
return [];
|
|
347
|
-
};
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
* Return all variable and potential variable types. This list always contains
|
|
351
|
-
* the empty string.
|
|
352
|
-
* @param {?Workspace} ws The workspace used to look for potential
|
|
353
|
-
* variables. This can be different than the workspace stored on this object
|
|
354
|
-
* if the passed in ws is a flyout workspace.
|
|
355
|
-
* @return {!Array<string>} List of variable types.
|
|
356
|
-
* @package
|
|
357
|
-
*/
|
|
358
|
-
VariableMap.prototype.getVariableTypes = function(ws) {
|
|
359
|
-
const variableMap = {};
|
|
360
|
-
object.mixin(variableMap, this.variableMap_);
|
|
361
|
-
if (ws && ws.getPotentialVariableMap()) {
|
|
362
|
-
object.mixin(variableMap, ws.getPotentialVariableMap().variableMap_);
|
|
303
|
+
/**
|
|
304
|
+
* Find the variable by the given ID and return it. Return null if not found.
|
|
305
|
+
* @param {string} id The ID to check for.
|
|
306
|
+
* @return {?VariableModel} The variable with the given ID.
|
|
307
|
+
*/
|
|
308
|
+
getVariableById(id) {
|
|
309
|
+
const keys = Object.keys(this.variableMap_);
|
|
310
|
+
for (let i = 0; i < keys.length; i++) {
|
|
311
|
+
const key = keys[i];
|
|
312
|
+
for (let j = 0, variable; (variable = this.variableMap_[key][j]); j++) {
|
|
313
|
+
if (variable.getId() === id) {
|
|
314
|
+
return variable;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return null;
|
|
363
319
|
}
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
320
|
+
/**
|
|
321
|
+
* Get a list containing all of the variables of a specified type. If type is
|
|
322
|
+
* null, return list of variables with empty string type.
|
|
323
|
+
* @param {?string} type Type of the variables to find.
|
|
324
|
+
* @return {!Array<!VariableModel>} The sought after variables of the
|
|
325
|
+
* passed in type. An empty array if none are found.
|
|
326
|
+
*/
|
|
327
|
+
getVariablesOfType(type) {
|
|
328
|
+
type = type || '';
|
|
329
|
+
const variableList = this.variableMap_[type];
|
|
330
|
+
if (variableList) {
|
|
331
|
+
return variableList.slice();
|
|
369
332
|
}
|
|
333
|
+
return [];
|
|
370
334
|
}
|
|
371
|
-
|
|
372
|
-
|
|
335
|
+
/**
|
|
336
|
+
* Return all variable and potential variable types. This list always
|
|
337
|
+
* contains the empty string.
|
|
338
|
+
* @param {?Workspace} ws The workspace used to look for potential
|
|
339
|
+
* variables. This can be different than the workspace stored on this object
|
|
340
|
+
* if the passed in ws is a flyout workspace.
|
|
341
|
+
* @return {!Array<string>} List of variable types.
|
|
342
|
+
* @package
|
|
343
|
+
*/
|
|
344
|
+
getVariableTypes(ws) {
|
|
345
|
+
const variableMap = {};
|
|
346
|
+
object.mixin(variableMap, this.variableMap_);
|
|
347
|
+
if (ws && ws.getPotentialVariableMap()) {
|
|
348
|
+
object.mixin(variableMap, ws.getPotentialVariableMap().variableMap_);
|
|
349
|
+
}
|
|
350
|
+
const types = Object.keys(variableMap);
|
|
351
|
+
let hasEmpty = false;
|
|
352
|
+
for (let i = 0; i < types.length; i++) {
|
|
353
|
+
if (types[i] === '') {
|
|
354
|
+
hasEmpty = true;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
if (!hasEmpty) {
|
|
358
|
+
types.push('');
|
|
359
|
+
}
|
|
360
|
+
return types;
|
|
373
361
|
}
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
allVariables = allVariables.concat(this.variableMap_[key]);
|
|
362
|
+
/**
|
|
363
|
+
* Return all variables of all types.
|
|
364
|
+
* @return {!Array<!VariableModel>} List of variable models.
|
|
365
|
+
*/
|
|
366
|
+
getAllVariables() {
|
|
367
|
+
let allVariables = [];
|
|
368
|
+
for (const key in this.variableMap_) {
|
|
369
|
+
allVariables = allVariables.concat(this.variableMap_[key]);
|
|
370
|
+
}
|
|
371
|
+
return allVariables;
|
|
385
372
|
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
for (let i = 0, variable; (variable = variables[i]); i++) {
|
|
398
|
-
allNames.push(variable.name);
|
|
373
|
+
/**
|
|
374
|
+
* Returns all of the variable names of all types.
|
|
375
|
+
* @return {!Array<string>} All of the variable names of all types.
|
|
376
|
+
*/
|
|
377
|
+
getAllVariableNames() {
|
|
378
|
+
const allNames = [];
|
|
379
|
+
for (const key in this.variableMap_) {
|
|
380
|
+
const variables = this.variableMap_[key];
|
|
381
|
+
for (let i = 0, variable; (variable = variables[i]); i++) {
|
|
382
|
+
allNames.push(variable.name);
|
|
383
|
+
}
|
|
399
384
|
}
|
|
385
|
+
return allNames;
|
|
400
386
|
}
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
if (blockVariables[j].getId() === id) {
|
|
418
|
-
uses.push(blocks[i]);
|
|
387
|
+
/**
|
|
388
|
+
* Find all the uses of a named variable.
|
|
389
|
+
* @param {string} id ID of the variable to find.
|
|
390
|
+
* @return {!Array<!Block>} Array of block usages.
|
|
391
|
+
*/
|
|
392
|
+
getVariableUsesById(id) {
|
|
393
|
+
const uses = [];
|
|
394
|
+
const blocks = this.workspace.getAllBlocks(false);
|
|
395
|
+
// Iterate through every block and check the name.
|
|
396
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
397
|
+
const blockVariables = blocks[i].getVarModels();
|
|
398
|
+
if (blockVariables) {
|
|
399
|
+
for (let j = 0; j < blockVariables.length; j++) {
|
|
400
|
+
if (blockVariables[j].getId() === id) {
|
|
401
|
+
uses.push(blocks[i]);
|
|
402
|
+
}
|
|
419
403
|
}
|
|
420
404
|
}
|
|
421
405
|
}
|
|
406
|
+
return uses;
|
|
422
407
|
}
|
|
423
|
-
|
|
424
|
-
};
|
|
408
|
+
}
|
|
425
409
|
|
|
426
410
|
exports.VariableMap = VariableMap;
|