@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,21 @@
|
|
|
1
|
+
/* SPDX-License-Identifier: MPL-2.0 */
|
|
2
|
+
.preview {
|
|
3
|
+
position: absolute;
|
|
4
|
+
/* Above the toolbox (40) and below the dragged block (50). */
|
|
5
|
+
z-index: 45;
|
|
6
|
+
display: grid;
|
|
7
|
+
place-items: center;
|
|
8
|
+
pointer-events: none;
|
|
9
|
+
-webkit-backdrop-filter: blur(4px);
|
|
10
|
+
backdrop-filter: blur(4px);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
.preview::before {
|
|
14
|
+
content: "";
|
|
15
|
+
width: 3.5rem;
|
|
16
|
+
height: 3.5rem;
|
|
17
|
+
border: 2px solid white;
|
|
18
|
+
border-radius: 50%;
|
|
19
|
+
background: #a52834 url("./trash.svg") center / 1.75rem no-repeat;
|
|
20
|
+
box-shadow: 0 2px 6px rgb(0 0 0 / 25%);
|
|
21
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
export function createPaletteDeletePreview(styles: Record<string, string>) {
|
|
3
|
+
|
|
4
|
+
const installed = new WeakSet();
|
|
5
|
+
|
|
6
|
+
const installPaletteDeletePreview = ScratchBlocks => {
|
|
7
|
+
const gesture = ScratchBlocks.Gesture?.prototype;
|
|
8
|
+
if (!gesture?.handleMove || !gesture.dispose || installed.has(gesture)) return;
|
|
9
|
+
installed.add(gesture);
|
|
10
|
+
|
|
11
|
+
const previews = new WeakMap();
|
|
12
|
+
const clear = current => {
|
|
13
|
+
previews.get(current)?.remove();
|
|
14
|
+
previews.delete(current);
|
|
15
|
+
};
|
|
16
|
+
const {handleMove, dispose} = gesture;
|
|
17
|
+
gesture.handleMove = function (event) {
|
|
18
|
+
const result = handleMove.call(this, event);
|
|
19
|
+
const dragger = this.blockDragger_;
|
|
20
|
+
// Reuse the dragger's decision, including deletability and competing connections.
|
|
21
|
+
if (!dragger?.wouldDeleteBlock_ || dragger.deleteArea_ !== ScratchBlocks.DELETE_AREA_TOOLBOX) {
|
|
22
|
+
clear(this);
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const workspace = this.startWorkspace_;
|
|
27
|
+
// Addons can report DELETE_AREA_TOOLBOX for drop targets outside the palette.
|
|
28
|
+
const hitbox = workspace.deleteAreaToolbox_;
|
|
29
|
+
const {clientX, clientY} = event;
|
|
30
|
+
if (!hitbox || clientX < hitbox.left || clientX > hitbox.left + hitbox.width ||
|
|
31
|
+
clientY < hitbox.top || clientY > hitbox.top + hitbox.height) {
|
|
32
|
+
clear(this);
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
const toolbox = workspace.getToolbox()?.HtmlDiv;
|
|
36
|
+
const flyout = workspace.getFlyout()?.svgBackground_;
|
|
37
|
+
const container = workspace.getInjectionDiv();
|
|
38
|
+
if (!toolbox || !container || workspace.isMutator) {
|
|
39
|
+
clear(this);
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// The hitbox extends offscreen. Use the visible toolbox and flyout for the overlay.
|
|
44
|
+
const bounds = container.getBoundingClientRect();
|
|
45
|
+
const menu = toolbox.getBoundingClientRect();
|
|
46
|
+
const blocks = toolbox.hasAttribute('data-horizontal-block-palette') ? menu : flyout?.getBoundingClientRect();
|
|
47
|
+
if (!blocks?.width || !blocks.height) {
|
|
48
|
+
clear(this);
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
const left = Math.max(bounds.left, Math.min(menu.left, blocks.left));
|
|
52
|
+
const top = Math.max(bounds.top, Math.min(menu.top, blocks.top));
|
|
53
|
+
const right = Math.min(bounds.right, Math.max(menu.right, blocks.right));
|
|
54
|
+
const bottom = Math.min(bounds.bottom, Math.max(menu.bottom, blocks.bottom));
|
|
55
|
+
if (right <= left || bottom <= top) {
|
|
56
|
+
clear(this);
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let preview = previews.get(this);
|
|
61
|
+
if (!preview) {
|
|
62
|
+
preview = container.ownerDocument.createElement('div');
|
|
63
|
+
preview.className = styles.preview;
|
|
64
|
+
preview.dataset.paletteDeletePreview = '';
|
|
65
|
+
preview.setAttribute('aria-hidden', 'true');
|
|
66
|
+
container.appendChild(preview);
|
|
67
|
+
previews.set(this, preview);
|
|
68
|
+
}
|
|
69
|
+
preview.style.inset = `${top - bounds.top}px ${bounds.right - right}px ${bounds.bottom - bottom}px ${left - bounds.left}px`;
|
|
70
|
+
return result;
|
|
71
|
+
};
|
|
72
|
+
gesture.dispose = function (...args) {
|
|
73
|
+
clear(this);
|
|
74
|
+
return dispose.apply(this, args);
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
return {installPaletteDeletePreview};
|
|
79
|
+
}
|
package/src/pin-off.svg
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="black" stroke-width="1.2" stroke-linejoin="round">
|
|
2
|
+
<!-- Matches the unpinned sprite-list control in turbowarp-plus-editor-workbench. -->
|
|
3
|
+
<path d="M9 2 14 7 12 8l-2 3-5-5 3-2 1-2Z"/>
|
|
4
|
+
<path d="m7.5 8.5-4 4"/>
|
|
5
|
+
<path d="M2 2l12 12" stroke-linecap="round"/>
|
|
6
|
+
</svg>
|
package/src/pin.svg
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="black" stroke-width="1.2" stroke-linejoin="round">
|
|
2
|
+
<!-- Matches the sprite-list pin in turbowarp-plus-editor-workbench. -->
|
|
3
|
+
<path d="M9 2 14 7 12 8l-2 3-5-5 3-2 1-2Z"/>
|
|
4
|
+
<path d="m7.5 8.5-4 4"/>
|
|
5
|
+
</svg>
|
package/src/script-arrows.ts
CHANGED
|
@@ -23,12 +23,31 @@ export interface ScriptRelationContext {
|
|
|
23
23
|
type Point = {x: number; y: number};
|
|
24
24
|
type Box = Point & {width: number; height: number};
|
|
25
25
|
type Anchor = {id: string; box: Box; point(side: 'left' | 'right', fraction: number): Point};
|
|
26
|
-
type Drawing = {relation: ScriptRelation; source?: Anchor; destination?: Anchor; count: number
|
|
26
|
+
type Drawing = {relation: ScriptRelation; source?: Anchor; destination?: Anchor; count: number;
|
|
27
|
+
serialRoute?: {right: number; top: number; clearance: number}};
|
|
28
|
+
|
|
29
|
+
function roundedRoute(points: Point[], radius: number) {
|
|
30
|
+
let path = `M ${points[0].x} ${points[0].y}`;
|
|
31
|
+
for (let index = 1; index < points.length - 1; index++) {
|
|
32
|
+
const before = points[index - 1], corner = points[index], after = points[index + 1];
|
|
33
|
+
const entering = Math.hypot(corner.x - before.x, corner.y - before.y);
|
|
34
|
+
const leaving = Math.hypot(after.x - corner.x, after.y - corner.y);
|
|
35
|
+
const size = Math.min(radius, entering / 2, leaving / 2);
|
|
36
|
+
if (!size) {path += ` L ${corner.x} ${corner.y}`; continue;}
|
|
37
|
+
const first = {x: corner.x + (before.x - corner.x) * size / entering,
|
|
38
|
+
y: corner.y + (before.y - corner.y) * size / entering};
|
|
39
|
+
const last = {x: corner.x + (after.x - corner.x) * size / leaving,
|
|
40
|
+
y: corner.y + (after.y - corner.y) * size / leaving};
|
|
41
|
+
path += ` L ${first.x} ${first.y} Q ${corner.x} ${corner.y} ${last.x} ${last.y}`;
|
|
42
|
+
}
|
|
43
|
+
const end = points.at(-1);
|
|
44
|
+
return `${path} L ${end.x} ${end.y}`;
|
|
45
|
+
}
|
|
27
46
|
const ns = 'http://www.w3.org/2000/svg';
|
|
28
47
|
// Keep the same sixteen-unit hourglass as the call graph's relation badge.
|
|
29
48
|
const relationWaitGlyph = 'M1 1h14M1 15h14M2 1v3l12 8v3M14 1v3l-12 8v3M2 4h12M2 12h12';
|
|
30
49
|
|
|
31
|
-
/** Keep the badge beside the receiving end of the
|
|
50
|
+
/** Keep the badge beside the receiving end of the path, leaving its stroke visible. */
|
|
32
51
|
function waitBadgePoint(path: string, offset: number): Point {
|
|
33
52
|
const values = path.match(/-?(?:\d*\.?\d+)(?:e[+-]?\d+)?/gi).map(Number);
|
|
34
53
|
const [sx, sy] = values;
|
|
@@ -150,6 +169,8 @@ export function attachScriptArrows({workspace, vm, getTarget, getRelations, getL
|
|
|
150
169
|
let geometryChanged = true;
|
|
151
170
|
let lastMatrix: DOMMatrix | undefined;
|
|
152
171
|
let edgeDrawings: {key: string; drawing: Drawing; group: SVGGElement; highlighted: boolean}[] = [];
|
|
172
|
+
let viewportDrawings: {drawing: Drawing; visible: boolean}[] = [];
|
|
173
|
+
let measuredAnchors = new Map<string, Anchor | undefined>();
|
|
153
174
|
const renderedGroups = new WeakMap<SVGGElement, Map<string, SVGGElement>>();
|
|
154
175
|
const syncTransform = (matrix: DOMMatrix) => {
|
|
155
176
|
// Workspace dragging lends the two native canvases to a separate SVG which must keep exactly two children.
|
|
@@ -214,15 +235,25 @@ export function attachScriptArrows({workspace, vm, getTarget, getRelations, getL
|
|
|
214
235
|
if (endpoint.targetId !== target.id) return;
|
|
215
236
|
const top = blockRoot(endpoint.scriptId, roots);
|
|
216
237
|
// A script hidden by the one-script view is not the same as its folded body.
|
|
217
|
-
if (!top ||
|
|
218
|
-
|
|
238
|
+
if (!top || top.closest('[data-tw-plus-hidden]')) return;
|
|
239
|
+
// Native viewport culling hides the whole stack, but its real endpoints still exist.
|
|
240
|
+
const culled = !projection && top.style.display === 'none';
|
|
241
|
+
if (!culled && !top.getClientRects().length) return;
|
|
219
242
|
const folded = isFoldedDefinition(target.blocks._blocks[endpoint.scriptId]);
|
|
220
243
|
let id = folded ? endpoint.scriptId : endpoint.blockId;
|
|
221
244
|
let root = blockRoot(id, roots);
|
|
222
245
|
// Native Blockly collapsing can also hide a caller inside a control block.
|
|
223
246
|
const visited = new Set<string>();
|
|
224
|
-
|
|
225
|
-
|
|
247
|
+
const hidden = (node: SVGElement) => {
|
|
248
|
+
if (node.closest('[data-tw-plus-hidden]')) return true;
|
|
249
|
+
if (!culled) return !node.getClientRects().length || view.getComputedStyle(node).display === 'none';
|
|
250
|
+
// A collapsed parent below the culled stack still needs its visible proxy anchor.
|
|
251
|
+
for (let ancestor = node; ancestor && ancestor !== top; ancestor = ancestor.parentElement as unknown as SVGElement) {
|
|
252
|
+
if (view.getComputedStyle(ancestor).display === 'none') return true;
|
|
253
|
+
}
|
|
254
|
+
return false;
|
|
255
|
+
};
|
|
256
|
+
while (root && hidden(root) && !visited.has(id)) {
|
|
226
257
|
visited.add(id);
|
|
227
258
|
id = target.blocks._blocks[id]?.parent ?? '';
|
|
228
259
|
root = blockRoot(id, roots);
|
|
@@ -249,7 +280,7 @@ export function attachScriptArrows({workspace, vm, getTarget, getRelations, getL
|
|
|
249
280
|
}};
|
|
250
281
|
}
|
|
251
282
|
|
|
252
|
-
function arrowGeometry({relation, source, destination, count}: Drawing, matrix: DOMMatrix, inverse: DOMMatrix,
|
|
283
|
+
function arrowGeometry({relation, source, destination, count, serialRoute}: Drawing, matrix: DOMMatrix, inverse: DOMMatrix,
|
|
253
284
|
area: DOMRect, highlighted: boolean) {
|
|
254
285
|
// Anchor an external link to the viewport edge; the list itself remains keyboard accessible.
|
|
255
286
|
const local = source ?? destination;
|
|
@@ -274,6 +305,14 @@ export function attachScriptArrows({workspace, vm, getTarget, getRelations, getL
|
|
|
274
305
|
` C ${left} ${top}, ${left} ${end.y}, ${end.x} ${end.y}`;
|
|
275
306
|
badgePoint = {x: middle, y: top - 6};
|
|
276
307
|
}
|
|
308
|
+
if (serialRoute && source && destination) {
|
|
309
|
+
const {right, top, clearance} = serialRoute;
|
|
310
|
+
// Leave room beside every script before turning vertically, then return through the gap above the hat.
|
|
311
|
+
const approach = end.x - clearance;
|
|
312
|
+
path = roundedRoute([start, {x: right, y: start.y}, {x: right, y: top},
|
|
313
|
+
{x: approach, y: top}, {x: approach, y: end.y}, end], clearance);
|
|
314
|
+
badgePoint = {x: right, y: (start.y + top) / 2};
|
|
315
|
+
}
|
|
277
316
|
const head = `M ${end.x - 9} ${end.y - 5} L ${end.x} ${end.y} L ${end.x - 9} ${end.y + 5}`;
|
|
278
317
|
let waitTransform: string | undefined;
|
|
279
318
|
if (relation.kind === 'message' && relation.wait) {
|
|
@@ -327,6 +366,16 @@ export function attachScriptArrows({workspace, vm, getTarget, getRelations, getL
|
|
|
327
366
|
return group;
|
|
328
367
|
}
|
|
329
368
|
|
|
369
|
+
function inViewport({source, destination}: Drawing, matrix: DOMMatrix, area: DOMRect) {
|
|
370
|
+
return [source, destination].some(anchor => {
|
|
371
|
+
if (!anchor) return false;
|
|
372
|
+
const first = transform(matrix, anchor.box);
|
|
373
|
+
const last = transform(matrix, {x: anchor.box.x + anchor.box.width, y: anchor.box.y + anchor.box.height});
|
|
374
|
+
return Math.max(first.x, last.x) > area.left && Math.min(first.x, last.x) < area.right &&
|
|
375
|
+
Math.max(first.y, last.y) > area.top && Math.min(first.y, last.y) < area.bottom;
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
|
|
330
379
|
function render() {
|
|
331
380
|
frame = undefined;
|
|
332
381
|
if (disposed) return;
|
|
@@ -353,7 +402,9 @@ export function attachScriptArrows({workspace, vm, getTarget, getRelations, getL
|
|
|
353
402
|
const area = (projection ?? workspace.getParentSvg()).getBoundingClientRect();
|
|
354
403
|
const sameScale = lastMatrix && matrix.a === lastMatrix.a && matrix.b === lastMatrix.b &&
|
|
355
404
|
matrix.c === lastMatrix.c && matrix.d === lastMatrix.d;
|
|
356
|
-
|
|
405
|
+
const reuseAnchors = !geometryChanged && !projection && sameScale;
|
|
406
|
+
if (reuseAnchors &&
|
|
407
|
+
viewportDrawings.every(item => item.visible === inViewport(item.drawing, matrix, area))) {
|
|
357
408
|
const geometry = edgeDrawings.map(item => ({...item,
|
|
358
409
|
shape: arrowGeometry(item.drawing, matrix, inverse, area, item.highlighted)}));
|
|
359
410
|
syncTransform(matrix);
|
|
@@ -373,6 +424,8 @@ export function attachScriptArrows({workspace, vm, getTarget, getRelations, getL
|
|
|
373
424
|
geometryChanged = false;
|
|
374
425
|
lastMatrix = matrix;
|
|
375
426
|
edgeDrawings = [];
|
|
427
|
+
viewportDrawings = [];
|
|
428
|
+
if (!reuseAnchors) measuredAnchors = new Map();
|
|
376
429
|
const paint: SVGElement[] = [];
|
|
377
430
|
const guides = document.createDocumentFragment();
|
|
378
431
|
const previousGroups = renderedGroups.get(drawingLayer);
|
|
@@ -395,11 +448,10 @@ export function attachScriptArrows({workspace, vm, getTarget, getRelations, getL
|
|
|
395
448
|
const inDrag = (endpoint?: ScriptEndpoint) => Boolean(endpoint && endpoint.targetId === target.id && draggedIds.has(endpoint.blockId));
|
|
396
449
|
const incident = (relation: ScriptRelation) => inDrag(relation.source) || inDrag(relation.destination);
|
|
397
450
|
const drawings = new Map<string, Drawing>();
|
|
398
|
-
const anchors = new Map<string, Anchor | undefined>();
|
|
399
451
|
const measuredAnchor = (endpoint: ScriptEndpoint) => {
|
|
400
452
|
const key = `${endpoint.targetId}\0${endpoint.scriptId}\0${endpoint.blockId}`;
|
|
401
|
-
if (!
|
|
402
|
-
return
|
|
453
|
+
if (!measuredAnchors.has(key)) measuredAnchors.set(key, anchor(endpoint, target, roots, inverse));
|
|
454
|
+
return measuredAnchors.get(key);
|
|
403
455
|
};
|
|
404
456
|
for (const relation of graph) {
|
|
405
457
|
if (!visibility[relation.kind]) continue;
|
|
@@ -413,6 +465,35 @@ export function attachScriptArrows({workspace, vm, getTarget, getRelations, getL
|
|
|
413
465
|
if (existing) existing.count++;
|
|
414
466
|
else drawings.set(key, {relation, source, destination, count: 1});
|
|
415
467
|
}
|
|
468
|
+
if (projection) {
|
|
469
|
+
const unit = 1 / (Math.hypot(matrix.a, matrix.b) || 1);
|
|
470
|
+
const scripts = new Map<string, Box>();
|
|
471
|
+
for (const root of projection.querySelectorAll<SVGGraphicsElement>('[data-script-view-block]')) {
|
|
472
|
+
const rect = root.getBoundingClientRect();
|
|
473
|
+
const first = transform(inverse, {x: rect.left, y: rect.top});
|
|
474
|
+
const last = transform(inverse, {x: rect.right, y: rect.bottom});
|
|
475
|
+
scripts.set(root.getAttribute('data-script-view-block'),
|
|
476
|
+
{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) continue;
|
|
482
|
+
const destination = scripts.get(drawing.relation.destination.scriptId);
|
|
483
|
+
if (!destination) continue;
|
|
484
|
+
drawing.serialRoute = {right: rightEdge + (96 + lane * 16) * unit,
|
|
485
|
+
top: destination.y - (14 + lane % 4 * 6) * unit, clearance: 8 * unit};
|
|
486
|
+
lane++;
|
|
487
|
+
}
|
|
488
|
+
// Reserve actual scrollable SVG width for the right-hand routes while preserving the block scale.
|
|
489
|
+
const box = projection.viewBox.baseVal;
|
|
490
|
+
const width = Math.ceil(rightEdge + (112 + Math.max(0, lane - 1) * 16) * unit);
|
|
491
|
+
if (lane && width > box.width) {
|
|
492
|
+
const scale = Math.hypot(matrix.a, matrix.b);
|
|
493
|
+
projection.setAttribute('viewBox', `${box.x} ${box.y} ${width} ${box.height}`);
|
|
494
|
+
projection.setAttribute('width', String(width * scale));
|
|
495
|
+
}
|
|
496
|
+
}
|
|
416
497
|
const related = new Set<string>();
|
|
417
498
|
for (const relation of graph) if (visibility[relation.kind] && dragging && incident(relation)) {
|
|
418
499
|
if (inDrag(relation.source) && relation.destination?.targetId === target.id && !inDrag(relation.destination)) {
|
|
@@ -428,7 +509,7 @@ export function attachScriptArrows({workspace, vm, getTarget, getRelations, getL
|
|
|
428
509
|
const root = blockRoot(id, roots);
|
|
429
510
|
if (root && root.getClientRects().length && !root.closest('[data-tw-plus-hidden]')) marked.set(root, 'related');
|
|
430
511
|
}
|
|
431
|
-
//
|
|
512
|
+
// Shade the whole stack, including its body; the original block paint and runtime glow stay intact.
|
|
432
513
|
for (const [root, role] of marked) {
|
|
433
514
|
const bounds = root.getBoundingClientRect();
|
|
434
515
|
const first = transform(inverse, {x: bounds.left, y: bounds.top});
|
|
@@ -438,6 +519,9 @@ export function attachScriptArrows({workspace, vm, getTarget, getRelations, getL
|
|
|
438
519
|
rx: '10', class: 'tw-script-relation-script-highlight', 'data-emphasis': role}));
|
|
439
520
|
}
|
|
440
521
|
for (const [key, drawing] of drawings) {
|
|
522
|
+
const visible = inViewport(drawing, matrix, area);
|
|
523
|
+
viewportDrawings.push({drawing, visible});
|
|
524
|
+
if (!visible) continue;
|
|
441
525
|
const {relation, source, destination, count} = drawing;
|
|
442
526
|
const highlighted = dragging ? incident(relation) :
|
|
443
527
|
emphasized && (emphasized === relation.source.scriptId || emphasized === relation.destination?.scriptId);
|
|
@@ -489,6 +573,8 @@ export function attachScriptArrows({workspace, vm, getTarget, getRelations, getL
|
|
|
489
573
|
if (destinations.childElementCount) destinations.replaceChildren();
|
|
490
574
|
destinations.hidden = true;
|
|
491
575
|
edgeDrawings = [];
|
|
576
|
+
viewportDrawings = [];
|
|
577
|
+
measuredAnchors.clear();
|
|
492
578
|
lastMatrix = undefined;
|
|
493
579
|
renderedGroups.delete(layer);
|
|
494
580
|
if (projectedLayer) renderedGroups.delete(projectedLayer);
|
|
@@ -5,6 +5,9 @@ import {attachScriptViewMenu, type ScriptViewMenuBlocks, type ScriptViewMenuWork
|
|
|
5
5
|
import type {PresentationElements} from './preview-chrome.ts';
|
|
6
6
|
import {attachScriptArrows, type ScriptRelationContext, type ScriptRelationVisibility} from './script-arrows.ts';
|
|
7
7
|
import {attachCodeZoomRange} from './code-zoom.ts';
|
|
8
|
+
import {setScriptProjection} from './script-projection.ts';
|
|
9
|
+
import {openSerialScriptEditor} from './serial-script-editor.ts';
|
|
10
|
+
import {isFoldedDefinition, setFoldedDefinition} from './procedure-edits.ts';
|
|
8
11
|
|
|
9
12
|
export type ScriptView = 'free' | 'z' | 'n';
|
|
10
13
|
|
|
@@ -39,10 +42,15 @@ export function attachScriptView({
|
|
|
39
42
|
surface.className = 'tw-script-view-surface';
|
|
40
43
|
surface.dataset.codeViewControl = '';
|
|
41
44
|
surface.tabIndex = 0;
|
|
42
|
-
surface.toggleAttribute('data-can-show-block', Boolean(canShowBlock));
|
|
45
|
+
surface.toggleAttribute('data-can-show-block', Boolean(canShowBlock || relationContext?.vm));
|
|
43
46
|
const contents = document.createElement('div');
|
|
44
47
|
surface.append(contents);
|
|
48
|
+
const gutter = document.createElement('div');
|
|
49
|
+
gutter.className = 'tw-script-fold-gutter';
|
|
50
|
+
gutter.dataset.codeViewControl = '';
|
|
51
|
+
const foldButtons = new Map<string, {button: HTMLButtonElement; top: number}>();
|
|
45
52
|
let view = initialView;
|
|
53
|
+
let editor: ReturnType<typeof openSerialScriptEditor>;
|
|
46
54
|
let frame: number | undefined;
|
|
47
55
|
const svg = workspace.getParentSvg();
|
|
48
56
|
const observer = new window.MutationObserver(records => {
|
|
@@ -51,6 +59,69 @@ export function attachScriptView({
|
|
|
51
59
|
const relations = relationContext && attachScriptArrows({workspace, getLocale, ...relationContext});
|
|
52
60
|
const japanese = () => /^ja(?:-|$)/i.test(getLocale());
|
|
53
61
|
|
|
62
|
+
function setFold(ids: string[], value?: boolean) {
|
|
63
|
+
const target = relationContext?.getTarget();
|
|
64
|
+
const runtime = relationContext?.vm?.runtime as {emitProjectChanged?(): void} | undefined;
|
|
65
|
+
if (!target || !runtime?.emitProjectChanged || (canShowBlock && !canShowBlock())) return;
|
|
66
|
+
for (const id of ids) {
|
|
67
|
+
const block = target.blocks._blocks[id];
|
|
68
|
+
if (block) setFoldedDefinition(block, value ?? !isFoldedDefinition(block));
|
|
69
|
+
}
|
|
70
|
+
runtime.emitProjectChanged();
|
|
71
|
+
update();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function positionFoldButtons() {
|
|
75
|
+
gutter.style.left = `${surface.offsetLeft}px`;
|
|
76
|
+
gutter.style.top = `${surface.offsetTop}px`;
|
|
77
|
+
gutter.style.height = `${surface.clientHeight}px`;
|
|
78
|
+
for (const {button, top} of foldButtons.values()) button.style.top = `${top - surface.scrollTop}px`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function paintFoldButtons(placements: ReturnType<typeof arrangeScripts>, scale: number) {
|
|
82
|
+
const target = relationContext?.getTarget();
|
|
83
|
+
const ids = new Set(placements.map(({script}) => script.id));
|
|
84
|
+
for (const [id, {button}] of foldButtons) if (!ids.has(id)) {button.remove(); foldButtons.delete(id);}
|
|
85
|
+
for (const {script, y} of placements) {
|
|
86
|
+
let entry = foldButtons.get(script.id);
|
|
87
|
+
if (!entry) {
|
|
88
|
+
const button = document.createElement('button');
|
|
89
|
+
button.type = 'button';
|
|
90
|
+
button.dataset.scriptFoldToggle = script.id;
|
|
91
|
+
button.addEventListener('click', event => {event.stopPropagation(); setFold([script.id]);});
|
|
92
|
+
button.addEventListener('pointerdown', event => event.stopPropagation());
|
|
93
|
+
entry = {button, top: 0};
|
|
94
|
+
foldButtons.set(script.id, entry);
|
|
95
|
+
gutter.append(button);
|
|
96
|
+
}
|
|
97
|
+
const block = target?.blocks._blocks[script.id];
|
|
98
|
+
const folded = isFoldedDefinition(block);
|
|
99
|
+
const label = japanese() ? (folded ? '折りたたみを解除' : '折りたたむ') : (folded ? 'Unfold script' : 'Fold script');
|
|
100
|
+
entry.top = y * scale;
|
|
101
|
+
entry.button.textContent = folded ? '▶' : '▼';
|
|
102
|
+
entry.button.disabled = !block || Boolean(canShowBlock && !canShowBlock());
|
|
103
|
+
entry.button.setAttribute('aria-label', label);
|
|
104
|
+
entry.button.setAttribute('aria-expanded', String(!folded));
|
|
105
|
+
entry.button.title = label;
|
|
106
|
+
}
|
|
107
|
+
positionFoldButtons();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function foldOptions() {
|
|
111
|
+
const target = relationContext?.getTarget();
|
|
112
|
+
const runtime = relationContext?.vm?.runtime as {emitProjectChanged?(): void} | undefined;
|
|
113
|
+
if (view === 'free' || !target || !runtime?.emitProjectChanged || (canShowBlock && !canShowBlock())) return [];
|
|
114
|
+
const roots = readScripts(workspace).map(script => target.blocks._blocks[script.id]).filter(Boolean);
|
|
115
|
+
const options = [
|
|
116
|
+
{value: true, text: japanese() ? 'すべて折りたたむ' : 'Fold all'},
|
|
117
|
+
{value: false, text: japanese() ? '折りたたみをすべて解除' : 'Unfold all'}
|
|
118
|
+
];
|
|
119
|
+
return options.map(({value, text}) => ({text,
|
|
120
|
+
enabled: roots.some(block => isFoldedDefinition(block) !== value),
|
|
121
|
+
callback() {setFold(readScripts(workspace).map(script => script.id), value);}
|
|
122
|
+
}));
|
|
123
|
+
}
|
|
124
|
+
|
|
54
125
|
function labels() {
|
|
55
126
|
const choice = choices.find(choice => choice.view === view);
|
|
56
127
|
surface.setAttribute('aria-label', japanese() ? choice.jaLabel : choice.label);
|
|
@@ -63,6 +134,7 @@ export function attachScriptView({
|
|
|
63
134
|
const ja = japanese();
|
|
64
135
|
return {
|
|
65
136
|
label: ja ? 'コードの並びの見え方を変更する' : 'Change code layout view',
|
|
137
|
+
options: foldOptions(),
|
|
66
138
|
choices: choices.map(choice => ({
|
|
67
139
|
text: ja ? choice.jaLabel : choice.label,
|
|
68
140
|
enabled: true,
|
|
@@ -85,8 +157,20 @@ export function attachScriptView({
|
|
|
85
157
|
surface.style.setProperty('--tw-script-view-left', !horizontal && bounds.left <= left + 2 ? `${bounds.width}px` : '0px');
|
|
86
158
|
surface.style.setProperty('--tw-script-view-right', !horizontal && bounds.right >= right - 2 ? `${bounds.width}px` : '0px');
|
|
87
159
|
}
|
|
88
|
-
|
|
89
|
-
|
|
160
|
+
const scale = workspace.scale || 1;
|
|
161
|
+
const scripts = readScripts(workspace).map(script => {
|
|
162
|
+
// Include raised hats and labels, and exclude folded bodies, when measuring the visible gap.
|
|
163
|
+
const box = (script.root as SVGGraphicsElement).getBBox();
|
|
164
|
+
return {...script, x: script.origin.x + box.x, y: script.origin.y + box.y,
|
|
165
|
+
width: box.width, height: box.height};
|
|
166
|
+
});
|
|
167
|
+
const placements = arrangeScripts(scripts, view, 'vertical', Math.max(64, 48 / scale))
|
|
168
|
+
.map(placement => ({...placement, x: Math.max(placement.x, 32 / scale)}));
|
|
169
|
+
paintScripts(contents, placements, scale);
|
|
170
|
+
paintFoldButtons(placements, scale);
|
|
171
|
+
const projection = contents.querySelector('svg');
|
|
172
|
+
relations?.setProjection(projection);
|
|
173
|
+
setScriptProjection(workspace, {surface, svg: projection});
|
|
90
174
|
}
|
|
91
175
|
|
|
92
176
|
function update() {
|
|
@@ -96,14 +180,20 @@ export function attachScriptView({
|
|
|
96
180
|
|
|
97
181
|
function setView(next: ScriptView) {
|
|
98
182
|
menu.close();
|
|
183
|
+
editor?.dispose();
|
|
184
|
+
editor = undefined;
|
|
99
185
|
observer.disconnect();
|
|
100
186
|
view = next;
|
|
101
187
|
surface.hidden = view === 'free';
|
|
188
|
+
gutter.hidden = view === 'free';
|
|
102
189
|
svg.toggleAttribute('data-script-serial', view !== 'free');
|
|
103
190
|
labels();
|
|
104
191
|
if (view === 'free') {
|
|
192
|
+
setScriptProjection(workspace);
|
|
105
193
|
relations?.setProjection(null);
|
|
106
194
|
contents.replaceChildren();
|
|
195
|
+
gutter.replaceChildren();
|
|
196
|
+
foldButtons.clear();
|
|
107
197
|
}
|
|
108
198
|
else {
|
|
109
199
|
observer.observe(workspace.getCanvas(), {attributes: true, childList: true, subtree: true});
|
|
@@ -120,19 +210,33 @@ export function attachScriptView({
|
|
|
120
210
|
surface.addEventListener('click', event => {
|
|
121
211
|
event.stopPropagation();
|
|
122
212
|
const id = (event.target as Element).closest('[data-script-view-block]')?.getAttribute('data-script-view-block');
|
|
123
|
-
if (!id || !canShowBlock
|
|
213
|
+
if (!id || (canShowBlock && !canShowBlock())) return;
|
|
214
|
+
if (relationContext?.vm) {
|
|
215
|
+
editor?.dispose();
|
|
216
|
+
editor = openSerialScriptEditor(relationContext.vm, relationContext.getTarget(), id, document, getLocale());
|
|
217
|
+
if (editor) return;
|
|
218
|
+
}
|
|
219
|
+
if (!canShowBlock?.()) return;
|
|
124
220
|
chooseView('free');
|
|
125
221
|
workspace.centerOnBlock(id);
|
|
126
222
|
});
|
|
127
223
|
const resize = new window.ResizeObserver(update);
|
|
128
224
|
resize.observe(host);
|
|
129
|
-
const language = new window.MutationObserver(labels);
|
|
225
|
+
const language = new window.MutationObserver(() => {labels(); update();});
|
|
130
226
|
language.observe(document.documentElement, {attributes: true, attributeFilter: ['lang']});
|
|
131
227
|
// The Blockly keyboard listener is document-wide.
|
|
132
228
|
surface.addEventListener('keydown', event => {
|
|
133
229
|
if (event.key !== 'Tab') event.stopPropagation();
|
|
134
230
|
});
|
|
135
|
-
|
|
231
|
+
gutter.addEventListener('keydown', event => {
|
|
232
|
+
if (event.key !== 'Tab') event.stopPropagation();
|
|
233
|
+
});
|
|
234
|
+
host.append(surface, gutter);
|
|
235
|
+
surface.addEventListener('scroll', () => {
|
|
236
|
+
positionFoldButtons();
|
|
237
|
+
const projection = contents.querySelector('svg');
|
|
238
|
+
if (view !== 'free' && projection) setScriptProjection(workspace, {surface, svg: projection});
|
|
239
|
+
}, {passive: true});
|
|
136
240
|
setView(initialView);
|
|
137
241
|
return {
|
|
138
242
|
update,
|
|
@@ -141,6 +245,8 @@ export function attachScriptView({
|
|
|
141
245
|
get view() { return view; },
|
|
142
246
|
setView,
|
|
143
247
|
dispose() {
|
|
248
|
+
editor?.dispose();
|
|
249
|
+
setScriptProjection(workspace);
|
|
144
250
|
restoreZoomRange();
|
|
145
251
|
resize.disconnect();
|
|
146
252
|
observer.disconnect();
|
|
@@ -149,6 +255,8 @@ export function attachScriptView({
|
|
|
149
255
|
menu.dispose();
|
|
150
256
|
relations?.dispose();
|
|
151
257
|
surface.remove();
|
|
258
|
+
gutter.remove();
|
|
259
|
+
foldButtons.clear();
|
|
152
260
|
svg.removeAttribute('data-script-serial');
|
|
153
261
|
}
|
|
154
262
|
};
|
|
@@ -40,9 +40,14 @@ export interface RenderedScript extends ScriptBounds {
|
|
|
40
40
|
export function readScripts(workspace: LayoutWorkspace): RenderedScript[] {
|
|
41
41
|
return workspace.getTopBlocks(false).flatMap(block => {
|
|
42
42
|
const root = block.getSvgRoot();
|
|
43
|
-
if (!root || !workspace.getCanvas().contains(root)) return [];
|
|
43
|
+
if (!root || !workspace.getCanvas().contains(root) || root.hasAttribute('data-tw-plus-hidden')) return [];
|
|
44
44
|
const origin = block.getRelativeToSurfaceXY();
|
|
45
|
-
|
|
45
|
+
let {topLeft, bottomRight} = block.getBoundingRectangle();
|
|
46
|
+
if (root.querySelector(':scope > [data-tw-plus-folded-mark]')) {
|
|
47
|
+
const box = (root as SVGGraphicsElement).getBBox();
|
|
48
|
+
topLeft = {x: origin.x + box.x, y: origin.y + box.y};
|
|
49
|
+
bottomRight = {x: topLeft.x + box.width, y: topLeft.y + box.height};
|
|
50
|
+
}
|
|
46
51
|
return [{
|
|
47
52
|
id: block.id, block, root, origin, ...topLeft,
|
|
48
53
|
width: bottomRight.x - topLeft.x, height: bottomRight.y - topLeft.y
|
package/src/script-layout.ts
CHANGED
|
@@ -15,7 +15,8 @@ export type LayoutDirection = 'vertical' | 'horizontal';
|
|
|
15
15
|
const alignmentTolerance = 24;
|
|
16
16
|
const spacing = 32;
|
|
17
17
|
|
|
18
|
-
export function arrangeScripts<T extends ScriptBounds>(scripts: readonly T[], order: ScriptOrder, direction: LayoutDirection
|
|
18
|
+
export function arrangeScripts<T extends ScriptBounds>(scripts: readonly T[], order: ScriptOrder, direction: LayoutDirection,
|
|
19
|
+
gap = spacing) {
|
|
19
20
|
const bandAxis = order === 'z' ? 'y' : 'x';
|
|
20
21
|
const scanAxis = order === 'z' ? 'x' : 'y';
|
|
21
22
|
const compare = (a: T, b: T, first: 'x' | 'y', second: 'x' | 'y') =>
|
|
@@ -29,12 +30,12 @@ export function arrangeScripts<T extends ScriptBounds>(scripts: readonly T[], or
|
|
|
29
30
|
else bands.push([script]);
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
let offset =
|
|
33
|
+
let offset = gap;
|
|
33
34
|
return bands.flatMap(band => band.sort((a, b) => compare(a, b, scanAxis, bandAxis))).map(script => {
|
|
34
35
|
const vertical = direction === 'vertical';
|
|
35
36
|
const x = vertical ? spacing : offset;
|
|
36
37
|
const y = vertical ? offset : spacing;
|
|
37
|
-
offset += Math.ceil(vertical ? script.height : script.width) +
|
|
38
|
+
offset += Math.ceil(vertical ? script.height : script.width) + gap;
|
|
38
39
|
return {script, x, y};
|
|
39
40
|
});
|
|
40
41
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type ScriptProjection = {surface: HTMLElement; svg: SVGSVGElement};
|
|
2
|
+
|
|
3
|
+
const projections = new WeakMap<object, ScriptProjection>();
|
|
4
|
+
|
|
5
|
+
export const getScriptProjection = (workspace: object) => projections.get(workspace);
|
|
6
|
+
|
|
7
|
+
export function setScriptProjection(workspace: {getInjectionDiv(): HTMLElement}, projection?: ScriptProjection) {
|
|
8
|
+
if (projection) projections.set(workspace, projection);
|
|
9
|
+
else projections.delete(workspace);
|
|
10
|
+
const container = workspace.getInjectionDiv();
|
|
11
|
+
container.dispatchEvent(new container.ownerDocument.defaultView.Event('scriptprojectionchange'));
|
|
12
|
+
}
|
package/src/script-view-menu.ts
CHANGED
|
@@ -40,6 +40,7 @@ export interface ScriptViewMenuWorkspace {
|
|
|
40
40
|
|
|
41
41
|
interface MenuConfig {
|
|
42
42
|
label: string;
|
|
43
|
+
options?: MenuOption[];
|
|
43
44
|
choices: (MenuOption & {checked: boolean})[];
|
|
44
45
|
}
|
|
45
46
|
|
|
@@ -160,7 +161,7 @@ export function attachScriptViewMenu(
|
|
|
160
161
|
// Only extend the options collected for this workspace, after the existing add-ons.
|
|
161
162
|
ContextMenu.populate_ = (options, rtl) => {
|
|
162
163
|
const config = getMenu();
|
|
163
|
-
const menu = populate.call(ContextMenu, [...options, triggerOption(config.label)], rtl);
|
|
164
|
+
const menu = populate.call(ContextMenu, [...options, triggerOption(config.label), ...(config.options ?? [])], rtl);
|
|
164
165
|
addSubmenu(menu, options.length, config, rtl, ContextMenu);
|
|
165
166
|
return menu;
|
|
166
167
|
};
|
|
@@ -177,7 +178,9 @@ export function attachScriptViewMenu(
|
|
|
177
178
|
event.stopPropagation();
|
|
178
179
|
const config = getMenu();
|
|
179
180
|
const rtl = workspace.RTL;
|
|
180
|
-
const menu = ContextMenu.populate_([triggerOption(config.label)
|
|
181
|
+
const menu = ContextMenu.populate_([triggerOption(config.label), ...(config.options ?? []).map(option => ({...option,
|
|
182
|
+
callback() {WidgetDiv.hideIfOwner(surface); option.callback();}
|
|
183
|
+
}))], rtl);
|
|
181
184
|
const outside = new document.defaultView.AbortController();
|
|
182
185
|
WidgetDiv.show(surface, rtl, () => {
|
|
183
186
|
outside.abort();
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {getCodeSession} from './workspace.ts';
|
|
2
|
+
|
|
3
|
+
/** Reuse the script editor: its root stays at its original project coordinates. */
|
|
4
|
+
export function openSerialScriptEditor(vm: object, target, id: string, document: Document, locale: string) {
|
|
5
|
+
const bridge = getCodeSession(vm).bridge;
|
|
6
|
+
if (!bridge || !target?.blocks._blocks[id]) 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: ReturnType<typeof bridge.editProcedure>;
|
|
22
|
+
try {
|
|
23
|
+
editor = bridge.editProcedure(area, {target, definitionId: id, editable: true, locale});
|
|
24
|
+
} catch (error) {
|
|
25
|
+
dialog.close();
|
|
26
|
+
dialog.remove();
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
let closed = false;
|
|
30
|
+
const dispose = () => {
|
|
31
|
+
if (closed) return;
|
|
32
|
+
closed = true;
|
|
33
|
+
editor.flush();
|
|
34
|
+
editor.dispose();
|
|
35
|
+
dialog.close();
|
|
36
|
+
dialog.remove();
|
|
37
|
+
};
|
|
38
|
+
close.addEventListener('click', dispose);
|
|
39
|
+
dialog.addEventListener('cancel', event => {event.preventDefault(); dispose();});
|
|
40
|
+
return {dispose};
|
|
41
|
+
}
|