@kubohiroya/turbowarp-plus-editor-code-views 0.21.2 → 0.22.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/EDITOR-MIGRATION.md +5 -0
- package/README.ja.md +16 -1
- package/dist/code-pane.d.ts +7 -1
- package/dist/code-pane.js +45 -3
- package/dist/code-popout.d.ts +10 -0
- package/dist/code-popout.js +402 -0
- package/dist/context-menu-icons.d.ts +15 -0
- package/dist/context-menu-icons.js +204 -0
- package/dist/desktop-code.d.ts +11 -0
- package/dist/desktop-code.js +47 -0
- package/dist/horizontal-block-palette.d.ts +39 -0
- package/dist/horizontal-block-palette.js +774 -0
- package/dist/palette-delete-preview.d.ts +3 -0
- package/dist/palette-delete-preview.js +73 -0
- package/dist/script-arrows.js +106 -12
- package/dist/script-layout-view.js +122 -6
- package/dist/script-layout-workspace.js +7 -2
- package/dist/script-layout.d.ts +1 -1
- package/dist/script-layout.js +3 -3
- package/dist/script-projection.d.ts +8 -0
- package/dist/script-projection.js +10 -0
- package/dist/script-view-menu.d.ts +1 -0
- package/dist/script-view-menu.js +4 -2
- package/dist/serial-script-editor.d.ts +4 -0
- package/dist/serial-script-editor.js +43 -0
- package/dist/workspace-overview.js +67 -25
- package/dist/workspace.d.ts +1 -0
- package/dist/workspace.js +3 -2
- package/package.json +27 -3
- package/src/code-pane.ts +41 -3
- package/src/code-popout.tsx +399 -0
- package/src/context-menu-icons.css +14 -0
- package/src/context-menu-icons.ts +215 -0
- package/src/desktop-code.ts +45 -0
- package/src/horizontal-block-palette.css +111 -0
- package/src/horizontal-block-palette.ts +765 -0
- package/src/palette-delete-preview.css +21 -0
- package/src/palette-delete-preview.ts +79 -0
- package/src/pin-off.svg +6 -0
- package/src/pin.svg +5 -0
- package/src/script-arrows.ts +98 -12
- package/src/script-layout-view.ts +114 -6
- package/src/script-layout-workspace.ts +7 -2
- package/src/script-layout.ts +4 -3
- package/src/script-projection.ts +12 -0
- package/src/script-view-menu.ts +5 -2
- package/src/serial-script-editor.ts +41 -0
- package/src/styles.css +80 -5
- package/src/trash.svg +3 -0
- package/src/workspace-overview.ts +63 -22
- package/src/workspace.ts +3 -2
- package/tsconfig.json +87 -1
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
export function createPaletteDeletePreview(styles) {
|
|
3
|
+
const installed = new WeakSet();
|
|
4
|
+
const installPaletteDeletePreview = ScratchBlocks => {
|
|
5
|
+
const gesture = ScratchBlocks.Gesture?.prototype;
|
|
6
|
+
if (!gesture?.handleMove || !gesture.dispose || installed.has(gesture))
|
|
7
|
+
return;
|
|
8
|
+
installed.add(gesture);
|
|
9
|
+
const previews = new WeakMap();
|
|
10
|
+
const clear = current => {
|
|
11
|
+
previews.get(current)?.remove();
|
|
12
|
+
previews.delete(current);
|
|
13
|
+
};
|
|
14
|
+
const { handleMove, dispose } = gesture;
|
|
15
|
+
gesture.handleMove = function (event) {
|
|
16
|
+
const result = handleMove.call(this, event);
|
|
17
|
+
const dragger = this.blockDragger_;
|
|
18
|
+
// Reuse the dragger's decision, including deletability and competing connections.
|
|
19
|
+
if (!dragger?.wouldDeleteBlock_ || dragger.deleteArea_ !== ScratchBlocks.DELETE_AREA_TOOLBOX) {
|
|
20
|
+
clear(this);
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
const workspace = this.startWorkspace_;
|
|
24
|
+
// Addons can report DELETE_AREA_TOOLBOX for drop targets outside the palette.
|
|
25
|
+
const hitbox = workspace.deleteAreaToolbox_;
|
|
26
|
+
const { clientX, clientY } = event;
|
|
27
|
+
if (!hitbox || clientX < hitbox.left || clientX > hitbox.left + hitbox.width ||
|
|
28
|
+
clientY < hitbox.top || clientY > hitbox.top + hitbox.height) {
|
|
29
|
+
clear(this);
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
const toolbox = workspace.getToolbox()?.HtmlDiv;
|
|
33
|
+
const flyout = workspace.getFlyout()?.svgBackground_;
|
|
34
|
+
const container = workspace.getInjectionDiv();
|
|
35
|
+
if (!toolbox || !container || workspace.isMutator) {
|
|
36
|
+
clear(this);
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
// The hitbox extends offscreen. Use the visible toolbox and flyout for the overlay.
|
|
40
|
+
const bounds = container.getBoundingClientRect();
|
|
41
|
+
const menu = toolbox.getBoundingClientRect();
|
|
42
|
+
const blocks = toolbox.hasAttribute('data-horizontal-block-palette') ? menu : flyout?.getBoundingClientRect();
|
|
43
|
+
if (!blocks?.width || !blocks.height) {
|
|
44
|
+
clear(this);
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
const left = Math.max(bounds.left, Math.min(menu.left, blocks.left));
|
|
48
|
+
const top = Math.max(bounds.top, Math.min(menu.top, blocks.top));
|
|
49
|
+
const right = Math.min(bounds.right, Math.max(menu.right, blocks.right));
|
|
50
|
+
const bottom = Math.min(bounds.bottom, Math.max(menu.bottom, blocks.bottom));
|
|
51
|
+
if (right <= left || bottom <= top) {
|
|
52
|
+
clear(this);
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
let preview = previews.get(this);
|
|
56
|
+
if (!preview) {
|
|
57
|
+
preview = container.ownerDocument.createElement('div');
|
|
58
|
+
preview.className = styles.preview;
|
|
59
|
+
preview.dataset.paletteDeletePreview = '';
|
|
60
|
+
preview.setAttribute('aria-hidden', 'true');
|
|
61
|
+
container.appendChild(preview);
|
|
62
|
+
previews.set(this, preview);
|
|
63
|
+
}
|
|
64
|
+
preview.style.inset = `${top - bounds.top}px ${bounds.right - right}px ${bounds.bottom - bottom}px ${left - bounds.left}px`;
|
|
65
|
+
return result;
|
|
66
|
+
};
|
|
67
|
+
gesture.dispose = function (...args) {
|
|
68
|
+
clear(this);
|
|
69
|
+
return dispose.apply(this, args);
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
return { installPaletteDeletePreview };
|
|
73
|
+
}
|
package/dist/script-arrows.js
CHANGED
|
@@ -1,9 +1,29 @@
|
|
|
1
1
|
import { isFoldedDefinition } from "./procedure-edits.js";
|
|
2
2
|
import { readScriptRelations } from "./script-relations.js";
|
|
3
|
+
function roundedRoute(points, radius) {
|
|
4
|
+
let path = `M ${points[0].x} ${points[0].y}`;
|
|
5
|
+
for (let index = 1; index < points.length - 1; index++) {
|
|
6
|
+
const before = points[index - 1], corner = points[index], after = points[index + 1];
|
|
7
|
+
const entering = Math.hypot(corner.x - before.x, corner.y - before.y);
|
|
8
|
+
const leaving = Math.hypot(after.x - corner.x, after.y - corner.y);
|
|
9
|
+
const size = Math.min(radius, entering / 2, leaving / 2);
|
|
10
|
+
if (!size) {
|
|
11
|
+
path += ` L ${corner.x} ${corner.y}`;
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
const first = { x: corner.x + (before.x - corner.x) * size / entering,
|
|
15
|
+
y: corner.y + (before.y - corner.y) * size / entering };
|
|
16
|
+
const last = { x: corner.x + (after.x - corner.x) * size / leaving,
|
|
17
|
+
y: corner.y + (after.y - corner.y) * size / leaving };
|
|
18
|
+
path += ` L ${first.x} ${first.y} Q ${corner.x} ${corner.y} ${last.x} ${last.y}`;
|
|
19
|
+
}
|
|
20
|
+
const end = points.at(-1);
|
|
21
|
+
return `${path} L ${end.x} ${end.y}`;
|
|
22
|
+
}
|
|
3
23
|
const ns = 'http://www.w3.org/2000/svg';
|
|
4
24
|
// Keep the same sixteen-unit hourglass as the call graph's relation badge.
|
|
5
25
|
const relationWaitGlyph = 'M1 1h14M1 15h14M2 1v3l12 8v3M14 1v3l-12 8v3M2 4h12M2 12h12';
|
|
6
|
-
/** Keep the badge beside the receiving end of the
|
|
26
|
+
/** Keep the badge beside the receiving end of the path, leaving its stroke visible. */
|
|
7
27
|
function waitBadgePoint(path, offset) {
|
|
8
28
|
const values = path.match(/-?(?:\d*\.?\d+)(?:e[+-]?\d+)?/gi).map(Number);
|
|
9
29
|
const [sx, sy] = values;
|
|
@@ -116,6 +136,8 @@ export function attachScriptArrows({ workspace, vm, getTarget, getRelations, get
|
|
|
116
136
|
let geometryChanged = true;
|
|
117
137
|
let lastMatrix;
|
|
118
138
|
let edgeDrawings = [];
|
|
139
|
+
let viewportDrawings = [];
|
|
140
|
+
let measuredAnchors = new Map();
|
|
119
141
|
const renderedGroups = new WeakMap();
|
|
120
142
|
const syncTransform = (matrix) => {
|
|
121
143
|
// Workspace dragging lends the two native canvases to a separate SVG which must keep exactly two children.
|
|
@@ -186,16 +208,30 @@ export function attachScriptArrows({ workspace, vm, getTarget, getRelations, get
|
|
|
186
208
|
return;
|
|
187
209
|
const top = blockRoot(endpoint.scriptId, roots);
|
|
188
210
|
// A script hidden by the one-script view is not the same as its folded body.
|
|
189
|
-
if (!top ||
|
|
190
|
-
|
|
211
|
+
if (!top || top.closest('[data-tw-plus-hidden]'))
|
|
212
|
+
return;
|
|
213
|
+
// Native viewport culling hides the whole stack, but its real endpoints still exist.
|
|
214
|
+
const culled = !projection && top.style.display === 'none';
|
|
215
|
+
if (!culled && !top.getClientRects().length)
|
|
191
216
|
return;
|
|
192
217
|
const folded = isFoldedDefinition(target.blocks._blocks[endpoint.scriptId]);
|
|
193
218
|
let id = folded ? endpoint.scriptId : endpoint.blockId;
|
|
194
219
|
let root = blockRoot(id, roots);
|
|
195
220
|
// Native Blockly collapsing can also hide a caller inside a control block.
|
|
196
221
|
const visited = new Set();
|
|
197
|
-
|
|
198
|
-
|
|
222
|
+
const hidden = (node) => {
|
|
223
|
+
if (node.closest('[data-tw-plus-hidden]'))
|
|
224
|
+
return true;
|
|
225
|
+
if (!culled)
|
|
226
|
+
return !node.getClientRects().length || view.getComputedStyle(node).display === 'none';
|
|
227
|
+
// A collapsed parent below the culled stack still needs its visible proxy anchor.
|
|
228
|
+
for (let ancestor = node; ancestor && ancestor !== top; ancestor = ancestor.parentElement) {
|
|
229
|
+
if (view.getComputedStyle(ancestor).display === 'none')
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
return false;
|
|
233
|
+
};
|
|
234
|
+
while (root && hidden(root) && !visited.has(id)) {
|
|
199
235
|
visited.add(id);
|
|
200
236
|
id = target.blocks._blocks[id]?.parent ?? '';
|
|
201
237
|
root = blockRoot(id, roots);
|
|
@@ -225,7 +261,7 @@ export function attachScriptArrows({ workspace, vm, getTarget, getRelations, get
|
|
|
225
261
|
return points.get(key);
|
|
226
262
|
} };
|
|
227
263
|
}
|
|
228
|
-
function arrowGeometry({ relation, source, destination, count }, matrix, inverse, area, highlighted) {
|
|
264
|
+
function arrowGeometry({ relation, source, destination, count, serialRoute }, matrix, inverse, area, highlighted) {
|
|
229
265
|
// Anchor an external link to the viewport edge; the list itself remains keyboard accessible.
|
|
230
266
|
const local = source ?? destination;
|
|
231
267
|
const edgeX = source ? area.right - 12 : area.left + 12;
|
|
@@ -249,6 +285,14 @@ export function attachScriptArrows({ workspace, vm, getTarget, getRelations, get
|
|
|
249
285
|
` C ${left} ${top}, ${left} ${end.y}, ${end.x} ${end.y}`;
|
|
250
286
|
badgePoint = { x: middle, y: top - 6 };
|
|
251
287
|
}
|
|
288
|
+
if (serialRoute && source && destination) {
|
|
289
|
+
const { right, top, clearance } = serialRoute;
|
|
290
|
+
// Leave room beside every script before turning vertically, then return through the gap above the hat.
|
|
291
|
+
const approach = end.x - clearance;
|
|
292
|
+
path = roundedRoute([start, { x: right, y: start.y }, { x: right, y: top },
|
|
293
|
+
{ x: approach, y: top }, { x: approach, y: end.y }, end], clearance);
|
|
294
|
+
badgePoint = { x: right, y: (start.y + top) / 2 };
|
|
295
|
+
}
|
|
252
296
|
const head = `M ${end.x - 9} ${end.y - 5} L ${end.x} ${end.y} L ${end.x - 9} ${end.y + 5}`;
|
|
253
297
|
let waitTransform;
|
|
254
298
|
if (relation.kind === 'message' && relation.wait) {
|
|
@@ -299,6 +343,16 @@ export function attachScriptArrows({ workspace, vm, getTarget, getRelations, get
|
|
|
299
343
|
}
|
|
300
344
|
return group;
|
|
301
345
|
}
|
|
346
|
+
function inViewport({ source, destination }, matrix, area) {
|
|
347
|
+
return [source, destination].some(anchor => {
|
|
348
|
+
if (!anchor)
|
|
349
|
+
return false;
|
|
350
|
+
const first = transform(matrix, anchor.box);
|
|
351
|
+
const last = transform(matrix, { x: anchor.box.x + anchor.box.width, y: anchor.box.y + anchor.box.height });
|
|
352
|
+
return Math.max(first.x, last.x) > area.left && Math.min(first.x, last.x) < area.right &&
|
|
353
|
+
Math.max(first.y, last.y) > area.top && Math.min(first.y, last.y) < area.bottom;
|
|
354
|
+
});
|
|
355
|
+
}
|
|
302
356
|
function render() {
|
|
303
357
|
frame = undefined;
|
|
304
358
|
if (disposed)
|
|
@@ -336,7 +390,9 @@ export function attachScriptArrows({ workspace, vm, getTarget, getRelations, get
|
|
|
336
390
|
const area = (projection ?? workspace.getParentSvg()).getBoundingClientRect();
|
|
337
391
|
const sameScale = lastMatrix && matrix.a === lastMatrix.a && matrix.b === lastMatrix.b &&
|
|
338
392
|
matrix.c === lastMatrix.c && matrix.d === lastMatrix.d;
|
|
339
|
-
|
|
393
|
+
const reuseAnchors = !geometryChanged && !projection && sameScale;
|
|
394
|
+
if (reuseAnchors &&
|
|
395
|
+
viewportDrawings.every(item => item.visible === inViewport(item.drawing, matrix, area))) {
|
|
340
396
|
const geometry = edgeDrawings.map(item => ({ ...item,
|
|
341
397
|
shape: arrowGeometry(item.drawing, matrix, inverse, area, item.highlighted) }));
|
|
342
398
|
syncTransform(matrix);
|
|
@@ -359,6 +415,9 @@ export function attachScriptArrows({ workspace, vm, getTarget, getRelations, get
|
|
|
359
415
|
geometryChanged = false;
|
|
360
416
|
lastMatrix = matrix;
|
|
361
417
|
edgeDrawings = [];
|
|
418
|
+
viewportDrawings = [];
|
|
419
|
+
if (!reuseAnchors)
|
|
420
|
+
measuredAnchors = new Map();
|
|
362
421
|
const paint = [];
|
|
363
422
|
const guides = document.createDocumentFragment();
|
|
364
423
|
const previousGroups = renderedGroups.get(drawingLayer);
|
|
@@ -384,12 +443,11 @@ export function attachScriptArrows({ workspace, vm, getTarget, getRelations, get
|
|
|
384
443
|
const inDrag = (endpoint) => Boolean(endpoint && endpoint.targetId === target.id && draggedIds.has(endpoint.blockId));
|
|
385
444
|
const incident = (relation) => inDrag(relation.source) || inDrag(relation.destination);
|
|
386
445
|
const drawings = new Map();
|
|
387
|
-
const anchors = new Map();
|
|
388
446
|
const measuredAnchor = (endpoint) => {
|
|
389
447
|
const key = `${endpoint.targetId}\0${endpoint.scriptId}\0${endpoint.blockId}`;
|
|
390
|
-
if (!
|
|
391
|
-
|
|
392
|
-
return
|
|
448
|
+
if (!measuredAnchors.has(key))
|
|
449
|
+
measuredAnchors.set(key, anchor(endpoint, target, roots, inverse));
|
|
450
|
+
return measuredAnchors.get(key);
|
|
393
451
|
};
|
|
394
452
|
for (const relation of graph) {
|
|
395
453
|
if (!visibility[relation.kind])
|
|
@@ -408,6 +466,36 @@ export function attachScriptArrows({ workspace, vm, getTarget, getRelations, get
|
|
|
408
466
|
else
|
|
409
467
|
drawings.set(key, { relation, source, destination, count: 1 });
|
|
410
468
|
}
|
|
469
|
+
if (projection) {
|
|
470
|
+
const unit = 1 / (Math.hypot(matrix.a, matrix.b) || 1);
|
|
471
|
+
const scripts = new Map();
|
|
472
|
+
for (const root of projection.querySelectorAll('[data-script-view-block]')) {
|
|
473
|
+
const rect = root.getBoundingClientRect();
|
|
474
|
+
const first = transform(inverse, { x: rect.left, y: rect.top });
|
|
475
|
+
const last = transform(inverse, { x: rect.right, y: rect.bottom });
|
|
476
|
+
scripts.set(root.getAttribute('data-script-view-block'), { x: first.x, y: first.y, width: last.x - first.x, height: last.y - first.y });
|
|
477
|
+
}
|
|
478
|
+
const rightEdge = [...scripts.values()].reduce((edge, box) => Math.max(edge, box.x + box.width), 0);
|
|
479
|
+
let lane = 0;
|
|
480
|
+
for (const drawing of drawings.values()) {
|
|
481
|
+
if (!drawing.source || !drawing.destination)
|
|
482
|
+
continue;
|
|
483
|
+
const destination = scripts.get(drawing.relation.destination.scriptId);
|
|
484
|
+
if (!destination)
|
|
485
|
+
continue;
|
|
486
|
+
drawing.serialRoute = { right: rightEdge + (96 + lane * 16) * unit,
|
|
487
|
+
top: destination.y - (14 + lane % 4 * 6) * unit, clearance: 8 * unit };
|
|
488
|
+
lane++;
|
|
489
|
+
}
|
|
490
|
+
// Reserve actual scrollable SVG width for the right-hand routes while preserving the block scale.
|
|
491
|
+
const box = projection.viewBox.baseVal;
|
|
492
|
+
const width = Math.ceil(rightEdge + (112 + Math.max(0, lane - 1) * 16) * unit);
|
|
493
|
+
if (lane && width > box.width) {
|
|
494
|
+
const scale = Math.hypot(matrix.a, matrix.b);
|
|
495
|
+
projection.setAttribute('viewBox', `${box.x} ${box.y} ${width} ${box.height}`);
|
|
496
|
+
projection.setAttribute('width', String(width * scale));
|
|
497
|
+
}
|
|
498
|
+
}
|
|
411
499
|
const related = new Set();
|
|
412
500
|
for (const relation of graph)
|
|
413
501
|
if (visibility[relation.kind] && dragging && incident(relation)) {
|
|
@@ -426,7 +514,7 @@ export function attachScriptArrows({ workspace, vm, getTarget, getRelations, get
|
|
|
426
514
|
if (root && root.getClientRects().length && !root.closest('[data-tw-plus-hidden]'))
|
|
427
515
|
marked.set(root, 'related');
|
|
428
516
|
}
|
|
429
|
-
//
|
|
517
|
+
// Shade the whole stack, including its body; the original block paint and runtime glow stay intact.
|
|
430
518
|
for (const [root, role] of marked) {
|
|
431
519
|
const bounds = root.getBoundingClientRect();
|
|
432
520
|
const first = transform(inverse, { x: bounds.left, y: bounds.top });
|
|
@@ -436,6 +524,10 @@ export function attachScriptArrows({ workspace, vm, getTarget, getRelations, get
|
|
|
436
524
|
rx: '10', class: 'tw-script-relation-script-highlight', 'data-emphasis': role }));
|
|
437
525
|
}
|
|
438
526
|
for (const [key, drawing] of drawings) {
|
|
527
|
+
const visible = inViewport(drawing, matrix, area);
|
|
528
|
+
viewportDrawings.push({ drawing, visible });
|
|
529
|
+
if (!visible)
|
|
530
|
+
continue;
|
|
439
531
|
const { relation, source, destination, count } = drawing;
|
|
440
532
|
const highlighted = dragging ? incident(relation) :
|
|
441
533
|
emphasized && (emphasized === relation.source.scriptId || emphasized === relation.destination?.scriptId);
|
|
@@ -496,6 +588,8 @@ export function attachScriptArrows({ workspace, vm, getTarget, getRelations, get
|
|
|
496
588
|
destinations.replaceChildren();
|
|
497
589
|
destinations.hidden = true;
|
|
498
590
|
edgeDrawings = [];
|
|
591
|
+
viewportDrawings = [];
|
|
592
|
+
measuredAnchors.clear();
|
|
499
593
|
lastMatrix = undefined;
|
|
500
594
|
renderedGroups.delete(layer);
|
|
501
595
|
if (projectedLayer)
|
|
@@ -4,6 +4,9 @@ import { openRearrangement } from "./script-layout-dialog.js";
|
|
|
4
4
|
import { attachScriptViewMenu } from "./script-view-menu.js";
|
|
5
5
|
import { attachScriptArrows } from "./script-arrows.js";
|
|
6
6
|
import { attachCodeZoomRange } from "./code-zoom.js";
|
|
7
|
+
import { setScriptProjection } from "./script-projection.js";
|
|
8
|
+
import { openSerialScriptEditor } from "./serial-script-editor.js";
|
|
9
|
+
import { isFoldedDefinition, setFoldedDefinition } from "./procedure-edits.js";
|
|
7
10
|
const choices = [
|
|
8
11
|
{ view: 'free', label: 'Normal layout view', jaLabel: '通常配置の見え方' },
|
|
9
12
|
{ view: 'z', label: 'Z layout view', jaLabel: 'Z配置の見え方' },
|
|
@@ -19,10 +22,15 @@ export function attachScriptView({ workspace, ScratchBlocks, getLocale, mount, i
|
|
|
19
22
|
surface.className = 'tw-script-view-surface';
|
|
20
23
|
surface.dataset.codeViewControl = '';
|
|
21
24
|
surface.tabIndex = 0;
|
|
22
|
-
surface.toggleAttribute('data-can-show-block', Boolean(canShowBlock));
|
|
25
|
+
surface.toggleAttribute('data-can-show-block', Boolean(canShowBlock || relationContext?.vm));
|
|
23
26
|
const contents = document.createElement('div');
|
|
24
27
|
surface.append(contents);
|
|
28
|
+
const gutter = document.createElement('div');
|
|
29
|
+
gutter.className = 'tw-script-fold-gutter';
|
|
30
|
+
gutter.dataset.codeViewControl = '';
|
|
31
|
+
const foldButtons = new Map();
|
|
25
32
|
let view = initialView;
|
|
33
|
+
let editor;
|
|
26
34
|
let frame;
|
|
27
35
|
const svg = workspace.getParentSvg();
|
|
28
36
|
const observer = new window.MutationObserver(records => {
|
|
@@ -31,6 +39,73 @@ export function attachScriptView({ workspace, ScratchBlocks, getLocale, mount, i
|
|
|
31
39
|
});
|
|
32
40
|
const relations = relationContext && attachScriptArrows({ workspace, getLocale, ...relationContext });
|
|
33
41
|
const japanese = () => /^ja(?:-|$)/i.test(getLocale());
|
|
42
|
+
function setFold(ids, value) {
|
|
43
|
+
const target = relationContext?.getTarget();
|
|
44
|
+
const runtime = relationContext?.vm?.runtime;
|
|
45
|
+
if (!target || !runtime?.emitProjectChanged || (canShowBlock && !canShowBlock()))
|
|
46
|
+
return;
|
|
47
|
+
for (const id of ids) {
|
|
48
|
+
const block = target.blocks._blocks[id];
|
|
49
|
+
if (block)
|
|
50
|
+
setFoldedDefinition(block, value ?? !isFoldedDefinition(block));
|
|
51
|
+
}
|
|
52
|
+
runtime.emitProjectChanged();
|
|
53
|
+
update();
|
|
54
|
+
}
|
|
55
|
+
function positionFoldButtons() {
|
|
56
|
+
gutter.style.left = `${surface.offsetLeft}px`;
|
|
57
|
+
gutter.style.top = `${surface.offsetTop}px`;
|
|
58
|
+
gutter.style.height = `${surface.clientHeight}px`;
|
|
59
|
+
for (const { button, top } of foldButtons.values())
|
|
60
|
+
button.style.top = `${top - surface.scrollTop}px`;
|
|
61
|
+
}
|
|
62
|
+
function paintFoldButtons(placements, scale) {
|
|
63
|
+
const target = relationContext?.getTarget();
|
|
64
|
+
const ids = new Set(placements.map(({ script }) => script.id));
|
|
65
|
+
for (const [id, { button }] of foldButtons)
|
|
66
|
+
if (!ids.has(id)) {
|
|
67
|
+
button.remove();
|
|
68
|
+
foldButtons.delete(id);
|
|
69
|
+
}
|
|
70
|
+
for (const { script, y } of placements) {
|
|
71
|
+
let entry = foldButtons.get(script.id);
|
|
72
|
+
if (!entry) {
|
|
73
|
+
const button = document.createElement('button');
|
|
74
|
+
button.type = 'button';
|
|
75
|
+
button.dataset.scriptFoldToggle = script.id;
|
|
76
|
+
button.addEventListener('click', event => { event.stopPropagation(); setFold([script.id]); });
|
|
77
|
+
button.addEventListener('pointerdown', event => event.stopPropagation());
|
|
78
|
+
entry = { button, top: 0 };
|
|
79
|
+
foldButtons.set(script.id, entry);
|
|
80
|
+
gutter.append(button);
|
|
81
|
+
}
|
|
82
|
+
const block = target?.blocks._blocks[script.id];
|
|
83
|
+
const folded = isFoldedDefinition(block);
|
|
84
|
+
const label = japanese() ? (folded ? '折りたたみを解除' : '折りたたむ') : (folded ? 'Unfold script' : 'Fold script');
|
|
85
|
+
entry.top = y * scale;
|
|
86
|
+
entry.button.textContent = folded ? '▶' : '▼';
|
|
87
|
+
entry.button.disabled = !block || Boolean(canShowBlock && !canShowBlock());
|
|
88
|
+
entry.button.setAttribute('aria-label', label);
|
|
89
|
+
entry.button.setAttribute('aria-expanded', String(!folded));
|
|
90
|
+
entry.button.title = label;
|
|
91
|
+
}
|
|
92
|
+
positionFoldButtons();
|
|
93
|
+
}
|
|
94
|
+
function foldOptions() {
|
|
95
|
+
const target = relationContext?.getTarget();
|
|
96
|
+
const runtime = relationContext?.vm?.runtime;
|
|
97
|
+
if (view === 'free' || !target || !runtime?.emitProjectChanged || (canShowBlock && !canShowBlock()))
|
|
98
|
+
return [];
|
|
99
|
+
const roots = readScripts(workspace).map(script => target.blocks._blocks[script.id]).filter(Boolean);
|
|
100
|
+
const options = [
|
|
101
|
+
{ value: true, text: japanese() ? 'すべて折りたたむ' : 'Fold all' },
|
|
102
|
+
{ value: false, text: japanese() ? '折りたたみをすべて解除' : 'Unfold all' }
|
|
103
|
+
];
|
|
104
|
+
return options.map(({ value, text }) => ({ text,
|
|
105
|
+
enabled: roots.some(block => isFoldedDefinition(block) !== value),
|
|
106
|
+
callback() { setFold(readScripts(workspace).map(script => script.id), value); }
|
|
107
|
+
}));
|
|
108
|
+
}
|
|
34
109
|
function labels() {
|
|
35
110
|
const choice = choices.find(choice => choice.view === view);
|
|
36
111
|
surface.setAttribute('aria-label', japanese() ? choice.jaLabel : choice.label);
|
|
@@ -40,6 +115,7 @@ export function attachScriptView({ workspace, ScratchBlocks, getLocale, mount, i
|
|
|
40
115
|
const ja = japanese();
|
|
41
116
|
return {
|
|
42
117
|
label: ja ? 'コードの並びの見え方を変更する' : 'Change code layout view',
|
|
118
|
+
options: foldOptions(),
|
|
43
119
|
choices: choices.map(choice => ({
|
|
44
120
|
text: ja ? choice.jaLabel : choice.label,
|
|
45
121
|
enabled: true,
|
|
@@ -62,8 +138,20 @@ export function attachScriptView({ workspace, ScratchBlocks, getLocale, mount, i
|
|
|
62
138
|
surface.style.setProperty('--tw-script-view-left', !horizontal && bounds.left <= left + 2 ? `${bounds.width}px` : '0px');
|
|
63
139
|
surface.style.setProperty('--tw-script-view-right', !horizontal && bounds.right >= right - 2 ? `${bounds.width}px` : '0px');
|
|
64
140
|
}
|
|
65
|
-
|
|
66
|
-
|
|
141
|
+
const scale = workspace.scale || 1;
|
|
142
|
+
const scripts = readScripts(workspace).map(script => {
|
|
143
|
+
// Include raised hats and labels, and exclude folded bodies, when measuring the visible gap.
|
|
144
|
+
const box = script.root.getBBox();
|
|
145
|
+
return { ...script, x: script.origin.x + box.x, y: script.origin.y + box.y,
|
|
146
|
+
width: box.width, height: box.height };
|
|
147
|
+
});
|
|
148
|
+
const placements = arrangeScripts(scripts, view, 'vertical', Math.max(64, 48 / scale))
|
|
149
|
+
.map(placement => ({ ...placement, x: Math.max(placement.x, 32 / scale) }));
|
|
150
|
+
paintScripts(contents, placements, scale);
|
|
151
|
+
paintFoldButtons(placements, scale);
|
|
152
|
+
const projection = contents.querySelector('svg');
|
|
153
|
+
relations?.setProjection(projection);
|
|
154
|
+
setScriptProjection(workspace, { surface, svg: projection });
|
|
67
155
|
}
|
|
68
156
|
function update() {
|
|
69
157
|
if (view === 'free' || frame !== undefined)
|
|
@@ -72,14 +160,20 @@ export function attachScriptView({ workspace, ScratchBlocks, getLocale, mount, i
|
|
|
72
160
|
}
|
|
73
161
|
function setView(next) {
|
|
74
162
|
menu.close();
|
|
163
|
+
editor?.dispose();
|
|
164
|
+
editor = undefined;
|
|
75
165
|
observer.disconnect();
|
|
76
166
|
view = next;
|
|
77
167
|
surface.hidden = view === 'free';
|
|
168
|
+
gutter.hidden = view === 'free';
|
|
78
169
|
svg.toggleAttribute('data-script-serial', view !== 'free');
|
|
79
170
|
labels();
|
|
80
171
|
if (view === 'free') {
|
|
172
|
+
setScriptProjection(workspace);
|
|
81
173
|
relations?.setProjection(null);
|
|
82
174
|
contents.replaceChildren();
|
|
175
|
+
gutter.replaceChildren();
|
|
176
|
+
foldButtons.clear();
|
|
83
177
|
}
|
|
84
178
|
else {
|
|
85
179
|
observer.observe(workspace.getCanvas(), { attributes: true, childList: true, subtree: true });
|
|
@@ -94,21 +188,39 @@ export function attachScriptView({ workspace, ScratchBlocks, getLocale, mount, i
|
|
|
94
188
|
surface.addEventListener('click', event => {
|
|
95
189
|
event.stopPropagation();
|
|
96
190
|
const id = event.target.closest('[data-script-view-block]')?.getAttribute('data-script-view-block');
|
|
97
|
-
if (!id || !canShowBlock
|
|
191
|
+
if (!id || (canShowBlock && !canShowBlock()))
|
|
192
|
+
return;
|
|
193
|
+
if (relationContext?.vm) {
|
|
194
|
+
editor?.dispose();
|
|
195
|
+
editor = openSerialScriptEditor(relationContext.vm, relationContext.getTarget(), id, document, getLocale());
|
|
196
|
+
if (editor)
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (!canShowBlock?.())
|
|
98
200
|
return;
|
|
99
201
|
chooseView('free');
|
|
100
202
|
workspace.centerOnBlock(id);
|
|
101
203
|
});
|
|
102
204
|
const resize = new window.ResizeObserver(update);
|
|
103
205
|
resize.observe(host);
|
|
104
|
-
const language = new window.MutationObserver(labels);
|
|
206
|
+
const language = new window.MutationObserver(() => { labels(); update(); });
|
|
105
207
|
language.observe(document.documentElement, { attributes: true, attributeFilter: ['lang'] });
|
|
106
208
|
// The Blockly keyboard listener is document-wide.
|
|
107
209
|
surface.addEventListener('keydown', event => {
|
|
108
210
|
if (event.key !== 'Tab')
|
|
109
211
|
event.stopPropagation();
|
|
110
212
|
});
|
|
111
|
-
|
|
213
|
+
gutter.addEventListener('keydown', event => {
|
|
214
|
+
if (event.key !== 'Tab')
|
|
215
|
+
event.stopPropagation();
|
|
216
|
+
});
|
|
217
|
+
host.append(surface, gutter);
|
|
218
|
+
surface.addEventListener('scroll', () => {
|
|
219
|
+
positionFoldButtons();
|
|
220
|
+
const projection = contents.querySelector('svg');
|
|
221
|
+
if (view !== 'free' && projection)
|
|
222
|
+
setScriptProjection(workspace, { surface, svg: projection });
|
|
223
|
+
}, { passive: true });
|
|
112
224
|
setView(initialView);
|
|
113
225
|
return {
|
|
114
226
|
update,
|
|
@@ -117,6 +229,8 @@ export function attachScriptView({ workspace, ScratchBlocks, getLocale, mount, i
|
|
|
117
229
|
get view() { return view; },
|
|
118
230
|
setView,
|
|
119
231
|
dispose() {
|
|
232
|
+
editor?.dispose();
|
|
233
|
+
setScriptProjection(workspace);
|
|
120
234
|
restoreZoomRange();
|
|
121
235
|
resize.disconnect();
|
|
122
236
|
observer.disconnect();
|
|
@@ -126,6 +240,8 @@ export function attachScriptView({ workspace, ScratchBlocks, getLocale, mount, i
|
|
|
126
240
|
menu.dispose();
|
|
127
241
|
relations?.dispose();
|
|
128
242
|
surface.remove();
|
|
243
|
+
gutter.remove();
|
|
244
|
+
foldButtons.clear();
|
|
129
245
|
svg.removeAttribute('data-script-serial');
|
|
130
246
|
}
|
|
131
247
|
};
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
export function readScripts(workspace) {
|
|
2
2
|
return workspace.getTopBlocks(false).flatMap(block => {
|
|
3
3
|
const root = block.getSvgRoot();
|
|
4
|
-
if (!root || !workspace.getCanvas().contains(root))
|
|
4
|
+
if (!root || !workspace.getCanvas().contains(root) || root.hasAttribute('data-tw-plus-hidden'))
|
|
5
5
|
return [];
|
|
6
6
|
const origin = block.getRelativeToSurfaceXY();
|
|
7
|
-
|
|
7
|
+
let { topLeft, bottomRight } = block.getBoundingRectangle();
|
|
8
|
+
if (root.querySelector(':scope > [data-tw-plus-folded-mark]')) {
|
|
9
|
+
const box = root.getBBox();
|
|
10
|
+
topLeft = { x: origin.x + box.x, y: origin.y + box.y };
|
|
11
|
+
bottomRight = { x: topLeft.x + box.width, y: topLeft.y + box.height };
|
|
12
|
+
}
|
|
8
13
|
return [{
|
|
9
14
|
id: block.id, block, root, origin, ...topLeft,
|
|
10
15
|
width: bottomRight.x - topLeft.x, height: bottomRight.y - topLeft.y
|
package/dist/script-layout.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export interface ScriptBounds extends Point {
|
|
|
9
9
|
}
|
|
10
10
|
export type ScriptOrder = 'z' | 'n';
|
|
11
11
|
export type LayoutDirection = 'vertical' | 'horizontal';
|
|
12
|
-
export declare function arrangeScripts<T extends ScriptBounds>(scripts: readonly T[], order: ScriptOrder, direction: LayoutDirection): {
|
|
12
|
+
export declare function arrangeScripts<T extends ScriptBounds>(scripts: readonly T[], order: ScriptOrder, direction: LayoutDirection, gap?: number): {
|
|
13
13
|
script: T;
|
|
14
14
|
x: number;
|
|
15
15
|
y: number;
|
package/dist/script-layout.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const alignmentTolerance = 24;
|
|
2
2
|
const spacing = 32;
|
|
3
|
-
export function arrangeScripts(scripts, order, direction) {
|
|
3
|
+
export function arrangeScripts(scripts, order, direction, gap = spacing) {
|
|
4
4
|
const bandAxis = order === 'z' ? 'y' : 'x';
|
|
5
5
|
const scanAxis = order === 'z' ? 'x' : 'y';
|
|
6
6
|
const compare = (a, b, first, second) => a[first] - b[first] || a[second] - b[second] || (a.id < b.id ? -1 : Number(a.id > b.id));
|
|
@@ -14,12 +14,12 @@ export function arrangeScripts(scripts, order, direction) {
|
|
|
14
14
|
else
|
|
15
15
|
bands.push([script]);
|
|
16
16
|
}
|
|
17
|
-
let offset =
|
|
17
|
+
let offset = gap;
|
|
18
18
|
return bands.flatMap(band => band.sort((a, b) => compare(a, b, scanAxis, bandAxis))).map(script => {
|
|
19
19
|
const vertical = direction === 'vertical';
|
|
20
20
|
const x = vertical ? spacing : offset;
|
|
21
21
|
const y = vertical ? offset : spacing;
|
|
22
|
-
offset += Math.ceil(vertical ? script.height : script.width) +
|
|
22
|
+
offset += Math.ceil(vertical ? script.height : script.width) + gap;
|
|
23
23
|
return { script, x, y };
|
|
24
24
|
});
|
|
25
25
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type ScriptProjection = {
|
|
2
|
+
surface: HTMLElement;
|
|
3
|
+
svg: SVGSVGElement;
|
|
4
|
+
};
|
|
5
|
+
export declare const getScriptProjection: (workspace: object) => ScriptProjection;
|
|
6
|
+
export declare function setScriptProjection(workspace: {
|
|
7
|
+
getInjectionDiv(): HTMLElement;
|
|
8
|
+
}, projection?: ScriptProjection): void;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const projections = new WeakMap();
|
|
2
|
+
export const getScriptProjection = (workspace) => projections.get(workspace);
|
|
3
|
+
export function setScriptProjection(workspace, projection) {
|
|
4
|
+
if (projection)
|
|
5
|
+
projections.set(workspace, projection);
|
|
6
|
+
else
|
|
7
|
+
projections.delete(workspace);
|
|
8
|
+
const container = workspace.getInjectionDiv();
|
|
9
|
+
container.dispatchEvent(new container.ownerDocument.defaultView.Event('scriptprojectionchange'));
|
|
10
|
+
}
|
package/dist/script-view-menu.js
CHANGED
|
@@ -108,7 +108,7 @@ export function attachScriptViewMenu({ ContextMenu, WidgetDiv }, workspace, host
|
|
|
108
108
|
// Only extend the options collected for this workspace, after the existing add-ons.
|
|
109
109
|
ContextMenu.populate_ = (options, rtl) => {
|
|
110
110
|
const config = getMenu();
|
|
111
|
-
const menu = populate.call(ContextMenu, [...options, triggerOption(config.label)], rtl);
|
|
111
|
+
const menu = populate.call(ContextMenu, [...options, triggerOption(config.label), ...(config.options ?? [])], rtl);
|
|
112
112
|
addSubmenu(menu, options.length, config, rtl, ContextMenu);
|
|
113
113
|
return menu;
|
|
114
114
|
};
|
|
@@ -126,7 +126,9 @@ export function attachScriptViewMenu({ ContextMenu, WidgetDiv }, workspace, host
|
|
|
126
126
|
event.stopPropagation();
|
|
127
127
|
const config = getMenu();
|
|
128
128
|
const rtl = workspace.RTL;
|
|
129
|
-
const menu = ContextMenu.populate_([triggerOption(config.label)
|
|
129
|
+
const menu = ContextMenu.populate_([triggerOption(config.label), ...(config.options ?? []).map(option => ({ ...option,
|
|
130
|
+
callback() { WidgetDiv.hideIfOwner(surface); option.callback(); }
|
|
131
|
+
}))], rtl);
|
|
130
132
|
const outside = new document.defaultView.AbortController();
|
|
131
133
|
WidgetDiv.show(surface, rtl, () => {
|
|
132
134
|
outside.abort();
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { getCodeSession } from "./workspace.js";
|
|
2
|
+
/** Reuse the script editor: its root stays at its original project coordinates. */
|
|
3
|
+
export function openSerialScriptEditor(vm, target, id, document, locale) {
|
|
4
|
+
const bridge = getCodeSession(vm).bridge;
|
|
5
|
+
if (!bridge || !target?.blocks._blocks[id])
|
|
6
|
+
return;
|
|
7
|
+
const dialog = document.createElement('dialog');
|
|
8
|
+
dialog.className = 'tw-serial-script-editor';
|
|
9
|
+
const title = document.createElement('h2');
|
|
10
|
+
title.textContent = /^ja(?:-|$)/i.test(locale) ? 'スクリプトを編集' : 'Edit script';
|
|
11
|
+
title.id = `script-editor-${document.defaultView.crypto.randomUUID()}`;
|
|
12
|
+
dialog.setAttribute('aria-labelledby', title.id);
|
|
13
|
+
const area = document.createElement('div');
|
|
14
|
+
area.dataset.serialScriptEditor = id;
|
|
15
|
+
const close = document.createElement('button');
|
|
16
|
+
close.type = 'button';
|
|
17
|
+
close.textContent = /^ja(?:-|$)/i.test(locale) ? '閉じる' : 'Close';
|
|
18
|
+
dialog.append(title, area, close);
|
|
19
|
+
document.body.append(dialog);
|
|
20
|
+
dialog.showModal();
|
|
21
|
+
let editor;
|
|
22
|
+
try {
|
|
23
|
+
editor = bridge.editProcedure(area, { target, definitionId: id, editable: true, locale });
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
dialog.close();
|
|
27
|
+
dialog.remove();
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
let closed = false;
|
|
31
|
+
const dispose = () => {
|
|
32
|
+
if (closed)
|
|
33
|
+
return;
|
|
34
|
+
closed = true;
|
|
35
|
+
editor.flush();
|
|
36
|
+
editor.dispose();
|
|
37
|
+
dialog.close();
|
|
38
|
+
dialog.remove();
|
|
39
|
+
};
|
|
40
|
+
close.addEventListener('click', dispose);
|
|
41
|
+
dialog.addEventListener('cancel', event => { event.preventDefault(); dispose(); });
|
|
42
|
+
return { dispose };
|
|
43
|
+
}
|