@kubohiroya/turbowarp-plus-editor-workbench 0.4.2 → 0.6.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 +23 -0
- package/README.md +23 -0
- package/dist/sprite-tabs.d.ts +11 -3
- package/dist/sprite-tabs.js +197 -41
- package/dist/workspace-layout-store.d.ts +51 -0
- package/dist/workspace-layout-store.js +177 -0
- package/dist/workspace-layout.d.ts +5 -2
- package/dist/workspace-layout.js +8 -4
- package/package.json +3 -2
- package/src/sprite-tabs.ts +196 -33
- package/src/workspace-layout-store.ts +221 -0
- package/src/workspace-layout.ts +9 -5
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps the Dockview workspace of each project in IndexedDB, so a project opens with the tabs it was left with.
|
|
3
|
+
*
|
|
4
|
+
* A project is told apart by the file or folder handle it was opened from, by the key the desktop app gives the
|
|
5
|
+
* opened path, or, without either, by its name together with the shape of its sprites. Only the projects used most
|
|
6
|
+
* recently are kept, so layouts of projects nobody opens any more are removed without asking.
|
|
7
|
+
*/
|
|
8
|
+
/** The database a person can delete from the browser's developer tools to forget every layout. */
|
|
9
|
+
export const workspaceLayoutDatabase = 'twPlusWorkspaceLayout';
|
|
10
|
+
export const workspaceLayoutLimit = 100;
|
|
11
|
+
export const workspaceLayoutMaxBytes = 256 * 1024;
|
|
12
|
+
const layoutsStore = 'layouts';
|
|
13
|
+
const preferencesStore = 'preferences';
|
|
14
|
+
const tabsPositionKey = 'tabsPosition';
|
|
15
|
+
const legacyKeys = ['twPlusDockviewLayout', 'twPlusDockviewLayout.tabs'];
|
|
16
|
+
const result = (request) => new Promise((resolve, reject) => {
|
|
17
|
+
request.onsuccess = () => resolve(request.result);
|
|
18
|
+
request.onerror = () => reject(request.error);
|
|
19
|
+
});
|
|
20
|
+
// Created with the transaction, before any request finishes, so the completion is never missed.
|
|
21
|
+
const completion = (transaction) => new Promise((resolve, reject) => {
|
|
22
|
+
transaction.oncomplete = () => resolve();
|
|
23
|
+
transaction.onerror = transaction.onabort = () => reject(transaction.error ?? new Error('The transaction was aborted.'));
|
|
24
|
+
});
|
|
25
|
+
function openDatabase(factory) {
|
|
26
|
+
const request = factory.open(workspaceLayoutDatabase, 1);
|
|
27
|
+
request.onupgradeneeded = () => {
|
|
28
|
+
const database = request.result;
|
|
29
|
+
const layouts = database.createObjectStore(layoutsStore, { keyPath: 'id', autoIncrement: true });
|
|
30
|
+
layouts.createIndex('name', 'name');
|
|
31
|
+
layouts.createIndex('desktopKey', 'desktopKey');
|
|
32
|
+
layouts.createIndex('lastUsedAt', 'lastUsedAt');
|
|
33
|
+
database.createObjectStore(preferencesStore);
|
|
34
|
+
};
|
|
35
|
+
return result(request);
|
|
36
|
+
}
|
|
37
|
+
const recent = (records) => records.sort((a, b) => b.lastUsedAt - a.lastUsedAt);
|
|
38
|
+
async function find(database, identity, signature) {
|
|
39
|
+
const transaction = database.transaction(layoutsStore, 'readonly');
|
|
40
|
+
const finished = completion(transaction);
|
|
41
|
+
const store = transaction.objectStore(layoutsStore);
|
|
42
|
+
const records = identity.kind === 'desktop' ?
|
|
43
|
+
await result(store.index('desktopKey').getAll(identity.key)) :
|
|
44
|
+
await result(store.index('name').getAll(identity.name));
|
|
45
|
+
await finished;
|
|
46
|
+
const candidates = recent(records.filter(record => record.kind === identity.kind));
|
|
47
|
+
if (identity.kind === 'desktop')
|
|
48
|
+
return candidates[0];
|
|
49
|
+
if (identity.kind === 'anonymous')
|
|
50
|
+
return candidates.find(record => record.signature === signature);
|
|
51
|
+
for (const record of candidates) {
|
|
52
|
+
try {
|
|
53
|
+
if (record.handle && await identity.handle.isSameEntry(record.handle))
|
|
54
|
+
return record;
|
|
55
|
+
}
|
|
56
|
+
catch { /* A handle from another browser profile or a removed file cannot be compared. */ }
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async function touch(database, id, now) {
|
|
60
|
+
const transaction = database.transaction(layoutsStore, 'readwrite');
|
|
61
|
+
const finished = completion(transaction);
|
|
62
|
+
const store = transaction.objectStore(layoutsStore);
|
|
63
|
+
const record = await result(store.get(id));
|
|
64
|
+
if (record)
|
|
65
|
+
store.put({ ...record, lastUsedAt: now });
|
|
66
|
+
await finished;
|
|
67
|
+
}
|
|
68
|
+
async function save(database, record, limit) {
|
|
69
|
+
const transaction = database.transaction(layoutsStore, 'readwrite');
|
|
70
|
+
const finished = completion(transaction);
|
|
71
|
+
const store = transaction.objectStore(layoutsStore);
|
|
72
|
+
const id = await result(store.put(record));
|
|
73
|
+
if (record.id === undefined) {
|
|
74
|
+
// In the same transaction as the new record, so tabs saving at once never keep more than the limit.
|
|
75
|
+
let excess = await result(store.count()) - limit;
|
|
76
|
+
if (excess > 0) {
|
|
77
|
+
const cursor = store.index('lastUsedAt').openCursor();
|
|
78
|
+
await new Promise((resolve, reject) => {
|
|
79
|
+
cursor.onerror = () => reject(cursor.error);
|
|
80
|
+
cursor.onsuccess = () => {
|
|
81
|
+
const current = cursor.result;
|
|
82
|
+
if (!current || excess <= 0)
|
|
83
|
+
return resolve();
|
|
84
|
+
if (current.primaryKey !== id) {
|
|
85
|
+
current.delete();
|
|
86
|
+
excess--;
|
|
87
|
+
}
|
|
88
|
+
current.continue();
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
await finished;
|
|
94
|
+
return id;
|
|
95
|
+
}
|
|
96
|
+
function identityFields(identity) {
|
|
97
|
+
if (identity.kind === 'desktop')
|
|
98
|
+
return { kind: identity.kind, name: identity.name, desktopKey: identity.key };
|
|
99
|
+
if (identity.kind === 'anonymous')
|
|
100
|
+
return { kind: identity.kind, name: identity.name };
|
|
101
|
+
return { kind: identity.kind, name: identity.name, handle: identity.handle };
|
|
102
|
+
}
|
|
103
|
+
const defaultLegacyStorage = () => {
|
|
104
|
+
try {
|
|
105
|
+
return globalThis.localStorage ?? null;
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
export function createWorkspaceLayoutStore({ indexedDB: factory = globalThis.indexedDB, limit = workspaceLayoutLimit, maxBytes = workspaceLayoutMaxBytes, now = Date.now, legacyStorage = defaultLegacyStorage() } = {}) {
|
|
112
|
+
let database;
|
|
113
|
+
const connect = () => database ??= factory ? openDatabase(factory).then(opened => {
|
|
114
|
+
opened.onversionchange = () => {
|
|
115
|
+
opened.close();
|
|
116
|
+
database = undefined;
|
|
117
|
+
};
|
|
118
|
+
return opened;
|
|
119
|
+
}, error => {
|
|
120
|
+
database = undefined;
|
|
121
|
+
throw error;
|
|
122
|
+
}) : Promise.reject(new Error('IndexedDB is unavailable.'));
|
|
123
|
+
// Writes run one at a time, so a record is created once even when changes come quickly.
|
|
124
|
+
let queue = Promise.resolve();
|
|
125
|
+
const enqueue = (task) => {
|
|
126
|
+
const next = queue.then(task, task);
|
|
127
|
+
queue = next.catch(() => undefined);
|
|
128
|
+
return next;
|
|
129
|
+
};
|
|
130
|
+
for (const key of legacyKeys) {
|
|
131
|
+
try {
|
|
132
|
+
legacyStorage?.removeItem(key);
|
|
133
|
+
}
|
|
134
|
+
catch { /* Storage can be unavailable. */ }
|
|
135
|
+
}
|
|
136
|
+
const bytes = (text) => new TextEncoder().encode(text).length;
|
|
137
|
+
return {
|
|
138
|
+
async open(identity, signature) {
|
|
139
|
+
const opened = await connect();
|
|
140
|
+
const found = await find(opened, identity, signature);
|
|
141
|
+
let id = found?.id;
|
|
142
|
+
let written = false;
|
|
143
|
+
if (id !== undefined)
|
|
144
|
+
await enqueue(() => touch(opened, id, now()));
|
|
145
|
+
const fields = identityFields(identity);
|
|
146
|
+
return {
|
|
147
|
+
layout: found?.layout,
|
|
148
|
+
exact: Boolean(found) && identity.kind !== 'anonymous',
|
|
149
|
+
get saved() { return id !== undefined || written; },
|
|
150
|
+
write(layout, nextSignature) {
|
|
151
|
+
if (bytes(layout) > maxBytes)
|
|
152
|
+
return Promise.resolve();
|
|
153
|
+
written = true;
|
|
154
|
+
return enqueue(async () => {
|
|
155
|
+
id = await save(opened, { ...fields, ...(id !== undefined && { id }), signature: nextSignature, layout,
|
|
156
|
+
lastUsedAt: now() }, limit);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
},
|
|
161
|
+
async readTabsPosition() {
|
|
162
|
+
const opened = await connect();
|
|
163
|
+
const transaction = opened.transaction(preferencesStore, 'readonly');
|
|
164
|
+
const value = await result(transaction.objectStore(preferencesStore).get(tabsPositionKey));
|
|
165
|
+
return value === 'left' || value === 'top' ? value : undefined;
|
|
166
|
+
},
|
|
167
|
+
writeTabsPosition(position) {
|
|
168
|
+
return enqueue(async () => {
|
|
169
|
+
const opened = await connect();
|
|
170
|
+
const transaction = opened.transaction(preferencesStore, 'readwrite');
|
|
171
|
+
const finished = completion(transaction);
|
|
172
|
+
transaction.objectStore(preferencesStore).put(position, tabsPositionKey);
|
|
173
|
+
await finished;
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
}
|
|
@@ -55,6 +55,9 @@ export declare function workspaceWithoutTransientPanels(grid: Grid, activeGroup:
|
|
|
55
55
|
grid: Grid;
|
|
56
56
|
activeGroup: string;
|
|
57
57
|
} | undefined;
|
|
58
|
-
/**
|
|
59
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Rebuild only the supported docked grid and logical panel references from storage. Without `project`, a layout
|
|
60
|
+
* of any project is read; it is for a project already found by its file, folder or desktop key.
|
|
61
|
+
*/
|
|
62
|
+
export declare function readWorkspaceLayout(raw: string, project?: string): WorkspaceLayout | undefined;
|
|
60
63
|
export {};
|
package/dist/workspace-layout.js
CHANGED
|
@@ -98,9 +98,12 @@ const validId = (value) => typeof value === 'string' && value.length > 0 && valu
|
|
|
98
98
|
const coordinate = (value) => typeof value === 'number' && Number.isFinite(value) && Math.abs(value) <= 100000;
|
|
99
99
|
const size = (value) => typeof value === 'number' && Number.isFinite(value) && value >= 0 && value <= 100000;
|
|
100
100
|
const record = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
101
|
-
/**
|
|
101
|
+
/**
|
|
102
|
+
* Rebuild only the supported docked grid and logical panel references from storage. Without `project`, a layout
|
|
103
|
+
* of any project is read; it is for a project already found by its file, folder or desktop key.
|
|
104
|
+
*/
|
|
102
105
|
export function readWorkspaceLayout(raw, project) {
|
|
103
|
-
if (raw.length >
|
|
106
|
+
if (raw.length > 256 * 1024)
|
|
104
107
|
return;
|
|
105
108
|
let input;
|
|
106
109
|
try {
|
|
@@ -109,7 +112,8 @@ export function readWorkspaceLayout(raw, project) {
|
|
|
109
112
|
catch {
|
|
110
113
|
return;
|
|
111
114
|
}
|
|
112
|
-
if (!record(input) || input.version !== 1 || input.project !==
|
|
115
|
+
if (!record(input) || input.version !== 1 || typeof input.project !== 'string' ||
|
|
116
|
+
(project !== undefined && input.project !== project) || !record(input.grid) ||
|
|
113
117
|
!record(input.panels) || !validId(input.activeGroup))
|
|
114
118
|
return;
|
|
115
119
|
const { width, height, orientation } = input.grid;
|
|
@@ -185,7 +189,7 @@ export function readWorkspaceLayout(raw, project) {
|
|
|
185
189
|
const popouts = readPopouts(input.popouts, panels);
|
|
186
190
|
if (!popouts)
|
|
187
191
|
return;
|
|
188
|
-
return { version: 1, project, grid: { root, width, height, orientation }, activeGroup: input.activeGroup,
|
|
192
|
+
return { version: 1, project: input.project, grid: { root, width, height, orientation }, activeGroup: input.activeGroup,
|
|
189
193
|
editingView: typeof editingView === 'string' ? editingView : undefined, panels,
|
|
190
194
|
...(popouts.length && { popouts }) };
|
|
191
195
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubohiroya/turbowarp-plus-editor-workbench",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.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",
|
|
@@ -64,7 +64,8 @@
|
|
|
64
64
|
"typescript": "^6.0.3",
|
|
65
65
|
"jsdom": "26.1.0",
|
|
66
66
|
"@types/react": "18.3.31",
|
|
67
|
-
"@types/react-dom": "18.3.7"
|
|
67
|
+
"@types/react-dom": "18.3.7",
|
|
68
|
+
"fake-indexeddb": "6.2.6"
|
|
68
69
|
},
|
|
69
70
|
"repository": {
|
|
70
71
|
"type": "git",
|
package/src/sprite-tabs.ts
CHANGED
|
@@ -15,6 +15,8 @@ import type {DocumentPanelRequest} from './document-workspace.ts';
|
|
|
15
15
|
import {dockPopoutGroups, positionOnScreen, readWorkspaceLayout, workspaceWithoutTransientPanels}
|
|
16
16
|
from './workspace-layout.ts';
|
|
17
17
|
import type {PanelReference, SavedPopout, WorkspaceLayout} from './workspace-layout.ts';
|
|
18
|
+
import {createWorkspaceLayoutStore} from './workspace-layout-store.ts';
|
|
19
|
+
import type {WorkspaceLayoutSession, WorkspaceLayoutStore, WorkspaceProjectIdentity} from './workspace-layout-store.ts';
|
|
18
20
|
import type {SpriteVariableEdit, SpriteVariableEditError} from '@kubohiroya/turbowarp-plus-editor-project-model/sprite-variable-edit';
|
|
19
21
|
import {SpriteMessagePane} from '@kubohiroya/turbowarp-plus-editor-sprite-panes/sprite-message-pane';
|
|
20
22
|
import type {MessageRenameError} from '@kubohiroya/turbowarp-plus-editor-sprite-panes/sprite-message-pane';
|
|
@@ -22,6 +24,8 @@ import type {MessageUse, ProjectMessage, TargetMessages} from '@kubohiroya/turbo
|
|
|
22
24
|
import {createRenameMenuItem, spriteRenameLabel} from './target-tab-menu.ts';
|
|
23
25
|
|
|
24
26
|
export {getDocumentWorkspace} from './document-workspace.ts';
|
|
27
|
+
export {createWorkspaceLayoutStore, workspaceLayoutDatabase, workspaceLayoutLimit} from './workspace-layout-store.ts';
|
|
28
|
+
export type {WorkspaceLayoutStore, WorkspaceProjectIdentity} from './workspace-layout-store.ts';
|
|
25
29
|
export type {SpriteAssetKind, SpriteAssetPreviewItem} from '@kubohiroya/turbowarp-plus-editor-sprite-panes/sprite-asset-pane';
|
|
26
30
|
export {editSpriteVariable} from '@kubohiroya/turbowarp-plus-editor-project-model/sprite-variable-edit';
|
|
27
31
|
|
|
@@ -145,9 +149,12 @@ interface TabOptions<Snapshot = unknown> {
|
|
|
145
149
|
resize(): void;
|
|
146
150
|
reopenLabel: string;
|
|
147
151
|
multiView?: boolean;
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
152
|
+
/** Keep the workspace of each project and reopen it with the project. */
|
|
153
|
+
workspaceLayout?: {
|
|
154
|
+
/** IndexedDB by default. */
|
|
155
|
+
store?: WorkspaceLayoutStore;
|
|
156
|
+
/** The file, folder or desktop key the loaded project came from. Without one, it is known by its name. */
|
|
157
|
+
identify?(): WorkspaceProjectIdentity | undefined;
|
|
151
158
|
isProjectLoading?(): boolean;
|
|
152
159
|
subscribeProject?(listener: () => void): (() => void) | undefined;
|
|
153
160
|
};
|
|
@@ -266,21 +273,36 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
|
|
|
266
273
|
let nextView = 0;
|
|
267
274
|
let renameDialog: HTMLDialogElement | undefined;
|
|
268
275
|
const text = options.renameLabels ?? renameLabels.en;
|
|
269
|
-
const persistence = multiView && options.
|
|
276
|
+
const persistence = multiView && options.workspaceLayout;
|
|
277
|
+
const layoutStore = persistence ? persistence.store ?? createWorkspaceLayoutStore() : undefined;
|
|
278
|
+
/** Where the layout of the loaded project is saved, once that project is identified. */
|
|
279
|
+
let layoutSession: WorkspaceLayoutSession | undefined;
|
|
280
|
+
/** Counts project loads, so a lookup that ends after another project opened is dropped. */
|
|
281
|
+
let projectGeneration = 0;
|
|
282
|
+
let openingGeneration: number | undefined;
|
|
283
|
+
let projectLoading = false;
|
|
284
|
+
/** The shape of the layout the project opened with, which is not worth a record of its own. */
|
|
285
|
+
let initialShape: string | undefined;
|
|
286
|
+
let disposed = false;
|
|
270
287
|
const window = document.defaultView;
|
|
271
288
|
const documentWorkspace = multiView && getDocumentWorkspace(vm);
|
|
272
289
|
let pendingRestore = Boolean(persistence);
|
|
273
290
|
let restoreFrame: number | undefined;
|
|
274
291
|
let restoreTimeout: number | undefined;
|
|
275
292
|
let saveFrame: number | undefined;
|
|
293
|
+
/** Layout writes handed to the store that have not finished yet. */
|
|
294
|
+
let layoutWrites = 0;
|
|
295
|
+
/** Whether the tab position chosen for every project is still being read. */
|
|
296
|
+
let readingTabs = Boolean(layoutStore);
|
|
297
|
+
let lastWrite: Promise<unknown> = Promise.resolve();
|
|
298
|
+
const settledWaiters: (() => void)[] = [];
|
|
276
299
|
const openingGroups = new Set<string>();
|
|
277
300
|
/** Panels that were in a separate window when the layout was saved, until the user reopens them. */
|
|
278
301
|
const reopenable = new Map<string, SavedPopout>();
|
|
279
302
|
const originalTargets = () => vm.runtime.targets.filter(target => target.isOriginal);
|
|
280
|
-
// Whether tabs stand in a column at the left of each split. It is the user's preference
|
|
281
|
-
const tabsKey = persistence ? persistence.key + '.tabs' : undefined;
|
|
303
|
+
// Whether tabs stand in a column at the left of each split. It is the user's preference for every project.
|
|
282
304
|
let verticalTabs = false;
|
|
283
|
-
|
|
305
|
+
let tabsChosen = false;
|
|
284
306
|
|
|
285
307
|
const dockview = createDockview(root, {
|
|
286
308
|
theme: themeLight,
|
|
@@ -464,12 +486,15 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
|
|
|
464
486
|
return {...menu, init() {}};
|
|
465
487
|
}
|
|
466
488
|
|
|
467
|
-
function setVerticalTabs(vertical: boolean) {
|
|
489
|
+
function setVerticalTabs(vertical: boolean, chosen = true) {
|
|
468
490
|
verticalTabs = vertical;
|
|
469
491
|
const position = vertical ? 'left' : 'top';
|
|
470
492
|
dockview.updateOptions({defaultHeaderPosition: position});
|
|
471
493
|
for (const group of dockview.groups) group.api.setHeaderPosition(position);
|
|
472
|
-
|
|
494
|
+
if (chosen) {
|
|
495
|
+
tabsChosen = true;
|
|
496
|
+
layoutStore?.writeTabsPosition(position).catch(() => undefined);
|
|
497
|
+
}
|
|
473
498
|
refreshSurfaces();
|
|
474
499
|
}
|
|
475
500
|
|
|
@@ -861,10 +886,10 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
|
|
|
861
886
|
scheduleSave();
|
|
862
887
|
}
|
|
863
888
|
|
|
864
|
-
|
|
865
|
-
|
|
889
|
+
/** The current workspace as it is saved, or nothing while it cannot be described yet. */
|
|
890
|
+
function captureLayout(): WorkspaceLayout | undefined {
|
|
866
891
|
const targets = originalTargets();
|
|
867
|
-
if (!
|
|
892
|
+
if (!targets.length) return;
|
|
868
893
|
const serialized = dockview.toJSON();
|
|
869
894
|
if (serialized.grid.width <= 0 || serialized.grid.height <= 0) return;
|
|
870
895
|
const docked = dockPopoutGroups(serialized.grid, serialized.activeGroup, serialized.popoutGroups);
|
|
@@ -889,15 +914,66 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
|
|
|
889
914
|
.map(popout => ({...popout, panels: popout.panels.filter(id =>
|
|
890
915
|
reopenable.get(id) === popout && views.has(id) && panels[id]?.kind === 'sprite')}))
|
|
891
916
|
.filter(popout => popout.panels.length);
|
|
892
|
-
|
|
917
|
+
return {version: 1, project: projectSignature(targets), ...saved,
|
|
893
918
|
editingView: active?.id, panels, ...(popouts.length && {popouts})};
|
|
894
|
-
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
function writeLayout() {
|
|
922
|
+
saveFrame = undefined;
|
|
923
|
+
try {
|
|
924
|
+
if (!persistence || !layoutSession || pendingRestore || syncing || persistence.isProjectLoading?.()) return;
|
|
925
|
+
const snapshot = captureLayout();
|
|
926
|
+
if (!snapshot) return;
|
|
927
|
+
// Opening a project and leaving its tabs as they were is not worth a record that pushes out another project.
|
|
928
|
+
if (!layoutSession.saved && initialShape !== undefined && layoutShape(snapshot) === initialShape) return;
|
|
929
|
+
layoutWrites++;
|
|
930
|
+
lastWrite = layoutSession.write(JSON.stringify(snapshot), snapshot.project).catch(() => undefined).finally(() => {
|
|
931
|
+
layoutWrites--;
|
|
932
|
+
updateLayoutState();
|
|
933
|
+
});
|
|
934
|
+
} finally {
|
|
935
|
+
updateLayoutState();
|
|
936
|
+
}
|
|
895
937
|
}
|
|
896
938
|
|
|
897
939
|
function scheduleSave() {
|
|
898
|
-
if (persistence && !pendingRestore && saveFrame === undefined) {
|
|
940
|
+
if (persistence && layoutSession && !pendingRestore && saveFrame === undefined) {
|
|
899
941
|
saveFrame = window.requestAnimationFrame(writeLayout);
|
|
942
|
+
updateLayoutState();
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
const layoutBusy = () => !disposed && (pendingRestore || readingTabs || saveFrame !== undefined || layoutWrites > 0);
|
|
947
|
+
|
|
948
|
+
/**
|
|
949
|
+
* Tell the host and its tests when the layout of the loaded project is restored and every change is saved.
|
|
950
|
+
* Dockview reports a layout change in a microtask after the change, so being idle is confirmed only after
|
|
951
|
+
* that report had its chance to schedule a save.
|
|
952
|
+
*/
|
|
953
|
+
function updateLayoutState() {
|
|
954
|
+
if (!persistence) return;
|
|
955
|
+
if (layoutBusy()) {
|
|
956
|
+
root.dataset.workspaceLayout = 'pending';
|
|
957
|
+
return;
|
|
900
958
|
}
|
|
959
|
+
queueMicrotask(() => {
|
|
960
|
+
if (layoutBusy()) return;
|
|
961
|
+
root.dataset.workspaceLayout = 'settled';
|
|
962
|
+
for (const resolve of settledWaiters.splice(0)) resolve();
|
|
963
|
+
});
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
/** Resolves once the loaded project's layout is restored and its changes are written to the store. */
|
|
967
|
+
function layoutSettled() {
|
|
968
|
+
if (!persistence) return Promise.resolve();
|
|
969
|
+
if (saveFrame !== undefined) {
|
|
970
|
+
window.cancelAnimationFrame(saveFrame);
|
|
971
|
+
writeLayout();
|
|
972
|
+
}
|
|
973
|
+
return new Promise<void>(resolve => {
|
|
974
|
+
settledWaiters.push(resolve);
|
|
975
|
+
updateLayoutState();
|
|
976
|
+
}).then(() => lastWrite).then(() => undefined);
|
|
901
977
|
}
|
|
902
978
|
|
|
903
979
|
function flushLayout() {
|
|
@@ -910,45 +986,102 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
|
|
|
910
986
|
function finishRestore() {
|
|
911
987
|
pendingRestore = false;
|
|
912
988
|
window.clearTimeout(restoreTimeout);
|
|
989
|
+
restoreTimeout = undefined;
|
|
990
|
+
updateLayoutState();
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
/** The panels and splits of a layout, without sizes or selection, to tell whether the user changed it. */
|
|
994
|
+
function layoutShape(layout: WorkspaceLayout) {
|
|
995
|
+
type Node = WorkspaceLayout['grid']['root'];
|
|
996
|
+
const reference = (id: string) => layout.panels[id] ?? null;
|
|
997
|
+
const shape = (node: Node): unknown => Array.isArray(node.data) ? node.data.map(shape) :
|
|
998
|
+
node.data.views.map(reference);
|
|
999
|
+
return JSON.stringify([layout.grid.orientation, shape(layout.grid.root),
|
|
1000
|
+
(layout.popouts ?? []).map(popout => popout.panels.map(reference))]);
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
function identifyProject(): WorkspaceProjectIdentity {
|
|
1004
|
+
try {
|
|
1005
|
+
const identity = persistence && persistence.identify?.();
|
|
1006
|
+
if (identity) return identity;
|
|
1007
|
+
} catch { /* The project is still known by its name and sprites. */ }
|
|
1008
|
+
return {kind: 'anonymous', name: ''};
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
/** Find where the loaded project keeps its layout. It runs once for each project load. */
|
|
1012
|
+
function openLayoutSession(signature: string) {
|
|
1013
|
+
if (!layoutStore || openingGeneration === projectGeneration) return;
|
|
1014
|
+
const generation = openingGeneration = projectGeneration;
|
|
1015
|
+
layoutStore.open(identifyProject(), signature).then(session => {
|
|
1016
|
+
if (disposed || generation !== projectGeneration) return;
|
|
1017
|
+
layoutSession = session;
|
|
1018
|
+
scheduleRestore();
|
|
1019
|
+
}, () => {
|
|
1020
|
+
// Without IndexedDB, the project opens with the default layout and nothing is saved.
|
|
1021
|
+
if (!disposed && generation === projectGeneration) finishRestore();
|
|
1022
|
+
});
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
/** Keep the layout the project opened with, so leaving it unchanged adds no record. */
|
|
1026
|
+
function keepInitialLayout() {
|
|
1027
|
+
const layout = captureLayout();
|
|
1028
|
+
initialShape = layout && layoutShape(layout);
|
|
1029
|
+
finishRestore();
|
|
913
1030
|
}
|
|
914
1031
|
|
|
915
1032
|
function restoreLayout() {
|
|
916
1033
|
restoreFrame = undefined;
|
|
917
|
-
if (!
|
|
1034
|
+
if (!persistence || persistence.isProjectLoading?.()) return;
|
|
918
1035
|
const targets = originalTargets();
|
|
919
1036
|
if (!targets.length || !vm.editingTarget) return;
|
|
1037
|
+
const signature = projectSignature(targets);
|
|
1038
|
+
if (!layoutSession) return openLayoutSession(signature);
|
|
1039
|
+
if (!pendingRestore) return;
|
|
920
1040
|
reopenable.clear();
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
if (!stored) return finishRestore();
|
|
1041
|
+
// A project found by its file, folder or desktop key keeps its tabs even after its sprites were renamed.
|
|
1042
|
+
const exact = layoutSession.exact;
|
|
1043
|
+
const raw = layoutSession.layout;
|
|
1044
|
+
const stored = raw ? readWorkspaceLayout(raw, exact ? undefined : signature) : undefined;
|
|
1045
|
+
if (!stored) return keepInitialLayout();
|
|
927
1046
|
const resolvedSprites = new Map<string, View<Snapshot>>();
|
|
928
1047
|
const resolvedDocuments = new Map<string, DocumentPanelRequest>();
|
|
929
1048
|
const panels: SerializedDockview['panels'] = Object.create(null);
|
|
1049
|
+
const dropped = new Set<string>();
|
|
930
1050
|
for (const [id, reference] of Object.entries(stored.panels)) {
|
|
931
1051
|
if (reference.kind === 'sprite') {
|
|
932
1052
|
const target = targets.find(item => item.isStage === reference.stage && targetName(item) === reference.name);
|
|
933
|
-
if (
|
|
1053
|
+
if (id === stagePanelId) return keepInitialLayout();
|
|
1054
|
+
if (!target || !sectionAvailable(reference.section)) {
|
|
1055
|
+
if (!exact) return keepInitialLayout();
|
|
1056
|
+
dropped.add(id);
|
|
1057
|
+
continue;
|
|
1058
|
+
}
|
|
934
1059
|
resolvedSprites.set(id, {id, target, section: reference.section, number: reference.number,
|
|
935
1060
|
assets: createAssetSelections(target, locale)});
|
|
936
1061
|
panels[id] = {id, contentComponent: 'sprite', params: {icon: getIcon(target)}};
|
|
937
1062
|
} else if (reference.kind === 'stage') {
|
|
938
|
-
if (
|
|
1063
|
+
if (id !== stagePanelId) return keepInitialLayout();
|
|
1064
|
+
if (!stagePane) {
|
|
1065
|
+
if (!exact) return keepInitialLayout();
|
|
1066
|
+
dropped.add(id);
|
|
1067
|
+
continue;
|
|
1068
|
+
}
|
|
939
1069
|
panels[id] = {id, title: stagePane.title, contentComponent: stagePanelId, renderer: 'always'};
|
|
940
1070
|
} else {
|
|
941
|
-
if (id === stagePanelId) return
|
|
1071
|
+
if (id === stagePanelId) return keepInitialLayout();
|
|
942
1072
|
const request = documentWorkspace && documentWorkspace.resolve(reference);
|
|
943
1073
|
if (!request) {
|
|
944
1074
|
// The project can finish loading before the Blockly host publishes its document catalog.
|
|
945
|
-
restoreTimeout ??= window.setTimeout(
|
|
1075
|
+
restoreTimeout ??= window.setTimeout(keepInitialLayout, 2000);
|
|
946
1076
|
return;
|
|
947
1077
|
}
|
|
948
1078
|
resolvedDocuments.set(id, request);
|
|
949
1079
|
panels[id] = {id, title: documentPanelTitle(request), contentComponent: documentComponent, renderer: 'always'};
|
|
950
1080
|
}
|
|
951
1081
|
}
|
|
1082
|
+
const kept = workspaceWithoutTransientPanels(stored.grid, stored.activeGroup, dropped, stored.editingView);
|
|
1083
|
+
if (!kept) return keepInitialLayout();
|
|
1084
|
+
const editingView = stored.editingView && !dropped.has(stored.editingView) ? stored.editingView : undefined;
|
|
952
1085
|
assignTabNumbers(resolvedSprites.values(), new Set([...targets.map(baseTitle),
|
|
953
1086
|
...Object.values(panels).map(panel => panel.title)]));
|
|
954
1087
|
for (const [id, view] of resolvedSprites) panels[id].title = tabTitle(view);
|
|
@@ -961,12 +1094,12 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
|
|
|
961
1094
|
closed = new WeakSet(targets.filter(target => !represented.has(target)));
|
|
962
1095
|
for (const [id, view] of resolvedSprites) views.set(id, view);
|
|
963
1096
|
for (const [id, request] of resolvedDocuments) documents.set(id, {request});
|
|
964
|
-
dockview.fromJSON({grid:
|
|
1097
|
+
dockview.fromJSON({grid: kept.grid, activeGroup: kept.activeGroup, panels});
|
|
965
1098
|
for (const popout of stored.popouts ?? []) {
|
|
966
1099
|
for (const id of popout.panels) if (views.has(id)) reopenable.set(id, popout);
|
|
967
1100
|
}
|
|
968
1101
|
const selected = dockview.activePanel;
|
|
969
|
-
show(views.has(selected?.id) ? selected : dockview.getPanel(
|
|
1102
|
+
show(views.has(selected?.id) ? selected : (editingView && dockview.getPanel(editingView)) ?? selected);
|
|
970
1103
|
} catch {
|
|
971
1104
|
dockview.clear();
|
|
972
1105
|
for (const view of views.values()) disposeView(view);
|
|
@@ -981,7 +1114,27 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
|
|
|
981
1114
|
}
|
|
982
1115
|
|
|
983
1116
|
function scheduleRestore() {
|
|
984
|
-
if (pendingRestore && restoreFrame === undefined)
|
|
1117
|
+
if (persistence && (pendingRestore || !layoutSession) && restoreFrame === undefined) {
|
|
1118
|
+
restoreFrame = window.requestAnimationFrame(restoreLayout);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
/** Each loaded project has its own layout, so a new load reopens the tabs that project was left with. */
|
|
1123
|
+
function projectChanged() {
|
|
1124
|
+
const loading = Boolean(persistence && persistence.isProjectLoading?.());
|
|
1125
|
+
if (loading && !projectLoading) {
|
|
1126
|
+
projectGeneration++;
|
|
1127
|
+
layoutSession = undefined;
|
|
1128
|
+
initialShape = undefined;
|
|
1129
|
+
pendingRestore = true;
|
|
1130
|
+
if (saveFrame !== undefined) window.cancelAnimationFrame(saveFrame);
|
|
1131
|
+
saveFrame = undefined;
|
|
1132
|
+
window.clearTimeout(restoreTimeout);
|
|
1133
|
+
restoreTimeout = undefined;
|
|
1134
|
+
updateLayoutState();
|
|
1135
|
+
}
|
|
1136
|
+
projectLoading = loading;
|
|
1137
|
+
scheduleRestore();
|
|
985
1138
|
}
|
|
986
1139
|
|
|
987
1140
|
function refresh() {
|
|
@@ -1060,15 +1213,23 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
|
|
|
1060
1213
|
});
|
|
1061
1214
|
const disconnectDocuments = documentWorkspace && documentWorkspace.connect(openDocument);
|
|
1062
1215
|
const unsubscribeDocuments = persistence && documentWorkspace && documentWorkspace.subscribe(scheduleRestore);
|
|
1063
|
-
|
|
1216
|
+
projectLoading = Boolean(persistence && persistence.isProjectLoading?.());
|
|
1217
|
+
const unsubscribeProject = persistence && persistence.subscribeProject?.(projectChanged);
|
|
1218
|
+
layoutStore?.readTabsPosition().then(position => {
|
|
1219
|
+
if (!disposed && !tabsChosen && position === 'left') setVerticalTabs(true, false);
|
|
1220
|
+
}, () => undefined).finally(() => {
|
|
1221
|
+
readingTabs = false;
|
|
1222
|
+
updateLayoutState();
|
|
1223
|
+
});
|
|
1064
1224
|
if (persistence) window.addEventListener('pagehide', flushLayout);
|
|
1225
|
+
updateLayoutState();
|
|
1065
1226
|
root.addEventListener('click', guardDocumentClose, true);
|
|
1066
1227
|
root.addEventListener('keydown', guardDocumentClose, true);
|
|
1067
1228
|
vm.on('targetsUpdate', refresh);
|
|
1068
1229
|
refresh();
|
|
1069
1230
|
return {
|
|
1070
1231
|
select, selectFromList, targetTabs, openStage, closeStage, split, closeSplit, activateView,
|
|
1071
|
-
setViewSection, syncActiveSection, refreshSurfaces,
|
|
1232
|
+
setViewSection, syncActiveSection, refreshSurfaces, layoutSettled,
|
|
1072
1233
|
rename(id: string) {
|
|
1073
1234
|
const target = vm.runtime.targets.find(item => item.id === id);
|
|
1074
1235
|
if (target) openRenameDialog(target);
|
|
@@ -1082,6 +1243,8 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
|
|
|
1082
1243
|
views: () => [...views.values()].map(view => describe(view)),
|
|
1083
1244
|
dispose() {
|
|
1084
1245
|
flushLayout();
|
|
1246
|
+
disposed = true;
|
|
1247
|
+
for (const resolve of settledWaiters.splice(0)) resolve();
|
|
1085
1248
|
window.cancelAnimationFrame(restoreFrame);
|
|
1086
1249
|
window.clearTimeout(restoreTimeout);
|
|
1087
1250
|
if (persistence) window.removeEventListener('pagehide', flushLayout);
|
|
@@ -1237,7 +1400,7 @@ function SpriteTabs<Snapshot>(props: SpriteTabsProps<Snapshot>) {
|
|
|
1237
1400
|
resize: () => latest.current.resize(),
|
|
1238
1401
|
reopenLabel: props.reopenLabel,
|
|
1239
1402
|
multiView: props.multiView,
|
|
1240
|
-
|
|
1403
|
+
workspaceLayout: props.workspaceLayout,
|
|
1241
1404
|
stagePane: props.stagePane,
|
|
1242
1405
|
variables: hasVariables,
|
|
1243
1406
|
messages: hasMessages,
|
|
@@ -1258,7 +1421,7 @@ function SpriteTabs<Snapshot>(props: SpriteTabsProps<Snapshot>) {
|
|
|
1258
1421
|
instance.dispose();
|
|
1259
1422
|
};
|
|
1260
1423
|
}, [content, props.enabled, props.vm, props.reopenLabel, props.multiView, props.locale,
|
|
1261
|
-
props.viewAdapter, props.stagePane, props.
|
|
1424
|
+
props.viewAdapter, props.stagePane, props.workspaceLayout, props.renameLabels, props.popouts, props.className,
|
|
1262
1425
|
hasVariables, hasMessages, hasToolbar, hasAssetPreview]);
|
|
1263
1426
|
React.useLayoutEffect(() => {
|
|
1264
1427
|
controller.current?.syncActiveSection(props.activeSection);
|