@kubohiroya/turbowarp-plus-editor-workbench 0.5.0 → 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 CHANGED
@@ -90,6 +90,7 @@ workspaceLayout: {
90
90
  - 最後に使ったのが新しい順に 100 件を残す。開いただけでも使ったとみなす。古い記録は新しい記録を作るときに消す。256 KB を超えるレイアウトは保存しない。
91
91
  - タブの位置(上/左)は全作品で共通の設定にする。
92
92
  - IndexedDB が使えないときは既定のレイアウトで開き、何も保存しない。
93
+ - IndexedDB の応答は非同期なので、保存と再現は変更より後に終わる。`controller.layoutSettled()` は、読み込んだ作品のレイアウトの再現、タブの位置の読み込み、すべての変更の書き込みが終わると解決する。同じ状態を作業領域の要素の `data-workspace-layout`(`"pending"` または `"settled"`)にも示すので、ブラウザーテストは記録が現れるまでデータベースを読み直すのではなく、この属性を待つ。
93
94
  - すべてのレイアウトを消すには、ブラウザーの開発者ツールで `twPlusWorkspaceLayout` データベースを削除する(Chrome では Application の IndexedDB)。コンソールで `indexedDB.deleteDatabase('twPlusWorkspaceLayout')` を実行してもよい。
94
95
  - 以前の版は `localStorage`(`twPlusDockviewLayout` と `twPlusDockviewLayout.tabs`)に 1 件だけ保存していた。これらのキーは移さずに消す。
95
96
 
package/README.md CHANGED
@@ -92,6 +92,7 @@ workspaceLayout: {
92
92
  - The 100 most recently used projects keep their layout; opening a project counts as using it. Older records are removed when a new one is made. A layout over 256 KB is not saved.
93
93
  - The tab position (top or left) is one preference for every project.
94
94
  - Without IndexedDB, projects open with the default layout and nothing is saved.
95
+ - Saving and restoring finish later than the change, because IndexedDB answers asynchronously. `controller.layoutSettled()` resolves once the loaded project's layout is restored, the tab position is read and every change is written. The workspace element carries the same state as `data-workspace-layout="pending"` or `"settled"`, so a browser test waits for that attribute instead of reading the database until the record appears.
95
96
  - To forget every layout, delete the `twPlusWorkspaceLayout` database from the browser's developer tools (in Chrome: Application, then IndexedDB), or run `indexedDB.deleteDatabase('twPlusWorkspaceLayout')` in the console.
96
97
  - Earlier versions kept a single layout in `localStorage` (`twPlusDockviewLayout` and `twPlusDockviewLayout.tabs`). Those keys are removed without being moved.
97
98
 
@@ -152,6 +152,7 @@ export declare function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HT
152
152
  setViewSection: (id: string, section: number) => boolean;
153
153
  syncActiveSection: (section: number) => void;
154
154
  refreshSurfaces: () => void;
155
+ layoutSettled: () => Promise<any>;
155
156
  rename(id: string): void;
156
157
  selectAsset(id: string, kind: AssetKind, index: number): void;
157
158
  currentView: () => SpriteTabView;
@@ -207,6 +208,7 @@ export declare function useSpriteTabs<Snapshot>({ available, enabled, locale, ..
207
208
  setViewSection: (id: string, section: number) => boolean;
208
209
  syncActiveSection: (section: number) => void;
209
210
  refreshSurfaces: () => void;
211
+ layoutSettled: () => Promise<any>;
210
212
  rename(id: string): void;
211
213
  selectAsset(id: string, kind: AssetKind, index: number): void;
212
214
  currentView: () => SpriteTabView;
@@ -138,6 +138,12 @@ export function mountSpriteTabs(root, content, options) {
138
138
  let restoreFrame;
139
139
  let restoreTimeout;
140
140
  let saveFrame;
141
+ /** Layout writes handed to the store that have not finished yet. */
142
+ let layoutWrites = 0;
143
+ /** Whether the tab position chosen for every project is still being read. */
144
+ let readingTabs = Boolean(layoutStore);
145
+ let lastWrite = Promise.resolve();
146
+ const settledWaiters = [];
141
147
  const openingGroups = new Set();
142
148
  /** Panels that were in a separate window when the layout was saved, until the user reopens them. */
143
149
  const reopenable = new Map();
@@ -806,21 +812,65 @@ export function mountSpriteTabs(root, content, options) {
806
812
  }
807
813
  function writeLayout() {
808
814
  saveFrame = undefined;
809
- if (!persistence || !layoutSession || pendingRestore || syncing || persistence.isProjectLoading?.())
810
- return;
811
- const snapshot = captureLayout();
812
- if (!snapshot)
813
- return;
814
- // Opening a project and leaving its tabs as they were is not worth a record that pushes out another project.
815
- if (!layoutSession.saved && initialShape !== undefined && layoutShape(snapshot) === initialShape)
816
- return;
817
- layoutSession.write(JSON.stringify(snapshot), snapshot.project).catch(() => undefined);
815
+ try {
816
+ if (!persistence || !layoutSession || pendingRestore || syncing || persistence.isProjectLoading?.())
817
+ return;
818
+ const snapshot = captureLayout();
819
+ if (!snapshot)
820
+ return;
821
+ // Opening a project and leaving its tabs as they were is not worth a record that pushes out another project.
822
+ if (!layoutSession.saved && initialShape !== undefined && layoutShape(snapshot) === initialShape)
823
+ return;
824
+ layoutWrites++;
825
+ lastWrite = layoutSession.write(JSON.stringify(snapshot), snapshot.project).catch(() => undefined).finally(() => {
826
+ layoutWrites--;
827
+ updateLayoutState();
828
+ });
829
+ }
830
+ finally {
831
+ updateLayoutState();
832
+ }
818
833
  }
819
834
  function scheduleSave() {
820
835
  if (persistence && layoutSession && !pendingRestore && saveFrame === undefined) {
821
836
  saveFrame = window.requestAnimationFrame(writeLayout);
837
+ updateLayoutState();
822
838
  }
823
839
  }
840
+ const layoutBusy = () => !disposed && (pendingRestore || readingTabs || saveFrame !== undefined || layoutWrites > 0);
841
+ /**
842
+ * Tell the host and its tests when the layout of the loaded project is restored and every change is saved.
843
+ * Dockview reports a layout change in a microtask after the change, so being idle is confirmed only after
844
+ * that report had its chance to schedule a save.
845
+ */
846
+ function updateLayoutState() {
847
+ if (!persistence)
848
+ return;
849
+ if (layoutBusy()) {
850
+ root.dataset.workspaceLayout = 'pending';
851
+ return;
852
+ }
853
+ queueMicrotask(() => {
854
+ if (layoutBusy())
855
+ return;
856
+ root.dataset.workspaceLayout = 'settled';
857
+ for (const resolve of settledWaiters.splice(0))
858
+ resolve();
859
+ });
860
+ }
861
+ /** Resolves once the loaded project's layout is restored and its changes are written to the store. */
862
+ function layoutSettled() {
863
+ if (!persistence)
864
+ return Promise.resolve();
865
+ if (saveFrame !== undefined) {
866
+ window.cancelAnimationFrame(saveFrame);
867
+ writeLayout();
868
+ }
869
+ return new Promise(resolve => {
870
+ settledWaiters.push(resolve);
871
+ updateLayoutState();
872
+ }).then(() => lastWrite).then(() => undefined);
873
+ }
824
874
  function flushLayout() {
825
875
  const pending = saveFrame !== undefined;
826
876
  if (pending)
@@ -833,6 +883,7 @@ export function mountSpriteTabs(root, content, options) {
833
883
  pendingRestore = false;
834
884
  window.clearTimeout(restoreTimeout);
835
885
  restoreTimeout = undefined;
886
+ updateLayoutState();
836
887
  }
837
888
  /** The panels and splits of a layout, without sizes or selection, to tell whether the user changed it. */
838
889
  function layoutShape(layout) {
@@ -996,6 +1047,7 @@ export function mountSpriteTabs(root, content, options) {
996
1047
  saveFrame = undefined;
997
1048
  window.clearTimeout(restoreTimeout);
998
1049
  restoreTimeout = undefined;
1050
+ updateLayoutState();
999
1051
  }
1000
1052
  projectLoading = loading;
1001
1053
  scheduleRestore();
@@ -1091,16 +1143,20 @@ export function mountSpriteTabs(root, content, options) {
1091
1143
  layoutStore?.readTabsPosition().then(position => {
1092
1144
  if (!disposed && !tabsChosen && position === 'left')
1093
1145
  setVerticalTabs(true, false);
1094
- }, () => undefined);
1146
+ }, () => undefined).finally(() => {
1147
+ readingTabs = false;
1148
+ updateLayoutState();
1149
+ });
1095
1150
  if (persistence)
1096
1151
  window.addEventListener('pagehide', flushLayout);
1152
+ updateLayoutState();
1097
1153
  root.addEventListener('click', guardDocumentClose, true);
1098
1154
  root.addEventListener('keydown', guardDocumentClose, true);
1099
1155
  vm.on('targetsUpdate', refresh);
1100
1156
  refresh();
1101
1157
  return {
1102
1158
  select, selectFromList, targetTabs, openStage, closeStage, split, closeSplit, activateView,
1103
- setViewSection, syncActiveSection, refreshSurfaces,
1159
+ setViewSection, syncActiveSection, refreshSurfaces, layoutSettled,
1104
1160
  rename(id) {
1105
1161
  const target = vm.runtime.targets.find(item => item.id === id);
1106
1162
  if (target)
@@ -1116,6 +1172,8 @@ export function mountSpriteTabs(root, content, options) {
1116
1172
  dispose() {
1117
1173
  flushLayout();
1118
1174
  disposed = true;
1175
+ for (const resolve of settledWaiters.splice(0))
1176
+ resolve();
1119
1177
  window.cancelAnimationFrame(restoreFrame);
1120
1178
  window.clearTimeout(restoreTimeout);
1121
1179
  if (persistence)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-plus-editor-workbench",
3
- "version": "0.5.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",
@@ -290,6 +290,12 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
290
290
  let restoreFrame: number | undefined;
291
291
  let restoreTimeout: number | undefined;
292
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)[] = [];
293
299
  const openingGroups = new Set<string>();
294
300
  /** Panels that were in a separate window when the layout was saved, until the user reopens them. */
295
301
  const reopenable = new Map<string, SavedPopout>();
@@ -914,20 +920,62 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
914
920
 
915
921
  function writeLayout() {
916
922
  saveFrame = undefined;
917
- if (!persistence || !layoutSession || pendingRestore || syncing || persistence.isProjectLoading?.()) return;
918
- const snapshot = captureLayout();
919
- if (!snapshot) return;
920
- // Opening a project and leaving its tabs as they were is not worth a record that pushes out another project.
921
- if (!layoutSession.saved && initialShape !== undefined && layoutShape(snapshot) === initialShape) return;
922
- layoutSession.write(JSON.stringify(snapshot), snapshot.project).catch(() => 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
+ }
923
937
  }
924
938
 
925
939
  function scheduleSave() {
926
940
  if (persistence && layoutSession && !pendingRestore && saveFrame === undefined) {
927
941
  saveFrame = window.requestAnimationFrame(writeLayout);
942
+ updateLayoutState();
928
943
  }
929
944
  }
930
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;
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);
977
+ }
978
+
931
979
  function flushLayout() {
932
980
  const pending = saveFrame !== undefined;
933
981
  if (pending) window.cancelAnimationFrame(saveFrame!);
@@ -939,6 +987,7 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
939
987
  pendingRestore = false;
940
988
  window.clearTimeout(restoreTimeout);
941
989
  restoreTimeout = undefined;
990
+ updateLayoutState();
942
991
  }
943
992
 
944
993
  /** The panels and splits of a layout, without sizes or selection, to tell whether the user changed it. */
@@ -1082,6 +1131,7 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
1082
1131
  saveFrame = undefined;
1083
1132
  window.clearTimeout(restoreTimeout);
1084
1133
  restoreTimeout = undefined;
1134
+ updateLayoutState();
1085
1135
  }
1086
1136
  projectLoading = loading;
1087
1137
  scheduleRestore();
@@ -1167,15 +1217,19 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
1167
1217
  const unsubscribeProject = persistence && persistence.subscribeProject?.(projectChanged);
1168
1218
  layoutStore?.readTabsPosition().then(position => {
1169
1219
  if (!disposed && !tabsChosen && position === 'left') setVerticalTabs(true, false);
1170
- }, () => undefined);
1220
+ }, () => undefined).finally(() => {
1221
+ readingTabs = false;
1222
+ updateLayoutState();
1223
+ });
1171
1224
  if (persistence) window.addEventListener('pagehide', flushLayout);
1225
+ updateLayoutState();
1172
1226
  root.addEventListener('click', guardDocumentClose, true);
1173
1227
  root.addEventListener('keydown', guardDocumentClose, true);
1174
1228
  vm.on('targetsUpdate', refresh);
1175
1229
  refresh();
1176
1230
  return {
1177
1231
  select, selectFromList, targetTabs, openStage, closeStage, split, closeSplit, activateView,
1178
- setViewSection, syncActiveSection, refreshSurfaces,
1232
+ setViewSection, syncActiveSection, refreshSurfaces, layoutSettled,
1179
1233
  rename(id: string) {
1180
1234
  const target = vm.runtime.targets.find(item => item.id === id);
1181
1235
  if (target) openRenameDialog(target);
@@ -1190,6 +1244,7 @@ export function mountSpriteTabs<Snapshot>(root: HTMLElement, content: HTMLElemen
1190
1244
  dispose() {
1191
1245
  flushLayout();
1192
1246
  disposed = true;
1247
+ for (const resolve of settledWaiters.splice(0)) resolve();
1193
1248
  window.cancelAnimationFrame(restoreFrame);
1194
1249
  window.clearTimeout(restoreTimeout);
1195
1250
  if (persistence) window.removeEventListener('pagehide', flushLayout);