@gramps-connect/story 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.
@@ -79,18 +79,19 @@ export declare function pruneMedia(doc: StoryDoc, files: Map<string, Uint8Array>
79
79
  /** "Josef Meyer" -> "Josef Meyer.story.zip", minus characters some file
80
80
  * systems refuse. */
81
81
  export declare function storyFileName(title: string): string;
82
- /** Undo and redo over whole documents. Typing in one field makes one step,
83
- * not one per keystroke: a change with the same `key` within COALESCE_MS
84
- * of the last replaces it. */
85
- export interface History {
86
- past: StoryDoc[];
87
- present: StoryDoc;
88
- future: StoryDoc[];
82
+ /** Undo and redo over whole documents (a StoryDoc here; any value that's
83
+ * replaced, never changed in place, works). Typing in one field makes one
84
+ * step, not one per keystroke: a change with the same `key` within
85
+ * COALESCE_MS of the last replaces it. */
86
+ export interface History<T = StoryDoc> {
87
+ past: T[];
88
+ present: T;
89
+ future: T[];
89
90
  lastKey?: string;
90
91
  lastAt?: number;
91
92
  }
92
93
  export declare const COALESCE_MS = 1500;
93
- export declare function startHistory(doc: StoryDoc): History;
94
- export declare function record(history: History, next: StoryDoc, key?: string, now?: number): History;
95
- export declare function undo(history: History): History;
96
- export declare function redo(history: History): History;
94
+ export declare function startHistory<T = StoryDoc>(doc: T): History<T>;
95
+ export declare function record<T>(history: History<T>, next: T, key?: string, now?: number): History<T>;
96
+ export declare function undo<T>(history: History<T>): History<T>;
97
+ export declare function redo<T>(history: History<T>): History<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gramps-connect/story",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Read, write, validate and present .story.zip files: portable, versioned family-history stories.",
5
5
  "license": "AGPL-3.0-or-later",
6
6
  "type": "module",
@@ -228,13 +228,14 @@ export function storyFileName(title: string): string {
228
228
  return `${safe || "story"}.story.zip`;
229
229
  }
230
230
 
231
- /** Undo and redo over whole documents. Typing in one field makes one step,
232
- * not one per keystroke: a change with the same `key` within COALESCE_MS
233
- * of the last replaces it. */
234
- export interface History {
235
- past: StoryDoc[];
236
- present: StoryDoc;
237
- future: StoryDoc[];
231
+ /** Undo and redo over whole documents (a StoryDoc here; any value that's
232
+ * replaced, never changed in place, works). Typing in one field makes one
233
+ * step, not one per keystroke: a change with the same `key` within
234
+ * COALESCE_MS of the last replaces it. */
235
+ export interface History<T = StoryDoc> {
236
+ past: T[];
237
+ present: T;
238
+ future: T[];
238
239
  lastKey?: string;
239
240
  lastAt?: number;
240
241
  }
@@ -242,24 +243,24 @@ export interface History {
242
243
  export const COALESCE_MS = 1500;
243
244
  const MAX_STEPS = 200;
244
245
 
245
- export function startHistory(doc: StoryDoc): History {
246
+ export function startHistory<T = StoryDoc>(doc: T): History<T> {
246
247
  return { past: [], present: doc, future: [] };
247
248
  }
248
249
 
249
- export function record(history: History, next: StoryDoc, key?: string, now = Date.now()): History {
250
+ export function record<T>(history: History<T>, next: T, key?: string, now = Date.now()): History<T> {
250
251
  if (next === history.present) return history;
251
252
  const coalesce = key !== undefined && key === history.lastKey && history.lastAt !== undefined && now - history.lastAt < COALESCE_MS;
252
253
  const past = coalesce ? history.past : [...history.past, history.present].slice(-MAX_STEPS);
253
254
  return { past, present: next, future: [], lastKey: key, lastAt: now };
254
255
  }
255
256
 
256
- export function undo(history: History): History {
257
+ export function undo<T>(history: History<T>): History<T> {
257
258
  const previous = history.past[history.past.length - 1];
258
259
  if (!previous) return history;
259
260
  return { past: history.past.slice(0, -1), present: previous, future: [history.present, ...history.future] };
260
261
  }
261
262
 
262
- export function redo(history: History): History {
263
+ export function redo<T>(history: History<T>): History<T> {
263
264
  const [next, ...future] = history.future;
264
265
  if (!next) return history;
265
266
  return { past: [...history.past, history.present], present: next, future };