squiffy-runtime 7.0.0-beta.1 → 7.0.0-beta.11

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 (111) hide show
  1. package/dist/assets.d.ts +1 -0
  2. package/dist/classes.test.d.ts +1 -0
  3. package/dist/confirmDialog.d.ts +8 -0
  4. package/dist/events.d.ts +12 -0
  5. package/dist/every.d.ts +12 -0
  6. package/dist/every.test.d.ts +1 -0
  7. package/dist/helpers.test.d.ts +1 -0
  8. package/dist/numbers.d.ts +3 -0
  9. package/dist/panels.d.ts +14 -0
  10. package/dist/panels.test.d.ts +1 -0
  11. package/dist/paths.d.ts +15 -0
  12. package/dist/paths.test.d.ts +1 -0
  13. package/dist/persistent.test.d.ts +1 -0
  14. package/dist/playerSettings.d.ts +17 -0
  15. package/dist/preview-host.d.ts +7 -3
  16. package/dist/preview-host.js +47 -5
  17. package/dist/preview-host.js.map +2 -2
  18. package/dist/preview-html.js +1 -1
  19. package/dist/preview-protocol.d.ts +16 -0
  20. package/dist/preview.d.ts +1 -0
  21. package/dist/saveSlots.d.ts +16 -0
  22. package/dist/saves.test.d.ts +1 -0
  23. package/dist/savesDialog.d.ts +43 -0
  24. package/dist/savesDialog.test.d.ts +1 -0
  25. package/dist/scripts.test.d.ts +1 -0
  26. package/dist/settings.test.d.ts +1 -0
  27. package/dist/settingsDialog.d.ts +17 -0
  28. package/dist/skill.test.d.ts +1 -0
  29. package/dist/sound.d.ts +35 -0
  30. package/dist/sound.test.d.ts +1 -0
  31. package/dist/squiffy.page.css +155 -0
  32. package/dist/squiffy.preview.html +1469 -136
  33. package/dist/squiffy.runtime.css +1123 -20
  34. package/dist/squiffy.runtime.d.ts +13 -1
  35. package/dist/squiffy.runtime.global.js +144 -79
  36. package/dist/squiffy.runtime.global.js.map +1 -1
  37. package/dist/squiffy.runtime.js +6771 -4424
  38. package/dist/squiffy.runtime.js.map +1 -1
  39. package/dist/state.d.ts +17 -0
  40. package/dist/storyLists.d.ts +4 -0
  41. package/dist/storyLists.test.d.ts +1 -0
  42. package/dist/style.test.d.ts +1 -0
  43. package/dist/textProcessor.d.ts +27 -0
  44. package/dist/transitions.test.d.ts +1 -0
  45. package/dist/types.d.ts +125 -0
  46. package/dist/types.plugins.d.ts +1 -0
  47. package/dist/uiLinks.test.d.ts +1 -0
  48. package/dist/utils.d.ts +2 -1
  49. package/dist/utils.test.d.ts +1 -0
  50. package/package.json +11 -6
  51. package/src/__snapshots__/squiffy.runtime.test.ts.snap +22 -22
  52. package/src/animation.test.ts +44 -0
  53. package/src/animation.ts +9 -5
  54. package/src/assets.test.ts +1 -3
  55. package/src/assets.ts +1 -1
  56. package/src/classes.test.ts +201 -0
  57. package/src/confirmDialog.ts +66 -0
  58. package/src/events.ts +19 -0
  59. package/src/every.test.ts +25 -0
  60. package/src/every.ts +94 -0
  61. package/src/helpers.test.ts +34 -0
  62. package/src/numbers.ts +21 -0
  63. package/src/panels.test.ts +225 -0
  64. package/src/panels.ts +187 -0
  65. package/src/paths.test.ts +66 -0
  66. package/src/paths.ts +155 -0
  67. package/src/persistent.test.ts +155 -0
  68. package/src/playerSettings.ts +128 -0
  69. package/src/pluginManager.ts +1 -0
  70. package/src/plugins/animate.ts +28 -4
  71. package/src/plugins/live.ts +18 -5
  72. package/src/plugins/plugins.test.ts +93 -6
  73. package/src/plugins/replaceLabel.ts +7 -3
  74. package/src/preview-host.test.ts +97 -1
  75. package/src/preview-host.ts +71 -10
  76. package/src/preview-protocol.ts +25 -3
  77. package/src/preview.main.ts +1 -0
  78. package/src/preview.test.ts +203 -4
  79. package/src/preview.ts +178 -12
  80. package/src/saveSlots.ts +159 -0
  81. package/src/saves.test.ts +199 -0
  82. package/src/savesDialog.test.ts +290 -0
  83. package/src/savesDialog.ts +499 -0
  84. package/src/scripts.test.ts +130 -0
  85. package/src/settings.test.ts +233 -0
  86. package/src/settingsDialog.ts +160 -0
  87. package/src/skill.test.ts +67 -0
  88. package/src/sound.test.ts +273 -0
  89. package/src/sound.ts +207 -0
  90. package/src/squiffy.page.css +155 -0
  91. package/src/squiffy.runtime.css +1123 -20
  92. package/src/squiffy.runtime.test.ts +2806 -44
  93. package/src/squiffy.runtime.ts +1111 -64
  94. package/src/state.ts +172 -11
  95. package/src/storyLists.test.ts +134 -0
  96. package/src/storyLists.ts +57 -0
  97. package/src/style.test.ts +89 -0
  98. package/src/textProcessor.ts +739 -58
  99. package/src/transitions.test.ts +343 -0
  100. package/src/types.plugins.ts +3 -0
  101. package/src/types.ts +209 -1
  102. package/src/uiLinks.test.ts +156 -0
  103. package/src/updater.ts +26 -8
  104. package/src/utils.test.ts +60 -0
  105. package/src/utils.ts +46 -14
  106. package/vite.config.ts +3 -4
  107. package/vite.preview.config.ts +16 -5
  108. package/dist/infixExpressions.d.ts +0 -2
  109. package/src/infixExpressions.test.ts +0 -187
  110. package/src/infixExpressions.ts +0 -323
  111. /package/dist/{infixExpressions.test.d.ts → animation.test.d.ts} +0 -0
@@ -1,323 +0,0 @@
1
- // Rewrites infix conditions in {{#if}}, {{#unless}}, {{else if}} and {{else unless}}
2
- // into the equivalent prefix helper calls, before the text reaches Handlebars. e.g.
3
- // {{#if score > 100 and has_key}} -> {{#if (and (gt score 100) has_key)}}
4
- //
5
- // Precedence, from tightest to loosest: comparisons, not/!, and/&&, or/||. So "not" applies to
6
- // a whole comparison, as it reads in English: not gender = "male" means not (gender = "male").
7
- // Conditions that don't use any infix operator are passed through unchanged.
8
-
9
- type TokenType = "op" | "(" | ")" | "value" | "ident";
10
-
11
- interface Token {
12
- type: TokenType;
13
- text: string;
14
- }
15
-
16
- type Node =
17
- | { kind: "raw", text: string }
18
- | { kind: "call", name: string, args: Node[] };
19
-
20
- const comparisonHelpers: Record<string, string> = {
21
- "=": "eq",
22
- "==": "eq",
23
- "!=": "ne",
24
- ">": "gt",
25
- "<": "lt",
26
- ">=": "gte",
27
- "<=": "lte",
28
- };
29
-
30
- const symbolicOps = ["==", "!=", ">=", "<=", "&&", "||", "=", ">", "<", "!"];
31
- const keywordOps = ["and", "or", "not"];
32
- const literals = ["true", "false", "null", "undefined"];
33
-
34
- // Characters that end an identifier/path. "[" starts a Handlebars literal segment,
35
- // e.g. [my attribute], which may contain any of these.
36
- const identTerminators = new Set([..." \t\r\n!\"'()<>=&|{}~"]);
37
-
38
- class ExpressionError extends Error {}
39
-
40
- function tokenize(expr: string): Token[] {
41
- const tokens: Token[] = [];
42
- let i = 0;
43
- while (i < expr.length) {
44
- const c = expr[i];
45
- if (/\s/.test(c)) {
46
- i++;
47
- continue;
48
- }
49
- if (c === "(" || c === ")") {
50
- tokens.push({ type: c, text: c });
51
- i++;
52
- continue;
53
- }
54
- if (c === "\"" || c === "'") {
55
- let j = i + 1;
56
- while (j < expr.length && expr[j] !== c) {
57
- if (expr[j] === "\\") j++;
58
- j++;
59
- }
60
- if (j >= expr.length) {
61
- throw new ExpressionError("unterminated string");
62
- }
63
- tokens.push({ type: "value", text: expr.slice(i, j + 1) });
64
- i = j + 1;
65
- continue;
66
- }
67
- const op = symbolicOps.find(o => expr.startsWith(o, i));
68
- if (op) {
69
- tokens.push({ type: "op", text: op });
70
- i += op.length;
71
- continue;
72
- }
73
- let j = i;
74
- while (j < expr.length && !identTerminators.has(expr[j])) {
75
- if (expr[j] === "[") {
76
- const close = expr.indexOf("]", j);
77
- if (close === -1) {
78
- throw new ExpressionError("missing ']'");
79
- }
80
- j = close;
81
- }
82
- j++;
83
- }
84
- if (j === i) {
85
- throw new ExpressionError(`unexpected '${c}'`);
86
- }
87
- const word = expr.slice(i, j);
88
- if (keywordOps.includes(word)) {
89
- tokens.push({ type: "op", text: word });
90
- } else if (literals.includes(word) || /^-?\d+(\.\d+)?$/.test(word)) {
91
- tokens.push({ type: "value", text: word });
92
- } else {
93
- tokens.push({ type: "ident", text: word });
94
- }
95
- i = j;
96
- }
97
- return tokens;
98
- }
99
-
100
- class Parser {
101
- private pos = 0;
102
- usedInfix = false;
103
-
104
- constructor(private tokens: Token[]) {}
105
-
106
- private peek(offset = 0): Token | undefined {
107
- return this.tokens[this.pos + offset];
108
- }
109
-
110
- private next(): Token {
111
- return this.tokens[this.pos++];
112
- }
113
-
114
- private isOp(token: Token | undefined, ...ops: string[]) {
115
- return token?.type === "op" && ops.includes(token.text);
116
- }
117
-
118
- private startsPrimary(token: Token | undefined) {
119
- return token?.type === "ident" || token?.type === "value" || token?.type === "(";
120
- }
121
-
122
- private describe(token: Token | undefined) {
123
- return token ? `'${token.text}'` : "end of expression";
124
- }
125
-
126
- // Top level: an expression, optionally followed by hash arguments (e.g. includeZero=true)
127
- parseCondition(): Node[] {
128
- const result = [this.parseOr()];
129
- while (this.peek()?.type === "ident" && this.isOp(this.peek(1), "=")) {
130
- result.push(this.parseHashPair());
131
- }
132
- if (this.peek()) {
133
- throw new ExpressionError(`unexpected ${this.describe(this.peek())}`);
134
- }
135
- return result;
136
- }
137
-
138
- private parseHashPair(): Node {
139
- const key = this.next().text;
140
- this.next(); // "="
141
- return { kind: "raw", text: `${key}=${emit(this.parsePrimary())}` };
142
- }
143
-
144
- private parseOr(): Node {
145
- return this.parseChain(["or", "||"], "or", () => this.parseAnd());
146
- }
147
-
148
- private parseAnd(): Node {
149
- return this.parseChain(["and", "&&"], "and", () => this.parseNot());
150
- }
151
-
152
- private parseChain(ops: string[], helper: string, parseOperand: () => Node): Node {
153
- const operands = [parseOperand()];
154
- while (this.isOp(this.peek(), ...ops)) {
155
- const op = this.next().text;
156
- if (!this.peek()) {
157
- throw new ExpressionError(`expected a value after '${op}'`);
158
- }
159
- operands.push(parseOperand());
160
- }
161
- if (operands.length === 1) return operands[0];
162
- this.usedInfix = true;
163
- return { kind: "call", name: helper, args: operands };
164
- }
165
-
166
- private parseNot(): Node {
167
- if (this.isOp(this.peek(), "not", "!")) {
168
- const op = this.next().text;
169
- if (!this.peek()) {
170
- throw new ExpressionError(`expected a value after '${op}'`);
171
- }
172
- this.usedInfix = true;
173
- return { kind: "call", name: "not", args: [this.parseNot()] };
174
- }
175
- return this.parseComparison();
176
- }
177
-
178
- private parseComparison(): Node {
179
- const left = this.parsePrimary();
180
- const opToken = this.peek();
181
- if (opToken?.type !== "op" || !(opToken.text in comparisonHelpers)) {
182
- return left;
183
- }
184
- this.next();
185
- if (!this.peek()) {
186
- throw new ExpressionError(`expected a value after '${opToken.text}'`);
187
- }
188
- const right = this.parsePrimary();
189
- const following = this.peek();
190
- if (following?.type === "op" && following.text in comparisonHelpers) {
191
- throw new ExpressionError(`comparisons can't be chained (at '${following.text}'), use "and" to combine them`);
192
- }
193
- this.usedInfix = true;
194
- return { kind: "call", name: comparisonHelpers[opToken.text], args: [left, right] };
195
- }
196
-
197
- private parsePrimary(): Node {
198
- const token = this.peek();
199
- if (token?.type === "ident" || token?.type === "value") {
200
- this.next();
201
- return { kind: "raw", text: token.text };
202
- }
203
- if (token?.type === "(") {
204
- this.next();
205
- const node = this.isHelperCall() ? this.parseHelperCall() : this.parseOr();
206
- if (this.peek()?.type !== ")") {
207
- throw new ExpressionError(`expected ')' but found ${this.describe(this.peek())}`);
208
- }
209
- this.next();
210
- return node;
211
- }
212
- if (this.isOp(token, "not", "!")) {
213
- const example = token?.text === "!" ? "!value" : "not value";
214
- throw new ExpressionError(`'${token?.text}' must be inside parentheses here, e.g. (${example})`);
215
- }
216
- if (token?.type === "op") {
217
- throw new ExpressionError(`expected a value before '${token.text}'`);
218
- }
219
- throw new ExpressionError(token ? `unexpected ${this.describe(token)}` : "expected a value");
220
- }
221
-
222
- // Inside parentheses, decide between an existing prefix helper call like (seen "intro")
223
- // or (and a b), and an infix group like (score > 100).
224
- private isHelperCall() {
225
- const first = this.peek();
226
- const second = this.peek(1);
227
- if (this.isOp(first, "and", "or")) return true;
228
- return first?.type === "ident" && this.startsPrimary(second);
229
- }
230
-
231
- private parseHelperCall(): Node {
232
- const name = this.next().text;
233
- const args: Node[] = [];
234
- while (this.peek() && this.peek()?.type !== ")") {
235
- if (this.peek()?.type === "ident" && this.isOp(this.peek(1), "=")) {
236
- args.push(this.parseHashPair());
237
- } else {
238
- args.push(this.parsePrimary());
239
- }
240
- }
241
- return { kind: "call", name, args };
242
- }
243
- }
244
-
245
- function emit(node: Node): string {
246
- if (node.kind === "raw") return node.text;
247
- return `(${node.name} ${node.args.map(emit).join(" ")})`;
248
- }
249
-
250
- // Rewrites a single condition. Returns null if it doesn't use infix syntax.
251
- export function rewriteInfixCondition(condition: string): string | null {
252
- let tokens: Token[];
253
- try {
254
- tokens = tokenize(condition);
255
- } catch {
256
- // Leave anything we can't tokenize for Handlebars to report
257
- return null;
258
- }
259
- const hasOperators = tokens.some(t => t.type === "op");
260
- const parser = new Parser(tokens);
261
- let nodes: Node[];
262
- let error: ExpressionError | null = null;
263
- try {
264
- nodes = parser.parseCondition();
265
- } catch (e) {
266
- if (!hasOperators || !(e instanceof ExpressionError)) return null;
267
- error = e;
268
- nodes = [];
269
- }
270
- if (error) {
271
- throw new Error(`Invalid condition "${condition.trim()}": ${error.message}`);
272
- }
273
- if (!parser.usedInfix) return null;
274
- return nodes.map(emit).join(" ");
275
- }
276
-
277
- const blockStart = /(\\*)\{\{(~?\s*)(#if|#unless|else\s+if|else\s+unless)(?=[\s(!])/g;
278
-
279
- // Finds the end of a mustache tag starting at `from`, skipping over quoted strings.
280
- // Returns the index of the closing "}}" (or "~}}"), or -1.
281
- function findTagEnd(text: string, from: number): number {
282
- let quote: string | null = null;
283
- for (let i = from; i < text.length; i++) {
284
- const c = text[i];
285
- if (quote) {
286
- if (c === "\\") i++;
287
- else if (c === quote) quote = null;
288
- } else if (c === "\"" || c === "'") {
289
- quote = c;
290
- } else if (text.startsWith("~}}", i)) {
291
- return i;
292
- } else if (text.startsWith("}}", i)) {
293
- return i;
294
- }
295
- }
296
- return -1;
297
- }
298
-
299
- export function rewriteInfixExpressions(text: string): string {
300
- if (!text.includes("{{")) return text;
301
-
302
- let result = "";
303
- let last = 0;
304
- blockStart.lastIndex = 0;
305
- let match: RegExpExecArray | null;
306
- while ((match = blockStart.exec(text)) !== null) {
307
- // An odd number of backslashes escapes the mustache
308
- if (match[1].length % 2 === 1) continue;
309
-
310
- const argStart = match.index + match[0].length;
311
- const argEnd = findTagEnd(text, argStart);
312
- if (argEnd === -1) break;
313
-
314
- const condition = text.slice(argStart, argEnd);
315
- const rewritten = rewriteInfixCondition(condition);
316
- if (rewritten !== null) {
317
- result += text.slice(last, argStart) + " " + rewritten;
318
- last = argEnd;
319
- }
320
- blockStart.lastIndex = argEnd;
321
- }
322
- return result + text.slice(last);
323
- }