@openpresentation/opf-editor 0.26.8 → 0.26.9

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.md CHANGED
@@ -707,7 +707,7 @@ const persistence = createPersistence(editor, {
707
707
  storage: "indexeddb", // default; or "localstorage", "memory", false, or your own { get, set, delete } adapter
708
708
  onRestorePrompt: ui.prompt, // or return "restore" | "discard" | "later" (or a promise) yourself
709
709
  onStatus: ui.status, // "Saved on this device at 2:03 PM", or why autosave is off
710
- beforeFlush: () => canvas.commit(), // commit a draft before every write and before the page unloads
710
+ beforeFlush: () => canvas.commit(), // commit a draft before an explicit flush and before the page unloads (the debounced write leaves a draft alone)
711
711
  });
712
712
  await persistence.ready; // { available, offered }: storage opened and read (it does not wait for the user's decision)
713
713
  await persistence.markSaved(); // after the host saved the document somewhere of its own: not unsaved, and the stored copy says so
@@ -63,7 +63,7 @@ export interface PersistenceOptions {
63
63
  onRestorePrompt?: (offer: RestoreOffer, actions: { restore(): Promise<boolean>; discard(): Promise<boolean> }) => RestoreChoice | Promise<RestoreChoice>;
64
64
  /** Called on every status change, for an indicator. */
65
65
  onStatus?: (status: AutosaveStatus) => void;
66
- /** Called synchronously before each explicit, debounced or lifecycle flush, even before any session edit. Commit the active draft; returning false or throwing refuses the write and reports a draft error. */
66
+ /** Called synchronously before each explicit or lifecycle flush (`flush()`, `markSaved()`, `beforeunload`, `pagehide`), even before any session edit. The debounced autosave does not call it: it writes the committed document and leaves an edit in progress alone. Commit the active draft; returning false or throwing refuses the write and reports a draft error. */
67
67
  beforeFlush?: () => boolean | void;
68
68
  /** Warn before the page closes while the document is `dirty` (default true). */
69
69
  warnOnUnload?: boolean;
@@ -223,29 +223,38 @@ export function createPersistence(editor, options = {}) {
223
223
  if (!firstDirtyAt) firstDirtyAt = at;
224
224
  clearTimeout(timer);
225
225
  const wait = Math.max(0, Math.min(debounceMs, firstDirtyAt + maxWaitMs - at));
226
- timer = setTimeout(() => { void flush(); }, wait);
226
+ timer = setTimeout(() => { void flushNow(false); }, wait);
227
227
  }
228
228
  /** Write the document now (cancelling the debounce) and resolve when it is stored. Resolves false when nothing could be written. */
229
229
  function flush() {
230
+ return flushNow(true);
231
+ }
232
+ // The debounce timer writes what is committed and leaves an edit in progress alone: committing it would close the inline editor
233
+ // under the user's hands, a moment after an earlier change (the timer starts at that change) and long before they stop typing.
234
+ // The draft is not lost: the page is still open, and every flush that precedes leaving (unload, page hide, `flush()`, `markSaved()`)
235
+ // commits it first.
236
+ function flushNow(commitDraft) {
230
237
  clearTimeout(timer);
231
238
  firstDirtyAt = 0;
232
239
  if (destroyed) return Promise.resolve(false);
233
- // Commit synchronously, including a draft in an otherwise untouched session. The lifecycle
234
- // warning below must see that edit before it decides whether leaving would lose work.
235
- // A commit emits a change; clear its debounce again and avoid recursive host flush hooks.
236
- if (preparingFlush) return chain;
237
- preparingFlush = true;
238
- try {
239
- if (options.beforeFlush?.() === false) throw new Error("Commit or cancel the active draft first.");
240
- flushRefused = false;
241
- } catch (error) {
242
- flushRefused = true;
243
- setStatus({ state: "error", error: "draft", message: `Autosave could not commit the active draft (${String(error?.message ?? error)}). Keep this page open to recover your work.` });
244
- return Promise.resolve(false);
245
- } finally {
246
- preparingFlush = false;
247
- clearTimeout(timer);
248
- firstDirtyAt = 0;
240
+ if (commitDraft) {
241
+ // Commit synchronously, including a draft in an otherwise untouched session. The lifecycle
242
+ // warning below must see that edit before it decides whether leaving would lose work.
243
+ // A commit emits a change; clear its debounce again and avoid recursive host flush hooks.
244
+ if (preparingFlush) return chain;
245
+ preparingFlush = true;
246
+ try {
247
+ if (options.beforeFlush?.() === false) throw new Error("Commit or cancel the active draft first.");
248
+ flushRefused = false;
249
+ } catch (error) {
250
+ flushRefused = true;
251
+ setStatus({ state: "error", error: "draft", message: `Autosave could not commit the active draft (${String(error?.message ?? error)}). Keep this page open to recover your work.` });
252
+ return Promise.resolve(false);
253
+ } finally {
254
+ preparingFlush = false;
255
+ clearTimeout(timer);
256
+ firstDirtyAt = 0;
257
+ }
249
258
  }
250
259
  if (!touched) return chain.then(() => true);
251
260
  chain = chain.then(writeRecord, writeRecord);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpresentation/opf-editor",
3
- "version": "0.26.8",
3
+ "version": "0.26.9",
4
4
  "description": "Embeddable local OPF editor primitives built on the deterministic renderer.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -287,10 +287,10 @@
287
287
  "@lezer/highlight": "1.2.5",
288
288
  "jsonc-parser": "3.3.1",
289
289
  "yaml": "^2.9.1",
290
- "@openpresentation/opf": "^0.26.8"
290
+ "@openpresentation/opf": "^0.26.9"
291
291
  },
292
292
  "peerDependencies": {
293
- "@openpresentation/opf-render": "^0.26.8"
293
+ "@openpresentation/opf-render": "^0.26.9"
294
294
  },
295
295
  "peerDependenciesMeta": {
296
296
  "@openpresentation/opf-render": {
@@ -349,9 +349,9 @@
349
349
  "pdf-lib": "1.17.1",
350
350
  "pdfjs-dist": "6.4.299",
351
351
  "playwright": "1.64.0",
352
- "@openpresentation/gallery": "0.26.8",
353
- "@openpresentation/opf-pptx": "0.26.8",
354
- "@openpresentation/opf-render": "0.26.8"
352
+ "@openpresentation/opf-pptx": "0.26.9",
353
+ "@openpresentation/gallery": "0.26.9",
354
+ "@openpresentation/opf-render": "0.26.9"
355
355
  },
356
356
  "scripts": {
357
357
  "build": "node scripts/build.mjs",
@@ -402,5 +402,5 @@
402
402
  "test:export-menu-browser": "node test/export-menu-browser.mjs",
403
403
  "test:agent-handoff-browser": "node test/agent-handoff-browser.mjs"
404
404
  },
405
- "gitHead": "bb6faa40bebc1654f607bf6a434c43b5e95d0b1e"
405
+ "gitHead": "7563d8dea2ba24a36b032614a50f665876d145e5"
406
406
  }