@kubohiroya/turbowarp-plus-editor-code-views 0.0.0-stage → 0.2.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/LICENSE +373 -0
- package/README.ja.md +42 -0
- package/README.md +41 -2
- package/dist/asset-edit-session.d.ts +24 -0
- package/dist/asset-edit-session.js +58 -0
- package/dist/block-argument-text.d.ts +39 -0
- package/dist/block-argument-text.js +170 -0
- package/dist/block-drag.d.ts +17 -0
- package/dist/block-drag.js +184 -0
- package/dist/code-extensions.d.ts +38 -0
- package/dist/code-extensions.js +418 -0
- package/dist/code-pane.d.ts +80 -0
- package/dist/code-pane.js +398 -0
- package/dist/custom-extension-fields.d.ts +45 -0
- package/dist/custom-extension-fields.js +144 -0
- package/dist/detached-block-palette-view.d.ts +10 -0
- package/dist/detached-block-palette-view.js +62 -0
- package/dist/detached-block-palette.d.ts +41 -0
- package/dist/detached-block-palette.js +197 -0
- package/dist/dockview-unsaved.d.ts +45 -0
- package/dist/dockview-unsaved.js +123 -0
- package/dist/history.d.ts +57 -0
- package/dist/history.js +85 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +729 -0
- package/dist/native-extension-field.d.ts +26 -0
- package/dist/native-extension-field.js +204 -0
- package/dist/native-field-svg.d.ts +20 -0
- package/dist/native-field-svg.js +156 -0
- package/dist/palette-category-reorder.d.ts +4 -0
- package/dist/palette-category-reorder.js +167 -0
- package/dist/palette-category-search.d.ts +26 -0
- package/dist/palette-category-search.js +74 -0
- package/dist/palette-dropdown-resize.d.ts +19 -0
- package/dist/palette-dropdown-resize.js +135 -0
- package/dist/parent-field-layout.d.ts +2 -0
- package/dist/parent-field-layout.js +90 -0
- package/dist/parent-field-mirror.d.ts +3 -0
- package/dist/parent-field-mirror.js +290 -0
- package/dist/parent-field-resources.d.ts +9 -0
- package/dist/parent-field-resources.js +199 -0
- package/dist/preview-chrome.d.ts +9 -0
- package/dist/preview-chrome.js +78 -0
- package/dist/procedure-editor.d.ts +9 -0
- package/dist/procedure-editor.js +122 -0
- package/dist/procedure-edits.d.ts +16 -0
- package/dist/procedure-edits.js +47 -0
- package/dist/programmatic-extension-field.d.ts +20 -0
- package/dist/programmatic-extension-field.js +69 -0
- package/dist/protocol.d.ts +15 -0
- package/dist/protocol.js +36 -0
- package/dist/script-layout-dialog.d.ts +10 -0
- package/dist/script-layout-dialog.js +93 -0
- package/dist/script-layout-view.d.ts +68 -0
- package/dist/script-layout-view.js +164 -0
- package/dist/script-layout-workspace.d.ts +45 -0
- package/dist/script-layout-workspace.js +55 -0
- package/dist/script-layout.d.ts +17 -0
- package/dist/script-layout.js +25 -0
- package/dist/script-view-menu.d.ts +46 -0
- package/dist/script-view-menu.js +161 -0
- package/dist/sprite-code-windows.d.ts +32 -0
- package/dist/sprite-code-windows.js +180 -0
- package/dist/stage-code-prewarm.d.ts +52 -0
- package/dist/stage-code-prewarm.js +216 -0
- package/dist/target-combobox.d.ts +10 -0
- package/dist/target-combobox.js +92 -0
- package/dist/variable-references.d.ts +1 -0
- package/dist/variable-references.js +87 -0
- package/dist/workspace.d.ts +104 -0
- package/dist/workspace.js +1102 -0
- package/package.json +100 -4
- package/src/asset-edit-session.ts +72 -0
- package/src/block-argument-text.ts +172 -0
- package/src/block-drag.ts +190 -0
- package/src/code-extensions.ts +394 -0
- package/src/code-pane.ts +393 -0
- package/src/custom-extension-fields.ts +154 -0
- package/src/detached-block-palette-view.tsx +97 -0
- package/src/detached-block-palette.ts +218 -0
- package/src/dockview-unsaved.ts +134 -0
- package/src/history.ts +114 -0
- package/src/index.ts +680 -0
- package/src/native-extension-field.ts +201 -0
- package/src/native-field-svg.ts +140 -0
- package/src/palette-category-reorder.ts +155 -0
- package/src/palette-category-search.ts +82 -0
- package/src/palette-dropdown-resize.ts +132 -0
- package/src/parent-field-layout.ts +88 -0
- package/src/parent-field-mirror.ts +260 -0
- package/src/parent-field-resources.ts +179 -0
- package/src/preview-chrome.ts +82 -0
- package/src/procedure-editor.ts +123 -0
- package/src/procedure-edits.ts +46 -0
- package/src/programmatic-extension-field.ts +76 -0
- package/src/protocol.ts +47 -0
- package/src/script-layout-dialog.ts +104 -0
- package/src/script-layout-view.ts +198 -0
- package/src/script-layout-workspace.ts +95 -0
- package/src/script-layout.ts +40 -0
- package/src/script-view-menu.ts +213 -0
- package/src/sprite-code-windows.tsx +183 -0
- package/src/stage-code-prewarm.ts +242 -0
- package/src/styles.css +595 -0
- package/src/target-combobox.tsx +106 -0
- package/src/variable-references.ts +85 -0
- package/src/workspace.ts +1125 -0
- package/tsconfig.json +23 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import ReactDOM from 'react-dom';
|
|
3
|
+
import Palette from './detached-block-palette-view.tsx';
|
|
4
|
+
|
|
5
|
+
type Point = {x: number; y: number};
|
|
6
|
+
type PaletteCategory = {id: string; label: string; color?: string; image?: string};
|
|
7
|
+
export type PaletteItem = {
|
|
8
|
+
label: string;
|
|
9
|
+
preview?: SVGSVGElement;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
insert?(point?: Point): void;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type PaletteSource = {
|
|
15
|
+
surface: SVGSVGElement;
|
|
16
|
+
categories(): PaletteCategory[];
|
|
17
|
+
read(id: string): PaletteItem[];
|
|
18
|
+
subscribe(refresh: () => void): () => void;
|
|
19
|
+
setDetached(detached: boolean): void;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
type PaletteWindowOptions = {
|
|
23
|
+
locale: string;
|
|
24
|
+
categoryIcons: Readonly<Record<string, string>>;
|
|
25
|
+
decorate(document: Document): () => void;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const sources = new WeakMap<object, PaletteSource>();
|
|
29
|
+
const opened = new WeakMap<PaletteSource, {popup: Window; close(): void}>();
|
|
30
|
+
const dragType = 'application/x-tw-plus-palette-block';
|
|
31
|
+
|
|
32
|
+
export function openDetachedBlockPalette(workspace: object, {locale, categoryIcons, decorate}: PaletteWindowOptions) {
|
|
33
|
+
const source = sources.get(workspace);
|
|
34
|
+
const owner = source?.surface.ownerDocument.defaultView;
|
|
35
|
+
if (!owner) return;
|
|
36
|
+
const existing = opened.get(source);
|
|
37
|
+
if (existing && !existing.popup.closed) {
|
|
38
|
+
existing.popup.focus();
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const popup = owner.open('about:blank', '', 'popup,width=720,height=440');
|
|
42
|
+
if (!popup) return;
|
|
43
|
+
const doc = popup.document;
|
|
44
|
+
doc.title = /^ja(?:-|$)/i.test(locale) ? 'ブロックパレット' : 'Block palette';
|
|
45
|
+
const undecorate = decorate(doc);
|
|
46
|
+
const mount = doc.createElement('div');
|
|
47
|
+
doc.body.append(mount);
|
|
48
|
+
const events = new AbortController();
|
|
49
|
+
let drag: PaletteItem | undefined;
|
|
50
|
+
|
|
51
|
+
const close = () => {
|
|
52
|
+
if (events.signal.aborted) return;
|
|
53
|
+
events.abort();
|
|
54
|
+
drag = undefined;
|
|
55
|
+
opened.delete(source);
|
|
56
|
+
ReactDOM.unmountComponentAtNode(mount);
|
|
57
|
+
source.setDetached(false);
|
|
58
|
+
undecorate();
|
|
59
|
+
if (!popup.closed) popup.close();
|
|
60
|
+
};
|
|
61
|
+
opened.set(source, {popup, close});
|
|
62
|
+
popup.addEventListener('pagehide', close, {signal: events.signal});
|
|
63
|
+
owner.addEventListener('pagehide', close, {signal: events.signal});
|
|
64
|
+
source.surface.addEventListener('dragover', event => {
|
|
65
|
+
if (!drag || !event.dataTransfer?.types.includes(dragType)) return;
|
|
66
|
+
event.preventDefault();
|
|
67
|
+
event.dataTransfer.dropEffect = 'copy';
|
|
68
|
+
}, {signal: events.signal});
|
|
69
|
+
source.surface.addEventListener('drop', event => {
|
|
70
|
+
if (!drag || !event.dataTransfer?.getData(dragType)) return;
|
|
71
|
+
event.preventDefault();
|
|
72
|
+
drag.insert({x: event.clientX, y: event.clientY});
|
|
73
|
+
drag = undefined;
|
|
74
|
+
}, {signal: events.signal});
|
|
75
|
+
try {
|
|
76
|
+
source.setDetached(true);
|
|
77
|
+
ReactDOM.render(React.createElement(Palette, {
|
|
78
|
+
source, locale, categoryIcons, close,
|
|
79
|
+
startDrag(item, data) {
|
|
80
|
+
drag = item;
|
|
81
|
+
data.effectAllowed = 'copy';
|
|
82
|
+
data.setData(dragType, 'block');
|
|
83
|
+
},
|
|
84
|
+
endDrag() { drag = undefined; }
|
|
85
|
+
}), mount);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
close();
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function registerDetachedBlockPalette(workspace: object, source: PaletteSource) {
|
|
93
|
+
sources.set(workspace, source);
|
|
94
|
+
return () => {
|
|
95
|
+
opened.get(source)?.close();
|
|
96
|
+
sources.delete(workspace);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function connectDetachedBlockPalette({blocks, workspace, flyout, menu, categories, show, setDetached}: {
|
|
101
|
+
blocks: any;
|
|
102
|
+
workspace: any;
|
|
103
|
+
flyout: any;
|
|
104
|
+
menu: HTMLElement;
|
|
105
|
+
categories(): PaletteCategory[];
|
|
106
|
+
show(id: string): void;
|
|
107
|
+
setDetached(detached: boolean): void;
|
|
108
|
+
}) {
|
|
109
|
+
const surface: SVGSVGElement = workspace.getParentSvg();
|
|
110
|
+
const ns = 'http://www.w3.org/2000/svg';
|
|
111
|
+
const win = surface.ownerDocument.defaultView;
|
|
112
|
+
const placement = (point?: Point) => {
|
|
113
|
+
const bounds = surface.getBoundingClientRect();
|
|
114
|
+
const matrix = workspace.getCanvas().getScreenCTM();
|
|
115
|
+
if (!matrix || !bounds.width || !bounds.height) return;
|
|
116
|
+
const position = surface.createSVGPoint();
|
|
117
|
+
position.x = point?.x ?? bounds.left + bounds.width / 2;
|
|
118
|
+
position.y = point?.y ?? bounds.top + bounds.height / 2;
|
|
119
|
+
return position.matrixTransform(matrix.inverse());
|
|
120
|
+
};
|
|
121
|
+
return registerDetachedBlockPalette(workspace, {
|
|
122
|
+
surface, categories, setDetached,
|
|
123
|
+
read(id) {
|
|
124
|
+
show(id);
|
|
125
|
+
const items: PaletteItem[] = [];
|
|
126
|
+
for (const button of flyout.buttons_) {
|
|
127
|
+
if (button.getIsCategoryLabel()) continue;
|
|
128
|
+
const callback = button.callback_;
|
|
129
|
+
items.push({label: blocks.utils.replaceMessageReferences(button.getText()), ...(callback && {insert() {
|
|
130
|
+
if (!placement()) return;
|
|
131
|
+
surface.ownerDocument.defaultView.focus();
|
|
132
|
+
callback(button);
|
|
133
|
+
}})});
|
|
134
|
+
}
|
|
135
|
+
for (const block of flyout.workspace_.getTopBlocks(true)) {
|
|
136
|
+
const original: SVGGElement = block.getSvgRoot();
|
|
137
|
+
const {x, y, width, height} = original.getBBox();
|
|
138
|
+
if (!width || !height) continue;
|
|
139
|
+
const preview = original.ownerDocument.createElementNS(ns, 'svg');
|
|
140
|
+
preview.setAttribute('viewBox', [x, y, width, height].join(' '));
|
|
141
|
+
preview.setAttribute('width', String(width));
|
|
142
|
+
preview.setAttribute('height', String(height));
|
|
143
|
+
const graphic = original.cloneNode(true) as SVGGElement;
|
|
144
|
+
graphic.removeAttribute('transform');
|
|
145
|
+
preview.append(graphic);
|
|
146
|
+
const xml = blocks.Xml.blockToDom(block, true);
|
|
147
|
+
const label = block.toString();
|
|
148
|
+
items.push({label, preview, disabled: Boolean(block.disabled), insert(point) {
|
|
149
|
+
const position = placement(point);
|
|
150
|
+
if (!position) return;
|
|
151
|
+
const previous = blocks.Events.getGroup();
|
|
152
|
+
blocks.Events.setGroup(true);
|
|
153
|
+
try {
|
|
154
|
+
const inserted = blocks.Xml.domToBlock(xml.cloneNode(true), workspace);
|
|
155
|
+
inserted.moveBy(position.x, position.y);
|
|
156
|
+
} finally {
|
|
157
|
+
blocks.Events.setGroup(previous);
|
|
158
|
+
}
|
|
159
|
+
}});
|
|
160
|
+
}
|
|
161
|
+
return items;
|
|
162
|
+
},
|
|
163
|
+
subscribe(refresh) {
|
|
164
|
+
const observer = new win.MutationObserver(refresh);
|
|
165
|
+
observer.observe(menu, {childList: true, subtree: true, characterData: true});
|
|
166
|
+
const onChange = event => {
|
|
167
|
+
if (event.type?.startsWith('var_')) refresh();
|
|
168
|
+
};
|
|
169
|
+
workspace.addChangeListener(onChange);
|
|
170
|
+
return () => {
|
|
171
|
+
observer.disconnect();
|
|
172
|
+
workspace.removeChangeListener(onChange);
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function installGestureOwnerDocument(blocks) {
|
|
179
|
+
const prototype = blocks.Gesture.prototype;
|
|
180
|
+
const bind = prototype.bindMouseEvents;
|
|
181
|
+
prototype.bindMouseEvents = function (event) {
|
|
182
|
+
const inputDocument = event.target?.ownerDocument;
|
|
183
|
+
bind.call(this, event);
|
|
184
|
+
if (!inputDocument || inputDocument === document) return;
|
|
185
|
+
// Move the registered handlers so Blockly's gesture cleanup still owns them.
|
|
186
|
+
for (const binding of [...this.onMoveWrapper_, ...this.onUpWrapper_]) {
|
|
187
|
+
const [node, type, handler] = binding;
|
|
188
|
+
node.removeEventListener(type, handler, false);
|
|
189
|
+
inputDocument.addEventListener(type, handler, {passive: false});
|
|
190
|
+
binding[0] = inputDocument;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
const dragger = blocks.BlockDragger.prototype;
|
|
194
|
+
const start = dragger.startBlockDrag;
|
|
195
|
+
const dispose = dragger.dispose;
|
|
196
|
+
const inlineDrags = new WeakSet();
|
|
197
|
+
dragger.startBlockDrag = function (...args) {
|
|
198
|
+
const block = this.draggingBlock_;
|
|
199
|
+
if (block.useDragSurface_ && block.getSvgRoot().ownerDocument !== document) {
|
|
200
|
+
// The optimized drag surface looks up its injection container in the opener.
|
|
201
|
+
block.useDragSurface_ = false;
|
|
202
|
+
inlineDrags.add(this);
|
|
203
|
+
}
|
|
204
|
+
return start.apply(this, args);
|
|
205
|
+
};
|
|
206
|
+
dragger.dispose = function () {
|
|
207
|
+
if (inlineDrags.delete(this)) this.draggingBlock_.useDragSurface_ = true;
|
|
208
|
+
return dispose.call(this);
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function moveBlockWidgets(blocks, target: Document) {
|
|
213
|
+
const widgets = [blocks.WidgetDiv.DIV, blocks.DropDownDiv.DIV, blocks.Tooltip.DIV]
|
|
214
|
+
.filter(widget => widget && widget.ownerDocument !== target);
|
|
215
|
+
if (!widgets.length) return;
|
|
216
|
+
blocks.hideChaff();
|
|
217
|
+
target.body.append(...widgets);
|
|
218
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
type Reference = {providerId: string; documentId: string};
|
|
2
|
+
type Asset = {assetId: string; name: string; dataFormat: string; rotationCenterX?: number; rotationCenterY?: number;
|
|
3
|
+
bitmapResolution?: number; format?: string; rate?: number; sampleCount?: number};
|
|
4
|
+
type Target = {id: string; isOriginal: boolean; extensionStorage?: unknown;
|
|
5
|
+
toJSON(): {costume: unknown; costumes: Asset[]; sounds: Asset[]}};
|
|
6
|
+
type TabSubject = {target?: Target; document?: Reference};
|
|
7
|
+
type Project = {
|
|
8
|
+
runtime: {
|
|
9
|
+
targets: Target[];
|
|
10
|
+
on(event: 'PROJECT_LOADED', listener: () => void): void;
|
|
11
|
+
removeListener(event: 'PROJECT_LOADED', listener: () => void): void;
|
|
12
|
+
};
|
|
13
|
+
on(event: string, listener: () => void): void;
|
|
14
|
+
removeListener(event: string, listener: () => void): void;
|
|
15
|
+
};
|
|
16
|
+
type Store = {getState(): {scratchGui: {projectChanged: boolean}}; subscribe(listener: () => void): () => void};
|
|
17
|
+
|
|
18
|
+
export const documentEditEvent = 'tw-plus-document-edit-state';
|
|
19
|
+
|
|
20
|
+
const documentKey = ({providerId, documentId}: Reference) => JSON.stringify([providerId, documentId]);
|
|
21
|
+
|
|
22
|
+
function targetSnapshot(target: Target) {
|
|
23
|
+
// Use the VM's target model without invoking project serialization or copying asset bytes.
|
|
24
|
+
const {costume: _current, costumes, sounds, ...properties} = target.toJSON();
|
|
25
|
+
const assets = costumes.map(({assetId, name, dataFormat, rotationCenterX, rotationCenterY, bitmapResolution}) =>
|
|
26
|
+
[assetId, name, dataFormat, rotationCenterX, rotationCenterY, bitmapResolution]);
|
|
27
|
+
const audio = sounds.map(({assetId, name, dataFormat, format, rate, sampleCount}) =>
|
|
28
|
+
[assetId, name, dataFormat, format, rate, sampleCount]);
|
|
29
|
+
return JSON.stringify([properties, assets, audio, target.extensionStorage]);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function mountDockviewUnsaved(vm: Project, root: HTMLElement, locale: string) {
|
|
33
|
+
const document = root.ownerDocument;
|
|
34
|
+
const window = document.defaultView as Window & {ReduxStore?: Store};
|
|
35
|
+
const store = window.ReduxStore;
|
|
36
|
+
if (!store) return;
|
|
37
|
+
const label = locale.startsWith('ja') ? '未保存の変更' : 'Unsaved changes';
|
|
38
|
+
const tabs = new Set<() => void>();
|
|
39
|
+
const applied = new Set<string>();
|
|
40
|
+
const drafts = new Set<string>();
|
|
41
|
+
|
|
42
|
+
function snapshot() {
|
|
43
|
+
return new Map(vm.runtime.targets.filter(target => target.isOriginal).map(target => [target.id, targetSnapshot(target)]));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let baseline = snapshot();
|
|
47
|
+
let current = baseline;
|
|
48
|
+
let frame: number | undefined;
|
|
49
|
+
let dirty = store.getState().scratchGui.projectChanged;
|
|
50
|
+
const refreshTabs = () => tabs.forEach(refresh => refresh());
|
|
51
|
+
const update = () => {
|
|
52
|
+
frame = undefined;
|
|
53
|
+
current = snapshot();
|
|
54
|
+
refreshTabs();
|
|
55
|
+
};
|
|
56
|
+
const schedule = () => {
|
|
57
|
+
frame ??= window.requestAnimationFrame(update);
|
|
58
|
+
};
|
|
59
|
+
const resetBaseline = () => {
|
|
60
|
+
if (frame !== undefined) window.cancelAnimationFrame(frame);
|
|
61
|
+
frame = undefined;
|
|
62
|
+
baseline = current = snapshot();
|
|
63
|
+
applied.clear();
|
|
64
|
+
refreshTabs();
|
|
65
|
+
};
|
|
66
|
+
const loaded = () => {
|
|
67
|
+
drafts.clear();
|
|
68
|
+
resetBaseline();
|
|
69
|
+
};
|
|
70
|
+
const unsubscribe = store.subscribe(() => {
|
|
71
|
+
const next = store.getState().scratchGui.projectChanged;
|
|
72
|
+
if (dirty === next) return;
|
|
73
|
+
dirty = next;
|
|
74
|
+
if (dirty) schedule();
|
|
75
|
+
else resetBaseline();
|
|
76
|
+
});
|
|
77
|
+
vm.on('PROJECT_CHANGED', schedule);
|
|
78
|
+
vm.runtime.on('PROJECT_LOADED', loaded);
|
|
79
|
+
// Some hosts create the workbench before installing their default project.
|
|
80
|
+
const initialTargets = () => {
|
|
81
|
+
if (!dirty && !baseline.size && vm.runtime.targets.length) resetBaseline();
|
|
82
|
+
};
|
|
83
|
+
vm.on('targetsUpdate', initialTargets);
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
tab(element: HTMLElement, subject: () => TabSubject) {
|
|
87
|
+
const title = element.querySelector('.dv-default-tab-content');
|
|
88
|
+
const marker = document.createElement('span');
|
|
89
|
+
marker.dataset.twPlusUnsavedMarker = '';
|
|
90
|
+
marker.dataset.twPlusUnsavedTab = '';
|
|
91
|
+
marker.setAttribute('aria-hidden', 'true');
|
|
92
|
+
marker.title = label;
|
|
93
|
+
marker.hidden = true;
|
|
94
|
+
title?.after(marker);
|
|
95
|
+
const refresh = () => {
|
|
96
|
+
const {target, document: reference} = subject();
|
|
97
|
+
const key = reference && documentKey(reference);
|
|
98
|
+
const changed = target && dirty && baseline.get(target.id) !== current.get(target.id) ||
|
|
99
|
+
key && (drafts.has(key) || applied.has(key));
|
|
100
|
+
marker.hidden = !changed;
|
|
101
|
+
const tab = element.closest('[role="tab"]');
|
|
102
|
+
if (changed) tab?.setAttribute('aria-description', label);
|
|
103
|
+
else tab?.removeAttribute('aria-description');
|
|
104
|
+
};
|
|
105
|
+
tabs.add(refresh);
|
|
106
|
+
refresh();
|
|
107
|
+
// Dockview sets up the renderer before inserting it into its tab element.
|
|
108
|
+
queueMicrotask(() => { if (tabs.has(refresh)) refresh(); });
|
|
109
|
+
return () => { tabs.delete(refresh); };
|
|
110
|
+
},
|
|
111
|
+
document(element: HTMLElement, reference: Reference) {
|
|
112
|
+
const key = documentKey(reference);
|
|
113
|
+
const changed = (event: CustomEvent<{draft: boolean; applied: boolean}>) => {
|
|
114
|
+
if (event.detail.draft) drafts.add(key);
|
|
115
|
+
else drafts.delete(key);
|
|
116
|
+
if (event.detail.applied) applied.add(key);
|
|
117
|
+
refreshTabs();
|
|
118
|
+
};
|
|
119
|
+
element.addEventListener(documentEditEvent, changed as EventListener);
|
|
120
|
+
return () => {
|
|
121
|
+
element.removeEventListener(documentEditEvent, changed as EventListener);
|
|
122
|
+
if (drafts.delete(key)) refreshTabs();
|
|
123
|
+
};
|
|
124
|
+
},
|
|
125
|
+
dispose() {
|
|
126
|
+
if (frame !== undefined) window.cancelAnimationFrame(frame);
|
|
127
|
+
unsubscribe();
|
|
128
|
+
vm.removeListener('PROJECT_CHANGED', schedule);
|
|
129
|
+
vm.runtime.removeListener('PROJECT_LOADED', loaded);
|
|
130
|
+
vm.removeListener('targetsUpdate', initialTargets);
|
|
131
|
+
tabs.clear();
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
package/src/history.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
interface CommentData {
|
|
2
|
+
id: string;
|
|
3
|
+
blockId: string | null;
|
|
4
|
+
text: string;
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
minimized: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface CodeTarget {
|
|
12
|
+
isStage: boolean;
|
|
13
|
+
blocks: {
|
|
14
|
+
_blocks: Record<string, unknown>;
|
|
15
|
+
_scripts: string[];
|
|
16
|
+
getScripts(): string[];
|
|
17
|
+
resetCache(): void;
|
|
18
|
+
emitProjectChanged(): void;
|
|
19
|
+
toXML(comments: Record<string, CommentData>): string;
|
|
20
|
+
};
|
|
21
|
+
comments: Record<string, CommentData & {toXML(): string}>;
|
|
22
|
+
variables: Record<string, {name?: string; type?: string; toXML(local?: boolean): string}>;
|
|
23
|
+
createComment(id: string, blockId: string | null, text: string, x: number, y: number,
|
|
24
|
+
width: number, height: number, minimized: boolean): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const historyLimit = 100;
|
|
28
|
+
|
|
29
|
+
export const capture = (target: CodeTarget) => structuredClone({
|
|
30
|
+
blocks: target.blocks._blocks,
|
|
31
|
+
scripts: target.blocks.getScripts(),
|
|
32
|
+
comments: target.comments
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export function restore(target: CodeTarget, snapshot: ReturnType<typeof capture>) {
|
|
36
|
+
const {blocks, scripts, comments} = structuredClone(snapshot);
|
|
37
|
+
// Keep the live Blocks container, which also belongs to any clones of the sprite.
|
|
38
|
+
target.blocks._blocks = Object.assign(Object.create(null), blocks);
|
|
39
|
+
target.blocks._scripts = scripts;
|
|
40
|
+
// Recreate VM Comment instances so projections and SB3 retain attached and workspace comments.
|
|
41
|
+
target.comments = Object.create(null);
|
|
42
|
+
for (const {id, blockId, text, x, y, width, height, minimized} of Object.values(comments)) {
|
|
43
|
+
target.createComment(id, blockId, text, x, y, width, height, minimized);
|
|
44
|
+
}
|
|
45
|
+
target.blocks.resetCache();
|
|
46
|
+
target.blocks.emitProjectChanged();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
type BroadcastStage = Pick<CodeTarget, 'variables'> & {
|
|
50
|
+
createVariable(id: string, name: string, type: 'broadcast_msg', isCloud: false): void;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export function createHistory(target: CodeTarget, stage: BroadcastStage) {
|
|
54
|
+
const captureRevision = () => {
|
|
55
|
+
const snapshot = capture(target);
|
|
56
|
+
const messages = new Map<string, string>();
|
|
57
|
+
for (const block of Object.values(snapshot.blocks)) {
|
|
58
|
+
const id = (block as {fields?: {BROADCAST_OPTION?: {id?: string}}}).fields?.BROADCAST_OPTION?.id;
|
|
59
|
+
const message = id && stage.variables[id];
|
|
60
|
+
if (message?.type === 'broadcast_msg' && typeof message.name === 'string') messages.set(id, message.name);
|
|
61
|
+
}
|
|
62
|
+
return {...snapshot, messages: [...messages]};
|
|
63
|
+
};
|
|
64
|
+
const past: ReturnType<typeof captureRevision>[] = [];
|
|
65
|
+
const future: ReturnType<typeof captureRevision>[] = [];
|
|
66
|
+
let present = captureRevision();
|
|
67
|
+
let revision = 0;
|
|
68
|
+
return {
|
|
69
|
+
get state() {return {revision, canUndo: past.length > 0, canRedo: future.length > 0};},
|
|
70
|
+
commit() {
|
|
71
|
+
const next = captureRevision();
|
|
72
|
+
if (JSON.stringify(next) === JSON.stringify(present)) return false;
|
|
73
|
+
past.push(present);
|
|
74
|
+
if (past.length > historyLimit) past.shift();
|
|
75
|
+
present = next;
|
|
76
|
+
future.length = 0;
|
|
77
|
+
revision++;
|
|
78
|
+
return true;
|
|
79
|
+
},
|
|
80
|
+
renameSpriteReferences(oldName: string, newName: string) {
|
|
81
|
+
// Use the pinned VM's field rules for snapshots without touching the live block cache.
|
|
82
|
+
const blocks = Object.create(target.blocks);
|
|
83
|
+
blocks.resetCache = () => {};
|
|
84
|
+
for (const snapshot of [...past, present, ...future]) {
|
|
85
|
+
blocks._blocks = snapshot.blocks;
|
|
86
|
+
blocks.updateAssetName(oldName, newName, 'sprite');
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
step(redo: boolean) {
|
|
90
|
+
const next = (redo ? future : past).pop();
|
|
91
|
+
if (!next) return false;
|
|
92
|
+
(redo ? past : future).push(present);
|
|
93
|
+
present = next;
|
|
94
|
+
// The VM prunes unused broadcast IDs on workspace refresh.
|
|
95
|
+
// Restore messages that were still registered in this revision.
|
|
96
|
+
for (const [id, name] of present.messages) {
|
|
97
|
+
if (!Object.hasOwn(stage.variables, id)) stage.createVariable(id, name, 'broadcast_msg', false);
|
|
98
|
+
}
|
|
99
|
+
restore(target, present);
|
|
100
|
+
revision++;
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function workspaceXML(vm: {runtime: {getTargetForStage(): CodeTarget}}, target: CodeTarget) {
|
|
107
|
+
const stage = vm.runtime.getTargetForStage();
|
|
108
|
+
const globals = Object.values(stage.variables).map(variable => variable.toXML());
|
|
109
|
+
const locals = target.isStage ? [] : Object.values(target.variables).map(variable => variable.toXML(true));
|
|
110
|
+
const comments = Object.values(target.comments).filter(comment => comment.blockId === null)
|
|
111
|
+
.map(comment => comment.toXML());
|
|
112
|
+
return `<xml xmlns="http://www.w3.org/1999/xhtml"><variables>${globals.join('')}${locals.join('')}</variables>` +
|
|
113
|
+
`${comments.join('')}${target.blocks.toXML(target.comments)}</xml>`;
|
|
114
|
+
}
|