@kubohiroya/sb3-toolchain 0.3.0 → 0.5.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/README.ja.md +151 -0
- package/README.md +9 -6
- package/docs/extension-bundles.md +45 -6
- package/docs/extension-id-migration.md +2 -0
- package/docs/ja/extension-bundles.md +465 -0
- package/docs/ja/extension-id-migration.md +69 -0
- package/docs/ja/source-format-v1.md +205 -0
- package/docs/ja/workflows.md +265 -0
- package/docs/source-format-v1.md +28 -9
- package/docs/workflows.md +33 -13
- package/package.json +4 -3
- package/src/build.js +8 -2
- package/src/cli.js +31 -4
- package/src/constants.js +1 -1
- package/src/extension-bundle.js +32 -1
- package/src/extension-dependencies.js +32 -19
- package/src/extension-sync.js +109 -20
- package/src/index.js +1 -0
- package/src/npm-extension-source.js +111 -0
- package/src/source.js +16 -2
- package/src/turbowarp-clean-up.js +295 -0
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
export const turboWarpCleanUpLayout = Object.freeze({
|
|
4
|
+
columnGap: 96,
|
|
5
|
+
columnTolerance: 256,
|
|
6
|
+
rowGap: 72,
|
|
7
|
+
startX: 48,
|
|
8
|
+
startY: 64,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const minimumBlockHeight = 48;
|
|
12
|
+
const minimumBlockWidth = 160;
|
|
13
|
+
const nestedStackIndent = 64;
|
|
14
|
+
const nestedStackPadding = 24;
|
|
15
|
+
const nextBlockOverlap = 4;
|
|
16
|
+
|
|
17
|
+
function assert(condition, message) {
|
|
18
|
+
if (!condition) {
|
|
19
|
+
throw new Error(message);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function isObject(value) {
|
|
24
|
+
return value && typeof value === 'object' && !Array.isArray(value);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function compareNumbers(left, right) {
|
|
28
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function inputBlockId(input, blocks) {
|
|
32
|
+
if (!Array.isArray(input)) return null;
|
|
33
|
+
for (const value of input.slice(1)) {
|
|
34
|
+
if (typeof value === 'string' && Object.hasOwn(blocks, value)) return value;
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function displayTextLength(block) {
|
|
40
|
+
let length = typeof block.opcode === 'string' ? block.opcode.length : 0;
|
|
41
|
+
if (isObject(block.fields)) {
|
|
42
|
+
for (const field of Object.values(block.fields)) {
|
|
43
|
+
if (Array.isArray(field) && field[0] !== undefined && field[0] !== null) {
|
|
44
|
+
length += String(field[0]).length;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (isObject(block.mutation) && typeof block.mutation.proccode === 'string') {
|
|
49
|
+
length += block.mutation.proccode.length;
|
|
50
|
+
}
|
|
51
|
+
return length;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function inputDisplayTextLength(input, blocks) {
|
|
55
|
+
const childId = inputBlockId(input, blocks);
|
|
56
|
+
if (childId) return displayTextLength(blocks[childId]);
|
|
57
|
+
for (const value of input.slice(1)) {
|
|
58
|
+
if (!Array.isArray(value) || value.length < 2) continue;
|
|
59
|
+
const displayValue = value[1];
|
|
60
|
+
if (
|
|
61
|
+
typeof displayValue === 'string' ||
|
|
62
|
+
typeof displayValue === 'number' ||
|
|
63
|
+
typeof displayValue === 'boolean'
|
|
64
|
+
) {
|
|
65
|
+
return String(displayValue).length;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return 0;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function verticalChildIds(block, blocks) {
|
|
72
|
+
const childIds = [];
|
|
73
|
+
if (isObject(block.inputs)) {
|
|
74
|
+
for (const [inputName, input] of Object.entries(block.inputs)) {
|
|
75
|
+
const childId = inputBlockId(input, blocks);
|
|
76
|
+
if (childId && /^SUBSTACK/u.test(inputName)) childIds.push(childId);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (typeof block.next === 'string' && Object.hasOwn(blocks, block.next)) {
|
|
80
|
+
childIds.push(block.next);
|
|
81
|
+
}
|
|
82
|
+
return childIds;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function measureStack(blockId, blocks) {
|
|
86
|
+
const measured = new Map();
|
|
87
|
+
const active = new Set();
|
|
88
|
+
const work = [{blockId, finalize: false}];
|
|
89
|
+
|
|
90
|
+
while (work.length > 0) {
|
|
91
|
+
const frame = work.pop();
|
|
92
|
+
if (measured.has(frame.blockId) || !isObject(blocks[frame.blockId])) continue;
|
|
93
|
+
const block = blocks[frame.blockId];
|
|
94
|
+
if (!frame.finalize) {
|
|
95
|
+
if (active.has(frame.blockId)) continue;
|
|
96
|
+
active.add(frame.blockId);
|
|
97
|
+
work.push({blockId: frame.blockId, finalize: true});
|
|
98
|
+
const childIds = verticalChildIds(block, blocks);
|
|
99
|
+
for (let index = childIds.length - 1; index >= 0; index -= 1) {
|
|
100
|
+
const childId = childIds[index];
|
|
101
|
+
if (!active.has(childId) && !measured.has(childId)) {
|
|
102
|
+
work.push({blockId: childId, finalize: false});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
let height = minimumBlockHeight;
|
|
109
|
+
let width = Math.max(minimumBlockWidth, 80 + displayTextLength(block) * 8);
|
|
110
|
+
if (isObject(block.inputs)) {
|
|
111
|
+
for (const [inputName, input] of Object.entries(block.inputs)) {
|
|
112
|
+
const childId = inputBlockId(input, blocks);
|
|
113
|
+
if (childId && /^SUBSTACK/u.test(inputName)) {
|
|
114
|
+
const nested = measured.get(childId) ?? {height: 0, width: 0};
|
|
115
|
+
height += nested.height + nestedStackPadding;
|
|
116
|
+
width = Math.max(width, nested.width + nestedStackIndent);
|
|
117
|
+
} else {
|
|
118
|
+
width += 24 + inputDisplayTextLength(input, blocks) * 8;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (typeof block.next === 'string' && Object.hasOwn(blocks, block.next)) {
|
|
123
|
+
const next = measured.get(block.next) ?? {height: 0, width: 0};
|
|
124
|
+
height += Math.max(0, next.height - nextBlockOverlap);
|
|
125
|
+
width = Math.max(width, next.width);
|
|
126
|
+
}
|
|
127
|
+
measured.set(frame.blockId, {height, width});
|
|
128
|
+
active.delete(frame.blockId);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return measured.get(blockId) ?? {height: 0, width: 0};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function groupIntoColumns(scripts) {
|
|
135
|
+
const columns = [];
|
|
136
|
+
for (const script of scripts) {
|
|
137
|
+
let bestColumn = null;
|
|
138
|
+
let bestError = Number(turboWarpCleanUpLayout.columnTolerance);
|
|
139
|
+
for (const column of columns) {
|
|
140
|
+
const error = Math.abs(script.originalX - column.x);
|
|
141
|
+
if (error < bestError) {
|
|
142
|
+
bestColumn = column;
|
|
143
|
+
bestError = error;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (bestColumn) {
|
|
147
|
+
bestColumn.count += 1;
|
|
148
|
+
bestColumn.x = (bestColumn.x * (bestColumn.count - 1) + script.originalX) / bestColumn.count;
|
|
149
|
+
bestColumn.scripts.push(script);
|
|
150
|
+
} else {
|
|
151
|
+
columns.push({count: 1, scripts: [script], x: script.originalX});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
columns.sort((left, right) => compareNumbers(left.x, right.x));
|
|
155
|
+
for (const column of columns) {
|
|
156
|
+
column.scripts.sort(
|
|
157
|
+
(left, right) =>
|
|
158
|
+
compareNumbers(left.originalY, right.originalY) ||
|
|
159
|
+
compareNumbers(left.inputOrder, right.inputOrder),
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
return columns;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function findTopBlockId(blockId, blocks) {
|
|
166
|
+
const visited = new Set();
|
|
167
|
+
let currentId = blockId;
|
|
168
|
+
while (typeof currentId === 'string' && !visited.has(currentId)) {
|
|
169
|
+
visited.add(currentId);
|
|
170
|
+
const block = blocks[currentId];
|
|
171
|
+
if (!isObject(block)) return null;
|
|
172
|
+
if (block.topLevel === true || block.parent === null) return currentId;
|
|
173
|
+
currentId = block.parent;
|
|
174
|
+
}
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function moveAttachedComments(target, blocks, movements) {
|
|
179
|
+
if (!isObject(target.comments)) return 0;
|
|
180
|
+
let movedCommentCount = 0;
|
|
181
|
+
for (const comment of Object.values(target.comments)) {
|
|
182
|
+
if (!isObject(comment) || typeof comment.blockId !== 'string') continue;
|
|
183
|
+
const topBlockId = findTopBlockId(comment.blockId, blocks);
|
|
184
|
+
const movement = movements.get(topBlockId);
|
|
185
|
+
if (!movement || (!movement.deltaX && !movement.deltaY)) continue;
|
|
186
|
+
if (Number.isFinite(comment.x)) comment.x += movement.deltaX;
|
|
187
|
+
if (Number.isFinite(comment.y)) comment.y += movement.deltaY;
|
|
188
|
+
movedCommentCount += 1;
|
|
189
|
+
}
|
|
190
|
+
return movedCommentCount;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function attachedCommentWidths(target, blocks) {
|
|
194
|
+
const widths = new Map();
|
|
195
|
+
if (!isObject(target.comments)) return widths;
|
|
196
|
+
for (const comment of Object.values(target.comments)) {
|
|
197
|
+
if (
|
|
198
|
+
!isObject(comment) ||
|
|
199
|
+
typeof comment.blockId !== 'string' ||
|
|
200
|
+
!Number.isFinite(comment.x) ||
|
|
201
|
+
!Number.isFinite(comment.width)
|
|
202
|
+
) {
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
const topBlockId = findTopBlockId(comment.blockId, blocks);
|
|
206
|
+
const topBlock = blocks[topBlockId];
|
|
207
|
+
if (!topBlockId || !isObject(topBlock) || !Number.isFinite(topBlock.x)) continue;
|
|
208
|
+
const width = Math.max(comment.width, comment.x + comment.width - topBlock.x);
|
|
209
|
+
widths.set(topBlockId, Math.max(widths.get(topBlockId) ?? 0, width));
|
|
210
|
+
}
|
|
211
|
+
return widths;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function cleanUpTarget(target, targetIndex) {
|
|
215
|
+
if (target.blocks === undefined) {
|
|
216
|
+
return {movedCommentCount: 0, movedScriptCount: 0, scriptCount: 0};
|
|
217
|
+
}
|
|
218
|
+
assert(isObject(target.blocks), `Project target ${targetIndex} blocks must be an object.`);
|
|
219
|
+
const blocks = target.blocks;
|
|
220
|
+
const scripts = [];
|
|
221
|
+
let inputOrder = 0;
|
|
222
|
+
for (const [blockId, block] of Object.entries(blocks)) {
|
|
223
|
+
if (!isObject(block) || block.topLevel !== true) continue;
|
|
224
|
+
assert(
|
|
225
|
+
Number.isFinite(block.x) && Number.isFinite(block.y),
|
|
226
|
+
`Top-level block ${JSON.stringify(blockId)} in project target ${targetIndex} requires numeric x and y coordinates.`,
|
|
227
|
+
);
|
|
228
|
+
scripts.push({
|
|
229
|
+
block,
|
|
230
|
+
blockId,
|
|
231
|
+
inputOrder,
|
|
232
|
+
originalX: block.x,
|
|
233
|
+
originalY: block.y,
|
|
234
|
+
size: measureStack(blockId, blocks),
|
|
235
|
+
});
|
|
236
|
+
inputOrder += 1;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const movements = new Map();
|
|
240
|
+
const commentWidths = attachedCommentWidths(target, blocks);
|
|
241
|
+
let cursorX = turboWarpCleanUpLayout.startX;
|
|
242
|
+
let movedScriptCount = 0;
|
|
243
|
+
for (const column of groupIntoColumns(scripts)) {
|
|
244
|
+
let cursorY = turboWarpCleanUpLayout.startY;
|
|
245
|
+
let columnWidth = 0;
|
|
246
|
+
for (const script of column.scripts) {
|
|
247
|
+
const deltaX = cursorX - script.originalX;
|
|
248
|
+
const deltaY = cursorY - script.originalY;
|
|
249
|
+
script.block.x = cursorX;
|
|
250
|
+
script.block.y = cursorY;
|
|
251
|
+
movements.set(script.blockId, {deltaX, deltaY});
|
|
252
|
+
if (deltaX || deltaY) movedScriptCount += 1;
|
|
253
|
+
cursorY += script.size.height + turboWarpCleanUpLayout.rowGap;
|
|
254
|
+
columnWidth = Math.max(
|
|
255
|
+
columnWidth,
|
|
256
|
+
script.size.width,
|
|
257
|
+
commentWidths.get(script.blockId) ?? 0,
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
cursorX += columnWidth + turboWarpCleanUpLayout.columnGap;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return {
|
|
264
|
+
movedCommentCount: moveAttachedComments(target, blocks, movements),
|
|
265
|
+
movedScriptCount,
|
|
266
|
+
scriptCount: scripts.length,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export function cleanUpTurboWarpBlocks(project) {
|
|
271
|
+
assert(isObject(project), 'TurboWarp project must be an object.');
|
|
272
|
+
assert(Array.isArray(project.targets), 'TurboWarp project targets must be an array.');
|
|
273
|
+
const cleanedProject = structuredClone(project);
|
|
274
|
+
let movedCommentCount = 0;
|
|
275
|
+
let movedScriptCount = 0;
|
|
276
|
+
let scriptCount = 0;
|
|
277
|
+
let targetCount = 0;
|
|
278
|
+
|
|
279
|
+
for (const [targetIndex, target] of cleanedProject.targets.entries()) {
|
|
280
|
+
assert(isObject(target), `Project target ${targetIndex} must be an object.`);
|
|
281
|
+
const result = cleanUpTarget(target, targetIndex);
|
|
282
|
+
if (result.scriptCount > 0) targetCount += 1;
|
|
283
|
+
movedCommentCount += result.movedCommentCount;
|
|
284
|
+
movedScriptCount += result.movedScriptCount;
|
|
285
|
+
scriptCount += result.scriptCount;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return {
|
|
289
|
+
movedCommentCount,
|
|
290
|
+
movedScriptCount,
|
|
291
|
+
project: cleanedProject,
|
|
292
|
+
scriptCount,
|
|
293
|
+
targetCount,
|
|
294
|
+
};
|
|
295
|
+
}
|