@kubohiroya/turbowarp-plus-editor-code-views 0.20.0 → 0.21.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 +8 -0
- package/dist/code-pane.js +49 -5
- package/dist/code-zoom.d.ts +3 -0
- package/dist/code-zoom.js +30 -0
- package/dist/script-arrows.d.ts +11 -1
- package/dist/script-arrows.js +268 -100
- package/dist/script-layout-view.d.ts +18 -1
- package/dist/script-layout-view.js +13 -1
- package/dist/sprite-code-windows.d.ts +1 -0
- package/dist/sprite-code-windows.js +1 -0
- package/dist/workspace-overview.d.ts +16 -0
- package/dist/workspace-overview.js +221 -0
- package/dist/workspace.d.ts +3 -0
- package/dist/workspace.js +4 -2
- package/package.json +1 -1
- package/src/code-pane.ts +45 -5
- package/src/code-zoom.ts +28 -0
- package/src/script-arrows.ts +244 -98
- package/src/script-layout-view.ts +16 -2
- package/src/sprite-code-windows.tsx +1 -0
- package/src/styles.css +41 -1
- package/src/workspace-overview.ts +197 -0
- package/src/workspace.ts +6 -3
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
type OverviewOptions = {
|
|
2
|
+
workspace: any;
|
|
3
|
+
ScratchBlocks: any;
|
|
4
|
+
zoomHeight?: number;
|
|
5
|
+
getCorner?(metrics: any): {right: number; bottom: number};
|
|
6
|
+
watermark?: HTMLElement;
|
|
7
|
+
getWatermark?(): HTMLElement | undefined | null;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const svgNamespace = 'http://www.w3.org/2000/svg';
|
|
11
|
+
|
|
12
|
+
/** A cached overview drawn from public block geometry; offscreen culling never changes the project map. */
|
|
13
|
+
export function attachWorkspaceOverview({workspace, ScratchBlocks, zoomHeight = 0, getCorner,
|
|
14
|
+
watermark, getWatermark}: OverviewOptions) {
|
|
15
|
+
const container = workspace.getInjectionDiv() as HTMLElement;
|
|
16
|
+
const document = container.ownerDocument;
|
|
17
|
+
const view = document.defaultView;
|
|
18
|
+
const canvas = workspace.getCanvas() as SVGElement;
|
|
19
|
+
const element = <K extends keyof SVGElementTagNameMap>(tag: K) => document.createElementNS(svgNamespace, tag);
|
|
20
|
+
const map = element('svg');
|
|
21
|
+
map.dataset.workspaceMinimap = '';
|
|
22
|
+
map.classList.add('tw-workspace-overview');
|
|
23
|
+
map.setAttribute('aria-hidden', 'true');
|
|
24
|
+
const shapes = element('g');
|
|
25
|
+
const clip = element('svg');
|
|
26
|
+
clip.setAttribute('overflow', 'hidden');
|
|
27
|
+
const viewport = element('rect');
|
|
28
|
+
viewport.classList.add('tw-workspace-overview-viewport');
|
|
29
|
+
clip.append(viewport);
|
|
30
|
+
map.append(shapes, clip);
|
|
31
|
+
container.append(map);
|
|
32
|
+
const paths = new Map<string, SVGPathElement>();
|
|
33
|
+
let needsShapes = true;
|
|
34
|
+
let frame: number | undefined;
|
|
35
|
+
let disposed = false;
|
|
36
|
+
const attribute = (node: Element, name: string, value: string | number) => {
|
|
37
|
+
const text = String(value);
|
|
38
|
+
if (node.getAttribute(name) !== text) node.setAttribute(name, text);
|
|
39
|
+
};
|
|
40
|
+
const style = (node: HTMLElement | SVGElement, name: string, value: string) => {
|
|
41
|
+
if (node.style.getPropertyValue(name) !== value) node.style.setProperty(name, value);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
function refreshShapes() {
|
|
45
|
+
const retained = new Set<string>();
|
|
46
|
+
const additions = document.createDocumentFragment();
|
|
47
|
+
for (const block of workspace.getAllBlocks(false)) {
|
|
48
|
+
const root = block.getSvgRoot() as SVGElement | null;
|
|
49
|
+
if (!root || !canvas.contains(root) || root.closest('[data-tw-plus-hidden]')) continue;
|
|
50
|
+
const source = root.querySelector(':scope > .blocklyPath');
|
|
51
|
+
const data = source?.getAttribute('d');
|
|
52
|
+
if (!data) continue;
|
|
53
|
+
retained.add(block.id);
|
|
54
|
+
let path = paths.get(block.id);
|
|
55
|
+
if (!path) {
|
|
56
|
+
path = element('path');
|
|
57
|
+
paths.set(block.id, path);
|
|
58
|
+
additions.append(path);
|
|
59
|
+
}
|
|
60
|
+
const position = block.getRelativeToSurfaceXY();
|
|
61
|
+
attribute(path, 'd', data);
|
|
62
|
+
attribute(path, 'fill', source.getAttribute('fill') || block.getColour());
|
|
63
|
+
attribute(path, 'transform', `translate(${position.x} ${position.y})${block.RTL ? ' scale(-1 1)' : ''}`);
|
|
64
|
+
}
|
|
65
|
+
for (const [id, path] of paths) if (!retained.has(id)) {path.remove(); paths.delete(id);}
|
|
66
|
+
if (additions.childNodes.length) shapes.append(additions);
|
|
67
|
+
needsShapes = false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function render() {
|
|
71
|
+
frame = undefined;
|
|
72
|
+
if (disposed) return;
|
|
73
|
+
const metrics = workspace.isVisible() && workspace.getMetrics();
|
|
74
|
+
if (!metrics || metrics.viewWidth <= 32 || metrics.viewHeight <= 32) {map.toggleAttribute('hidden', true); return;}
|
|
75
|
+
const scale = workspace.scale;
|
|
76
|
+
const x = metrics.viewLeft / scale, y = metrics.viewTop / scale;
|
|
77
|
+
const width = metrics.viewWidth / scale, height = metrics.viewHeight / scale;
|
|
78
|
+
const content = metrics.contentWidth > 0 && metrics.contentHeight > 0;
|
|
79
|
+
const area = content ? {x: metrics.contentLeft / scale, y: metrics.contentTop / scale,
|
|
80
|
+
width: metrics.contentWidth / scale, height: metrics.contentHeight / scale} : {x, y, width, height};
|
|
81
|
+
if (!workspace.scrollbar && content) {
|
|
82
|
+
const right = Math.max(x + width, area.x + area.width), bottom = Math.max(y + height, area.y + area.height);
|
|
83
|
+
area.x = Math.min(x, area.x); area.y = Math.min(y, area.y);
|
|
84
|
+
area.width = right - area.x; area.height = bottom - area.y;
|
|
85
|
+
}
|
|
86
|
+
const corner = getCorner?.(metrics);
|
|
87
|
+
if (!watermark?.isConnected) watermark = getWatermark?.() ?? undefined;
|
|
88
|
+
const mapWidth = Math.min(164, metrics.viewWidth * .4) * .8;
|
|
89
|
+
let mapHeight = Math.min(104, metrics.viewHeight * .35) * .8;
|
|
90
|
+
const spacing = zoomHeight ? zoomHeight + 8 : 0;
|
|
91
|
+
if (watermark && corner) mapHeight = Math.min(mapHeight, Math.max(0, corner.bottom - spacing - 8) * .6);
|
|
92
|
+
const right = corner ? container.clientWidth - corner.right : undefined;
|
|
93
|
+
const bottom = corner ? container.clientHeight - corner.bottom + spacing :
|
|
94
|
+
(metrics.toolboxPosition === ScratchBlocks.TOOLBOX_AT_BOTTOM ? metrics.toolboxHeight : 0) + 16;
|
|
95
|
+
const borderX = container.offsetWidth - container.clientWidth - container.clientLeft;
|
|
96
|
+
const borderY = container.offsetHeight - container.clientHeight - container.clientTop;
|
|
97
|
+
|
|
98
|
+
// All layout reads above precede writes. Identical values do not invalidate the editor's styles.
|
|
99
|
+
if (needsShapes) refreshShapes();
|
|
100
|
+
map.toggleAttribute('hidden', false);
|
|
101
|
+
const box = `${area.x} ${area.y} ${area.width} ${area.height}`;
|
|
102
|
+
attribute(map, 'viewBox', box); attribute(clip, 'viewBox', box);
|
|
103
|
+
for (const [name, value] of Object.entries(area)) attribute(clip, name, value);
|
|
104
|
+
for (const [name, value] of Object.entries({x, y, width, height})) attribute(viewport, name, value);
|
|
105
|
+
style(map, 'width', `${mapWidth}px`); style(map, 'height', `${mapHeight}px`);
|
|
106
|
+
style(map, 'bottom', `${bottom}px`);
|
|
107
|
+
if (right !== undefined) style(map, 'right', `${right}px`);
|
|
108
|
+
else style(map, 'left', `${metrics.absoluteLeft + 12}px`);
|
|
109
|
+
if (watermark && corner) {
|
|
110
|
+
if (!watermark.classList.contains('tw-workspace-overview-watermark')) watermark.classList.add('tw-workspace-overview-watermark');
|
|
111
|
+
if (!watermark.hasAttribute('data-plus-workspace-watermark')) watermark.dataset.plusWorkspaceWatermark = '';
|
|
112
|
+
style(watermark, '--plus-watermark-right', `${right + borderX}px`);
|
|
113
|
+
style(watermark, '--plus-watermark-bottom', `${bottom + mapHeight + 8 + borderY}px`);
|
|
114
|
+
style(watermark, '--plus-watermark-size', `${Math.max(0, Math.min(64, corner.bottom - spacing - mapHeight - 8))}px`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const schedule = () => {
|
|
118
|
+
if (!disposed && frame === undefined) frame = view.requestAnimationFrame(render);
|
|
119
|
+
};
|
|
120
|
+
const changes = new view.MutationObserver(records => {
|
|
121
|
+
needsShapes ||= records.some(record => record.type === 'childList' || record.target !== canvas);
|
|
122
|
+
schedule();
|
|
123
|
+
});
|
|
124
|
+
// Native intersection culling only writes display styles. Those blocks still belong in the overview.
|
|
125
|
+
changes.observe(canvas, {subtree: true, childList: true,
|
|
126
|
+
attributeFilter: ['transform', 'd', 'fill', 'data-tw-plus-hidden']});
|
|
127
|
+
const surface = container.querySelector('.blocklyWsDragSurface');
|
|
128
|
+
const scrolling = new view.MutationObserver(schedule);
|
|
129
|
+
if (surface) scrolling.observe(surface, {attributes: true});
|
|
130
|
+
const resize = new view.ResizeObserver(schedule);
|
|
131
|
+
resize.observe(container);
|
|
132
|
+
const events = new view.AbortController();
|
|
133
|
+
view.addEventListener('resize', schedule, {signal: events.signal});
|
|
134
|
+
view.addEventListener('scroll', schedule, {signal: events.signal, passive: true});
|
|
135
|
+
|
|
136
|
+
const moveView = (left: number, top: number) => {
|
|
137
|
+
const metrics = workspace.getMetrics();
|
|
138
|
+
if (!metrics || !workspace.scrollbar) return;
|
|
139
|
+
const clamp = (value: number, maximum: number) => Math.max(0, Math.min(value, Math.max(0, maximum)));
|
|
140
|
+
workspace.scrollbar.set(clamp(left * workspace.scale - metrics.contentLeft, metrics.contentWidth - metrics.viewWidth),
|
|
141
|
+
clamp(top * workspace.scale - metrics.contentTop, metrics.contentHeight - metrics.viewHeight));
|
|
142
|
+
};
|
|
143
|
+
type Pointer = {id: number; x: number; y: number; start: DOMPoint; inverse: DOMMatrix;
|
|
144
|
+
left: number; top: number; viewport: boolean; moved: boolean};
|
|
145
|
+
let pointer: Pointer | undefined;
|
|
146
|
+
const point = (event: PointerEvent, inverse: DOMMatrix) =>
|
|
147
|
+
new view.DOMPoint(event.clientX, event.clientY).matrixTransform(inverse);
|
|
148
|
+
map.addEventListener('pointerdown', event => {
|
|
149
|
+
if (!event.isPrimary || event.button !== 0 || pointer || !workspace.scrollbar) return;
|
|
150
|
+
const matrix = map.getScreenCTM(), metrics = workspace.getMetrics();
|
|
151
|
+
if (!matrix || !metrics) return;
|
|
152
|
+
const inverse = matrix.inverse();
|
|
153
|
+
pointer = {id: event.pointerId, x: event.clientX, y: event.clientY, start: point(event, inverse), inverse,
|
|
154
|
+
left: metrics.viewLeft / workspace.scale, top: metrics.viewTop / workspace.scale,
|
|
155
|
+
viewport: event.target === viewport, moved: false};
|
|
156
|
+
map.setPointerCapture(event.pointerId);
|
|
157
|
+
}, {signal: events.signal});
|
|
158
|
+
const move = (event: PointerEvent) => {
|
|
159
|
+
if (pointer?.id !== event.pointerId) return;
|
|
160
|
+
if (!pointer.moved && Math.hypot(event.clientX - pointer.x, event.clientY - pointer.y) >= 3) {
|
|
161
|
+
pointer.moved = true;
|
|
162
|
+
if (pointer.viewport) {map.dataset.dragging = ''; ScratchBlocks.hideChaff(true);}
|
|
163
|
+
}
|
|
164
|
+
if (pointer.moved && pointer.viewport) {
|
|
165
|
+
const current = point(event, pointer.inverse);
|
|
166
|
+
moveView(pointer.left + current.x - pointer.start.x, pointer.top + current.y - pointer.start.y);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
map.addEventListener('pointermove', move, {signal: events.signal});
|
|
170
|
+
const finish = (event: PointerEvent) => {
|
|
171
|
+
if (pointer?.id !== event.pointerId) return;
|
|
172
|
+
if (event.type === 'pointerup') {
|
|
173
|
+
move(event);
|
|
174
|
+
if (!pointer.moved) {
|
|
175
|
+
const current = point(event, pointer.inverse), metrics = workspace.getMetrics();
|
|
176
|
+
if (metrics) moveView(current.x - metrics.viewWidth / (2 * workspace.scale),
|
|
177
|
+
current.y - metrics.viewHeight / (2 * workspace.scale));
|
|
178
|
+
ScratchBlocks.hideChaff(true);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
pointer = undefined;
|
|
182
|
+
delete map.dataset.dragging;
|
|
183
|
+
};
|
|
184
|
+
for (const type of ['pointerup', 'pointercancel', 'lostpointercapture']) map.addEventListener(type, finish, {signal: events.signal});
|
|
185
|
+
schedule();
|
|
186
|
+
return {dispose() {
|
|
187
|
+
disposed = true;
|
|
188
|
+
events.abort(); changes.disconnect(); scrolling.disconnect(); resize.disconnect();
|
|
189
|
+
if (frame !== undefined) view.cancelAnimationFrame(frame);
|
|
190
|
+
map.remove(); paths.clear();
|
|
191
|
+
if (watermark) {
|
|
192
|
+
watermark.classList.remove('tw-workspace-overview-watermark');
|
|
193
|
+
delete watermark.dataset.plusWorkspaceWatermark;
|
|
194
|
+
for (const name of ['right', 'bottom', 'size']) watermark.style.removeProperty(`--plus-watermark-${name}`);
|
|
195
|
+
}
|
|
196
|
+
}};
|
|
197
|
+
}
|
package/src/workspace.ts
CHANGED
|
@@ -5,8 +5,8 @@ import {isFoldedDefinition, removeObsoleteProcedureInputs, setFoldedDefinition}
|
|
|
5
5
|
import {decodeEdit, encodeEdit, isCommentEdit} from './protocol.ts';
|
|
6
6
|
import {captureCodePreviewChrome} from './preview-chrome.ts';
|
|
7
7
|
import type {PresentationElements} from './preview-chrome.ts';
|
|
8
|
-
import {attachScriptView, getDockedScriptLayout, type ScriptView} from './script-layout-view.ts';
|
|
9
|
-
import {attachScriptArrows, registerScriptRelationNavigation} from './script-arrows.ts';
|
|
8
|
+
import {attachScriptView, getDockedScriptLayout, getDockedRelationVisibility, type ScriptView} from './script-layout-view.ts';
|
|
9
|
+
import {attachScriptArrows, registerScriptRelationNavigation, type ScriptRelationVisibility} from './script-arrows.ts';
|
|
10
10
|
import type {RelationBlock, ScriptEndpoint} from './script-relations.ts';
|
|
11
11
|
import {fitScriptInWorkspace} from './script-fit.ts';
|
|
12
12
|
|
|
@@ -21,6 +21,7 @@ export interface Presentation {
|
|
|
21
21
|
y: number;
|
|
22
22
|
visible: boolean;
|
|
23
23
|
scriptView?: ScriptView;
|
|
24
|
+
relationVisibility?: ScriptRelationVisibility;
|
|
24
25
|
/** Missing for a presentation read back from a saved workspace; the preview then draws without it. */
|
|
25
26
|
chrome?: ReturnType<typeof captureCodePreviewChrome>;
|
|
26
27
|
}
|
|
@@ -447,6 +448,7 @@ export function attachCodeWorkspace({vm, ScratchBlocks: blocks, workspace, optio
|
|
|
447
448
|
y: workspace.scrollY + metrics.absoluteTop,
|
|
448
449
|
visible,
|
|
449
450
|
scriptView: getDockedScriptLayout(vm)?.view.view,
|
|
451
|
+
relationVisibility: getDockedRelationVisibility(vm),
|
|
450
452
|
chrome: captureCodePreviewChrome(workspace, loadedTarget, presentationElements, includeNavigation)
|
|
451
453
|
};
|
|
452
454
|
}
|
|
@@ -587,7 +589,7 @@ export function attachCodeWorkspace({vm, ScratchBlocks: blocks, workspace, optio
|
|
|
587
589
|
const minimap = attachMinimap?.(preview, blocks, watermark);
|
|
588
590
|
const scriptView = attachScriptView({workspace: preview, ScratchBlocks: blocks, mount: chrome.parentElement,
|
|
589
591
|
getLocale: () => locale ?? workspace.getInjectionDiv().ownerDocument.documentElement.lang,
|
|
590
|
-
relations: {vm, getTarget: () => target},
|
|
592
|
+
relations: {vm, getTarget: () => target, initialVisibility: view.relationVisibility ?? presentation?.relationVisibility},
|
|
591
593
|
initialView: view.scriptView ?? presentation?.scriptView,
|
|
592
594
|
onChange: next => {
|
|
593
595
|
if (presentation) presentation.scriptView = next;
|
|
@@ -624,6 +626,7 @@ export function attachCodeWorkspace({vm, ScratchBlocks: blocks, workspace, optio
|
|
|
624
626
|
observer.observe(element);
|
|
625
627
|
return {
|
|
626
628
|
workspace: preview,
|
|
629
|
+
setRelationVisibility: (visibility: ScriptRelationVisibility) => scriptView.relations?.setVisibility(visibility),
|
|
627
630
|
update() {
|
|
628
631
|
if (vm.runtime.getTargetById(target.id) !== target) return;
|
|
629
632
|
// A view that shows one script previews only that script.
|