@embedpdf/plugin-history 3.0.0-next.16 → 3.0.0-next.18
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/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +13 -7
- package/dist/index.d.ts +13 -7
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["EngineError","EngineErrorCode","HistoryToken","HISTORY_DEFAULTS","HistoryToken"],"sources":["../src/model.ts","../src/controller.ts","../src/history.plugin.ts","../src/state.ts"],"sourcesContent":["/**\n * The history as a value, and the pure steps that change it.\n *\n * An entry is one step of the history: one change this session staged, or the\n * changes of one typing session (its pauses). Each change is a part. A part is\n * named by the change the next step undoes: the change itself at first, then\n * the undo that undid it, then the redo that undid that undo, and so on. An\n * undo and a redo are the same thing to the engine: the undo of a change.\n *\n * Entries are numbered in the order they were recorded (`seq`). The undo\n * stack holds them oldest first and the redo stack newest first, so the next\n * step is always the last of its stack, and an entry put back after a refused\n * step finds its place by its number.\n */\nimport type { ChangeLabel, PredictedOp } from '@embedpdf/core';\n\nexport interface HistoryPart {\n /** The order it was recorded in. */\n readonly seq: number;\n /** The change it began as, which is what a refusal of that change names. Never changes. */\n readonly id: string;\n /** The change the next step undoes: the change itself, or its latest undo or redo. */\n readonly opId: string;\n /** What the change shows: what a redo of it shows until the engine answers. */\n readonly forward: readonly PredictedOp[];\n /** What undoing it shows until the engine answers. */\n readonly backward: readonly PredictedOp[];\n}\n\nexport interface HistoryEntry {\n /** The order it was recorded in. */\n readonly seq: number;\n readonly label: ChangeLabel;\n /** Consecutive changes with this key join it (typing); `null` for every other change. */\n readonly merge: string | null;\n /** Its changes, oldest first. */\n readonly parts: readonly HistoryPart[];\n}\n\nexport interface History {\n /** What undo undoes, oldest first: the last is next. */\n readonly undo: readonly HistoryEntry[];\n /** What redo redoes, newest first: the last is next. */\n readonly redo: readonly HistoryEntry[];\n /**\n * The merge key the newest entry still takes changes under: set when a change with a key is\n * recorded, `null` again once anything else happens.\n */\n readonly merging: string | null;\n readonly nextSeq: number;\n}\n\nexport type Direction = 'undo' | 'redo';\n\n/** A change this session staged, as the history records it. */\nexport interface RecordedChange {\n readonly opId: string;\n readonly label: ChangeLabel;\n readonly merge: string | null;\n readonly shows: readonly PredictedOp[];\n readonly undo: readonly PredictedOp[];\n}\n\nexport const emptyHistory = (): History => ({ undo: [], redo: [], merging: null, nextSeq: 0 });\n\n/** The stack a step takes its entry from, and the one it puts it on. */\nconst stacksOf = (direction: Direction) =>\n direction === 'undo'\n ? ({ from: 'undo', to: 'redo' } as const)\n : ({ from: 'redo', to: 'undo' } as const);\n\n/** The entry the next step of `direction` takes, or `null`. */\nexport const nextOf = (history: History, direction: Direction): HistoryEntry | null =>\n history[direction].at(-1) ?? null;\n\n/**\n * A new change: it joins the newest entry when typing goes on under the same key, else it is a\n * new entry on top, and the oldest go past `limit`. Anything that could be redone is forgotten.\n */\nexport function recorded(history: History, change: RecordedChange, limit: number): History {\n const part: HistoryPart = {\n seq: history.nextSeq,\n id: change.opId,\n opId: change.opId,\n forward: change.shows,\n backward: change.undo,\n };\n const top = history.undo.at(-1);\n if (top && change.merge !== null && history.merging === change.merge) {\n return {\n undo: [...history.undo.slice(0, -1), { ...top, parts: [...top.parts, part] }],\n redo: [],\n merging: change.merge,\n nextSeq: history.nextSeq + 1,\n };\n }\n const entry: HistoryEntry = {\n seq: part.seq,\n label: change.label,\n merge: change.merge,\n parts: [part],\n };\n return {\n undo: [...history.undo, entry].slice(-Math.max(1, limit)),\n redo: [],\n merging: change.merge,\n nextSeq: history.nextSeq + 1,\n };\n}\n\n/**\n * A step was staged: the entry `seq` leaves the top of its stack for the top of the other, each\n * part now named by the change that staged it (`opIds`, by part id).\n */\nexport function stepped(\n history: History,\n direction: Direction,\n seq: number,\n opIds: ReadonlyMap<string, string>,\n): History {\n const { from, to } = stacksOf(direction);\n const entry = history[from].find((candidate) => candidate.seq === seq);\n if (!entry) return history;\n const moved: HistoryEntry = {\n ...entry,\n parts: entry.parts.map((part) => ({ ...part, opId: opIds.get(part.id) ?? part.opId })),\n };\n return {\n ...history,\n [from]: history[from].filter((candidate) => candidate !== entry),\n [to]: [...history[to], moved],\n merging: null,\n } as History;\n}\n\n/** Every entry without the parts `drop` picks, and without the entries left with none. */\nconst withoutParts = (\n entries: readonly HistoryEntry[],\n drop: (part: HistoryPart) => boolean,\n): readonly HistoryEntry[] => {\n let changed = false;\n const kept: HistoryEntry[] = [];\n for (const entry of entries) {\n const parts = entry.parts.filter((part) => !drop(part));\n if (parts.length === entry.parts.length) {\n kept.push(entry);\n continue;\n }\n changed = true;\n if (parts.length) kept.push({ ...entry, parts });\n }\n return changed ? kept : entries;\n};\n\n/**\n * The change `id` never happened (the engine refused it), or did nothing that can be undone:\n * its part goes, from whichever stack holds it.\n */\nexport function forgotten(history: History, id: string): History {\n const undo = withoutParts(history.undo, (part) => part.id === id);\n const redo = withoutParts(history.redo, (part) => part.id === id);\n return undo === history.undo && redo === history.redo ? history : { ...history, undo, redo };\n}\n\n/**\n * The engine refused one change of a step, so that part didn't move: it goes back where it\n * was, as `part` was before the step, into its entry there (or a new one with the entry's\n * number, in its place), wherever a later step had put it since. Its other parts stay where\n * the step put them.\n */\nexport function stepRefused(\n history: History,\n direction: Direction,\n entry: Pick<HistoryEntry, 'seq' | 'label' | 'merge'>,\n part: HistoryPart,\n): History {\n const { from } = stacksOf(direction);\n const { undo, redo } = forgotten(history, part.id);\n const left = { undo, redo };\n const back = [...left[from]];\n const at = back.findIndex((candidate) => candidate.seq === entry.seq);\n if (at >= 0) {\n const parts = [...back[at]!.parts, part].sort((left, right) => left.seq - right.seq);\n back[at] = { ...back[at]!, parts };\n } else {\n // The undo stack is oldest first, the redo stack newest first.\n const before = (candidate: HistoryEntry) =>\n from === 'undo' ? candidate.seq < entry.seq : candidate.seq > entry.seq;\n const index = back.filter(before).length;\n back.splice(index, 0, { ...entry, parts: [part] });\n }\n return { ...history, ...left, [from]: back, merging: null };\n}\n\n/**\n * The engine can no longer undo the entry `seq` (a final change or a new version came after\n * it, or it is too old): it goes, and so does everything recorded before it.\n */\nexport function unavailable(history: History, seq: number): History {\n const newer = (entry: HistoryEntry) => entry.seq > seq;\n return {\n ...history,\n undo: history.undo.filter(newer),\n redo: history.redo.filter(newer),\n merging: null,\n };\n}\n\n/** Nothing can be undone or redone. */\nexport const cleared = (history: History): History =>\n history.undo.length === 0 && history.redo.length === 0 && history.merging === null\n ? history\n : { ...history, undo: [], redo: [], merging: null };\n","/**\n * The history controller. It records every change this session stages on the\n * document's change queue, as it is staged, so an undo right after an action\n * undoes that action even before the engine answered it. Undo and redo are\n * changes on the same queue: the undo of a change, by its `opId`. The views\n * show them at once, from what the change said undoing it looks like, and\n * the engine applies the reverse it recorded.\n *\n * The history keeps no copy of the document. What it holds is names, labels\n * and what each step looks like, and it changes on four things: a change\n * staged, a step staged, an answer, and a change that ends undo (a final\n * change, a new version).\n */\nimport type { PendingChange, PluginContext, PluginError } from '@embedpdf/core';\nimport { EngineError, EngineErrorCode, type ChangeResult } from '@embedpdf/engine-core/runtime';\n\nimport type {\n HistoryCapability,\n HistorySettings,\n HistoryUndoFailedEvent,\n HistoryUndoneEvent,\n} from './contract';\nimport {\n cleared,\n forgotten,\n nextOf,\n recorded,\n stepped,\n stepRefused,\n unavailable,\n type Direction,\n type History,\n type HistoryEntry,\n type HistoryPart,\n} from './model';\n\n/** The document events after which nothing before them can be undone. */\nconst ENDS_UNDO = new Set([\n 'redaction.applied',\n 'pages.flattened',\n 'signatures.completed',\n 'forms.repaired',\n 'document.versioned',\n]);\n\n/** Whether the engine refused an undo because it can't be undone any more. */\nconst isUnavailable = (error: PluginError): boolean =>\n EngineError.is((error as { cause?: unknown }).cause, EngineErrorCode.UndoUnavailable);\n\n/** How many of an undo's parts were left alone, in whole or in part. */\nconst skippedIn = (result: ChangeResult): number =>\n result.items.filter(\n (item) => item.type === 'skipped' || ('skipped' in item && (item.skipped?.length ?? 0) > 0),\n ).length;\n\nexport function createHistoryController(ctx: PluginContext<History, HistorySettings>) {\n const settings = ctx.settings();\n const undone = ctx.events.source<HistoryUndoneEvent>();\n const undoFailed = ctx.events.source<HistoryUndoFailedEvent>();\n\n /**\n * The changes the engine will never apply, while a step is on its way: a refused change, and\n * a step that undid one. A step that names one is refused too, and changes nothing more.\n */\n const dead = new Set<string>();\n /** The parts forgotten while a step is on its way: a refusal of their step puts nothing back. */\n const gone = new Set<string>();\n let inFlight = 0;\n\n const forget = (ids: Iterable<string>): void => {\n if (inFlight === 0) return;\n for (const id of ids) gone.add(id);\n };\n const partsOf = (entries: readonly HistoryEntry[]) =>\n entries.flatMap((entry) => entry.parts.map((part) => part.id));\n\n const clear = (): void => {\n const history = ctx.state.get();\n forget(partsOf([...history.undo, ...history.redo]));\n ctx.state.update(cleared);\n };\n\n /**\n * One step's change for one part, and what its answer does. `moot`: it named a change the\n * engine never applied, or a part the history forgot, so its refusal says nothing new.\n */\n const answerOf = (\n direction: Direction,\n entry: HistoryEntry,\n part: HistoryPart,\n change: PendingChange,\n ): Promise<{ skipped: number } | { error: PluginError } | { moot: true }> =>\n change.result.then(\n (result) => {\n if (!result.meta.undoable) {\n // It did nothing (every part was left alone): there is nothing to step back over.\n forget([part.id]);\n ctx.state.update(forgotten, part.id);\n }\n return { skipped: skippedIn(result) };\n },\n (error: PluginError) => {\n dead.add(change.opId);\n if (dead.has(part.opId) || gone.has(part.id)) return { moot: true };\n if (isUnavailable(error)) {\n const history = ctx.state.get();\n const older = [...history.undo, ...history.redo].filter(\n (other) => other.seq <= entry.seq,\n );\n forget(partsOf(older));\n ctx.state.update(unavailable, entry.seq);\n } else {\n // The part didn't move: it goes back, and the step can be tried again.\n ctx.state.update(stepRefused, direction, entry, part);\n }\n return { error };\n },\n );\n\n /**\n * Take one step: the undo of each part of the next entry, as one change each (undo goes\n * newest first, redo oldest first), staged now, so the views show it at once.\n */\n const step = (direction: Direction): void => {\n // What is still being typed is part of the history from now on: send it first.\n ctx.changes.sendHolds();\n const entry = nextOf(ctx.state.get(), direction);\n if (!entry) return;\n const parts = direction === 'undo' ? [...entry.parts].reverse() : entry.parts;\n const staged: { part: HistoryPart; change: PendingChange }[] = [];\n for (const part of parts) {\n try {\n const change = ctx.changes.stage({\n label: entry.label,\n undoOf: part.opId,\n shows: direction === 'undo' ? part.backward : part.forward,\n });\n staged.push({ part, change });\n } catch (error) {\n // The document closed: the parts staged so far move, the rest stay.\n if (staged.length === 0) throw error;\n break;\n }\n }\n ctx.state.update(\n stepped,\n direction,\n entry.seq,\n new Map(staged.map(({ part, change }) => [part.id, change.opId])),\n );\n inFlight += 1;\n void Promise.all(\n staged.map(({ part, change }) => answerOf(direction, entry, part, change)),\n ).then((answers) => {\n inFlight -= 1;\n if (inFlight === 0) {\n dead.clear();\n gone.clear();\n }\n const redo = direction === 'redo';\n if (answers.every((answer) => 'moot' in answer)) return;\n const failure = answers.find((answer) => 'error' in answer);\n if (failure && 'error' in failure) {\n undoFailed.emit({\n label: entry.label,\n redo,\n reason: isUnavailable(failure.error) ? 'unavailable' : 'refused',\n error: failure.error,\n });\n return;\n }\n const skipped = answers.reduce(\n (sum, answer) => sum + ('skipped' in answer ? answer.skipped : 0),\n 0,\n );\n undone.emit({ label: entry.label, redo, skipped });\n });\n };\n\n /** A change still being typed: what undo would undo before anything in the history. */\n const openHold = (): PendingChange | null =>\n ctx.changes.pending().find((change) => change.held && change.history) ?? null;\n\n const api: HistoryCapability = {\n ...settings.api,\n undo: () => step('undo'),\n redo: () => step('redo'),\n canUndo: () => openHold() !== null || ctx.state.get().undo.length > 0,\n canRedo: () => openHold() === null && ctx.state.get().redo.length > 0,\n getUndoLabel: () => openHold()?.label ?? nextOf(ctx.state.get(), 'undo')?.label ?? null,\n getRedoLabel: () =>\n openHold() === null ? (nextOf(ctx.state.get(), 'redo')?.label ?? null) : null,\n clear,\n onUndone: undone.on,\n onUndoFailed: undoFailed.on,\n };\n\n return {\n api,\n connect() {\n // A change this session staged is a step of the history, unless it moves along the\n // history (an undo or a redo) or isn't one (`history: false`).\n ctx.listen(ctx.changes.onStaged, (change) => {\n if (change.undoOf !== null || !change.history) return;\n ctx.state.update(recorded, change, settings.get().limit);\n });\n ctx.listen(ctx.changes.onSettled, (settled) => {\n const { change } = settled;\n if (change.undoOf !== null || !change.history) return;\n // A change the engine refused never happened; one that did nothing can't be undone.\n if (settled.status === 'refused' || !settled.result.meta.undoable) {\n if (settled.status === 'refused' && inFlight > 0) dead.add(change.opId);\n forget([change.opId]);\n ctx.state.update(forgotten, change.opId);\n }\n });\n ctx.listen(ctx.doc.events, (event) => {\n if (ENDS_UNDO.has(event.type)) clear();\n });\n },\n };\n}\n","import { definePlugin } from '@embedpdf/core';\n\nimport { HISTORY_DEFAULTS, HistoryToken, type HistoryConfig } from './contract';\nimport { createHistoryController } from './controller';\nimport { emptyHistory } from './model';\n\n/**\n * Undo and redo, one history per open document, of what this session changes through the\n * viewer's plugins: every change they stage on the document's change queue is a step. No\n * dependencies and no engine state of its own. `config` is the settings the app registers,\n * over {@link HISTORY_DEFAULTS}.\n */\nexport const historyPlugin = (config?: HistoryConfig) =>\n definePlugin({\n id: 'history',\n token: HistoryToken,\n scope: 'document',\n state: emptyHistory,\n settings: { defaults: HISTORY_DEFAULTS, registered: config },\n create: createHistoryController,\n });\n","/**\n * The history page's State table as code: what `useHistoryState()` returns,\n * and the same fields in every other framework. What the undo and redo\n * buttons read.\n */\nimport { defineState } from '@embedpdf/core';\n\nimport { HistoryToken } from './contract';\n\nexport const historyState = defineState(HistoryToken, {\n read: (history) => ({\n canUndo: history.canUndo(),\n canRedo: history.canRedo(),\n undoLabel: history.getUndoLabel(),\n redoLabel: history.getRedoLabel(),\n }),\n empty: {\n canUndo: false,\n canRedo: false,\n undoLabel: null,\n redoLabel: null,\n },\n});\n"],"mappings":";;;;;AA+DA,MAAa,sBAA+B;CAAE,MAAM,C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CACP,OAAO;CACP,UAAU;EAAE,UAAUC,iBAAAA;EAAkB,YAAY;CAAO;CAC3D,QAAQ;AACV,CAAC;;;;;;;;ACXH,MAAa,gBAAA,GAAA,eAAA,YAAA,CAA2BC,iBAAAA,cAAc;CACpD,OAAO,aAAa;EAClB,SAAS,QAAQ,QAAQ;EACzB,SAAS,QAAQ,QAAQ;EACzB,WAAW,QAAQ,aAAa;EAChC,WAAW,QAAQ,aAAa;CAClC;CACA,OAAO;EACL,SAAS;EACT,SAAS;EACT,WAAW;EACX,WAAW;CACb;AACF,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["EngineError","EngineErrorCode","HistoryToken","HISTORY_DEFAULTS","HistoryToken"],"sources":["../src/model.ts","../src/controller.ts","../src/history.plugin.ts","../src/state.ts"],"sourcesContent":["/**\n * The history as a value, and the pure steps that change it.\n *\n * An entry is one step of the history: one change this session staged, or the\n * changes of one typing session (its pauses). Each change is a part. A part is\n * named by the change the next step undoes: the change itself at first, then\n * the undo that undid it, then the redo that undid that undo, and so on. An\n * undo and a redo are the same thing to the engine: the undo of a change.\n *\n * Entries are numbered in the order they were recorded (`seq`). The undo\n * stack holds them oldest first and the redo stack newest first, so the next\n * step is always the last of its stack, and an entry put back after a refused\n * step finds its place by its number.\n */\nimport type { ChangeLabel, PredictedOp } from '@embedpdf/core';\n\nexport interface HistoryPart {\n /** The order it was recorded in. */\n readonly seq: number;\n /** The change it began as, which is what a refusal of that change names. Never changes. */\n readonly id: string;\n /** The change the next step undoes: the change itself, or its latest undo or redo. */\n readonly opId: string;\n /** What the change shows: what a redo of it shows until the engine answers. */\n readonly forward: readonly PredictedOp[];\n /** What undoing it shows until the engine answers. */\n readonly backward: readonly PredictedOp[];\n}\n\nexport interface HistoryEntry {\n /** The order it was recorded in. */\n readonly seq: number;\n readonly label: ChangeLabel;\n /** Consecutive changes with this key join it (typing); `null` for every other change. */\n readonly merge: string | null;\n /** Its changes, oldest first. */\n readonly parts: readonly HistoryPart[];\n}\n\nexport interface History {\n /** What undo undoes, oldest first: the last is next. */\n readonly undo: readonly HistoryEntry[];\n /** What redo redoes, newest first: the last is next. */\n readonly redo: readonly HistoryEntry[];\n /**\n * The merge key the newest entry still takes changes under: set when a change with a key is\n * recorded, `null` again once anything else happens.\n */\n readonly merging: string | null;\n readonly nextSeq: number;\n}\n\nexport type Direction = 'undo' | 'redo';\n\n/** A change this session staged, as the history records it. */\nexport interface RecordedChange {\n readonly opId: string;\n readonly label: ChangeLabel;\n readonly merge: string | null;\n readonly shows: readonly PredictedOp[];\n readonly undo: readonly PredictedOp[];\n}\n\nexport const emptyHistory = (): History => ({ undo: [], redo: [], merging: null, nextSeq: 0 });\n\n/** The stack a step takes its entry from, and the one it puts it on. */\nconst stacksOf = (direction: Direction) =>\n direction === 'undo'\n ? ({ from: 'undo', to: 'redo' } as const)\n : ({ from: 'redo', to: 'undo' } as const);\n\n/** The entry the next step of `direction` takes, or `null`. */\nexport const nextOf = (history: History, direction: Direction): HistoryEntry | null =>\n history[direction].at(-1) ?? null;\n\n/**\n * A new change: it joins the newest entry when typing goes on under the same key, else it is a\n * new entry on top, and the oldest go past `limit`. Anything that could be redone is forgotten.\n */\nexport function recorded(history: History, change: RecordedChange, limit: number): History {\n const part: HistoryPart = {\n seq: history.nextSeq,\n id: change.opId,\n opId: change.opId,\n forward: change.shows,\n backward: change.undo,\n };\n const top = history.undo.at(-1);\n if (top && change.merge !== null && history.merging === change.merge) {\n return {\n undo: [...history.undo.slice(0, -1), { ...top, parts: [...top.parts, part] }],\n redo: [],\n merging: change.merge,\n nextSeq: history.nextSeq + 1,\n };\n }\n const entry: HistoryEntry = {\n seq: part.seq,\n label: change.label,\n merge: change.merge,\n parts: [part],\n };\n return {\n undo: [...history.undo, entry].slice(-Math.max(1, limit)),\n redo: [],\n merging: change.merge,\n nextSeq: history.nextSeq + 1,\n };\n}\n\n/**\n * A step was staged: the entry `seq` leaves the top of its stack for the top of the other, each\n * part now named by the change that staged it (`opIds`, by part id).\n */\nexport function stepped(\n history: History,\n direction: Direction,\n seq: number,\n opIds: ReadonlyMap<string, string>,\n): History {\n const { from, to } = stacksOf(direction);\n const entry = history[from].find((candidate) => candidate.seq === seq);\n if (!entry) return history;\n const moved: HistoryEntry = {\n ...entry,\n parts: entry.parts.map((part) => ({ ...part, opId: opIds.get(part.id) ?? part.opId })),\n };\n return {\n ...history,\n [from]: history[from].filter((candidate) => candidate !== entry),\n [to]: [...history[to], moved],\n merging: null,\n } as History;\n}\n\n/** Every entry without the parts `drop` picks, and without the entries left with none. */\nconst withoutParts = (\n entries: readonly HistoryEntry[],\n drop: (part: HistoryPart) => boolean,\n): readonly HistoryEntry[] => {\n let changed = false;\n const kept: HistoryEntry[] = [];\n for (const entry of entries) {\n const parts = entry.parts.filter((part) => !drop(part));\n if (parts.length === entry.parts.length) {\n kept.push(entry);\n continue;\n }\n changed = true;\n if (parts.length) kept.push({ ...entry, parts });\n }\n return changed ? kept : entries;\n};\n\n/**\n * The change `id` never happened (the engine refused it), or did nothing that can be undone:\n * its part goes, from whichever stack holds it.\n */\nexport function forgotten(history: History, id: string): History {\n const undo = withoutParts(history.undo, (part) => part.id === id);\n const redo = withoutParts(history.redo, (part) => part.id === id);\n return undo === history.undo && redo === history.redo ? history : { ...history, undo, redo };\n}\n\n/**\n * The engine refused one change of a step, so that part didn't move: it goes back where it\n * was, as `part` was before the step, into its entry there (or a new one with the entry's\n * number, in its place), wherever a later step had put it since. Its other parts stay where\n * the step put them.\n */\nexport function stepRefused(\n history: History,\n direction: Direction,\n entry: Pick<HistoryEntry, 'seq' | 'label' | 'merge'>,\n part: HistoryPart,\n): History {\n const { from } = stacksOf(direction);\n const { undo, redo } = forgotten(history, part.id);\n const left = { undo, redo };\n const back = [...left[from]];\n const at = back.findIndex((candidate) => candidate.seq === entry.seq);\n if (at >= 0) {\n const parts = [...back[at]!.parts, part].sort((left, right) => left.seq - right.seq);\n back[at] = { ...back[at]!, parts };\n } else {\n // The undo stack is oldest first, the redo stack newest first.\n const before = (candidate: HistoryEntry) =>\n from === 'undo' ? candidate.seq < entry.seq : candidate.seq > entry.seq;\n const index = back.filter(before).length;\n back.splice(index, 0, { ...entry, parts: [part] });\n }\n return { ...history, ...left, [from]: back, merging: null };\n}\n\n/**\n * The engine can no longer undo the entry `seq` (a final change or a new version came after\n * it, or it is too old): it goes, and so does everything recorded before it.\n */\nexport function unavailable(history: History, seq: number): History {\n const newer = (entry: HistoryEntry) => entry.seq > seq;\n return {\n ...history,\n undo: history.undo.filter(newer),\n redo: history.redo.filter(newer),\n merging: null,\n };\n}\n\n/** Nothing can be undone or redone. */\nexport const cleared = (history: History): History =>\n history.undo.length === 0 && history.redo.length === 0 && history.merging === null\n ? history\n : { ...history, undo: [], redo: [], merging: null };\n","/**\n * The history controller. It records every change this session stages on the\n * document's change queue, as it is staged, so an undo right after an action\n * undoes that action even before the engine answered it. Undo and redo are\n * changes on the same queue: the undo of a change, by its `opId`. The views\n * show them at once, from what the change said undoing it looks like, and\n * the engine applies the reverse it recorded.\n *\n * The history keeps no copy of the document. What it holds is names, labels\n * and what each step looks like, and it changes on four things: a change\n * staged, a step staged, an answer, and a change that ends undo (a final\n * change, a new version).\n */\nimport type { PendingChange, PluginContext, PluginError } from '@embedpdf/core';\nimport { EngineError, EngineErrorCode, type ChangeResult } from '@embedpdf/engine-core/runtime';\n\nimport type {\n HistoryCapability,\n HistorySettings,\n HistoryUndoFailedEvent,\n HistoryUndoneEvent,\n} from './contract';\nimport {\n cleared,\n forgotten,\n nextOf,\n recorded,\n stepped,\n stepRefused,\n unavailable,\n type Direction,\n type History,\n type HistoryEntry,\n type HistoryPart,\n} from './model';\n\n/** The document events after which nothing before them can be undone. */\nconst ENDS_UNDO = new Set([\n 'redaction.applied',\n 'pages.flattened',\n 'signatures.completed',\n 'forms.repaired',\n 'document.versioned',\n]);\n\n/** Whether the engine refused an undo because it can't be undone any more. */\nconst isUnavailable = (error: PluginError): boolean =>\n EngineError.is((error as { cause?: unknown }).cause, EngineErrorCode.UndoUnavailable);\n\n/** How many of an undo's parts were left alone, in whole or in part. */\nconst skippedIn = (result: ChangeResult): number =>\n result.items.filter(\n (item) => item.type === 'skipped' || ('skipped' in item && (item.skipped?.length ?? 0) > 0),\n ).length;\n\nexport function createHistoryController(ctx: PluginContext<History, HistorySettings>) {\n const settings = ctx.settings();\n const undone = ctx.events.source<HistoryUndoneEvent>();\n const undoFailed = ctx.events.source<HistoryUndoFailedEvent>();\n\n /**\n * The changes the engine will never apply, while a step is on its way: a refused change, and\n * a step that undid one. A step that names one is refused too, and changes nothing more.\n */\n const dead = new Set<string>();\n /** The parts forgotten while a step is on its way: a refusal of their step puts nothing back. */\n const gone = new Set<string>();\n let inFlight = 0;\n\n const forget = (ids: Iterable<string>): void => {\n if (inFlight === 0) return;\n for (const id of ids) gone.add(id);\n };\n const partsOf = (entries: readonly HistoryEntry[]) =>\n entries.flatMap((entry) => entry.parts.map((part) => part.id));\n\n const clear = (): void => {\n const history = ctx.state.get();\n forget(partsOf([...history.undo, ...history.redo]));\n ctx.state.update(cleared);\n };\n\n /**\n * One step's change for one part, and what its answer does. `moot`: it named a change the\n * engine never applied, or a part the history forgot, so its refusal says nothing new.\n */\n const answerOf = (\n direction: Direction,\n entry: HistoryEntry,\n part: HistoryPart,\n change: PendingChange,\n ): Promise<{ skipped: number } | { error: PluginError } | { moot: true }> =>\n change.result.then(\n (result) => {\n if (!result.meta.undoable) {\n // It did nothing (every part was left alone): there is nothing to step back over.\n forget([part.id]);\n ctx.state.update(forgotten, part.id);\n }\n return { skipped: skippedIn(result) };\n },\n (error: PluginError) => {\n dead.add(change.opId);\n if (dead.has(part.opId) || gone.has(part.id)) return { moot: true };\n if (isUnavailable(error)) {\n const history = ctx.state.get();\n const older = [...history.undo, ...history.redo].filter(\n (other) => other.seq <= entry.seq,\n );\n forget(partsOf(older));\n ctx.state.update(unavailable, entry.seq);\n } else {\n // The part didn't move: it goes back, and the step can be tried again.\n ctx.state.update(stepRefused, direction, entry, part);\n }\n return { error };\n },\n );\n\n /**\n * Take one step: the undo of each part of the next entry, as one change each (undo goes\n * newest first, redo oldest first), staged now, so the views show it at once.\n */\n const step = (direction: Direction): void => {\n // What is still being typed is part of the history from now on: send it first.\n ctx.changes.sendHolds();\n const entry = nextOf(ctx.state.get(), direction);\n if (!entry) return;\n const parts = direction === 'undo' ? [...entry.parts].reverse() : entry.parts;\n const staged: { part: HistoryPart; change: PendingChange }[] = [];\n for (const part of parts) {\n try {\n const change = ctx.changes.stage({\n label: entry.label,\n undoOf: part.opId,\n shows: direction === 'undo' ? part.backward : part.forward,\n });\n staged.push({ part, change });\n } catch (error) {\n // The document closed: the parts staged so far move, the rest stay.\n if (staged.length === 0) throw error;\n break;\n }\n }\n ctx.state.update(\n stepped,\n direction,\n entry.seq,\n new Map(staged.map(({ part, change }) => [part.id, change.opId])),\n );\n inFlight += 1;\n void Promise.all(\n staged.map(({ part, change }) => answerOf(direction, entry, part, change)),\n ).then((answers) => {\n inFlight -= 1;\n if (inFlight === 0) {\n dead.clear();\n gone.clear();\n }\n const redo = direction === 'redo';\n if (answers.every((answer) => 'moot' in answer)) return;\n const failure = answers.find((answer) => 'error' in answer);\n if (failure && 'error' in failure) {\n undoFailed.emit({\n label: entry.label,\n redo,\n reason: isUnavailable(failure.error) ? 'unavailable' : 'refused',\n error: failure.error,\n });\n return;\n }\n const skipped = answers.reduce(\n (sum, answer) => sum + ('skipped' in answer ? answer.skipped : 0),\n 0,\n );\n undone.emit({ label: entry.label, redo, skipped });\n });\n };\n\n /** A change still being typed: what undo would undo before anything in the history. */\n const openHold = (): PendingChange | null =>\n ctx.changes.pending().find((change) => change.held && change.history) ?? null;\n\n const api: HistoryCapability = {\n ...settings.api,\n undo: () => step('undo'),\n redo: () => step('redo'),\n canUndo: () => openHold() !== null || ctx.state.get().undo.length > 0,\n canRedo: () => openHold() === null && ctx.state.get().redo.length > 0,\n getUndoLabel: () => openHold()?.label ?? nextOf(ctx.state.get(), 'undo')?.label ?? null,\n getRedoLabel: () =>\n openHold() === null ? (nextOf(ctx.state.get(), 'redo')?.label ?? null) : null,\n clear,\n onUndone: undone.on,\n onUndoFailed: undoFailed.on,\n };\n\n return {\n api,\n connect() {\n // A change this session staged is a step of the history, unless it moves along the\n // history (an undo or a redo) or isn't one (`history: false`).\n ctx.listen(ctx.changes.onStaged, (change) => {\n if (change.undoOf !== null || !change.history) return;\n ctx.state.update(recorded, change, settings.get().limit);\n });\n ctx.listen(ctx.changes.onSettled, (settled) => {\n const { change } = settled;\n if (change.undoOf !== null || !change.history) return;\n // A change the engine refused never happened; one that did nothing can't be undone.\n if (settled.status === 'refused' || !settled.result.meta.undoable) {\n if (settled.status === 'refused' && inFlight > 0) dead.add(change.opId);\n forget([change.opId]);\n ctx.state.update(forgotten, change.opId);\n }\n });\n ctx.listen(ctx.doc.events, (event) => {\n if (ENDS_UNDO.has(event.type)) clear();\n });\n },\n };\n}\n","import { definePlugin } from '@embedpdf/core';\n\nimport { HISTORY_DEFAULTS, HistoryToken, type HistoryConfig } from './contract';\nimport { createHistoryController } from './controller';\nimport { emptyHistory } from './model';\n\n/**\n * Undo and redo, one history per open document, of what this session changes through the\n * viewer's plugins: every change they stage on the document's change queue is a step. No\n * dependencies and no engine state of its own. `config` is the settings the app registers,\n * over {@link HISTORY_DEFAULTS}.\n */\nexport const historyPlugin = (config?: HistoryConfig) =>\n definePlugin({\n id: 'history',\n token: HistoryToken,\n scope: 'document',\n state: emptyHistory,\n settings: { defaults: HISTORY_DEFAULTS, registered: config },\n create: createHistoryController,\n });\n","/**\n * The history page's State table as code: what `useHistoryState()` returns,\n * and the same fields in every other framework. What the undo and redo\n * buttons read.\n */\nimport { defineState, type ChangeLabel } from '@embedpdf/core';\n\nimport { HistoryToken } from './contract';\n\n/** Whether undo and redo can run, and what each would change. */\nexport interface HistoryState {\n /** Whether there is a change to undo. */\n readonly canUndo: boolean;\n /** Whether there is an undone change to redo. */\n readonly canRedo: boolean;\n /** What undo would undo, for a tooltip (\"Undo move of 3 annotations\"), or `null`. */\n readonly undoLabel: ChangeLabel | null;\n /** What redo would redo, or `null`. */\n readonly redoLabel: ChangeLabel | null;\n}\n\nexport const historyState = defineState(HistoryToken, {\n read: (history): HistoryState => ({\n canUndo: history.canUndo(),\n canRedo: history.canRedo(),\n undoLabel: history.getUndoLabel(),\n redoLabel: history.getRedoLabel(),\n }),\n empty: {\n canUndo: false,\n canRedo: false,\n undoLabel: null,\n redoLabel: null,\n },\n});\n"],"mappings":";;;;;AA+DA,MAAa,sBAA+B;CAAE,MAAM,CAAC;CAAG,MAAM,CAAC;CAAG,SAAS;CAAM,SAAS;AAAE;;AAG5F,MAAM,YAAY,cAChB,cAAc,SACT;CAAE,MAAM;CAAQ,IAAI;AAAO,IAC3B;CAAE,MAAM;CAAQ,IAAI;AAAO;;AAGlC,MAAa,UAAU,SAAkB,cACvC,QAAQ,UAAU,CAAC,GAAG,EAAE,KAAK;;;;;AAM/B,SAAgB,SAAS,SAAkB,QAAwB,OAAwB;CACzF,MAAM,OAAoB;EACxB,KAAK,QAAQ;EACb,IAAI,OAAO;EACX,MAAM,OAAO;EACb,SAAS,OAAO;EAChB,UAAU,OAAO;CACnB;CACA,MAAM,MAAM,QAAQ,KAAK,GAAG,EAAE;CAC9B,IAAI,OAAO,OAAO,UAAU,QAAQ,QAAQ,YAAY,OAAO,OAC7D,OAAO;EACL,MAAM,CAAC,GAAG,QAAQ,KAAK,MAAM,GAAG,EAAE,GAAG;GAAE,GAAG;GAAK,OAAO,CAAC,GAAG,IAAI,OAAO,IAAI;EAAE,CAAC;EAC5E,MAAM,CAAC;EACP,SAAS,OAAO;EAChB,SAAS,QAAQ,UAAU;CAC7B;CAEF,MAAM,QAAsB;EAC1B,KAAK,KAAK;EACV,OAAO,OAAO;EACd,OAAO,OAAO;EACd,OAAO,CAAC,IAAI;CACd;CACA,OAAO;EACL,MAAM,CAAC,GAAG,QAAQ,MAAM,KAAK,CAAC,CAAC,MAAM,CAAC,KAAK,IAAI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,GAAG,KAAK,IAAI,OAAO,IAAI;KACtE,OAAO,CAAC,OAAO,IAAI,CAAC;KACpB,IAAI,MAAM,OAAO,WAAW,OAAO,IAAI;IACzC;GACF,CAAC;GACD,IAAI,OAAO,IAAI,IAAI,SAAS,UAAU;IACpC,IAAI,UAAU,IAAI,MAAM,IAAI,GAAG,MAAM;GACvC,CAAC;EACH;CACF;AACF;;;;;;;;;ACjNA,MAAa,iBAAiB,YAAA,GAAA,eAAA,aAAA,CACf;CACX,IAAI;CACJ,OAAOC,iBAAAA;CACP,OAAO;CACP,OAAO;CACP,UAAU;EAAE,UAAUC,iBAAAA;EAAkB,YAAY;CAAO;CAC3D,QAAQ;AACV,CAAC;;;;;;;;ACCH,MAAa,gBAAA,GAAA,eAAA,YAAA,CAA2BC,iBAAAA,cAAc;CACpD,OAAO,aAA2B;EAChC,SAAS,QAAQ,QAAQ;EACzB,SAAS,QAAQ,QAAQ;EACzB,WAAW,QAAQ,aAAa;EAChC,WAAW,QAAQ,aAAa;CAClC;CACA,OAAO;EACL,SAAS;EACT,SAAS;EACT,WAAW;EACX,WAAW;CACb;AACF,CAAC"}
|
package/dist/index.d.cts
CHANGED
|
@@ -45,12 +45,18 @@ interface History {
|
|
|
45
45
|
declare const historyPlugin: (config?: HistoryConfig) => import("@embedpdf/core").PluginDef<History, HistoryCapability, HistorySettings>;
|
|
46
46
|
//#endregion
|
|
47
47
|
//#region src/state.d.ts
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
/** Whether undo and redo can run, and what each would change. */
|
|
49
|
+
interface HistoryState {
|
|
50
|
+
/** Whether there is a change to undo. */
|
|
51
|
+
readonly canUndo: boolean;
|
|
52
|
+
/** Whether there is an undone change to redo. */
|
|
53
|
+
readonly canRedo: boolean;
|
|
54
|
+
/** What undo would undo, for a tooltip ("Undo move of 3 annotations"), or `null`. */
|
|
55
|
+
readonly undoLabel: ChangeLabel | null;
|
|
56
|
+
/** What redo would redo, or `null`. */
|
|
57
|
+
readonly redoLabel: ChangeLabel | null;
|
|
58
|
+
}
|
|
59
|
+
declare const historyState: import("@embedpdf/core").StateDeclaration<HistoryCapability, HistoryState>;
|
|
54
60
|
//#endregion
|
|
55
|
-
export { HISTORY_DEFAULTS, HistoryCapability, HistoryConfig, HistorySettings, HistoryToken, HistoryUndoFailedEvent, HistoryUndoneEvent, historyPlugin, historyState };
|
|
61
|
+
export { HISTORY_DEFAULTS, HistoryCapability, HistoryConfig, HistorySettings, type HistoryState, HistoryToken, HistoryUndoFailedEvent, HistoryUndoneEvent, historyPlugin, historyState };
|
|
56
62
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -45,12 +45,18 @@ interface History {
|
|
|
45
45
|
declare const historyPlugin: (config?: HistoryConfig) => import("@embedpdf/core").PluginDef<History, HistoryCapability, HistorySettings>;
|
|
46
46
|
//#endregion
|
|
47
47
|
//#region src/state.d.ts
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
/** Whether undo and redo can run, and what each would change. */
|
|
49
|
+
interface HistoryState {
|
|
50
|
+
/** Whether there is a change to undo. */
|
|
51
|
+
readonly canUndo: boolean;
|
|
52
|
+
/** Whether there is an undone change to redo. */
|
|
53
|
+
readonly canRedo: boolean;
|
|
54
|
+
/** What undo would undo, for a tooltip ("Undo move of 3 annotations"), or `null`. */
|
|
55
|
+
readonly undoLabel: ChangeLabel | null;
|
|
56
|
+
/** What redo would redo, or `null`. */
|
|
57
|
+
readonly redoLabel: ChangeLabel | null;
|
|
58
|
+
}
|
|
59
|
+
declare const historyState: import("@embedpdf/core").StateDeclaration<HistoryCapability, HistoryState>;
|
|
54
60
|
//#endregion
|
|
55
|
-
export { HISTORY_DEFAULTS, HistoryCapability, HistoryConfig, HistorySettings, HistoryToken, HistoryUndoFailedEvent, HistoryUndoneEvent, historyPlugin, historyState };
|
|
61
|
+
export { HISTORY_DEFAULTS, HistoryCapability, HistoryConfig, HistorySettings, type HistoryState, HistoryToken, HistoryUndoFailedEvent, HistoryUndoneEvent, historyPlugin, historyState };
|
|
56
62
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/model.ts","../src/controller.ts","../src/history.plugin.ts","../src/state.ts"],"sourcesContent":["/**\n * The history as a value, and the pure steps that change it.\n *\n * An entry is one step of the history: one change this session staged, or the\n * changes of one typing session (its pauses). Each change is a part. A part is\n * named by the change the next step undoes: the change itself at first, then\n * the undo that undid it, then the redo that undid that undo, and so on. An\n * undo and a redo are the same thing to the engine: the undo of a change.\n *\n * Entries are numbered in the order they were recorded (`seq`). The undo\n * stack holds them oldest first and the redo stack newest first, so the next\n * step is always the last of its stack, and an entry put back after a refused\n * step finds its place by its number.\n */\nimport type { ChangeLabel, PredictedOp } from '@embedpdf/core';\n\nexport interface HistoryPart {\n /** The order it was recorded in. */\n readonly seq: number;\n /** The change it began as, which is what a refusal of that change names. Never changes. */\n readonly id: string;\n /** The change the next step undoes: the change itself, or its latest undo or redo. */\n readonly opId: string;\n /** What the change shows: what a redo of it shows until the engine answers. */\n readonly forward: readonly PredictedOp[];\n /** What undoing it shows until the engine answers. */\n readonly backward: readonly PredictedOp[];\n}\n\nexport interface HistoryEntry {\n /** The order it was recorded in. */\n readonly seq: number;\n readonly label: ChangeLabel;\n /** Consecutive changes with this key join it (typing); `null` for every other change. */\n readonly merge: string | null;\n /** Its changes, oldest first. */\n readonly parts: readonly HistoryPart[];\n}\n\nexport interface History {\n /** What undo undoes, oldest first: the last is next. */\n readonly undo: readonly HistoryEntry[];\n /** What redo redoes, newest first: the last is next. */\n readonly redo: readonly HistoryEntry[];\n /**\n * The merge key the newest entry still takes changes under: set when a change with a key is\n * recorded, `null` again once anything else happens.\n */\n readonly merging: string | null;\n readonly nextSeq: number;\n}\n\nexport type Direction = 'undo' | 'redo';\n\n/** A change this session staged, as the history records it. */\nexport interface RecordedChange {\n readonly opId: string;\n readonly label: ChangeLabel;\n readonly merge: string | null;\n readonly shows: readonly PredictedOp[];\n readonly undo: readonly PredictedOp[];\n}\n\nexport const emptyHistory = (): History => ({ undo: [], redo: [], merging: null, nextSeq: 0 });\n\n/** The stack a step takes its entry from, and the one it puts it on. */\nconst stacksOf = (direction: Direction) =>\n direction === 'undo'\n ? ({ from: 'undo', to: 'redo' } as const)\n : ({ from: 'redo', to: 'undo' } as const);\n\n/** The entry the next step of `direction` takes, or `null`. */\nexport const nextOf = (history: History, direction: Direction): HistoryEntry | null =>\n history[direction].at(-1) ?? null;\n\n/**\n * A new change: it joins the newest entry when typing goes on under the same key, else it is a\n * new entry on top, and the oldest go past `limit`. Anything that could be redone is forgotten.\n */\nexport function recorded(history: History, change: RecordedChange, limit: number): History {\n const part: HistoryPart = {\n seq: history.nextSeq,\n id: change.opId,\n opId: change.opId,\n forward: change.shows,\n backward: change.undo,\n };\n const top = history.undo.at(-1);\n if (top && change.merge !== null && history.merging === change.merge) {\n return {\n undo: [...history.undo.slice(0, -1), { ...top, parts: [...top.parts, part] }],\n redo: [],\n merging: change.merge,\n nextSeq: history.nextSeq + 1,\n };\n }\n const entry: HistoryEntry = {\n seq: part.seq,\n label: change.label,\n merge: change.merge,\n parts: [part],\n };\n return {\n undo: [...history.undo, entry].slice(-Math.max(1, limit)),\n redo: [],\n merging: change.merge,\n nextSeq: history.nextSeq + 1,\n };\n}\n\n/**\n * A step was staged: the entry `seq` leaves the top of its stack for the top of the other, each\n * part now named by the change that staged it (`opIds`, by part id).\n */\nexport function stepped(\n history: History,\n direction: Direction,\n seq: number,\n opIds: ReadonlyMap<string, string>,\n): History {\n const { from, to } = stacksOf(direction);\n const entry = history[from].find((candidate) => candidate.seq === seq);\n if (!entry) return history;\n const moved: HistoryEntry = {\n ...entry,\n parts: entry.parts.map((part) => ({ ...part, opId: opIds.get(part.id) ?? part.opId })),\n };\n return {\n ...history,\n [from]: history[from].filter((candidate) => candidate !== entry),\n [to]: [...history[to], moved],\n merging: null,\n } as History;\n}\n\n/** Every entry without the parts `drop` picks, and without the entries left with none. */\nconst withoutParts = (\n entries: readonly HistoryEntry[],\n drop: (part: HistoryPart) => boolean,\n): readonly HistoryEntry[] => {\n let changed = false;\n const kept: HistoryEntry[] = [];\n for (const entry of entries) {\n const parts = entry.parts.filter((part) => !drop(part));\n if (parts.length === entry.parts.length) {\n kept.push(entry);\n continue;\n }\n changed = true;\n if (parts.length) kept.push({ ...entry, parts });\n }\n return changed ? kept : entries;\n};\n\n/**\n * The change `id` never happened (the engine refused it), or did nothing that can be undone:\n * its part goes, from whichever stack holds it.\n */\nexport function forgotten(history: History, id: string): History {\n const undo = withoutParts(history.undo, (part) => part.id === id);\n const redo = withoutParts(history.redo, (part) => part.id === id);\n return undo === history.undo && redo === history.redo ? history : { ...history, undo, redo };\n}\n\n/**\n * The engine refused one change of a step, so that part didn't move: it goes back where it\n * was, as `part` was before the step, into its entry there (or a new one with the entry's\n * number, in its place), wherever a later step had put it since. Its other parts stay where\n * the step put them.\n */\nexport function stepRefused(\n history: History,\n direction: Direction,\n entry: Pick<HistoryEntry, 'seq' | 'label' | 'merge'>,\n part: HistoryPart,\n): History {\n const { from } = stacksOf(direction);\n const { undo, redo } = forgotten(history, part.id);\n const left = { undo, redo };\n const back = [...left[from]];\n const at = back.findIndex((candidate) => candidate.seq === entry.seq);\n if (at >= 0) {\n const parts = [...back[at]!.parts, part].sort((left, right) => left.seq - right.seq);\n back[at] = { ...back[at]!, parts };\n } else {\n // The undo stack is oldest first, the redo stack newest first.\n const before = (candidate: HistoryEntry) =>\n from === 'undo' ? candidate.seq < entry.seq : candidate.seq > entry.seq;\n const index = back.filter(before).length;\n back.splice(index, 0, { ...entry, parts: [part] });\n }\n return { ...history, ...left, [from]: back, merging: null };\n}\n\n/**\n * The engine can no longer undo the entry `seq` (a final change or a new version came after\n * it, or it is too old): it goes, and so does everything recorded before it.\n */\nexport function unavailable(history: History, seq: number): History {\n const newer = (entry: HistoryEntry) => entry.seq > seq;\n return {\n ...history,\n undo: history.undo.filter(newer),\n redo: history.redo.filter(newer),\n merging: null,\n };\n}\n\n/** Nothing can be undone or redone. */\nexport const cleared = (history: History): History =>\n history.undo.length === 0 && history.redo.length === 0 && history.merging === null\n ? history\n : { ...history, undo: [], redo: [], merging: null };\n","/**\n * The history controller. It records every change this session stages on the\n * document's change queue, as it is staged, so an undo right after an action\n * undoes that action even before the engine answered it. Undo and redo are\n * changes on the same queue: the undo of a change, by its `opId`. The views\n * show them at once, from what the change said undoing it looks like, and\n * the engine applies the reverse it recorded.\n *\n * The history keeps no copy of the document. What it holds is names, labels\n * and what each step looks like, and it changes on four things: a change\n * staged, a step staged, an answer, and a change that ends undo (a final\n * change, a new version).\n */\nimport type { PendingChange, PluginContext, PluginError } from '@embedpdf/core';\nimport { EngineError, EngineErrorCode, type ChangeResult } from '@embedpdf/engine-core/runtime';\n\nimport type {\n HistoryCapability,\n HistorySettings,\n HistoryUndoFailedEvent,\n HistoryUndoneEvent,\n} from './contract';\nimport {\n cleared,\n forgotten,\n nextOf,\n recorded,\n stepped,\n stepRefused,\n unavailable,\n type Direction,\n type History,\n type HistoryEntry,\n type HistoryPart,\n} from './model';\n\n/** The document events after which nothing before them can be undone. */\nconst ENDS_UNDO = new Set([\n 'redaction.applied',\n 'pages.flattened',\n 'signatures.completed',\n 'forms.repaired',\n 'document.versioned',\n]);\n\n/** Whether the engine refused an undo because it can't be undone any more. */\nconst isUnavailable = (error: PluginError): boolean =>\n EngineError.is((error as { cause?: unknown }).cause, EngineErrorCode.UndoUnavailable);\n\n/** How many of an undo's parts were left alone, in whole or in part. */\nconst skippedIn = (result: ChangeResult): number =>\n result.items.filter(\n (item) => item.type === 'skipped' || ('skipped' in item && (item.skipped?.length ?? 0) > 0),\n ).length;\n\nexport function createHistoryController(ctx: PluginContext<History, HistorySettings>) {\n const settings = ctx.settings();\n const undone = ctx.events.source<HistoryUndoneEvent>();\n const undoFailed = ctx.events.source<HistoryUndoFailedEvent>();\n\n /**\n * The changes the engine will never apply, while a step is on its way: a refused change, and\n * a step that undid one. A step that names one is refused too, and changes nothing more.\n */\n const dead = new Set<string>();\n /** The parts forgotten while a step is on its way: a refusal of their step puts nothing back. */\n const gone = new Set<string>();\n let inFlight = 0;\n\n const forget = (ids: Iterable<string>): void => {\n if (inFlight === 0) return;\n for (const id of ids) gone.add(id);\n };\n const partsOf = (entries: readonly HistoryEntry[]) =>\n entries.flatMap((entry) => entry.parts.map((part) => part.id));\n\n const clear = (): void => {\n const history = ctx.state.get();\n forget(partsOf([...history.undo, ...history.redo]));\n ctx.state.update(cleared);\n };\n\n /**\n * One step's change for one part, and what its answer does. `moot`: it named a change the\n * engine never applied, or a part the history forgot, so its refusal says nothing new.\n */\n const answerOf = (\n direction: Direction,\n entry: HistoryEntry,\n part: HistoryPart,\n change: PendingChange,\n ): Promise<{ skipped: number } | { error: PluginError } | { moot: true }> =>\n change.result.then(\n (result) => {\n if (!result.meta.undoable) {\n // It did nothing (every part was left alone): there is nothing to step back over.\n forget([part.id]);\n ctx.state.update(forgotten, part.id);\n }\n return { skipped: skippedIn(result) };\n },\n (error: PluginError) => {\n dead.add(change.opId);\n if (dead.has(part.opId) || gone.has(part.id)) return { moot: true };\n if (isUnavailable(error)) {\n const history = ctx.state.get();\n const older = [...history.undo, ...history.redo].filter(\n (other) => other.seq <= entry.seq,\n );\n forget(partsOf(older));\n ctx.state.update(unavailable, entry.seq);\n } else {\n // The part didn't move: it goes back, and the step can be tried again.\n ctx.state.update(stepRefused, direction, entry, part);\n }\n return { error };\n },\n );\n\n /**\n * Take one step: the undo of each part of the next entry, as one change each (undo goes\n * newest first, redo oldest first), staged now, so the views show it at once.\n */\n const step = (direction: Direction): void => {\n // What is still being typed is part of the history from now on: send it first.\n ctx.changes.sendHolds();\n const entry = nextOf(ctx.state.get(), direction);\n if (!entry) return;\n const parts = direction === 'undo' ? [...entry.parts].reverse() : entry.parts;\n const staged: { part: HistoryPart; change: PendingChange }[] = [];\n for (const part of parts) {\n try {\n const change = ctx.changes.stage({\n label: entry.label,\n undoOf: part.opId,\n shows: direction === 'undo' ? part.backward : part.forward,\n });\n staged.push({ part, change });\n } catch (error) {\n // The document closed: the parts staged so far move, the rest stay.\n if (staged.length === 0) throw error;\n break;\n }\n }\n ctx.state.update(\n stepped,\n direction,\n entry.seq,\n new Map(staged.map(({ part, change }) => [part.id, change.opId])),\n );\n inFlight += 1;\n void Promise.all(\n staged.map(({ part, change }) => answerOf(direction, entry, part, change)),\n ).then((answers) => {\n inFlight -= 1;\n if (inFlight === 0) {\n dead.clear();\n gone.clear();\n }\n const redo = direction === 'redo';\n if (answers.every((answer) => 'moot' in answer)) return;\n const failure = answers.find((answer) => 'error' in answer);\n if (failure && 'error' in failure) {\n undoFailed.emit({\n label: entry.label,\n redo,\n reason: isUnavailable(failure.error) ? 'unavailable' : 'refused',\n error: failure.error,\n });\n return;\n }\n const skipped = answers.reduce(\n (sum, answer) => sum + ('skipped' in answer ? answer.skipped : 0),\n 0,\n );\n undone.emit({ label: entry.label, redo, skipped });\n });\n };\n\n /** A change still being typed: what undo would undo before anything in the history. */\n const openHold = (): PendingChange | null =>\n ctx.changes.pending().find((change) => change.held && change.history) ?? null;\n\n const api: HistoryCapability = {\n ...settings.api,\n undo: () => step('undo'),\n redo: () => step('redo'),\n canUndo: () => openHold() !== null || ctx.state.get().undo.length > 0,\n canRedo: () => openHold() === null && ctx.state.get().redo.length > 0,\n getUndoLabel: () => openHold()?.label ?? nextOf(ctx.state.get(), 'undo')?.label ?? null,\n getRedoLabel: () =>\n openHold() === null ? (nextOf(ctx.state.get(), 'redo')?.label ?? null) : null,\n clear,\n onUndone: undone.on,\n onUndoFailed: undoFailed.on,\n };\n\n return {\n api,\n connect() {\n // A change this session staged is a step of the history, unless it moves along the\n // history (an undo or a redo) or isn't one (`history: false`).\n ctx.listen(ctx.changes.onStaged, (change) => {\n if (change.undoOf !== null || !change.history) return;\n ctx.state.update(recorded, change, settings.get().limit);\n });\n ctx.listen(ctx.changes.onSettled, (settled) => {\n const { change } = settled;\n if (change.undoOf !== null || !change.history) return;\n // A change the engine refused never happened; one that did nothing can't be undone.\n if (settled.status === 'refused' || !settled.result.meta.undoable) {\n if (settled.status === 'refused' && inFlight > 0) dead.add(change.opId);\n forget([change.opId]);\n ctx.state.update(forgotten, change.opId);\n }\n });\n ctx.listen(ctx.doc.events, (event) => {\n if (ENDS_UNDO.has(event.type)) clear();\n });\n },\n };\n}\n","import { definePlugin } from '@embedpdf/core';\n\nimport { HISTORY_DEFAULTS, HistoryToken, type HistoryConfig } from './contract';\nimport { createHistoryController } from './controller';\nimport { emptyHistory } from './model';\n\n/**\n * Undo and redo, one history per open document, of what this session changes through the\n * viewer's plugins: every change they stage on the document's change queue is a step. No\n * dependencies and no engine state of its own. `config` is the settings the app registers,\n * over {@link HISTORY_DEFAULTS}.\n */\nexport const historyPlugin = (config?: HistoryConfig) =>\n definePlugin({\n id: 'history',\n token: HistoryToken,\n scope: 'document',\n state: emptyHistory,\n settings: { defaults: HISTORY_DEFAULTS, registered: config },\n create: createHistoryController,\n });\n","/**\n * The history page's State table as code: what `useHistoryState()` returns,\n * and the same fields in every other framework. What the undo and redo\n * buttons read.\n */\nimport { defineState } from '@embedpdf/core';\n\nimport { HistoryToken } from './contract';\n\nexport const historyState = defineState(HistoryToken, {\n read: (history) => ({\n canUndo: history.canUndo(),\n canRedo: history.canRedo(),\n undoLabel: history.getUndoLabel(),\n redoLabel: history.getRedoLabel(),\n }),\n empty: {\n canUndo: false,\n canRedo: false,\n undoLabel: null,\n redoLabel: null,\n },\n});\n"],"mappings":";;;;AA+DA,MAAa,sBAA+B;CAAE,MAAM,CAAC;CAAG,MAAM,CAAC;CAAG,SAAS;CAAM,SAAS;AAAE;;AAG5F,MAAM,YAAY,cAChB,cAAc,SACT;CAAE,MAAM;CAAQ,IAAI;AAAO,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,SAAS,QAAQ,QAAQ;EACzB,WAAW,QAAQ,aAAa;EAChC,WAAW,QAAQ,aAAa;CAClC;CACA,OAAO;EACL,SAAS;EACT,SAAS;EACT,WAAW;EACX,WAAW;CACb;AACF,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/model.ts","../src/controller.ts","../src/history.plugin.ts","../src/state.ts"],"sourcesContent":["/**\n * The history as a value, and the pure steps that change it.\n *\n * An entry is one step of the history: one change this session staged, or the\n * changes of one typing session (its pauses). Each change is a part. A part is\n * named by the change the next step undoes: the change itself at first, then\n * the undo that undid it, then the redo that undid that undo, and so on. An\n * undo and a redo are the same thing to the engine: the undo of a change.\n *\n * Entries are numbered in the order they were recorded (`seq`). The undo\n * stack holds them oldest first and the redo stack newest first, so the next\n * step is always the last of its stack, and an entry put back after a refused\n * step finds its place by its number.\n */\nimport type { ChangeLabel, PredictedOp } from '@embedpdf/core';\n\nexport interface HistoryPart {\n /** The order it was recorded in. */\n readonly seq: number;\n /** The change it began as, which is what a refusal of that change names. Never changes. */\n readonly id: string;\n /** The change the next step undoes: the change itself, or its latest undo or redo. */\n readonly opId: string;\n /** What the change shows: what a redo of it shows until the engine answers. */\n readonly forward: readonly PredictedOp[];\n /** What undoing it shows until the engine answers. */\n readonly backward: readonly PredictedOp[];\n}\n\nexport interface HistoryEntry {\n /** The order it was recorded in. */\n readonly seq: number;\n readonly label: ChangeLabel;\n /** Consecutive changes with this key join it (typing); `null` for every other change. */\n readonly merge: string | null;\n /** Its changes, oldest first. */\n readonly parts: readonly HistoryPart[];\n}\n\nexport interface History {\n /** What undo undoes, oldest first: the last is next. */\n readonly undo: readonly HistoryEntry[];\n /** What redo redoes, newest first: the last is next. */\n readonly redo: readonly HistoryEntry[];\n /**\n * The merge key the newest entry still takes changes under: set when a change with a key is\n * recorded, `null` again once anything else happens.\n */\n readonly merging: string | null;\n readonly nextSeq: number;\n}\n\nexport type Direction = 'undo' | 'redo';\n\n/** A change this session staged, as the history records it. */\nexport interface RecordedChange {\n readonly opId: string;\n readonly label: ChangeLabel;\n readonly merge: string | null;\n readonly shows: readonly PredictedOp[];\n readonly undo: readonly PredictedOp[];\n}\n\nexport const emptyHistory = (): History => ({ undo: [], redo: [], merging: null, nextSeq: 0 });\n\n/** The stack a step takes its entry from, and the one it puts it on. */\nconst stacksOf = (direction: Direction) =>\n direction === 'undo'\n ? ({ from: 'undo', to: 'redo' } as const)\n : ({ from: 'redo', to: 'undo' } as const);\n\n/** The entry the next step of `direction` takes, or `null`. */\nexport const nextOf = (history: History, direction: Direction): HistoryEntry | null =>\n history[direction].at(-1) ?? null;\n\n/**\n * A new change: it joins the newest entry when typing goes on under the same key, else it is a\n * new entry on top, and the oldest go past `limit`. Anything that could be redone is forgotten.\n */\nexport function recorded(history: History, change: RecordedChange, limit: number): History {\n const part: HistoryPart = {\n seq: history.nextSeq,\n id: change.opId,\n opId: change.opId,\n forward: change.shows,\n backward: change.undo,\n };\n const top = history.undo.at(-1);\n if (top && change.merge !== null && history.merging === change.merge) {\n return {\n undo: [...history.undo.slice(0, -1), { ...top, parts: [...top.parts, part] }],\n redo: [],\n merging: change.merge,\n nextSeq: history.nextSeq + 1,\n };\n }\n const entry: HistoryEntry = {\n seq: part.seq,\n label: change.label,\n merge: change.merge,\n parts: [part],\n };\n return {\n undo: [...history.undo, entry].slice(-Math.max(1, limit)),\n redo: [],\n merging: change.merge,\n nextSeq: history.nextSeq + 1,\n };\n}\n\n/**\n * A step was staged: the entry `seq` leaves the top of its stack for the top of the other, each\n * part now named by the change that staged it (`opIds`, by part id).\n */\nexport function stepped(\n history: History,\n direction: Direction,\n seq: number,\n opIds: ReadonlyMap<string, string>,\n): History {\n const { from, to } = stacksOf(direction);\n const entry = history[from].find((candidate) => candidate.seq === seq);\n if (!entry) return history;\n const moved: HistoryEntry = {\n ...entry,\n parts: entry.parts.map((part) => ({ ...part, opId: opIds.get(part.id) ?? part.opId })),\n };\n return {\n ...history,\n [from]: history[from].filter((candidate) => candidate !== entry),\n [to]: [...history[to], moved],\n merging: null,\n } as History;\n}\n\n/** Every entry without the parts `drop` picks, and without the entries left with none. */\nconst withoutParts = (\n entries: readonly HistoryEntry[],\n drop: (part: HistoryPart) => boolean,\n): readonly HistoryEntry[] => {\n let changed = false;\n const kept: HistoryEntry[] = [];\n for (const entry of entries) {\n const parts = entry.parts.filter((part) => !drop(part));\n if (parts.length === entry.parts.length) {\n kept.push(entry);\n continue;\n }\n changed = true;\n if (parts.length) kept.push({ ...entry, parts });\n }\n return changed ? kept : entries;\n};\n\n/**\n * The change `id` never happened (the engine refused it), or did nothing that can be undone:\n * its part goes, from whichever stack holds it.\n */\nexport function forgotten(history: History, id: string): History {\n const undo = withoutParts(history.undo, (part) => part.id === id);\n const redo = withoutParts(history.redo, (part) => part.id === id);\n return undo === history.undo && redo === history.redo ? history : { ...history, undo, redo };\n}\n\n/**\n * The engine refused one change of a step, so that part didn't move: it goes back where it\n * was, as `part` was before the step, into its entry there (or a new one with the entry's\n * number, in its place), wherever a later step had put it since. Its other parts stay where\n * the step put them.\n */\nexport function stepRefused(\n history: History,\n direction: Direction,\n entry: Pick<HistoryEntry, 'seq' | 'label' | 'merge'>,\n part: HistoryPart,\n): History {\n const { from } = stacksOf(direction);\n const { undo, redo } = forgotten(history, part.id);\n const left = { undo, redo };\n const back = [...left[from]];\n const at = back.findIndex((candidate) => candidate.seq === entry.seq);\n if (at >= 0) {\n const parts = [...back[at]!.parts, part].sort((left, right) => left.seq - right.seq);\n back[at] = { ...back[at]!, parts };\n } else {\n // The undo stack is oldest first, the redo stack newest first.\n const before = (candidate: HistoryEntry) =>\n from === 'undo' ? candidate.seq < entry.seq : candidate.seq > entry.seq;\n const index = back.filter(before).length;\n back.splice(index, 0, { ...entry, parts: [part] });\n }\n return { ...history, ...left, [from]: back, merging: null };\n}\n\n/**\n * The engine can no longer undo the entry `seq` (a final change or a new version came after\n * it, or it is too old): it goes, and so does everything recorded before it.\n */\nexport function unavailable(history: History, seq: number): History {\n const newer = (entry: HistoryEntry) => entry.seq > seq;\n return {\n ...history,\n undo: history.undo.filter(newer),\n redo: history.redo.filter(newer),\n merging: null,\n };\n}\n\n/** Nothing can be undone or redone. */\nexport const cleared = (history: History): History =>\n history.undo.length === 0 && history.redo.length === 0 && history.merging === null\n ? history\n : { ...history, undo: [], redo: [], merging: null };\n","/**\n * The history controller. It records every change this session stages on the\n * document's change queue, as it is staged, so an undo right after an action\n * undoes that action even before the engine answered it. Undo and redo are\n * changes on the same queue: the undo of a change, by its `opId`. The views\n * show them at once, from what the change said undoing it looks like, and\n * the engine applies the reverse it recorded.\n *\n * The history keeps no copy of the document. What it holds is names, labels\n * and what each step looks like, and it changes on four things: a change\n * staged, a step staged, an answer, and a change that ends undo (a final\n * change, a new version).\n */\nimport type { PendingChange, PluginContext, PluginError } from '@embedpdf/core';\nimport { EngineError, EngineErrorCode, type ChangeResult } from '@embedpdf/engine-core/runtime';\n\nimport type {\n HistoryCapability,\n HistorySettings,\n HistoryUndoFailedEvent,\n HistoryUndoneEvent,\n} from './contract';\nimport {\n cleared,\n forgotten,\n nextOf,\n recorded,\n stepped,\n stepRefused,\n unavailable,\n type Direction,\n type History,\n type HistoryEntry,\n type HistoryPart,\n} from './model';\n\n/** The document events after which nothing before them can be undone. */\nconst ENDS_UNDO = new Set([\n 'redaction.applied',\n 'pages.flattened',\n 'signatures.completed',\n 'forms.repaired',\n 'document.versioned',\n]);\n\n/** Whether the engine refused an undo because it can't be undone any more. */\nconst isUnavailable = (error: PluginError): boolean =>\n EngineError.is((error as { cause?: unknown }).cause, EngineErrorCode.UndoUnavailable);\n\n/** How many of an undo's parts were left alone, in whole or in part. */\nconst skippedIn = (result: ChangeResult): number =>\n result.items.filter(\n (item) => item.type === 'skipped' || ('skipped' in item && (item.skipped?.length ?? 0) > 0),\n ).length;\n\nexport function createHistoryController(ctx: PluginContext<History, HistorySettings>) {\n const settings = ctx.settings();\n const undone = ctx.events.source<HistoryUndoneEvent>();\n const undoFailed = ctx.events.source<HistoryUndoFailedEvent>();\n\n /**\n * The changes the engine will never apply, while a step is on its way: a refused change, and\n * a step that undid one. A step that names one is refused too, and changes nothing more.\n */\n const dead = new Set<string>();\n /** The parts forgotten while a step is on its way: a refusal of their step puts nothing back. */\n const gone = new Set<string>();\n let inFlight = 0;\n\n const forget = (ids: Iterable<string>): void => {\n if (inFlight === 0) return;\n for (const id of ids) gone.add(id);\n };\n const partsOf = (entries: readonly HistoryEntry[]) =>\n entries.flatMap((entry) => entry.parts.map((part) => part.id));\n\n const clear = (): void => {\n const history = ctx.state.get();\n forget(partsOf([...history.undo, ...history.redo]));\n ctx.state.update(cleared);\n };\n\n /**\n * One step's change for one part, and what its answer does. `moot`: it named a change the\n * engine never applied, or a part the history forgot, so its refusal says nothing new.\n */\n const answerOf = (\n direction: Direction,\n entry: HistoryEntry,\n part: HistoryPart,\n change: PendingChange,\n ): Promise<{ skipped: number } | { error: PluginError } | { moot: true }> =>\n change.result.then(\n (result) => {\n if (!result.meta.undoable) {\n // It did nothing (every part was left alone): there is nothing to step back over.\n forget([part.id]);\n ctx.state.update(forgotten, part.id);\n }\n return { skipped: skippedIn(result) };\n },\n (error: PluginError) => {\n dead.add(change.opId);\n if (dead.has(part.opId) || gone.has(part.id)) return { moot: true };\n if (isUnavailable(error)) {\n const history = ctx.state.get();\n const older = [...history.undo, ...history.redo].filter(\n (other) => other.seq <= entry.seq,\n );\n forget(partsOf(older));\n ctx.state.update(unavailable, entry.seq);\n } else {\n // The part didn't move: it goes back, and the step can be tried again.\n ctx.state.update(stepRefused, direction, entry, part);\n }\n return { error };\n },\n );\n\n /**\n * Take one step: the undo of each part of the next entry, as one change each (undo goes\n * newest first, redo oldest first), staged now, so the views show it at once.\n */\n const step = (direction: Direction): void => {\n // What is still being typed is part of the history from now on: send it first.\n ctx.changes.sendHolds();\n const entry = nextOf(ctx.state.get(), direction);\n if (!entry) return;\n const parts = direction === 'undo' ? [...entry.parts].reverse() : entry.parts;\n const staged: { part: HistoryPart; change: PendingChange }[] = [];\n for (const part of parts) {\n try {\n const change = ctx.changes.stage({\n label: entry.label,\n undoOf: part.opId,\n shows: direction === 'undo' ? part.backward : part.forward,\n });\n staged.push({ part, change });\n } catch (error) {\n // The document closed: the parts staged so far move, the rest stay.\n if (staged.length === 0) throw error;\n break;\n }\n }\n ctx.state.update(\n stepped,\n direction,\n entry.seq,\n new Map(staged.map(({ part, change }) => [part.id, change.opId])),\n );\n inFlight += 1;\n void Promise.all(\n staged.map(({ part, change }) => answerOf(direction, entry, part, change)),\n ).then((answers) => {\n inFlight -= 1;\n if (inFlight === 0) {\n dead.clear();\n gone.clear();\n }\n const redo = direction === 'redo';\n if (answers.every((answer) => 'moot' in answer)) return;\n const failure = answers.find((answer) => 'error' in answer);\n if (failure && 'error' in failure) {\n undoFailed.emit({\n label: entry.label,\n redo,\n reason: isUnavailable(failure.error) ? 'unavailable' : 'refused',\n error: failure.error,\n });\n return;\n }\n const skipped = answers.reduce(\n (sum, answer) => sum + ('skipped' in answer ? answer.skipped : 0),\n 0,\n );\n undone.emit({ label: entry.label, redo, skipped });\n });\n };\n\n /** A change still being typed: what undo would undo before anything in the history. */\n const openHold = (): PendingChange | null =>\n ctx.changes.pending().find((change) => change.held && change.history) ?? null;\n\n const api: HistoryCapability = {\n ...settings.api,\n undo: () => step('undo'),\n redo: () => step('redo'),\n canUndo: () => openHold() !== null || ctx.state.get().undo.length > 0,\n canRedo: () => openHold() === null && ctx.state.get().redo.length > 0,\n getUndoLabel: () => openHold()?.label ?? nextOf(ctx.state.get(), 'undo')?.label ?? null,\n getRedoLabel: () =>\n openHold() === null ? (nextOf(ctx.state.get(), 'redo')?.label ?? null) : null,\n clear,\n onUndone: undone.on,\n onUndoFailed: undoFailed.on,\n };\n\n return {\n api,\n connect() {\n // A change this session staged is a step of the history, unless it moves along the\n // history (an undo or a redo) or isn't one (`history: false`).\n ctx.listen(ctx.changes.onStaged, (change) => {\n if (change.undoOf !== null || !change.history) return;\n ctx.state.update(recorded, change, settings.get().limit);\n });\n ctx.listen(ctx.changes.onSettled, (settled) => {\n const { change } = settled;\n if (change.undoOf !== null || !change.history) return;\n // A change the engine refused never happened; one that did nothing can't be undone.\n if (settled.status === 'refused' || !settled.result.meta.undoable) {\n if (settled.status === 'refused' && inFlight > 0) dead.add(change.opId);\n forget([change.opId]);\n ctx.state.update(forgotten, change.opId);\n }\n });\n ctx.listen(ctx.doc.events, (event) => {\n if (ENDS_UNDO.has(event.type)) clear();\n });\n },\n };\n}\n","import { definePlugin } from '@embedpdf/core';\n\nimport { HISTORY_DEFAULTS, HistoryToken, type HistoryConfig } from './contract';\nimport { createHistoryController } from './controller';\nimport { emptyHistory } from './model';\n\n/**\n * Undo and redo, one history per open document, of what this session changes through the\n * viewer's plugins: every change they stage on the document's change queue is a step. No\n * dependencies and no engine state of its own. `config` is the settings the app registers,\n * over {@link HISTORY_DEFAULTS}.\n */\nexport const historyPlugin = (config?: HistoryConfig) =>\n definePlugin({\n id: 'history',\n token: HistoryToken,\n scope: 'document',\n state: emptyHistory,\n settings: { defaults: HISTORY_DEFAULTS, registered: config },\n create: createHistoryController,\n });\n","/**\n * The history page's State table as code: what `useHistoryState()` returns,\n * and the same fields in every other framework. What the undo and redo\n * buttons read.\n */\nimport { defineState, type ChangeLabel } from '@embedpdf/core';\n\nimport { HistoryToken } from './contract';\n\n/** Whether undo and redo can run, and what each would change. */\nexport interface HistoryState {\n /** Whether there is a change to undo. */\n readonly canUndo: boolean;\n /** Whether there is an undone change to redo. */\n readonly canRedo: boolean;\n /** What undo would undo, for a tooltip (\"Undo move of 3 annotations\"), or `null`. */\n readonly undoLabel: ChangeLabel | null;\n /** What redo would redo, or `null`. */\n readonly redoLabel: ChangeLabel | null;\n}\n\nexport const historyState = defineState(HistoryToken, {\n read: (history): HistoryState => ({\n canUndo: history.canUndo(),\n canRedo: history.canRedo(),\n undoLabel: history.getUndoLabel(),\n redoLabel: history.getRedoLabel(),\n }),\n empty: {\n canUndo: false,\n canRedo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,aAAa;CACX,IAAI;CACJ,OAAO;CACP,OAAO;CACP,OAAO;CACP,UAAU;EAAE,UAAU;EAAkB,YAAY;CAAO;CAC3D,QAAQ;AACV,CAAC;;;;;;;;ACCH,MAAa,eAAe,YAAY,cAAc;CACpD,OAAO,aAA2B;EAChC,SAAS,QAAQ,QAAQ;EACzB,SAAS,QAAQ,QAAQ;EACzB,WAAW,QAAQ,aAAa;EAChC,WAAW,QAAQ,aAAa;CAClC;CACA,OAAO;EACL,SAAS;EACT,SAAS;EACT,WAAW;EACX,WAAW;CACb;AACF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@embedpdf/plugin-history",
|
|
3
|
-
"version": "3.0.0-next.
|
|
3
|
+
"version": "3.0.0-next.18",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Undo and redo per open document, built on the document's change queue: every change the viewer's plugins stage is a step, undone by reference while still pending.",
|
|
6
6
|
"type": "module",
|
|
@@ -22,16 +22,16 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@embedpdf/core": "3.0.0-next.
|
|
26
|
-
"@embedpdf/engine-core": "3.0.0-next.
|
|
25
|
+
"@embedpdf/core": "3.0.0-next.18",
|
|
26
|
+
"@embedpdf/engine-core": "3.0.0-next.18"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"typescript": "^5.0.0",
|
|
30
30
|
"vitest": "^2.1.9",
|
|
31
|
+
"@embedpdf/engine": "3.0.0-next.18",
|
|
31
32
|
"@embedpdf/tooling-build": "0.0.0",
|
|
32
|
-
"@embedpdf/
|
|
33
|
-
"@embedpdf/plugin-
|
|
34
|
-
"@embedpdf/plugin-interaction": "3.0.0-next.16"
|
|
33
|
+
"@embedpdf/plugin-annotation": "3.0.0-next.18",
|
|
34
|
+
"@embedpdf/plugin-interaction": "3.0.0-next.18"
|
|
35
35
|
},
|
|
36
36
|
"license": "Apache-2.0",
|
|
37
37
|
"sideEffects": false,
|