scratch-blocks 2.0.0-spork.3 → 2.0.0-spork.5
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/CHANGELOG.md +24 -0
- package/dist/main.js +1 -1
- package/dist/main.js.LICENSE.txt +3 -9
- package/package.json +4 -4
- package/src/blocks/procedures.ts +1 -1
- package/src/checkable_continuous_flyout.ts +116 -0
- package/src/checkbox_bubble.ts +25 -5
- package/src/colours.ts +1 -0
- package/src/context_menu_items.ts +24 -0
- package/src/css.ts +30 -21
- package/src/fields/field_matrix.ts +38 -27
- package/src/fields/field_note.ts +1 -1
- package/src/fields/scratch_field_angle.ts +9 -1
- package/src/fields/scratch_field_variable.ts +9 -8
- package/src/flyout_checkbox_icon.ts +15 -21
- package/src/index.ts +6 -3
- package/src/recyclable_block_flyout_inflater.ts +8 -151
- package/src/scratch_block_paster.ts +10 -0
- package/src/{scratch_comment_bubble.js → scratch_comment_bubble.ts} +52 -19
- package/src/scratch_comment_icon.ts +20 -5
- package/src/scratch_continuous_toolbox.ts +36 -30
- package/src/scratch_insertion_marker_previewer.ts +45 -0
- package/src/status_indicator_label_flyout_inflater.ts +8 -6
- package/src/variables.ts +1 -1
- package/src/xml.ts +57 -0
- package/src/checkable_continuous_flyout.js +0 -138
package/src/xml.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as Blockly from "blockly/core";
|
|
8
|
+
import { ScratchVariableModel } from "./scratch_variable_model";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Clears the workspace and loads the given serialized state.
|
|
12
|
+
*
|
|
13
|
+
* @param xml XML representation of a Blockly workspace.
|
|
14
|
+
* @param workspace The workspace to load the serialized data onto.
|
|
15
|
+
*/
|
|
16
|
+
export function clearWorkspaceAndLoadFromXml(
|
|
17
|
+
xml: Element,
|
|
18
|
+
workspace: Blockly.WorkspaceSvg
|
|
19
|
+
): string[] {
|
|
20
|
+
workspace.setResizesEnabled(false);
|
|
21
|
+
Blockly.Events.setGroup(true);
|
|
22
|
+
workspace.clear();
|
|
23
|
+
|
|
24
|
+
// Manually load variables to include the cloud and local properties that core
|
|
25
|
+
// Blockly is unaware of.
|
|
26
|
+
for (const variable of xml.querySelectorAll("variables variable")) {
|
|
27
|
+
const id = variable.getAttribute("id");
|
|
28
|
+
if (!id) continue;
|
|
29
|
+
const type = variable.getAttribute("type");
|
|
30
|
+
const name = variable.textContent;
|
|
31
|
+
const isLocal = variable.getAttribute("islocal") === "true";
|
|
32
|
+
const isCloud = variable.getAttribute("iscloud") === "true";
|
|
33
|
+
|
|
34
|
+
const variableModel = new ScratchVariableModel(
|
|
35
|
+
workspace,
|
|
36
|
+
name,
|
|
37
|
+
type,
|
|
38
|
+
id,
|
|
39
|
+
isLocal,
|
|
40
|
+
isCloud
|
|
41
|
+
);
|
|
42
|
+
Blockly.Events.fire(
|
|
43
|
+
new (Blockly.Events.get(Blockly.Events.VAR_CREATE))(variableModel)
|
|
44
|
+
);
|
|
45
|
+
workspace.getVariableMap().addVariable(variableModel);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Remove the `variables` element from the XML to prevent Blockly from
|
|
49
|
+
// throwing or stomping on the variables we created.
|
|
50
|
+
xml.querySelector("variables").remove();
|
|
51
|
+
|
|
52
|
+
// Defer to core for the rest of the deserialization.
|
|
53
|
+
const blockIds = Blockly.Xml.domToWorkspace(xml, workspace);
|
|
54
|
+
|
|
55
|
+
workspace.setResizesEnabled(true);
|
|
56
|
+
return blockIds;
|
|
57
|
+
}
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license
|
|
3
|
-
* Copyright 2024 Google LLC
|
|
4
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import * as Blockly from "blockly/core";
|
|
8
|
-
import { ContinuousFlyout } from "@blockly/continuous-toolbox";
|
|
9
|
-
import { RecyclableBlockFlyoutInflater } from "./recyclable_block_flyout_inflater";
|
|
10
|
-
import { StatusIndicatorLabel } from "./status_indicator_label";
|
|
11
|
-
|
|
12
|
-
export class CheckableContinuousFlyout extends ContinuousFlyout {
|
|
13
|
-
/**
|
|
14
|
-
* Creates a new CheckableContinuousFlyout.
|
|
15
|
-
*
|
|
16
|
-
* @param {!Blockly.Options} workspaceOptions Configuration options for the
|
|
17
|
-
* flyout workspace.
|
|
18
|
-
*/
|
|
19
|
-
constructor(workspaceOptions) {
|
|
20
|
-
workspaceOptions.modalInputs = false;
|
|
21
|
-
super(workspaceOptions);
|
|
22
|
-
this.tabWidth_ = -2;
|
|
23
|
-
this.MARGIN = 12;
|
|
24
|
-
this.GAP_Y = 12;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Displays the given contents in the flyout.
|
|
29
|
-
*
|
|
30
|
-
* @param {!Object} flyoutDef The new contents to show in the flyout.
|
|
31
|
-
*/
|
|
32
|
-
show(flyoutDef) {
|
|
33
|
-
super.show(flyoutDef);
|
|
34
|
-
const inflater = this.getInflaterForType("block");
|
|
35
|
-
if (inflater instanceof RecyclableBlockFlyoutInflater) {
|
|
36
|
-
inflater.emptyRecycledBlocks();
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Serializes a block to JSON in order to copy it to the main workspace.
|
|
42
|
-
*
|
|
43
|
-
* @param {!Blockly.BlockSvg} block The block to serialize.
|
|
44
|
-
* @returns {!Object} A JSON representation of the block.
|
|
45
|
-
*/
|
|
46
|
-
serializeBlock(block) {
|
|
47
|
-
const json = super.serializeBlock(block);
|
|
48
|
-
// Delete the serialized block's ID so that a new one is generated when it is
|
|
49
|
-
// placed on the workspace. Otherwise, the block on the workspace may be
|
|
50
|
-
// indistinguishable from the one in the flyout, which can cause reporter blocks
|
|
51
|
-
// to have their value dropdown shown in the wrong place.
|
|
52
|
-
delete json.id;
|
|
53
|
-
return json;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Set the state of a checkbox by block ID.
|
|
58
|
-
* @param {string} blockId ID of the block whose checkbox should be set
|
|
59
|
-
* @param {boolean} value Value to set the checkbox to.
|
|
60
|
-
* @public
|
|
61
|
-
*/
|
|
62
|
-
setCheckboxState(blockId, value) {
|
|
63
|
-
this.getWorkspace()
|
|
64
|
-
.getBlockById(blockId)
|
|
65
|
-
?.getIcon("checkbox")
|
|
66
|
-
?.setChecked(value);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
getFlyoutScale() {
|
|
70
|
-
return 0.675;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
getWidth() {
|
|
74
|
-
return 250;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Sets whether or not block recycling is enabled in the flyout.
|
|
79
|
-
*
|
|
80
|
-
* @param {boolean} enabled True if recycling should be enabled.
|
|
81
|
-
*/
|
|
82
|
-
setRecyclingEnabled(enabled) {
|
|
83
|
-
const inflater = this.getInflaterForType("block");
|
|
84
|
-
if (inflater instanceof RecyclableBlockFlyoutInflater) {
|
|
85
|
-
inflater.setRecyclingEnabled(enabled);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Records scroll position for each category in the toolbox.
|
|
91
|
-
* The scroll position is determined by the coordinates of each category's
|
|
92
|
-
* label after the entire flyout has been rendered.
|
|
93
|
-
* @package
|
|
94
|
-
*/
|
|
95
|
-
recordScrollPositions() {
|
|
96
|
-
// TODO(#211) Remove this once the continuous toolbox has been updated.
|
|
97
|
-
this.scrollPositions = [];
|
|
98
|
-
const categoryLabels = this.getContents()
|
|
99
|
-
.filter(
|
|
100
|
-
(item) =>
|
|
101
|
-
(item.type === "label" || item.type === "status_indicator_label") &&
|
|
102
|
-
item.element.isLabel() &&
|
|
103
|
-
this.getParentToolbox_().getCategoryByName(
|
|
104
|
-
item.element.getButtonText()
|
|
105
|
-
)
|
|
106
|
-
)
|
|
107
|
-
.map((item) => item.element);
|
|
108
|
-
for (const [index, label] of categoryLabels.entries()) {
|
|
109
|
-
this.scrollPositions.push({
|
|
110
|
-
name: label.getButtonText(),
|
|
111
|
-
position: label.getPosition(),
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Positions the contents of the flyout.
|
|
118
|
-
*
|
|
119
|
-
* @param {!Blockly.FlyoutItem[]} The flyout items to position.
|
|
120
|
-
*/
|
|
121
|
-
layout_(contents) {
|
|
122
|
-
// TODO(#211) Remove this once the continuous toolbox has been updated.
|
|
123
|
-
// Bypass the continuous flyout's layout method until the plugin is
|
|
124
|
-
// updated for the new flyout API.
|
|
125
|
-
Blockly.VerticalFlyout.prototype.layout_.call(this, contents);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Updates the state of status indicators for hardware-based extensions.
|
|
130
|
-
*/
|
|
131
|
-
refreshStatusButtons() {
|
|
132
|
-
for (const item of this.contents) {
|
|
133
|
-
if (item.element instanceof StatusIndicatorLabel) {
|
|
134
|
-
item.element.refreshStatus();
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|