@kubohiroya/turbowarp-plus-editor-code-views 0.18.0 → 0.19.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 +17 -1
- package/dist/code-pane.d.ts +14 -0
- package/dist/code-pane.js +36 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +59 -2
- package/dist/script-arrows.d.ts +47 -0
- package/dist/script-arrows.js +448 -0
- package/dist/script-layout-view.d.ts +24 -1
- package/dist/script-layout-view.js +15 -4
- package/dist/script-relations.d.ts +47 -0
- package/dist/script-relations.js +155 -0
- package/dist/sprite-code-windows.d.ts +1 -1
- package/dist/sprite-code-windows.js +1 -1
- package/dist/workspace.d.ts +3 -2
- package/dist/workspace.js +24 -2
- package/package.json +1 -1
- package/src/code-pane.ts +32 -1
- package/src/index.ts +55 -2
- package/src/script-arrows.ts +440 -0
- package/src/script-layout-view.ts +17 -4
- package/src/script-relations.ts +179 -0
- package/src/sprite-code-windows.tsx +1 -1
- package/src/styles.css +145 -0
- package/src/workspace.ts +26 -6
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
import { isFoldedDefinition } from "./procedure-edits.js";
|
|
2
|
+
import { readScriptRelations } from "./script-relations.js";
|
|
3
|
+
const ns = 'http://www.w3.org/2000/svg';
|
|
4
|
+
// Keep the same sixteen-unit hourglass as the call graph's relation badge.
|
|
5
|
+
const relationWaitGlyph = 'M1 1h14M1 15h14M2 1v3l12 8v3M14 1v3l-12 8v3M2 4h12M2 12h12';
|
|
6
|
+
/** Keep the badge beside the receiving end of the cubic curve, leaving its stroke visible. */
|
|
7
|
+
function waitBadgePoint(path, offset) {
|
|
8
|
+
const [sx, sy, ax, ay, bx, by, tx, ty] = path.match(/-?(?:\d*\.?\d+)(?:e[+-]?\d+)?/gi).map(Number);
|
|
9
|
+
const endX = tx - bx;
|
|
10
|
+
const endY = ty - by;
|
|
11
|
+
const dx = endX || endY ? endX : tx - sx;
|
|
12
|
+
const dy = endX || endY ? endY : ty - sy;
|
|
13
|
+
const length = Math.hypot(dx, dy) || 1;
|
|
14
|
+
const side = -dy < 0 || dy === 0 && dx > 0 ? -1 : 1;
|
|
15
|
+
return { x: tx - dx / length * offset * .75 - dy / length * offset * side,
|
|
16
|
+
y: ty - dy / length * offset * .75 + dx / length * offset * side };
|
|
17
|
+
}
|
|
18
|
+
const outlines = new WeakMap();
|
|
19
|
+
/** Intersect the actual outline, including a hat's overhang, rather than Blockly's layout padding. */
|
|
20
|
+
function outlinePoint(shape, bounds, side, fraction) {
|
|
21
|
+
const data = shape.getAttribute('d') ?? '';
|
|
22
|
+
let outline = outlines.get(shape);
|
|
23
|
+
if (!outline || outline.data !== data) {
|
|
24
|
+
const length = shape.getTotalLength();
|
|
25
|
+
const count = Math.max(32, Math.min(1024, Math.ceil(length / 2)));
|
|
26
|
+
const points = Array.from({ length: count + 1 }, (_, index) => {
|
|
27
|
+
const point = shape.getPointAtLength(length * index / count);
|
|
28
|
+
return { x: point.x, y: point.y };
|
|
29
|
+
});
|
|
30
|
+
outline = { data, points };
|
|
31
|
+
outlines.set(shape, outline);
|
|
32
|
+
}
|
|
33
|
+
const y = bounds.y + bounds.height * fraction;
|
|
34
|
+
let edge;
|
|
35
|
+
for (let index = 1; index < outline.points.length; index++) {
|
|
36
|
+
const before = outline.points[index - 1];
|
|
37
|
+
const after = outline.points[index];
|
|
38
|
+
if ((before.y <= y && after.y >= y) || (before.y >= y && after.y <= y)) {
|
|
39
|
+
const x = before.y === after.y ? (side === 'left' ? Math.min(before.x, after.x) : Math.max(before.x, after.x)) :
|
|
40
|
+
before.x + (after.x - before.x) * (y - before.y) / (after.y - before.y);
|
|
41
|
+
edge = edge === undefined ? x : side === 'left' ? Math.min(edge, x) : Math.max(edge, x);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return { x: edge ?? bounds.x + (side === 'right' ? bounds.width : 0), y };
|
|
45
|
+
}
|
|
46
|
+
// The shared workspace owns navigation even when an arrow is in a read-only preview.
|
|
47
|
+
const navigators = new WeakMap();
|
|
48
|
+
export function registerScriptRelationNavigation(vm, navigate) {
|
|
49
|
+
navigators.set(vm, navigate);
|
|
50
|
+
return () => { if (navigators.get(vm) === navigate)
|
|
51
|
+
navigators.delete(vm); };
|
|
52
|
+
}
|
|
53
|
+
const transform = (matrix, { x, y }) => ({
|
|
54
|
+
x: matrix.a * x + matrix.c * y + matrix.e,
|
|
55
|
+
y: matrix.b * x + matrix.d * y + matrix.f
|
|
56
|
+
});
|
|
57
|
+
/** One renderer and semantic stylesheet serve live code, folded scripts, previews and serial layouts. */
|
|
58
|
+
export function attachScriptArrows({ workspace, vm, getTarget, getRelations, getLocale, getTargetName, navigate }) {
|
|
59
|
+
const injection = workspace.getInjectionDiv();
|
|
60
|
+
const document = injection.ownerDocument;
|
|
61
|
+
const view = document.defaultView;
|
|
62
|
+
const canvas = workspace.getCanvas();
|
|
63
|
+
const layer = document.createElementNS(ns, 'g');
|
|
64
|
+
layer.setAttribute('data-script-relation-layer', '');
|
|
65
|
+
layer.setAttribute('aria-hidden', 'true');
|
|
66
|
+
layer.setAttribute('pointer-events', 'none');
|
|
67
|
+
canvas.prepend(layer);
|
|
68
|
+
const destinations = document.createElement('div');
|
|
69
|
+
destinations.className = 'tw-script-relation-guide tw-script-relation-destinations';
|
|
70
|
+
destinations.dataset.scriptRelationGuide = '';
|
|
71
|
+
destinations.dataset.codeViewControl = '';
|
|
72
|
+
destinations.hidden = true;
|
|
73
|
+
// Blockly listens on the document; operating these controls must not delete or undo a block.
|
|
74
|
+
destinations.addEventListener('keydown', event => {
|
|
75
|
+
if (event.key !== 'Tab')
|
|
76
|
+
event.stopPropagation();
|
|
77
|
+
});
|
|
78
|
+
injection.append(destinations);
|
|
79
|
+
let projection = null;
|
|
80
|
+
let projectedLayer;
|
|
81
|
+
let frame;
|
|
82
|
+
let disposed = false;
|
|
83
|
+
let dirty = true;
|
|
84
|
+
let graph = [];
|
|
85
|
+
let selected;
|
|
86
|
+
let enabled = true;
|
|
87
|
+
const markedScripts = new Map();
|
|
88
|
+
const text = () => /^ja(?:-|$)/i.test(getLocale?.() ?? document.documentElement.lang) ? {
|
|
89
|
+
title: '呼び出しの矢印', function: '関数呼び出し', message: 'メッセージ送信', wait: '送って待つ',
|
|
90
|
+
dynamic: '実行時に決定', missing: '接続先が見つかりません', ambiguous: '接続先が複数の定義に一致',
|
|
91
|
+
incoming: '呼び出し元', outgoing: '接続先', count: '件'
|
|
92
|
+
} : {
|
|
93
|
+
title: 'Script arrows', function: 'Function call', message: 'Message send', wait: 'Send and wait',
|
|
94
|
+
dynamic: 'Determined at runtime', missing: 'Destination not found', ambiguous: 'Multiple definitions match',
|
|
95
|
+
incoming: 'Source', outgoing: 'Destination', count: 'calls'
|
|
96
|
+
};
|
|
97
|
+
function element(tag, attributes = {}) {
|
|
98
|
+
const node = document.createElementNS(ns, tag);
|
|
99
|
+
for (const [key, value] of Object.entries(attributes))
|
|
100
|
+
node.setAttribute(key, value);
|
|
101
|
+
return node;
|
|
102
|
+
}
|
|
103
|
+
function markScripts(next) {
|
|
104
|
+
for (const [root, role] of markedScripts) {
|
|
105
|
+
if (next.get(root) !== role)
|
|
106
|
+
root.removeAttribute(`data-script-relation-${role === 'source' ? 'drag-source' : 'related'}`);
|
|
107
|
+
}
|
|
108
|
+
for (const [root, role] of next) {
|
|
109
|
+
if (markedScripts.get(root) !== role)
|
|
110
|
+
root.setAttribute(`data-script-relation-${role === 'source' ? 'drag-source' : 'related'}`, '');
|
|
111
|
+
}
|
|
112
|
+
markedScripts.clear();
|
|
113
|
+
for (const entry of next)
|
|
114
|
+
markedScripts.set(...entry);
|
|
115
|
+
}
|
|
116
|
+
function blockRoot(id, roots) {
|
|
117
|
+
// Blockly lends a dragged stack to an SVG outside the canvas; the Block keeps its SVG identity.
|
|
118
|
+
return roots.get(id) ?? (!projection ? workspace.getBlockById?.(id)?.getSvgRoot() ?? undefined : undefined);
|
|
119
|
+
}
|
|
120
|
+
function go(endpoint) {
|
|
121
|
+
const target = getTarget();
|
|
122
|
+
const current = vm?.runtime?.targets?.find(item => item.id === endpoint.targetId);
|
|
123
|
+
if (vm && (!current || !current.blocks._blocks[endpoint.blockId]))
|
|
124
|
+
return;
|
|
125
|
+
const handler = navigate ?? (vm && navigators.get(vm));
|
|
126
|
+
if (handler)
|
|
127
|
+
handler(endpoint);
|
|
128
|
+
else {
|
|
129
|
+
if (endpoint.targetId !== target?.id)
|
|
130
|
+
vm?.setEditingTarget?.(endpoint.targetId);
|
|
131
|
+
view.requestAnimationFrame(() => {
|
|
132
|
+
if (!disposed)
|
|
133
|
+
workspace.centerOnBlock(endpoint.blockId);
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function anchor(endpoint, target, roots, inverse) {
|
|
138
|
+
if (endpoint.targetId !== target.id)
|
|
139
|
+
return;
|
|
140
|
+
const top = blockRoot(endpoint.scriptId, roots);
|
|
141
|
+
// A script hidden by the one-script view is not the same as its folded body.
|
|
142
|
+
if (!top || !top.getClientRects().length || top.closest('[data-tw-plus-hidden]') ||
|
|
143
|
+
view.getComputedStyle(top).display === 'none')
|
|
144
|
+
return;
|
|
145
|
+
const folded = isFoldedDefinition(target.blocks._blocks[endpoint.scriptId]);
|
|
146
|
+
let id = folded ? endpoint.scriptId : endpoint.blockId;
|
|
147
|
+
let root = blockRoot(id, roots);
|
|
148
|
+
// Native Blockly collapsing can also hide a caller inside a control block.
|
|
149
|
+
const visited = new Set();
|
|
150
|
+
while (root && (!root.getClientRects().length || root.closest('[data-tw-plus-hidden]') ||
|
|
151
|
+
view.getComputedStyle(root).display === 'none') && !visited.has(id)) {
|
|
152
|
+
visited.add(id);
|
|
153
|
+
id = target.blocks._blocks[id]?.parent ?? '';
|
|
154
|
+
root = blockRoot(id, roots);
|
|
155
|
+
}
|
|
156
|
+
if (!root)
|
|
157
|
+
return;
|
|
158
|
+
const path = root.querySelector(':scope > .blocklyPath');
|
|
159
|
+
const matrix = (path ?? root).getScreenCTM();
|
|
160
|
+
if (!matrix)
|
|
161
|
+
return;
|
|
162
|
+
const block = workspace.getBlockById?.(id);
|
|
163
|
+
const bounds = path?.getBBox() ?? { x: workspace.RTL ? -(block?.width ?? 0) : 0,
|
|
164
|
+
y: 0, width: block?.width ?? 0, height: block?.height ?? 0 };
|
|
165
|
+
if (!bounds.width || !bounds.height)
|
|
166
|
+
return;
|
|
167
|
+
const convert = (point) => transform(inverse, transform(matrix, point));
|
|
168
|
+
const first = convert(bounds);
|
|
169
|
+
const last = convert({ x: bounds.x + bounds.width, y: bounds.y + bounds.height });
|
|
170
|
+
return { id, box: { x: Math.min(first.x, last.x), y: Math.min(first.y, last.y),
|
|
171
|
+
width: Math.abs(last.x - first.x), height: Math.abs(last.y - first.y) }, point(side, fraction) {
|
|
172
|
+
const localSide = first.x <= last.x ? side : side === 'left' ? 'right' : 'left';
|
|
173
|
+
return convert(path ? outlinePoint(path, bounds, localSide, fraction) :
|
|
174
|
+
{ x: bounds.x + (localSide === 'right' ? bounds.width : 0), y: bounds.y + bounds.height * fraction });
|
|
175
|
+
} };
|
|
176
|
+
}
|
|
177
|
+
function render() {
|
|
178
|
+
frame = undefined;
|
|
179
|
+
if (disposed)
|
|
180
|
+
return;
|
|
181
|
+
const strings = text();
|
|
182
|
+
const toolbox = injection.querySelector(':scope > .blocklyToolboxDiv');
|
|
183
|
+
const toolboxBounds = toolbox?.getBoundingClientRect();
|
|
184
|
+
const horizontal = toolbox?.hasAttribute('data-horizontal-block-palette');
|
|
185
|
+
// The horizontal palette sits above the blocks, including in the Editor's default layout.
|
|
186
|
+
destinations.style.top = `${horizontal ? Math.max(10, toolboxBounds.bottom - injection.getBoundingClientRect().top + 10) : 10}px`;
|
|
187
|
+
destinations.replaceChildren();
|
|
188
|
+
destinations.hidden = true;
|
|
189
|
+
const drawingRoot = projection ?? canvas;
|
|
190
|
+
const drawingLayer = projection ? projectedLayer : layer;
|
|
191
|
+
if (!drawingLayer)
|
|
192
|
+
return;
|
|
193
|
+
drawingLayer.replaceChildren();
|
|
194
|
+
drawingLayer.removeAttribute('data-script-dragging');
|
|
195
|
+
layer.style.display = projection ? 'none' : '';
|
|
196
|
+
if (!enabled) {
|
|
197
|
+
markScripts(new Map());
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const target = getTarget();
|
|
201
|
+
if (!target) {
|
|
202
|
+
markScripts(new Map());
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const matrix = drawingRoot.getScreenCTM();
|
|
206
|
+
if (!matrix) {
|
|
207
|
+
markScripts(new Map());
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const inverse = matrix.inverse();
|
|
211
|
+
if (![inverse.a, inverse.b, inverse.c, inverse.d, inverse.e, inverse.f].every(Number.isFinite)) {
|
|
212
|
+
markScripts(new Map());
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
if (getRelations)
|
|
216
|
+
graph = getRelations();
|
|
217
|
+
else if (dirty) {
|
|
218
|
+
graph = readScriptRelations(vm?.runtime?.targets ?? [target]);
|
|
219
|
+
dirty = false;
|
|
220
|
+
}
|
|
221
|
+
const roots = new Map();
|
|
222
|
+
for (const root of drawingRoot.querySelectorAll('[data-id]'))
|
|
223
|
+
roots.set(root.dataset.id, root);
|
|
224
|
+
const nativeSelection = [...roots.values()].find(root => root.classList.contains('blocklySelected'))?.dataset.id;
|
|
225
|
+
const emphasized = selected ?? scriptOf(nativeSelection, target);
|
|
226
|
+
const dragging = !projection && injection.querySelector('[data-id].blocklyDragging');
|
|
227
|
+
const draggedIds = new Set();
|
|
228
|
+
if (dragging) {
|
|
229
|
+
draggedIds.add(dragging.dataset.id);
|
|
230
|
+
for (const root of dragging.querySelectorAll('[data-id]'))
|
|
231
|
+
draggedIds.add(root.dataset.id);
|
|
232
|
+
}
|
|
233
|
+
const inDrag = (endpoint) => Boolean(endpoint && endpoint.targetId === target.id && draggedIds.has(endpoint.blockId));
|
|
234
|
+
const incident = (relation) => inDrag(relation.source) || inDrag(relation.destination);
|
|
235
|
+
drawingLayer.toggleAttribute('data-script-dragging', Boolean(dragging));
|
|
236
|
+
const drawings = new Map();
|
|
237
|
+
for (const relation of graph) {
|
|
238
|
+
if (relation.source.targetId !== target.id && relation.destination?.targetId !== target.id)
|
|
239
|
+
continue;
|
|
240
|
+
const source = anchor(relation.source, target, roots, inverse);
|
|
241
|
+
const destination = relation.destination && anchor(relation.destination, target, roots, inverse);
|
|
242
|
+
if (!source && !destination)
|
|
243
|
+
continue;
|
|
244
|
+
const key = JSON.stringify([relation.kind, source?.id ?? relation.source,
|
|
245
|
+
destination?.id ?? relation.destination, relation.wait, relation.unresolved, relation.label]);
|
|
246
|
+
const existing = drawings.get(key);
|
|
247
|
+
if (existing)
|
|
248
|
+
existing.count++;
|
|
249
|
+
else
|
|
250
|
+
drawings.set(key, { relation, source, destination, count: 1 });
|
|
251
|
+
}
|
|
252
|
+
const related = new Set();
|
|
253
|
+
for (const relation of graph)
|
|
254
|
+
if (dragging && incident(relation)) {
|
|
255
|
+
if (inDrag(relation.source) && relation.destination?.targetId === target.id && !inDrag(relation.destination)) {
|
|
256
|
+
related.add(relation.destination.scriptId);
|
|
257
|
+
}
|
|
258
|
+
if (inDrag(relation.destination) && relation.source.targetId === target.id && !inDrag(relation.source)) {
|
|
259
|
+
related.add(relation.source.scriptId);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
const marked = new Map();
|
|
263
|
+
if (dragging)
|
|
264
|
+
marked.set(dragging, 'source');
|
|
265
|
+
for (const id of related) {
|
|
266
|
+
const root = blockRoot(id, roots);
|
|
267
|
+
if (root && root.getClientRects().length && !root.closest('[data-tw-plus-hidden]'))
|
|
268
|
+
marked.set(root, 'related');
|
|
269
|
+
}
|
|
270
|
+
markScripts(marked);
|
|
271
|
+
// Frame the whole stack, including its body; the original block paint and runtime glow stay intact.
|
|
272
|
+
for (const [root, role] of marked) {
|
|
273
|
+
const bounds = root.getBoundingClientRect();
|
|
274
|
+
const first = transform(inverse, { x: bounds.left, y: bounds.top });
|
|
275
|
+
const last = transform(inverse, { x: bounds.right, y: bounds.bottom });
|
|
276
|
+
drawingLayer.append(element('rect', { x: String(first.x - 7), y: String(first.y - 7),
|
|
277
|
+
width: String(Math.max(0, last.x - first.x) + 14), height: String(Math.max(0, last.y - first.y) + 14),
|
|
278
|
+
rx: '10', class: 'tw-script-relation-script-highlight', 'data-emphasis': role }));
|
|
279
|
+
}
|
|
280
|
+
for (const { relation, source, destination, count } of drawings.values()) {
|
|
281
|
+
const highlighted = dragging ? incident(relation) :
|
|
282
|
+
emphasized && (emphasized === relation.source.scriptId || emphasized === relation.destination?.scriptId);
|
|
283
|
+
let external;
|
|
284
|
+
if ((!source || !destination) && highlighted) {
|
|
285
|
+
external = document.createElement('button');
|
|
286
|
+
external.type = 'button';
|
|
287
|
+
external.dataset.scriptRelation = relation.kind;
|
|
288
|
+
const endpoint = source ? relation.destination : relation.source;
|
|
289
|
+
const name = endpoint && (getTargetName?.(endpoint.targetId) ??
|
|
290
|
+
vm?.runtime?.targets?.find(item => item.id === endpoint.targetId)?.getName?.());
|
|
291
|
+
const reason = relation.unresolved && strings[relation.unresolved];
|
|
292
|
+
external.textContent = `${name ? `${name} · ` : ''}` +
|
|
293
|
+
`${relation.label}${reason ? ` · ${reason}` : ''}${relation.kind === 'message' && relation.wait ? ' ⏳' : ''}${count > 1 ? ` ×${count}` : ''}`;
|
|
294
|
+
external.disabled = !endpoint;
|
|
295
|
+
external.setAttribute('aria-label', `${strings[relation.kind]}: ${external.textContent}`);
|
|
296
|
+
external.toggleAttribute('data-highlighted', Boolean(highlighted));
|
|
297
|
+
if (endpoint)
|
|
298
|
+
external.addEventListener('click', () => go(endpoint));
|
|
299
|
+
destinations.append(external);
|
|
300
|
+
}
|
|
301
|
+
// Anchor an external link to the viewport edge; the list itself remains keyboard accessible.
|
|
302
|
+
const area = (projection ?? workspace.getParentSvg()).getBoundingClientRect();
|
|
303
|
+
const local = source ?? destination;
|
|
304
|
+
const edgeX = source ? area.right - 12 : area.left + 12;
|
|
305
|
+
const screenY = transform(matrix, { x: local.box.x, y: local.box.y + local.box.height / 2 }).y;
|
|
306
|
+
const edge = transform(inverse, { x: edgeX, y: Math.max(area.top + 12, Math.min(area.bottom - 12, screenY)) });
|
|
307
|
+
const forward = !source || !destination || destination.box.x >= source.box.x + source.box.width / 2;
|
|
308
|
+
let start = source ? source.point(forward ? 'right' : 'left', 0.5) : edge;
|
|
309
|
+
let end = destination ? destination.point(forward ? 'left' : 'right', Math.min(0.5, 24 / destination.box.height)) : edge;
|
|
310
|
+
let direction = forward ? 1 : -1;
|
|
311
|
+
const bend = Math.max(36, Math.abs(end.x - start.x) / 2);
|
|
312
|
+
let path = `M ${start.x} ${start.y} C ${start.x + bend * direction} ${start.y}, ${end.x - bend * direction} ${end.y}, ${end.x} ${end.y}`;
|
|
313
|
+
const recursive = source && destination && relation.source.scriptId === relation.destination.scriptId &&
|
|
314
|
+
relation.source.targetId === relation.destination.targetId;
|
|
315
|
+
let badgePoint = { x: (start.x + end.x) / 2, y: (start.y + end.y) / 2 - 6 };
|
|
316
|
+
if (recursive) {
|
|
317
|
+
start = source.point('right', 0.75);
|
|
318
|
+
end = destination.point('right', 0.25);
|
|
319
|
+
const outside = Math.max(start.x, end.x) + 72;
|
|
320
|
+
path = `M ${start.x} ${start.y} C ${outside} ${start.y + 32}, ${outside} ${end.y - 32}, ${end.x} ${end.y}`;
|
|
321
|
+
direction = -1;
|
|
322
|
+
badgePoint = { x: outside - 14, y: (start.y + end.y) / 2 - 6 };
|
|
323
|
+
}
|
|
324
|
+
const group = element('g', {
|
|
325
|
+
'data-script-relation': relation.kind,
|
|
326
|
+
'data-source-block': source?.id ?? relation.source.blockId,
|
|
327
|
+
'data-source-call': relation.source.blockId,
|
|
328
|
+
'data-destination-block': destination?.id ?? relation.destination?.blockId ?? '',
|
|
329
|
+
'data-count': String(count),
|
|
330
|
+
'data-wait': String(relation.wait)
|
|
331
|
+
});
|
|
332
|
+
group.toggleAttribute('data-highlighted', Boolean(highlighted));
|
|
333
|
+
group.toggleAttribute('data-recursive', Boolean(recursive));
|
|
334
|
+
const description = element('title');
|
|
335
|
+
const waiting = relation.kind === 'message' && relation.wait;
|
|
336
|
+
description.textContent = `${strings[relation.kind]}: ${relation.label}${waiting ? ` · ${strings.wait}` : ''}`;
|
|
337
|
+
group.append(description, element('path', {
|
|
338
|
+
class: 'tw-script-relation-line',
|
|
339
|
+
d: path
|
|
340
|
+
}), element('path', {
|
|
341
|
+
class: 'tw-script-relation-head',
|
|
342
|
+
d: `M ${end.x - 9 * direction} ${end.y - 5} L ${end.x} ${end.y} L ${end.x - 9 * direction} ${end.y + 5}`
|
|
343
|
+
}));
|
|
344
|
+
if (waiting) {
|
|
345
|
+
const scale = Math.hypot(matrix.a, matrix.b) || 1;
|
|
346
|
+
const point = waitBadgePoint(path, 24 / scale);
|
|
347
|
+
badgePoint = { x: point.x + 16 / scale, y: point.y + 4 / scale };
|
|
348
|
+
const badge = element('g', { class: 'tw-script-relation-wait', 'aria-label': strings.wait,
|
|
349
|
+
transform: `translate(${point.x} ${point.y}) scale(${1 / scale})` });
|
|
350
|
+
const title = element('title');
|
|
351
|
+
title.textContent = strings.wait;
|
|
352
|
+
badge.append(title, element('rect', { x: '-10', y: '-10', width: '20', height: '20', rx: '4' }), element('path', { class: 'tw-script-relation-wait-glyph', d: relationWaitGlyph, transform: 'translate(-8 -8)' }));
|
|
353
|
+
group.append(badge);
|
|
354
|
+
}
|
|
355
|
+
if (count > 1) {
|
|
356
|
+
const badge = element('text', { x: String(badgePoint.x), y: String(badgePoint.y),
|
|
357
|
+
class: 'tw-script-relation-badge', 'text-anchor': waiting ? 'start' : 'middle' });
|
|
358
|
+
badge.textContent = `×${count}`;
|
|
359
|
+
group.append(badge);
|
|
360
|
+
}
|
|
361
|
+
drawingLayer.append(group);
|
|
362
|
+
}
|
|
363
|
+
destinations.hidden = !destinations.childElementCount;
|
|
364
|
+
}
|
|
365
|
+
function update() {
|
|
366
|
+
if (!disposed && frame === undefined)
|
|
367
|
+
frame = view.requestAnimationFrame(render);
|
|
368
|
+
}
|
|
369
|
+
function changed() { dirty = true; update(); }
|
|
370
|
+
function scriptOf(id, target = getTarget()) {
|
|
371
|
+
const blocks = target?.blocks._blocks;
|
|
372
|
+
const visited = new Set();
|
|
373
|
+
while (id && blocks?.[id]?.parent && !visited.has(id)) {
|
|
374
|
+
visited.add(id);
|
|
375
|
+
id = blocks[id].parent;
|
|
376
|
+
}
|
|
377
|
+
return id;
|
|
378
|
+
}
|
|
379
|
+
function selectedScript(event) {
|
|
380
|
+
if (event.target.closest?.('[data-script-relation-guide]'))
|
|
381
|
+
return;
|
|
382
|
+
const node = event.target.closest?.('[data-id]');
|
|
383
|
+
const id = scriptOf(node?.dataset.id);
|
|
384
|
+
if (id !== selected) {
|
|
385
|
+
selected = id;
|
|
386
|
+
update();
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
const mutations = new view.MutationObserver(records => {
|
|
390
|
+
if (records.some(record => record.target !== layer && !layer.contains(record.target) &&
|
|
391
|
+
!record.attributeName?.startsWith('data-script-relation-')))
|
|
392
|
+
update();
|
|
393
|
+
});
|
|
394
|
+
mutations.observe(canvas, { attributes: true, childList: true, subtree: true });
|
|
395
|
+
// Its CSS translation changes on every drag step, while the workspace canvas stays untouched.
|
|
396
|
+
const dragSurface = injection.querySelector('.blocklyBlockDragSurface');
|
|
397
|
+
if (dragSurface)
|
|
398
|
+
mutations.observe(dragSurface, { attributes: true, childList: true, subtree: true });
|
|
399
|
+
const resized = new view.ResizeObserver(update);
|
|
400
|
+
resized.observe(injection);
|
|
401
|
+
const toolbox = injection.querySelector('.blocklyToolboxDiv');
|
|
402
|
+
if (toolbox)
|
|
403
|
+
resized.observe(toolbox);
|
|
404
|
+
injection.addEventListener('pointerover', selectedScript);
|
|
405
|
+
injection.addEventListener('pointerdown', selectedScript);
|
|
406
|
+
const language = new view.MutationObserver(update);
|
|
407
|
+
language.observe(document.documentElement, { attributes: true, attributeFilter: ['lang'] });
|
|
408
|
+
vm?.runtime?.on?.('PROJECT_CHANGED', changed);
|
|
409
|
+
vm?.on?.('targetsUpdate', changed);
|
|
410
|
+
vm?.on?.('workspaceUpdate', changed);
|
|
411
|
+
workspace.addChangeListener?.(changed);
|
|
412
|
+
update();
|
|
413
|
+
return {
|
|
414
|
+
update: changed,
|
|
415
|
+
get enabled() { return enabled; },
|
|
416
|
+
setEnabled(value) { enabled = value; update(); },
|
|
417
|
+
setProjection(svg) {
|
|
418
|
+
if (svg === projection) {
|
|
419
|
+
update();
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
projectedLayer?.remove();
|
|
423
|
+
projection = svg;
|
|
424
|
+
projectedLayer = svg ? element('g', { 'data-script-relation-layer': '', 'aria-hidden': 'true', 'pointer-events': 'none' }) : undefined;
|
|
425
|
+
if (projectedLayer)
|
|
426
|
+
svg.prepend(projectedLayer);
|
|
427
|
+
update();
|
|
428
|
+
},
|
|
429
|
+
dispose() {
|
|
430
|
+
disposed = true;
|
|
431
|
+
markScripts(new Map());
|
|
432
|
+
if (frame !== undefined)
|
|
433
|
+
view.cancelAnimationFrame(frame);
|
|
434
|
+
mutations.disconnect();
|
|
435
|
+
resized.disconnect();
|
|
436
|
+
language.disconnect();
|
|
437
|
+
workspace.removeChangeListener?.(changed);
|
|
438
|
+
vm?.runtime?.removeListener?.('PROJECT_CHANGED', changed);
|
|
439
|
+
vm?.removeListener?.('targetsUpdate', changed);
|
|
440
|
+
vm?.removeListener?.('workspaceUpdate', changed);
|
|
441
|
+
injection.removeEventListener('pointerover', selectedScript);
|
|
442
|
+
injection.removeEventListener('pointerdown', selectedScript);
|
|
443
|
+
projectedLayer?.remove();
|
|
444
|
+
layer.remove();
|
|
445
|
+
destinations.remove();
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
}
|
|
@@ -2,6 +2,7 @@ import { type LayoutWorkspace } from './script-layout-workspace.ts';
|
|
|
2
2
|
import { type RearrangementContext } from './script-layout-dialog.ts';
|
|
3
3
|
import { type ScriptViewMenuBlocks, type ScriptViewMenuWorkspace } from './script-view-menu.ts';
|
|
4
4
|
import type { PresentationElements } from './preview-chrome.ts';
|
|
5
|
+
import { type ScriptRelationContext } from './script-arrows.ts';
|
|
5
6
|
export type ScriptView = 'free' | 'z' | 'n';
|
|
6
7
|
interface ViewOptions {
|
|
7
8
|
workspace: LayoutWorkspace & ScriptViewMenuWorkspace;
|
|
@@ -12,9 +13,17 @@ interface ViewOptions {
|
|
|
12
13
|
canShowBlock?(): boolean;
|
|
13
14
|
onChange?(view: ScriptView): void;
|
|
14
15
|
hasWorkspaceMenu?(): boolean;
|
|
16
|
+
relations?: ScriptRelationContext;
|
|
15
17
|
}
|
|
16
|
-
export declare function attachScriptView({ workspace, ScratchBlocks, getLocale, mount, initialView, canShowBlock, onChange, hasWorkspaceMenu }: ViewOptions): {
|
|
18
|
+
export declare function attachScriptView({ workspace, ScratchBlocks, getLocale, mount, initialView, canShowBlock, onChange, hasWorkspaceMenu, relations: relationContext }: ViewOptions): {
|
|
17
19
|
update: () => void;
|
|
20
|
+
relations: {
|
|
21
|
+
update: () => void;
|
|
22
|
+
readonly enabled: boolean;
|
|
23
|
+
setEnabled(value: boolean): void;
|
|
24
|
+
setProjection(svg: SVGSVGElement | null): void;
|
|
25
|
+
dispose(): void;
|
|
26
|
+
};
|
|
18
27
|
labels: () => void;
|
|
19
28
|
readonly view: ScriptView;
|
|
20
29
|
setView: (next: ScriptView) => void;
|
|
@@ -44,6 +53,13 @@ interface DockedScriptLayoutOptions {
|
|
|
44
53
|
export declare function getDockedScriptLayout(vm: object): {
|
|
45
54
|
view: {
|
|
46
55
|
update: () => void;
|
|
56
|
+
relations: {
|
|
57
|
+
update: () => void;
|
|
58
|
+
readonly enabled: boolean;
|
|
59
|
+
setEnabled(value: boolean): void;
|
|
60
|
+
setProjection(svg: SVGSVGElement | null): void;
|
|
61
|
+
dispose(): void;
|
|
62
|
+
};
|
|
47
63
|
labels: () => void;
|
|
48
64
|
readonly view: ScriptView;
|
|
49
65
|
setView: (next: ScriptView) => void;
|
|
@@ -56,6 +72,13 @@ export declare function getDockedScriptLayout(vm: object): {
|
|
|
56
72
|
export declare function attachDockedScriptLayout({ vm, workspace, ScratchBlocks, canUse, locale, presentationElements }: DockedScriptLayoutOptions): {
|
|
57
73
|
view: {
|
|
58
74
|
update: () => void;
|
|
75
|
+
relations: {
|
|
76
|
+
update: () => void;
|
|
77
|
+
readonly enabled: boolean;
|
|
78
|
+
setEnabled(value: boolean): void;
|
|
79
|
+
setProjection(svg: SVGSVGElement | null): void;
|
|
80
|
+
dispose(): void;
|
|
81
|
+
};
|
|
59
82
|
labels: () => void;
|
|
60
83
|
readonly view: ScriptView;
|
|
61
84
|
setView: (next: ScriptView) => void;
|
|
@@ -2,12 +2,13 @@ import { arrangeScripts } from "./script-layout.js";
|
|
|
2
2
|
import { paintScripts, readScripts } from "./script-layout-workspace.js";
|
|
3
3
|
import { openRearrangement } from "./script-layout-dialog.js";
|
|
4
4
|
import { attachScriptViewMenu } from "./script-view-menu.js";
|
|
5
|
+
import { attachScriptArrows } from "./script-arrows.js";
|
|
5
6
|
const choices = [
|
|
6
7
|
{ view: 'free', label: 'Normal layout view', jaLabel: '通常配置の見え方' },
|
|
7
8
|
{ view: 'z', label: 'Z layout view', jaLabel: 'Z配置の見え方' },
|
|
8
9
|
{ view: 'n', label: 'N layout view', jaLabel: 'N配置の見え方' }
|
|
9
10
|
];
|
|
10
|
-
export function attachScriptView({ workspace, ScratchBlocks, getLocale, mount, initialView = 'free', canShowBlock, onChange, hasWorkspaceMenu = () => true }) {
|
|
11
|
+
export function attachScriptView({ workspace, ScratchBlocks, getLocale, mount, initialView = 'free', canShowBlock, onChange, hasWorkspaceMenu = () => true, relations: relationContext }) {
|
|
11
12
|
const injection = workspace.getInjectionDiv();
|
|
12
13
|
const host = mount ?? injection;
|
|
13
14
|
const document = host.ownerDocument;
|
|
@@ -22,7 +23,11 @@ export function attachScriptView({ workspace, ScratchBlocks, getLocale, mount, i
|
|
|
22
23
|
let view = initialView;
|
|
23
24
|
let frame;
|
|
24
25
|
const svg = workspace.getParentSvg();
|
|
25
|
-
const observer = new window.MutationObserver(
|
|
26
|
+
const observer = new window.MutationObserver(records => {
|
|
27
|
+
if (records.some(record => !record.target.closest?.('[data-script-relation-layer]')))
|
|
28
|
+
update();
|
|
29
|
+
});
|
|
30
|
+
const relations = relationContext && attachScriptArrows({ workspace, getLocale, ...relationContext });
|
|
26
31
|
const japanese = () => /^ja(?:-|$)/i.test(getLocale());
|
|
27
32
|
function labels() {
|
|
28
33
|
const choice = choices.find(choice => choice.view === view);
|
|
@@ -56,6 +61,7 @@ export function attachScriptView({ workspace, ScratchBlocks, getLocale, mount, i
|
|
|
56
61
|
surface.style.setProperty('--tw-script-view-right', !horizontal && bounds.right >= right - 2 ? `${bounds.width}px` : '0px');
|
|
57
62
|
}
|
|
58
63
|
paintScripts(contents, arrangeScripts(readScripts(workspace), view, 'vertical'), workspace.scale || 1);
|
|
64
|
+
relations?.setProjection(contents.querySelector('svg'));
|
|
59
65
|
}
|
|
60
66
|
function update() {
|
|
61
67
|
if (view === 'free' || frame !== undefined)
|
|
@@ -69,8 +75,10 @@ export function attachScriptView({ workspace, ScratchBlocks, getLocale, mount, i
|
|
|
69
75
|
surface.hidden = view === 'free';
|
|
70
76
|
svg.toggleAttribute('data-script-serial', view !== 'free');
|
|
71
77
|
labels();
|
|
72
|
-
if (view === 'free')
|
|
78
|
+
if (view === 'free') {
|
|
79
|
+
relations?.setProjection(null);
|
|
73
80
|
contents.replaceChildren();
|
|
81
|
+
}
|
|
74
82
|
else {
|
|
75
83
|
observer.observe(workspace.getCanvas(), { attributes: true, childList: true, subtree: true });
|
|
76
84
|
surface.scrollTo(0, 0);
|
|
@@ -102,6 +110,7 @@ export function attachScriptView({ workspace, ScratchBlocks, getLocale, mount, i
|
|
|
102
110
|
setView(initialView);
|
|
103
111
|
return {
|
|
104
112
|
update,
|
|
113
|
+
relations,
|
|
105
114
|
labels,
|
|
106
115
|
get view() { return view; },
|
|
107
116
|
setView,
|
|
@@ -112,6 +121,7 @@ export function attachScriptView({ workspace, ScratchBlocks, getLocale, mount, i
|
|
|
112
121
|
if (frame !== undefined)
|
|
113
122
|
window.cancelAnimationFrame(frame);
|
|
114
123
|
menu.dispose();
|
|
124
|
+
relations?.dispose();
|
|
115
125
|
surface.remove();
|
|
116
126
|
svg.removeAttribute('data-script-serial');
|
|
117
127
|
}
|
|
@@ -137,7 +147,8 @@ export function attachDockedScriptLayout({ vm, workspace, ScratchBlocks, canUse,
|
|
|
137
147
|
(!group || group.classList.contains('dv-active-group')));
|
|
138
148
|
};
|
|
139
149
|
const view = attachScriptView({ workspace, ScratchBlocks, getLocale: () => injection.ownerDocument.documentElement.lang || locale,
|
|
140
|
-
canShowBlock: canArrange
|
|
150
|
+
canShowBlock: canArrange, relations: { vm: vm,
|
|
151
|
+
getTarget: () => vm.editingTarget } });
|
|
141
152
|
host.setAttribute('data-code-hover-surface', '');
|
|
142
153
|
let dialog;
|
|
143
154
|
const attachment = {
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** The serialized program is the source of relations, even when a view omits a folded body. */
|
|
2
|
+
export interface RelationBlock {
|
|
3
|
+
id: string;
|
|
4
|
+
opcode: string;
|
|
5
|
+
parent?: string | null;
|
|
6
|
+
next?: string | null;
|
|
7
|
+
inputs?: Record<string, {
|
|
8
|
+
block?: string | null;
|
|
9
|
+
shadow?: string | null;
|
|
10
|
+
}>;
|
|
11
|
+
fields?: Record<string, {
|
|
12
|
+
value?: unknown;
|
|
13
|
+
id?: string;
|
|
14
|
+
}>;
|
|
15
|
+
mutation?: Record<string, unknown>;
|
|
16
|
+
topLevel?: boolean;
|
|
17
|
+
shadow?: boolean;
|
|
18
|
+
x?: number;
|
|
19
|
+
y?: number;
|
|
20
|
+
}
|
|
21
|
+
export interface RelationTarget {
|
|
22
|
+
id: string;
|
|
23
|
+
isOriginal?: boolean;
|
|
24
|
+
isStage?: boolean;
|
|
25
|
+
getName?(): string;
|
|
26
|
+
blocks: {
|
|
27
|
+
_blocks: Record<string, RelationBlock>;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export interface ScriptEndpoint {
|
|
31
|
+
targetId: string;
|
|
32
|
+
blockId: string;
|
|
33
|
+
scriptId: string;
|
|
34
|
+
}
|
|
35
|
+
export interface ScriptRelation {
|
|
36
|
+
kind: 'function' | 'message';
|
|
37
|
+
source: ScriptEndpoint;
|
|
38
|
+
destination?: ScriptEndpoint;
|
|
39
|
+
label: string;
|
|
40
|
+
wait: boolean;
|
|
41
|
+
unresolved?: 'dynamic' | 'missing' | 'ambiguous';
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Extract possible static calls without executing the project or modifying its blocks.
|
|
45
|
+
* Broadcasts fan out; conflicting function definitions and computed names stay unresolved.
|
|
46
|
+
*/
|
|
47
|
+
export declare function readScriptRelations(targets: readonly RelationTarget[]): ScriptRelation[];
|