@rohal12/spindle 0.51.4 → 0.52.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.
Files changed (64) hide show
  1. package/dist/pkg/format.js +1 -1
  2. package/dist/pkg/headless.js +4313 -1640
  3. package/dist/pkg/macro-registry.json +7 -7
  4. package/dist/pkg/story-variables.js +1636 -177
  5. package/package.json +5 -2
  6. package/src/automation/runner.ts +2 -1
  7. package/src/class-registry.ts +214 -103
  8. package/src/components/Passage.tsx +2 -2
  9. package/src/components/PassageDialog.tsx +2 -5
  10. package/src/components/StoryInterface.tsx +2 -4
  11. package/src/components/macros/Button.tsx +5 -31
  12. package/src/components/macros/Checkbox.tsx +7 -4
  13. package/src/components/macros/Computed.tsx +19 -13
  14. package/src/components/macros/For.tsx +29 -3
  15. package/src/components/macros/If.tsx +8 -0
  16. package/src/components/macros/Include.tsx +7 -6
  17. package/src/components/macros/MacroError.tsx +2 -1
  18. package/src/components/macros/MacroLink.tsx +12 -45
  19. package/src/components/macros/Meter.tsx +11 -3
  20. package/src/components/macros/Nobr.tsx +1 -0
  21. package/src/components/macros/PassageDisplay.tsx +3 -0
  22. package/src/components/macros/Print.tsx +4 -0
  23. package/src/components/macros/Radiobutton.tsx +5 -2
  24. package/src/components/macros/SaveManager.tsx +25 -8
  25. package/src/components/macros/Span.tsx +1 -0
  26. package/src/components/macros/StoryTitle.tsx +1 -0
  27. package/src/components/macros/Switch.tsx +13 -0
  28. package/src/components/macros/Unset.tsx +30 -10
  29. package/src/components/macros/VarDisplay.tsx +21 -4
  30. package/src/components/macros/Widget.tsx +20 -1
  31. package/src/components/macros/WidgetInvocation.tsx +17 -75
  32. package/src/components/macros/arg-utils.ts +107 -1
  33. package/src/components/macros/detached-body.tsx +68 -0
  34. package/src/components/macros/option-utils.ts +3 -2
  35. package/src/define-macro.ts +32 -5
  36. package/src/execute-mutation.ts +270 -67
  37. package/src/expression.ts +86 -55
  38. package/src/hooks/use-action.ts +18 -3
  39. package/src/hooks/use-interpolate.ts +36 -5
  40. package/src/index.tsx +10 -1
  41. package/src/interpolation.ts +394 -96
  42. package/src/js-lexer.ts +1231 -97
  43. package/src/markup/code-attributes.ts +64 -0
  44. package/src/markup/markdown.ts +188 -9
  45. package/src/markup/render.tsx +430 -49
  46. package/src/markup/tokenizer.ts +578 -110
  47. package/src/prng.ts +8 -8
  48. package/src/registry.ts +35 -0
  49. package/src/saves/save-manager.ts +339 -158
  50. package/src/saves/storage.ts +16 -7
  51. package/src/saves/types.ts +2 -1
  52. package/src/store.ts +521 -154
  53. package/src/story-api.ts +31 -67
  54. package/src/story-init.ts +1 -1
  55. package/src/story-variables.ts +98 -102
  56. package/src/triggers.ts +6 -5
  57. package/src/utils/error-message.ts +12 -0
  58. package/src/utils/live-locals.ts +10 -3
  59. package/src/utils/namespace.ts +71 -0
  60. package/src/utils/object-path.ts +99 -14
  61. package/src/utils/stable-key.ts +14 -9
  62. package/src/widgets/widget-registry.ts +9 -0
  63. package/types/index.d.ts +43 -7
  64. package/types/tooling.d.ts +1 -0
@@ -4,7 +4,13 @@ import type { SaveRecord, PlaythroughRecord, StorageBackend } from './types';
4
4
  // Memory backend
5
5
  // ---------------------------------------------------------------------------
6
6
 
7
+ /**
8
+ * In-memory storage. Like the other backends it stores copies: records and
9
+ * values go in and come out cloned, so changing a record after storing it,
10
+ * or one that was read, changes nothing stored.
11
+ */
7
12
  export function createMemoryBackend(): StorageBackend {
13
+ const copy = <T>(value: T): T => structuredClone(value);
8
14
  const saves = new Map<string, SaveRecord>();
9
15
  const playthroughs = new Map<string, PlaythroughRecord>();
10
16
  const meta = new Map<string, unknown>();
@@ -13,11 +19,12 @@ export function createMemoryBackend(): StorageBackend {
13
19
  type: 'memory',
14
20
 
15
21
  async putSave(record: SaveRecord): Promise<void> {
16
- saves.set(record.meta.id, record);
22
+ saves.set(record.meta.id, copy(record));
17
23
  },
18
24
 
19
25
  async getSave(id: string): Promise<SaveRecord | undefined> {
20
- return saves.get(id);
26
+ const record = saves.get(id);
27
+ return record && copy(record);
21
28
  },
22
29
 
23
30
  async deleteSave(id: string): Promise<void> {
@@ -25,7 +32,7 @@ export function createMemoryBackend(): StorageBackend {
25
32
  },
26
33
 
27
34
  async getSavesByIfid(ifid: string): Promise<SaveRecord[]> {
28
- return [...saves.values()].filter((s) => s.meta.ifid === ifid);
35
+ return [...saves.values()].filter((s) => s.meta.ifid === ifid).map(copy);
29
36
  },
30
37
 
31
38
  async deleteSavesByIfid(ifid: string): Promise<void> {
@@ -48,11 +55,13 @@ export function createMemoryBackend(): StorageBackend {
48
55
  },
49
56
 
50
57
  async putPlaythrough(record: PlaythroughRecord): Promise<void> {
51
- playthroughs.set(record.id, record);
58
+ playthroughs.set(record.id, copy(record));
52
59
  },
53
60
 
54
61
  async getPlaythroughsByIfid(ifid: string): Promise<PlaythroughRecord[]> {
55
- return [...playthroughs.values()].filter((p) => p.ifid === ifid);
62
+ return [...playthroughs.values()]
63
+ .filter((p) => p.ifid === ifid)
64
+ .map(copy);
56
65
  },
57
66
 
58
67
  async deletePlaythroughsByIfid(ifid: string): Promise<void> {
@@ -67,11 +76,11 @@ export function createMemoryBackend(): StorageBackend {
67
76
  },
68
77
 
69
78
  async getMeta<T = unknown>(key: string): Promise<T | undefined> {
70
- return meta.get(key) as T | undefined;
79
+ return copy(meta.get(key)) as T | undefined;
71
80
  },
72
81
 
73
82
  async setMeta(key: string, value: unknown): Promise<void> {
74
- meta.set(key, value);
83
+ meta.set(key, copy(value));
75
84
  },
76
85
 
77
86
  async deleteMeta(key: string): Promise<void> {
@@ -133,7 +133,8 @@ function isImportablePayload(value: unknown): value is SavePayload {
133
133
 
134
134
  const { history, historyIndex } = value;
135
135
  if (!Array.isArray(history) || history.length === 0) return false;
136
- if (!history.every(isSaveHistoryMoment)) return false;
136
+ // Array.from: every() skips the holes of a sparse array
137
+ if (!Array.from(history).every(isSaveHistoryMoment)) return false;
137
138
  if (
138
139
  typeof historyIndex !== 'number' ||
139
140
  !Number.isInteger(historyIndex) ||