@rohal12/spindle 0.51.4 → 0.52.1

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 (69) hide show
  1. package/dist/pkg/format.js +1 -1
  2. package/dist/pkg/headless.js +4512 -1702
  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/action-registry.ts +70 -18
  7. package/src/automation/runner.ts +2 -1
  8. package/src/class-registry.ts +214 -103
  9. package/src/components/Passage.tsx +2 -2
  10. package/src/components/PassageDialog.tsx +2 -5
  11. package/src/components/StoryInterface.tsx +2 -4
  12. package/src/components/macros/Button.tsx +5 -31
  13. package/src/components/macros/Checkbox.tsx +7 -4
  14. package/src/components/macros/Computed.tsx +28 -14
  15. package/src/components/macros/For.tsx +29 -3
  16. package/src/components/macros/If.tsx +8 -0
  17. package/src/components/macros/Include.tsx +7 -6
  18. package/src/components/macros/MacroError.tsx +2 -1
  19. package/src/components/macros/MacroLink.tsx +12 -45
  20. package/src/components/macros/Meter.tsx +11 -3
  21. package/src/components/macros/Nobr.tsx +1 -0
  22. package/src/components/macros/PassageDisplay.tsx +3 -0
  23. package/src/components/macros/Print.tsx +4 -0
  24. package/src/components/macros/Radiobutton.tsx +5 -2
  25. package/src/components/macros/SaveManager.tsx +25 -8
  26. package/src/components/macros/Set.tsx +5 -4
  27. package/src/components/macros/Span.tsx +1 -0
  28. package/src/components/macros/StoryTitle.tsx +1 -0
  29. package/src/components/macros/Switch.tsx +13 -0
  30. package/src/components/macros/Unset.tsx +31 -10
  31. package/src/components/macros/VarDisplay.tsx +21 -4
  32. package/src/components/macros/Widget.tsx +20 -1
  33. package/src/components/macros/WidgetInvocation.tsx +17 -75
  34. package/src/components/macros/arg-utils.ts +107 -1
  35. package/src/components/macros/detached-body.tsx +68 -0
  36. package/src/components/macros/option-utils.ts +3 -2
  37. package/src/define-macro.ts +32 -5
  38. package/src/execute-mutation.ts +271 -68
  39. package/src/expression.ts +91 -59
  40. package/src/hooks/use-action.ts +24 -3
  41. package/src/hooks/use-interpolate.ts +36 -5
  42. package/src/index.tsx +12 -2
  43. package/src/interpolation.ts +394 -96
  44. package/src/js-lexer.ts +1231 -97
  45. package/src/markup/ast.ts +7 -2
  46. package/src/markup/code-attributes.ts +64 -0
  47. package/src/markup/markdown.ts +188 -9
  48. package/src/markup/render.tsx +430 -49
  49. package/src/markup/tokenizer.ts +578 -110
  50. package/src/prng.ts +41 -8
  51. package/src/registry.ts +35 -0
  52. package/src/saves/save-manager.ts +346 -160
  53. package/src/saves/storage.ts +16 -7
  54. package/src/saves/types.ts +2 -1
  55. package/src/settings.ts +12 -9
  56. package/src/store.ts +640 -186
  57. package/src/story-api.ts +38 -73
  58. package/src/story-init.ts +1 -1
  59. package/src/story-variables.ts +98 -102
  60. package/src/triggers.ts +10 -12
  61. package/src/utils/counts.ts +45 -0
  62. package/src/utils/error-message.ts +12 -0
  63. package/src/utils/live-locals.ts +10 -3
  64. package/src/utils/namespace.ts +71 -0
  65. package/src/utils/object-path.ts +99 -14
  66. package/src/utils/stable-key.ts +14 -9
  67. package/src/widgets/widget-registry.ts +9 -0
  68. package/types/index.d.ts +43 -7
  69. 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) ||
package/src/settings.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { useStoryStore } from './store';
2
+ import { createNamespace, ownValue } from './utils/namespace';
2
3
 
3
4
  export interface ToggleConfig {
4
5
  label: string;
@@ -25,7 +26,9 @@ export type SettingDef =
25
26
  | { type: 'range'; config: RangeConfig };
26
27
 
27
28
  const definitions = new Map<string, SettingDef>();
28
- let values: Record<string, unknown> = {};
29
+ // No prototype, so a setting may be named `constructor` or `__proto__`:
30
+ // every name is plain storage (see utils/namespace.ts)
31
+ let values: Record<string, unknown> = createNamespace();
29
32
  let storageLoaded = false;
30
33
  let unsubscribeStoryData: (() => void) | null = null;
31
34
 
@@ -65,7 +68,7 @@ function loadFromStorage(): void {
65
68
  parsed !== null &&
66
69
  !Array.isArray(parsed)
67
70
  ) {
68
- values = { ...values, ...parsed };
71
+ values = createNamespace(values, parsed);
69
72
  }
70
73
  }
71
74
  } catch {
@@ -76,7 +79,7 @@ function loadFromStorage(): void {
76
79
  export const settings = {
77
80
  addToggle(name: string, config: ToggleConfig): void {
78
81
  definitions.set(name, { type: 'toggle', config });
79
- if (!(name in values)) {
82
+ if (!Object.prototype.hasOwnProperty.call(values, name)) {
80
83
  values[name] = config.default;
81
84
  }
82
85
  loadFromStorage();
@@ -84,7 +87,7 @@ export const settings = {
84
87
 
85
88
  addList(name: string, config: ListConfig): void {
86
89
  definitions.set(name, { type: 'list', config });
87
- if (!(name in values)) {
90
+ if (!Object.prototype.hasOwnProperty.call(values, name)) {
88
91
  values[name] = config.default;
89
92
  }
90
93
  loadFromStorage();
@@ -92,28 +95,28 @@ export const settings = {
92
95
 
93
96
  addRange(name: string, config: RangeConfig): void {
94
97
  definitions.set(name, { type: 'range', config });
95
- if (!(name in values)) {
98
+ if (!Object.prototype.hasOwnProperty.call(values, name)) {
96
99
  values[name] = config.default;
97
100
  }
98
101
  loadFromStorage();
99
102
  },
100
103
 
101
104
  get(name: string): unknown {
102
- return values[name];
105
+ return ownValue(values, name);
103
106
  },
104
107
 
105
108
  getToggle(name: string): boolean {
106
- const v = values[name];
109
+ const v = ownValue(values, name);
107
110
  return typeof v === 'boolean' ? v : false;
108
111
  },
109
112
 
110
113
  getList(name: string): string {
111
- const v = values[name];
114
+ const v = ownValue(values, name);
112
115
  return typeof v === 'string' ? v : '';
113
116
  },
114
117
 
115
118
  getRange(name: string): number {
116
- const v = values[name];
119
+ const v = ownValue(values, name);
117
120
  return typeof v === 'number' ? v : 0;
118
121
  },
119
122