@kubohiroya/turbowarp-plus-editor-workbench 0.18.1 → 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 +2 -0
- package/README.md +2 -0
- package/dist/project-screen-state.d.ts +10 -0
- package/dist/project-screen-state.js +8 -1
- package/dist/sprite-tabs.d.ts +8 -0
- package/dist/sprite-tabs.js +57 -6
- package/dist/workspace-layout.d.ts +9 -0
- package/dist/workspace-layout.js +16 -1
- package/package.json +1 -1
- package/src/project-screen-state.ts +18 -1
- package/src/sprite-tabs.ts +61 -7
- package/src/workspace-layout.ts +25 -2
package/README.ja.md
CHANGED
|
@@ -108,6 +108,8 @@ useSpriteTabs({...props, workspaceLayout: {...workspaceLayout, screenState}});
|
|
|
108
108
|
- 保存すると、`project.json` の隣に ZIP エントリ `tw-plus/workspace.json`(`{"version": 1, "savedAt": <ms>, "slices": {...}}`)を加える。Scratch と TurboWarp はこのエントリを無視するので、ファイルの互換性は変わらない。別のエディターで保存し直すと、エントリが消えるだけである。`project.json` は変えない。
|
|
109
109
|
- エディターの各部分は、自分のキーで `register(key, {capture, apply})` によりスライスを登録する。`capture` は保存中に同期で呼ばれる。`apply` は、そのスライスを含む作品を VM が読み込み終えたあとに作品ごとに1回呼ばれる。読み込み後に登録したスライスには、登録したときに呼ばれる。キーは `^[a-z][a-zA-Z0-9-]{0,63}$` に合うものとする。
|
|
110
110
|
- このパッケージは `drawer`(左側ドロワーで選んだ表示)と `workspace`([Dockview のレイアウトの保存](#dockview-のレイアウトの保存) の作業領域)を登録する。ファイルの作業領域は、このブラウザーがその作品について保存したレイアウトより新しいとき、またはブラウザーに記録がないときに使う。ファイルの保存後にレイアウトを変え、保存せずに開き直した場合は、ブラウザーの記録が使われる。
|
|
111
|
+
- 各スプライトのタブは、選んでいたコスチュームと音も、一覧での位置で保存する。ホストのタブアダプターが `serialize(view, snapshot)` と `deserialize(view, value)` を持つときは、スクロール位置やズームなどのコードエリアも保存する。画面に出ているタブについては、`serialize` をスナップショットなしで呼ぶので、コードエリアを変えずに読む。画面に出ているタブを読むのは作品ファイルの保存時だけなので、ブラウザーが保存するレイアウトには、ほかのタブのコードエリアだけが入る。
|
|
112
|
+
- SB3 として保存しない作品は、同じ状態を別の場所に保存する。`serialize()` は保存時に ZIP エントリへ書くテキストを返す。`prepareLoad(text)` を呼ぶと、次の `loadProject` は入力を読まずにこのテキストを使う。作品フォルダーは、これを `.tw-plus/workspace.json` として書く。
|
|
111
113
|
- `save: false` では、保存時にエントリを書かず、読み込んだファイルから引き継いだエントリも取り除く。読み込みは有効のままなので、以前に保存したファイルは保存時の画面で開く。
|
|
112
114
|
- 512 KiB を超えるエントリ、別の版のもの、形の合わないものは無視し、作品は通常の画面で開く。
|
|
113
115
|
|
package/README.md
CHANGED
|
@@ -110,6 +110,8 @@ useSpriteTabs({...props, workspaceLayout: {...workspaceLayout, screenState}});
|
|
|
110
110
|
- Saving adds the ZIP entry `tw-plus/workspace.json` next to `project.json`: `{"version": 1, "savedAt": <ms>, "slices": {...}}`. Scratch and TurboWarp ignore the entry, so the file stays compatible. Saving it in another editor only drops the entry. `project.json` is not changed.
|
|
111
111
|
- Each part of the editor registers a slice under its own key with `register(key, {capture, apply})`. `capture` is called synchronously while saving; `apply` is called once for each loaded project that carries the slice, after the VM finished loading it, or when the slice is registered later. Keys match `^[a-z][a-zA-Z0-9-]{0,63}$`.
|
|
112
112
|
- This package registers `drawer` (the selected left drawer view) and `workspace` (the Dockview workspace described under [Saved Dockview layout](#saved-dockview-layout)). The workspace from the file is used when it is newer than the layout this browser kept for the project, or when the browser has none. A layout changed after the file was saved, and reloaded without saving, therefore still wins.
|
|
113
|
+
- Each sprite tab also keeps the costume and sound it had selected, by their index in the list. When the host's tab adapter has `serialize(view, snapshot)` and `deserialize(view, value)`, the tab keeps its code area too, such as its scroll position and zoom; `serialize` is called without a snapshot for the tab on screen, and must then read the code area without changing it. The tab on screen is read only while saving the project file, so the layout this browser keeps has the code areas of the other tabs.
|
|
114
|
+
- A project that is not saved as an SB3 keeps the same state elsewhere: `serialize()` returns the text a save writes into the ZIP entry, and `prepareLoad(text)` makes the next `loadProject` use that text instead of reading its input. The project folder writes it as `.tw-plus/workspace.json`.
|
|
113
115
|
- With `save: false`, saving writes no entry and removes one carried over from the loaded file. Reading stays on, so files saved earlier still open as they were.
|
|
114
116
|
- Entries larger than 512 KiB, of another version or of another shape are ignored, and the project opens with its usual screen.
|
|
115
117
|
|
|
@@ -62,6 +62,16 @@ export interface ProjectScreenState {
|
|
|
62
62
|
whenLoaded(): Promise<SavedScreenState | undefined>;
|
|
63
63
|
/** The state a save would write now. */
|
|
64
64
|
capture(): SavedScreenState | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* The text a save writes, as in the ZIP entry, for a project saved somewhere else such as a project folder.
|
|
67
|
+
* Nothing while saving is turned off, nothing is registered or the state is too large.
|
|
68
|
+
*/
|
|
69
|
+
serialize(): string | undefined;
|
|
70
|
+
/**
|
|
71
|
+
* Use the state in `text` for the next project load instead of reading the input, for a project that is not
|
|
72
|
+
* loaded from an SB3, such as a project folder. Text that is not a screen state opens the default screen.
|
|
73
|
+
*/
|
|
74
|
+
prepareLoad(text: string | undefined): void;
|
|
65
75
|
/** Put the VM methods back. Hooks installed later on the same methods must be removed first. */
|
|
66
76
|
dispose(): void;
|
|
67
77
|
}
|
|
@@ -81,6 +81,8 @@ export function installProjectScreenState(vm, { Zip, save = true, now = Date.now
|
|
|
81
81
|
let current;
|
|
82
82
|
/** Slices already given the current project's state, so registering again does not apply it twice. */
|
|
83
83
|
let applied = new Set();
|
|
84
|
+
/** The state the next load uses instead of its input, set by `prepareLoad`. */
|
|
85
|
+
let prepared;
|
|
84
86
|
const apply = (slice, key) => {
|
|
85
87
|
if (!current || applied.has(slice) || !Object.hasOwn(current.slices, key))
|
|
86
88
|
return;
|
|
@@ -116,7 +118,8 @@ export function installProjectScreenState(vm, { Zip, save = true, now = Date.now
|
|
|
116
118
|
current = undefined;
|
|
117
119
|
applied = new Set();
|
|
118
120
|
// Read in parallel with the VM, which keeps its own copy of the bytes.
|
|
119
|
-
const reading = readScreenState(input, Zip);
|
|
121
|
+
const reading = prepared ? Promise.resolve(prepared.state) : readScreenState(input, Zip);
|
|
122
|
+
prepared = undefined;
|
|
120
123
|
const result = original.loadProject.call(this, input, ...args);
|
|
121
124
|
const done = result.then(() => reading, () => undefined).then(state => {
|
|
122
125
|
if (mine !== generation)
|
|
@@ -163,6 +166,10 @@ export function installProjectScreenState(vm, { Zip, save = true, now = Date.now
|
|
|
163
166
|
},
|
|
164
167
|
whenLoaded: () => loaded,
|
|
165
168
|
capture,
|
|
169
|
+
serialize: serialized,
|
|
170
|
+
prepareLoad(text) {
|
|
171
|
+
prepared = { state: text === undefined ? undefined : parseScreenState(text) };
|
|
172
|
+
},
|
|
166
173
|
dispose() {
|
|
167
174
|
vm.loadProject = original.loadProject;
|
|
168
175
|
vm._saveProjectZip = original._saveProjectZip;
|
package/dist/sprite-tabs.d.ts
CHANGED
|
@@ -28,6 +28,7 @@ interface Target {
|
|
|
28
28
|
getCostumes?(): {
|
|
29
29
|
assetId?: string;
|
|
30
30
|
}[];
|
|
31
|
+
getSounds?(): object[];
|
|
31
32
|
getCurrentCostume?(): {
|
|
32
33
|
asset?: CostumeAsset;
|
|
33
34
|
} | undefined;
|
|
@@ -57,6 +58,13 @@ export interface SpriteTabAdapter<Snapshot = unknown> {
|
|
|
57
58
|
renderInactive(container: HTMLElement, view: SpriteTabView, snapshot: Snapshot | undefined): void | (() => void);
|
|
58
59
|
restore(view: SpriteTabView, snapshot: Snapshot | undefined): void;
|
|
59
60
|
dispose?(view: SpriteTabView, snapshot: Snapshot): void;
|
|
61
|
+
/**
|
|
62
|
+
* Describe a snapshot of the code area as a small JSON object for the saved workspace, such as its scroll
|
|
63
|
+
* position and zoom. Without a snapshot, describe the mounted surface as it is now, without changing it.
|
|
64
|
+
*/
|
|
65
|
+
serialize?(view: SpriteTabView, snapshot: Snapshot | undefined): Record<string, unknown> | undefined;
|
|
66
|
+
/** Turn a value `serialize` wrote back into a snapshot. The value comes from a file, so check its shape. */
|
|
67
|
+
deserialize?(view: SpriteTabView, value: Record<string, unknown>): Snapshot | undefined;
|
|
60
68
|
}
|
|
61
69
|
/** Optional bridge used by a native asset editor to restore and report its selection. */
|
|
62
70
|
export interface SpriteAssetSelection {
|
package/dist/sprite-tabs.js
CHANGED
|
@@ -881,8 +881,57 @@ export function mountSpriteTabs(root, content, options) {
|
|
|
881
881
|
refreshSurfaces();
|
|
882
882
|
scheduleSave();
|
|
883
883
|
}
|
|
884
|
-
/** The
|
|
885
|
-
function
|
|
884
|
+
/** The selected costume and sound of a tab, and its code area, as the workspace saves them. */
|
|
885
|
+
function capturePane(view, live) {
|
|
886
|
+
const pane = {};
|
|
887
|
+
const lists = { costumes: view.target.getCostumes?.(), sounds: view.target.getSounds?.() };
|
|
888
|
+
for (const [kind, field] of [['costumes', 'costume'], ['sounds', 'sound']]) {
|
|
889
|
+
const index = lists[kind]?.indexOf(view.assets.selected.get(kind)) ?? -1;
|
|
890
|
+
if (index >= 0)
|
|
891
|
+
pane[field] = index;
|
|
892
|
+
}
|
|
893
|
+
if (viewAdapter?.serialize) {
|
|
894
|
+
const stored = view.snapshots?.get(0);
|
|
895
|
+
// The tab on screen keeps its code area in the shared editor, which only a save reads.
|
|
896
|
+
const showing = view === active && view.section === 0 && Boolean(slots.get(view.id)?.body.contains(content));
|
|
897
|
+
try {
|
|
898
|
+
const code = showing ? (live ? viewAdapter.serialize(describe(view), undefined) : undefined) :
|
|
899
|
+
stored && viewAdapter.serialize(stored.view, stored.snapshot);
|
|
900
|
+
if (code)
|
|
901
|
+
pane.code = code;
|
|
902
|
+
}
|
|
903
|
+
catch { /* The code area is left as the tab opens by default. */ }
|
|
904
|
+
}
|
|
905
|
+
return Object.keys(pane).length ? pane : undefined;
|
|
906
|
+
}
|
|
907
|
+
/** The asset selections and code snapshot a restored tab opens with. */
|
|
908
|
+
function restorePane(id, target, pane) {
|
|
909
|
+
const selected = new Map();
|
|
910
|
+
const costume = pane?.costume === undefined ? undefined : target.getCostumes?.()[pane.costume];
|
|
911
|
+
const sound = pane?.sound === undefined ? undefined : target.getSounds?.()[pane.sound];
|
|
912
|
+
if (costume)
|
|
913
|
+
selected.set('costumes', costume);
|
|
914
|
+
if (sound)
|
|
915
|
+
selected.set('sounds', sound);
|
|
916
|
+
const assets = createAssetSelections(target, locale, { selected, layouts: new Map(), scroll: new Map() });
|
|
917
|
+
let snapshots;
|
|
918
|
+
if (pane?.code && viewAdapter?.deserialize && pane.code !== null && typeof pane.code === 'object') {
|
|
919
|
+
const view = { id, targetId: target.id, isStage: target.isStage, section: 0, groupId: '',
|
|
920
|
+
active: false, groupActive: false };
|
|
921
|
+
try {
|
|
922
|
+
const snapshot = viewAdapter.deserialize(view, pane.code);
|
|
923
|
+
if (snapshot !== undefined)
|
|
924
|
+
snapshots = new Map([[0, { snapshot, view }]]);
|
|
925
|
+
}
|
|
926
|
+
catch { /* The code area opens as it does by default. */ }
|
|
927
|
+
}
|
|
928
|
+
return { assets, snapshots };
|
|
929
|
+
}
|
|
930
|
+
/**
|
|
931
|
+
* The current workspace as it is saved, or nothing while it cannot be described yet. `live` also reads the code
|
|
932
|
+
* area on screen, which only a save of the project file does.
|
|
933
|
+
*/
|
|
934
|
+
function captureLayout(live = false) {
|
|
886
935
|
const targets = originalTargets();
|
|
887
936
|
if (!targets.length)
|
|
888
937
|
return;
|
|
@@ -894,8 +943,9 @@ export function mountSpriteTabs(root, content, options) {
|
|
|
894
943
|
return;
|
|
895
944
|
const panels = Object.create(null);
|
|
896
945
|
for (const [id, view] of views) {
|
|
946
|
+
const pane = capturePane(view, live);
|
|
897
947
|
panels[id] = { kind: 'sprite', name: targetName(view.target), stage: view.target.isStage, section: view.section,
|
|
898
|
-
number: view.number };
|
|
948
|
+
number: view.number, ...(pane && { pane }) };
|
|
899
949
|
}
|
|
900
950
|
if (dockview.getPanel(stagePanelId))
|
|
901
951
|
panels[stagePanelId] = { kind: 'stage' };
|
|
@@ -1002,7 +1052,8 @@ export function mountSpriteTabs(root, content, options) {
|
|
|
1002
1052
|
}
|
|
1003
1053
|
/** The tab arrangement and drawer state used to detect a change from the initial layout. */
|
|
1004
1054
|
function layoutShape(layout) {
|
|
1005
|
-
|
|
1055
|
+
// What a tab shows inside its panes is not a change of the arrangement.
|
|
1056
|
+
const reference = (id) => layout.panels[id] ? { ...layout.panels[id], pane: undefined } : null;
|
|
1006
1057
|
const shape = (node) => Array.isArray(node.data) ? node.data.map(shape) :
|
|
1007
1058
|
node.data.views.map(reference);
|
|
1008
1059
|
return JSON.stringify([layout.grid.orientation, shape(layout.grid.root),
|
|
@@ -1049,7 +1100,7 @@ export function mountSpriteTabs(root, content, options) {
|
|
|
1049
1100
|
// Until the opened project's layout is restored, the tabs on screen are not that project's.
|
|
1050
1101
|
if (pendingRestore || !layoutSession)
|
|
1051
1102
|
return fileLayout ? JSON.parse(fileLayout.layout) : undefined;
|
|
1052
|
-
return captureLayout();
|
|
1103
|
+
return captureLayout(true);
|
|
1053
1104
|
}
|
|
1054
1105
|
/** Keep the layout the project opened with, so leaving it unchanged adds no record. */
|
|
1055
1106
|
function keepInitialLayout() {
|
|
@@ -1101,7 +1152,7 @@ export function mountSpriteTabs(root, content, options) {
|
|
|
1101
1152
|
continue;
|
|
1102
1153
|
}
|
|
1103
1154
|
resolvedSprites.set(id, { id, target, section: reference.section, number: reference.number,
|
|
1104
|
-
|
|
1155
|
+
...restorePane(id, target, reference.pane) });
|
|
1105
1156
|
panels[id] = { id, contentComponent: 'sprite', params: { icon: getIcon(target) } };
|
|
1106
1157
|
}
|
|
1107
1158
|
else if (reference.kind === 'stage') {
|
|
@@ -1,12 +1,21 @@
|
|
|
1
1
|
import type { SerializedDockview } from 'dockview';
|
|
2
2
|
import type { DocumentPanelRequest } from './document-workspace.ts';
|
|
3
3
|
type Grid = SerializedDockview['grid'];
|
|
4
|
+
/** What a sprite tab showed inside its panes. Every field is optional, and a missing one keeps the default. */
|
|
5
|
+
export interface SpritePaneState {
|
|
6
|
+
/** The index of the costume or sound the tab had selected in that list. */
|
|
7
|
+
costume?: number;
|
|
8
|
+
sound?: number;
|
|
9
|
+
/** The code area as the host's tab adapter serialized it, such as its scroll position and zoom. */
|
|
10
|
+
code?: unknown;
|
|
11
|
+
}
|
|
4
12
|
export type PanelReference = {
|
|
5
13
|
kind: 'sprite';
|
|
6
14
|
name: string;
|
|
7
15
|
stage: boolean;
|
|
8
16
|
section: number;
|
|
9
17
|
number?: number;
|
|
18
|
+
pane?: SpritePaneState;
|
|
10
19
|
} | {
|
|
11
20
|
kind: 'stage';
|
|
12
21
|
} | ({
|
package/dist/workspace-layout.js
CHANGED
|
@@ -166,8 +166,9 @@ export function readWorkspaceLayout(raw, project) {
|
|
|
166
166
|
const number = panel.number;
|
|
167
167
|
if (number !== undefined && (typeof number !== 'number' || !Number.isSafeInteger(number) || number < 2))
|
|
168
168
|
return;
|
|
169
|
+
const pane = readPaneState(panel.pane);
|
|
169
170
|
panels[id] = { kind: 'sprite', name: panel.name, stage: panel.stage, section: panel.section,
|
|
170
|
-
...(typeof number === 'number' && { number }) };
|
|
171
|
+
...(typeof number === 'number' && { number }), ...(pane && { pane }) };
|
|
171
172
|
}
|
|
172
173
|
else if (panel.kind === 'stage' && !hasStage) {
|
|
173
174
|
hasStage = true;
|
|
@@ -195,6 +196,20 @@ export function readWorkspaceLayout(raw, project) {
|
|
|
195
196
|
...(stageDrawer && { stageDrawer }),
|
|
196
197
|
...(popouts.length && { popouts }) };
|
|
197
198
|
}
|
|
199
|
+
const assetIndex = (value) => typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 && value <= 100000;
|
|
200
|
+
/** Keep only the fields of a pane state that have the right shape. The code state is checked by the host. */
|
|
201
|
+
function readPaneState(input) {
|
|
202
|
+
if (!record(input))
|
|
203
|
+
return;
|
|
204
|
+
const pane = {};
|
|
205
|
+
if (assetIndex(input.costume))
|
|
206
|
+
pane.costume = input.costume;
|
|
207
|
+
if (assetIndex(input.sound))
|
|
208
|
+
pane.sound = input.sound;
|
|
209
|
+
if (record(input.code))
|
|
210
|
+
pane.code = input.code;
|
|
211
|
+
return Object.keys(pane).length ? pane : undefined;
|
|
212
|
+
}
|
|
198
213
|
function readStageDrawer(input) {
|
|
199
214
|
if (!record(input) || typeof input.open !== 'boolean' || typeof input.selected !== 'boolean')
|
|
200
215
|
return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubohiroya/turbowarp-plus-editor-workbench",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"description": "The TurboWarp Plus Editor workbench: layout, dockable sprite tabs, stage, debugger tab and file menu",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -65,6 +65,16 @@ export interface ProjectScreenState {
|
|
|
65
65
|
whenLoaded(): Promise<SavedScreenState | undefined>;
|
|
66
66
|
/** The state a save would write now. */
|
|
67
67
|
capture(): SavedScreenState | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* The text a save writes, as in the ZIP entry, for a project saved somewhere else such as a project folder.
|
|
70
|
+
* Nothing while saving is turned off, nothing is registered or the state is too large.
|
|
71
|
+
*/
|
|
72
|
+
serialize(): string | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* Use the state in `text` for the next project load instead of reading the input, for a project that is not
|
|
75
|
+
* loaded from an SB3, such as a project folder. Text that is not a screen state opens the default screen.
|
|
76
|
+
*/
|
|
77
|
+
prepareLoad(text: string | undefined): void;
|
|
68
78
|
/** Put the VM methods back. Hooks installed later on the same methods must be removed first. */
|
|
69
79
|
dispose(): void;
|
|
70
80
|
}
|
|
@@ -129,6 +139,8 @@ export function installProjectScreenState(vm: ScreenStateVM, {Zip, save = true,
|
|
|
129
139
|
let current: SavedScreenState | undefined;
|
|
130
140
|
/** Slices already given the current project's state, so registering again does not apply it twice. */
|
|
131
141
|
let applied = new Set<ScreenStateSlice>();
|
|
142
|
+
/** The state the next load uses instead of its input, set by `prepareLoad`. */
|
|
143
|
+
let prepared: {state: SavedScreenState | undefined} | undefined;
|
|
132
144
|
|
|
133
145
|
const apply = (slice: ScreenStateSlice, key: string) => {
|
|
134
146
|
if (!current || applied.has(slice) || !Object.hasOwn(current.slices, key)) return;
|
|
@@ -162,7 +174,8 @@ export function installProjectScreenState(vm: ScreenStateVM, {Zip, save = true,
|
|
|
162
174
|
current = undefined;
|
|
163
175
|
applied = new Set();
|
|
164
176
|
// Read in parallel with the VM, which keeps its own copy of the bytes.
|
|
165
|
-
const reading = readScreenState(input, Zip);
|
|
177
|
+
const reading = prepared ? Promise.resolve(prepared.state) : readScreenState(input, Zip);
|
|
178
|
+
prepared = undefined;
|
|
166
179
|
const result = original.loadProject.call(this, input, ...args);
|
|
167
180
|
const done = result.then(() => reading, () => undefined).then(state => {
|
|
168
181
|
if (mine !== generation) return;
|
|
@@ -202,6 +215,10 @@ export function installProjectScreenState(vm: ScreenStateVM, {Zip, save = true,
|
|
|
202
215
|
},
|
|
203
216
|
whenLoaded: () => loaded,
|
|
204
217
|
capture,
|
|
218
|
+
serialize: serialized,
|
|
219
|
+
prepareLoad(text) {
|
|
220
|
+
prepared = {state: text === undefined ? undefined : parseScreenState(text)};
|
|
221
|
+
},
|
|
205
222
|
dispose() {
|
|
206
223
|
vm.loadProject = original.loadProject;
|
|
207
224
|
vm._saveProjectZip = original._saveProjectZip;
|
package/src/sprite-tabs.ts
CHANGED
|
@@ -15,7 +15,7 @@ import type {DocumentPanelRequest} from './document-workspace.ts';
|
|
|
15
15
|
import {projectFolderIconPath} from './drawer-views.ts';
|
|
16
16
|
import {dockPopoutGroups, positionOnScreen, readWorkspaceLayout, workspaceWithoutTransientPanels}
|
|
17
17
|
from './workspace-layout.ts';
|
|
18
|
-
import type {PanelReference, SavedPopout, WorkspaceLayout} from './workspace-layout.ts';
|
|
18
|
+
import type {PanelReference, SavedPopout, SpritePaneState, WorkspaceLayout} from './workspace-layout.ts';
|
|
19
19
|
import {createWorkspaceLayoutStore} from './workspace-layout-store.ts';
|
|
20
20
|
import type {WorkspaceLayoutSession, WorkspaceLayoutStore, WorkspaceProjectIdentity} from './workspace-layout-store.ts';
|
|
21
21
|
import type {ProjectScreenState, SavedScreenState} from './project-screen-state.ts';
|
|
@@ -46,6 +46,7 @@ interface Target {
|
|
|
46
46
|
isStage: boolean;
|
|
47
47
|
getName(): string;
|
|
48
48
|
getCostumes?(): {assetId?: string}[];
|
|
49
|
+
getSounds?(): object[];
|
|
49
50
|
getCurrentCostume?(): {asset?: CostumeAsset} | undefined;
|
|
50
51
|
}
|
|
51
52
|
|
|
@@ -74,6 +75,13 @@ export interface SpriteTabAdapter<Snapshot = unknown> {
|
|
|
74
75
|
renderInactive(container: HTMLElement, view: SpriteTabView, snapshot: Snapshot | undefined): void | (() => void);
|
|
75
76
|
restore(view: SpriteTabView, snapshot: Snapshot | undefined): void;
|
|
76
77
|
dispose?(view: SpriteTabView, snapshot: Snapshot): void;
|
|
78
|
+
/**
|
|
79
|
+
* Describe a snapshot of the code area as a small JSON object for the saved workspace, such as its scroll
|
|
80
|
+
* position and zoom. Without a snapshot, describe the mounted surface as it is now, without changing it.
|
|
81
|
+
*/
|
|
82
|
+
serialize?(view: SpriteTabView, snapshot: Snapshot | undefined): Record<string, unknown> | undefined;
|
|
83
|
+
/** Turn a value `serialize` wrote back into a snapshot. The value comes from a file, so check its shape. */
|
|
84
|
+
deserialize?(view: SpriteTabView, value: Record<string, unknown>): Snapshot | undefined;
|
|
77
85
|
}
|
|
78
86
|
|
|
79
87
|
/** Optional bridge used by a native asset editor to restore and report its selection. */
|
|
@@ -998,8 +1006,52 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
|
|
|
998
1006
|
scheduleSave();
|
|
999
1007
|
}
|
|
1000
1008
|
|
|
1001
|
-
/** The
|
|
1002
|
-
function
|
|
1009
|
+
/** The selected costume and sound of a tab, and its code area, as the workspace saves them. */
|
|
1010
|
+
function capturePane(view: View<Snapshot>, live: boolean): SpritePaneState | undefined {
|
|
1011
|
+
const pane: SpritePaneState = {};
|
|
1012
|
+
const lists = {costumes: view.target.getCostumes?.(), sounds: view.target.getSounds?.()};
|
|
1013
|
+
for (const [kind, field] of [['costumes', 'costume'], ['sounds', 'sound']] as const) {
|
|
1014
|
+
const index = lists[kind]?.indexOf(view.assets.selected.get(kind) as never) ?? -1;
|
|
1015
|
+
if (index >= 0) pane[field] = index;
|
|
1016
|
+
}
|
|
1017
|
+
if (viewAdapter?.serialize) {
|
|
1018
|
+
const stored = view.snapshots?.get(0);
|
|
1019
|
+
// The tab on screen keeps its code area in the shared editor, which only a save reads.
|
|
1020
|
+
const showing = view === active && view.section === 0 && Boolean(slots.get(view.id)?.body.contains(content));
|
|
1021
|
+
try {
|
|
1022
|
+
const code = showing ? (live ? viewAdapter.serialize(describe(view), undefined) : undefined) :
|
|
1023
|
+
stored && viewAdapter.serialize(stored.view, stored.snapshot);
|
|
1024
|
+
if (code) pane.code = code;
|
|
1025
|
+
} catch { /* The code area is left as the tab opens by default. */ }
|
|
1026
|
+
}
|
|
1027
|
+
return Object.keys(pane).length ? pane : undefined;
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
/** The asset selections and code snapshot a restored tab opens with. */
|
|
1031
|
+
function restorePane(id: string, target: Target, pane: SpritePaneState | undefined) {
|
|
1032
|
+
const selected = new Map<AssetKind, object>();
|
|
1033
|
+
const costume = pane?.costume === undefined ? undefined : target.getCostumes?.()[pane.costume];
|
|
1034
|
+
const sound = pane?.sound === undefined ? undefined : target.getSounds?.()[pane.sound];
|
|
1035
|
+
if (costume) selected.set('costumes', costume);
|
|
1036
|
+
if (sound) selected.set('sounds', sound);
|
|
1037
|
+
const assets = createAssetSelections(target, locale, {selected, layouts: new Map(), scroll: new Map()});
|
|
1038
|
+
let snapshots: View<Snapshot>['snapshots'];
|
|
1039
|
+
if (pane?.code && viewAdapter?.deserialize && pane.code !== null && typeof pane.code === 'object') {
|
|
1040
|
+
const view: SpriteTabView = {id, targetId: target.id, isStage: target.isStage, section: 0, groupId: '',
|
|
1041
|
+
active: false, groupActive: false};
|
|
1042
|
+
try {
|
|
1043
|
+
const snapshot = viewAdapter.deserialize(view, pane.code as Record<string, unknown>);
|
|
1044
|
+
if (snapshot !== undefined) snapshots = new Map([[0, {snapshot, view}]]);
|
|
1045
|
+
} catch { /* The code area opens as it does by default. */ }
|
|
1046
|
+
}
|
|
1047
|
+
return {assets, snapshots};
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
/**
|
|
1051
|
+
* The current workspace as it is saved, or nothing while it cannot be described yet. `live` also reads the code
|
|
1052
|
+
* area on screen, which only a save of the project file does.
|
|
1053
|
+
*/
|
|
1054
|
+
function captureLayout(live = false): WorkspaceLayout | undefined {
|
|
1003
1055
|
const targets = originalTargets();
|
|
1004
1056
|
if (!targets.length) return;
|
|
1005
1057
|
const serialized = dockview.toJSON();
|
|
@@ -1008,8 +1060,9 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
|
|
|
1008
1060
|
if (!docked) return;
|
|
1009
1061
|
const panels: Record<string, PanelReference> = Object.create(null);
|
|
1010
1062
|
for (const [id, view] of views) {
|
|
1063
|
+
const pane = capturePane(view, live);
|
|
1011
1064
|
panels[id] = {kind: 'sprite', name: targetName(view.target), stage: view.target.isStage, section: view.section,
|
|
1012
|
-
number: view.number};
|
|
1065
|
+
number: view.number, ...(pane && {pane})};
|
|
1013
1066
|
}
|
|
1014
1067
|
if (dockview.getPanel(stagePanelId)) panels[stagePanelId] = {kind: 'stage'};
|
|
1015
1068
|
const transient = new Set<string>();
|
|
@@ -1110,7 +1163,8 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
|
|
|
1110
1163
|
/** The tab arrangement and drawer state used to detect a change from the initial layout. */
|
|
1111
1164
|
function layoutShape(layout: WorkspaceLayout) {
|
|
1112
1165
|
type Node = WorkspaceLayout['grid']['root'];
|
|
1113
|
-
|
|
1166
|
+
// What a tab shows inside its panes is not a change of the arrangement.
|
|
1167
|
+
const reference = (id: string) => layout.panels[id] ? {...layout.panels[id], pane: undefined} : null;
|
|
1114
1168
|
const shape = (node: Node): unknown => Array.isArray(node.data) ? node.data.map(shape) :
|
|
1115
1169
|
node.data.views.map(reference);
|
|
1116
1170
|
return JSON.stringify([layout.grid.orientation, shape(layout.grid.root),
|
|
@@ -1154,7 +1208,7 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
|
|
|
1154
1208
|
function captureFileWorkspace(): WorkspaceLayout | undefined {
|
|
1155
1209
|
// Until the opened project's layout is restored, the tabs on screen are not that project's.
|
|
1156
1210
|
if (pendingRestore || !layoutSession) return fileLayout ? JSON.parse(fileLayout.layout) : undefined;
|
|
1157
|
-
return captureLayout();
|
|
1211
|
+
return captureLayout(true);
|
|
1158
1212
|
}
|
|
1159
1213
|
|
|
1160
1214
|
/** Keep the layout the project opened with, so leaving it unchanged adds no record. */
|
|
@@ -1202,7 +1256,7 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
|
|
|
1202
1256
|
continue;
|
|
1203
1257
|
}
|
|
1204
1258
|
resolvedSprites.set(id, {id, target, section: reference.section, number: reference.number,
|
|
1205
|
-
|
|
1259
|
+
...restorePane(id, target, reference.pane)});
|
|
1206
1260
|
panels[id] = {id, contentComponent: 'sprite', params: {icon: getIcon(target)}};
|
|
1207
1261
|
} else if (reference.kind === 'stage') {
|
|
1208
1262
|
if (id !== stagePanelId) return keepInitialLayout();
|
package/src/workspace-layout.ts
CHANGED
|
@@ -4,8 +4,17 @@ import type {DocumentPanelRequest} from './document-workspace.ts';
|
|
|
4
4
|
|
|
5
5
|
type Grid = SerializedDockview['grid'];
|
|
6
6
|
type Node = Grid['root'];
|
|
7
|
+
/** What a sprite tab showed inside its panes. Every field is optional, and a missing one keeps the default. */
|
|
8
|
+
export interface SpritePaneState {
|
|
9
|
+
/** The index of the costume or sound the tab had selected in that list. */
|
|
10
|
+
costume?: number;
|
|
11
|
+
sound?: number;
|
|
12
|
+
/** The code area as the host's tab adapter serialized it, such as its scroll position and zoom. */
|
|
13
|
+
code?: unknown;
|
|
14
|
+
}
|
|
15
|
+
|
|
7
16
|
export type PanelReference =
|
|
8
|
-
| {kind: 'sprite'; name: string; stage: boolean; section: number; number?: number}
|
|
17
|
+
| {kind: 'sprite'; name: string; stage: boolean; section: number; number?: number; pane?: SpritePaneState}
|
|
9
18
|
| {kind: 'stage'}
|
|
10
19
|
| ({kind: 'document'} & DocumentPanelRequest['reference']);
|
|
11
20
|
|
|
@@ -189,8 +198,9 @@ export function readWorkspaceLayout(raw: string, project?: string): WorkspaceLay
|
|
|
189
198
|
typeof panel.section === 'number' && Number.isInteger(panel.section) && panel.section >= 0 && panel.section <= 4) {
|
|
190
199
|
const number = panel.number;
|
|
191
200
|
if (number !== undefined && (typeof number !== 'number' || !Number.isSafeInteger(number) || number < 2)) return;
|
|
201
|
+
const pane = readPaneState(panel.pane);
|
|
192
202
|
panels[id] = {kind: 'sprite', name: panel.name, stage: panel.stage, section: panel.section,
|
|
193
|
-
...(typeof number === 'number' && {number})};
|
|
203
|
+
...(typeof number === 'number' && {number}), ...(pane && {pane})};
|
|
194
204
|
} else if (panel.kind === 'stage' && !hasStage) {
|
|
195
205
|
hasStage = true;
|
|
196
206
|
panels[id] = {kind: 'stage'};
|
|
@@ -212,6 +222,19 @@ export function readWorkspaceLayout(raw: string, project?: string): WorkspaceLay
|
|
|
212
222
|
...(popouts.length && {popouts})};
|
|
213
223
|
}
|
|
214
224
|
|
|
225
|
+
const assetIndex = (value: unknown): value is number =>
|
|
226
|
+
typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 && value <= 100000;
|
|
227
|
+
|
|
228
|
+
/** Keep only the fields of a pane state that have the right shape. The code state is checked by the host. */
|
|
229
|
+
function readPaneState(input: unknown): SpritePaneState | undefined {
|
|
230
|
+
if (!record(input)) return;
|
|
231
|
+
const pane: SpritePaneState = {};
|
|
232
|
+
if (assetIndex(input.costume)) pane.costume = input.costume;
|
|
233
|
+
if (assetIndex(input.sound)) pane.sound = input.sound;
|
|
234
|
+
if (record(input.code)) pane.code = input.code;
|
|
235
|
+
return Object.keys(pane).length ? pane : undefined;
|
|
236
|
+
}
|
|
237
|
+
|
|
215
238
|
function readStageDrawer(input: unknown): StageDrawerLayout | undefined {
|
|
216
239
|
if (!record(input) || typeof input.open !== 'boolean' || typeof input.selected !== 'boolean') return;
|
|
217
240
|
const {position, width, pinned} = input;
|