squiffy-runtime 6.0.0-beta.4 → 6.0.0-beta.5
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/squiffy.runtime.global.js +2 -2
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +72 -69
- package/dist/squiffy.runtime.js.map +1 -1
- package/dist/types.d.ts +1 -0
- package/package.json +3 -3
- package/src/plugins/plugins.test.ts +166 -19
- package/src/squiffy.runtime.test.ts +37 -0
- package/src/squiffy.runtime.ts +1 -0
- package/src/types.ts +1 -0
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "squiffy-runtime",
|
|
3
|
-
"version": "6.0.0-beta.
|
|
3
|
+
"version": "6.0.0-beta.5",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/textadventures/squiffy.git",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"esbuild": "^0.28.2",
|
|
39
39
|
"global-jsdom": "^30.0.0",
|
|
40
40
|
"jsdom": "^30.0.1",
|
|
41
|
-
"squiffy-compiler": "^6.0.0-beta.
|
|
41
|
+
"squiffy-compiler": "^6.0.0-beta.5",
|
|
42
42
|
"typescript": "^6.0.3",
|
|
43
43
|
"vite": "^8.2.2",
|
|
44
44
|
"vite-plugin-dts": "^5.1.0"
|
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
"handlebars": "^4.7.10",
|
|
49
49
|
"marked": "^18.0.11"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "55e706ccb3aee2527b5629984642b6681b47a00d"
|
|
52
52
|
}
|
|
@@ -1,8 +1,20 @@
|
|
|
1
|
-
import { expect, test, beforeEach, afterEach } from "vitest";
|
|
1
|
+
import { expect, test, beforeEach, afterEach, vi } from "vitest";
|
|
2
2
|
import globalJsdom from "global-jsdom";
|
|
3
3
|
import { init } from "../squiffy.runtime.js";
|
|
4
4
|
import { compile as squiffyCompile } from "squiffy-compiler";
|
|
5
5
|
|
|
6
|
+
// anime.js captures the global document when it's imported, which is before each test
|
|
7
|
+
// sets up jsdom, so it can't run here. Replace it with a stub that completes immediately.
|
|
8
|
+
vi.mock("animejs", () => ({
|
|
9
|
+
animate: (_targets: unknown, params: { onComplete?: () => void }) => {
|
|
10
|
+
setTimeout(() => params.onComplete?.());
|
|
11
|
+
},
|
|
12
|
+
stagger: () => 0,
|
|
13
|
+
text: {
|
|
14
|
+
split: (el: HTMLElement) => ({ chars: [el], words: [el], lines: [el] }),
|
|
15
|
+
},
|
|
16
|
+
}));
|
|
17
|
+
|
|
6
18
|
const html = `
|
|
7
19
|
<!DOCTYPE html>
|
|
8
20
|
<html>
|
|
@@ -70,10 +82,39 @@ const findLink = (element: HTMLElement, linkType: string, linkText: string) => {
|
|
|
70
82
|
return Array.from(links).find(link => link.textContent === linkText) as HTMLElement | undefined;
|
|
71
83
|
};
|
|
72
84
|
|
|
85
|
+
// jsdom doesn't run CSS transitions, so fadeReplace would wait forever for a
|
|
86
|
+
// "transitionend" event. Fire one whenever a fade class is added instead.
|
|
87
|
+
const completeFadeTransitions = () => {
|
|
88
|
+
const TransitionEvent = window.Event;
|
|
89
|
+
const observer = new MutationObserver(mutations => {
|
|
90
|
+
for (const mutation of mutations) {
|
|
91
|
+
const el = mutation.target as HTMLElement;
|
|
92
|
+
const oldClasses = (mutation.oldValue || "").split(" ");
|
|
93
|
+
for (const fadeClass of ["fade-out", "fade-in"]) {
|
|
94
|
+
if (el.classList.contains(fadeClass) && !oldClasses.includes(fadeClass)) {
|
|
95
|
+
setTimeout(() => el.dispatchEvent(new TransitionEvent("transitionend")));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
observer.observe(document.body, {
|
|
101
|
+
subtree: true,
|
|
102
|
+
attributes: true,
|
|
103
|
+
attributeFilter: ["class"],
|
|
104
|
+
attributeOldValue: true,
|
|
105
|
+
});
|
|
106
|
+
return () => observer.disconnect();
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const findPassageLink = (element: HTMLElement, passageName: string) =>
|
|
110
|
+
element.querySelector<HTMLElement>(`a.squiffy-link[data-passage="${passageName}"]`) ?? undefined;
|
|
111
|
+
|
|
73
112
|
let cleanup: { (): void };
|
|
113
|
+
let stopCompletingTransitions: { (): void };
|
|
74
114
|
|
|
75
115
|
beforeEach(() => {
|
|
76
116
|
cleanup = globalJsdom(html);
|
|
117
|
+
stopCompletingTransitions = completeFadeTransitions();
|
|
77
118
|
// Mock document.fonts for animations
|
|
78
119
|
Object.defineProperty(document, "fonts", {
|
|
79
120
|
value: { status: "loaded" },
|
|
@@ -83,6 +124,7 @@ beforeEach(() => {
|
|
|
83
124
|
});
|
|
84
125
|
|
|
85
126
|
afterEach(() => {
|
|
127
|
+
stopCompletingTransitions();
|
|
86
128
|
localStorage.clear();
|
|
87
129
|
cleanup();
|
|
88
130
|
});
|
|
@@ -156,9 +198,26 @@ Your score is {{#label "score"}}0{{/label}}.
|
|
|
156
198
|
expect(labelSpan?.textContent).toBe("0");
|
|
157
199
|
});
|
|
158
200
|
|
|
159
|
-
test
|
|
160
|
-
|
|
161
|
-
|
|
201
|
+
test("Replace plugin: replace labeled content", async () => {
|
|
202
|
+
const script = `
|
|
203
|
+
Your score is {{#label "score"}}0{{/label}}.
|
|
204
|
+
|
|
205
|
+
[Increase score]
|
|
206
|
+
|
|
207
|
+
[Increase score]:
|
|
208
|
+
{{#replace "score"}}**10**{{/replace}}
|
|
209
|
+
`;
|
|
210
|
+
|
|
211
|
+
const { squiffyApi, element } = await initScript(script);
|
|
212
|
+
|
|
213
|
+
const link = findPassageLink(element, "Increase score");
|
|
214
|
+
await squiffyApi.clickLink(link!);
|
|
215
|
+
|
|
216
|
+
const labelSpans = element.querySelectorAll(".squiffy-label-score");
|
|
217
|
+
expect(labelSpans.length).toBe(1);
|
|
218
|
+
expect(labelSpans[0].innerHTML).toBe("<strong>10</strong>");
|
|
219
|
+
expect(labelSpans[0].classList.contains("fade-out")).toBe(false);
|
|
220
|
+
expect(labelSpans[0].classList.contains("fade-in")).toBe(false);
|
|
162
221
|
});
|
|
163
222
|
|
|
164
223
|
// ===== RotateSequencePlugin Tests =====
|
|
@@ -262,31 +321,119 @@ Score: {{live "score"}}.
|
|
|
262
321
|
expect(liveSpan?.textContent).toBe("0");
|
|
263
322
|
});
|
|
264
323
|
|
|
265
|
-
test
|
|
266
|
-
|
|
267
|
-
|
|
324
|
+
test("Live plugin: updates when attribute changes", async () => {
|
|
325
|
+
const script = `
|
|
326
|
+
@set score = 0
|
|
327
|
+
|
|
328
|
+
Score: {{live "score"}}.
|
|
329
|
+
|
|
330
|
+
[Increase score]
|
|
331
|
+
|
|
332
|
+
[Increase score]:
|
|
333
|
+
@set score = 10
|
|
334
|
+
`;
|
|
335
|
+
|
|
336
|
+
const { squiffyApi, element } = await initScript(script);
|
|
337
|
+
|
|
338
|
+
const link = findPassageLink(element, "Increase score");
|
|
339
|
+
await squiffyApi.clickLink(link!);
|
|
340
|
+
|
|
341
|
+
// Live updates run in the background after the attribute is set, so wait for the fade to finish
|
|
342
|
+
const liveSpan = element.querySelector(".squiffy-live");
|
|
343
|
+
await vi.waitFor(() => {
|
|
344
|
+
expect(liveSpan?.textContent).toBe("10");
|
|
345
|
+
expect(liveSpan?.classList.contains("fade-in")).toBe(false);
|
|
346
|
+
});
|
|
268
347
|
});
|
|
269
348
|
|
|
270
|
-
test
|
|
271
|
-
|
|
272
|
-
|
|
349
|
+
test("Live plugin: embed section content", async () => {
|
|
350
|
+
const script = `
|
|
351
|
+
[[start]]:
|
|
352
|
+
@set mood = happy
|
|
353
|
+
|
|
354
|
+
Status: {{live "mood" section="status"}}
|
|
355
|
+
|
|
356
|
+
[Cheer up]
|
|
357
|
+
|
|
358
|
+
[Cheer up]:
|
|
359
|
+
@set mood = ecstatic
|
|
360
|
+
|
|
361
|
+
[[status]]:
|
|
362
|
+
You are {{mood}}.
|
|
363
|
+
`;
|
|
364
|
+
|
|
365
|
+
const { squiffyApi, element } = await initScript(script);
|
|
366
|
+
|
|
367
|
+
const liveSpan = element.querySelector(".squiffy-live");
|
|
368
|
+
expect(liveSpan?.getAttribute("data-section")).toBe("status");
|
|
369
|
+
expect(liveSpan?.textContent).toBe("You are happy.");
|
|
370
|
+
|
|
371
|
+
const link = findPassageLink(element, "Cheer up");
|
|
372
|
+
await squiffyApi.clickLink(link!);
|
|
373
|
+
|
|
374
|
+
await vi.waitFor(() => expect(liveSpan?.textContent).toBe("You are ecstatic."));
|
|
273
375
|
});
|
|
274
376
|
|
|
275
|
-
test
|
|
276
|
-
|
|
277
|
-
|
|
377
|
+
test("Live plugin: embed passage content", async () => {
|
|
378
|
+
const script = `
|
|
379
|
+
@set mood = happy
|
|
380
|
+
|
|
381
|
+
Status: {{live "mood" passage="status"}}
|
|
382
|
+
|
|
383
|
+
[Cheer up]
|
|
384
|
+
|
|
385
|
+
[Cheer up]:
|
|
386
|
+
@set mood = ecstatic
|
|
387
|
+
|
|
388
|
+
[status]:
|
|
389
|
+
You are {{mood}}.
|
|
390
|
+
`;
|
|
391
|
+
|
|
392
|
+
const { squiffyApi, element } = await initScript(script);
|
|
393
|
+
|
|
394
|
+
const liveSpan = element.querySelector(".squiffy-live");
|
|
395
|
+
expect(liveSpan?.getAttribute("data-passage")).toBe("status");
|
|
396
|
+
expect(liveSpan?.textContent).toBe("You are happy.");
|
|
397
|
+
|
|
398
|
+
const link = findPassageLink(element, "Cheer up");
|
|
399
|
+
await squiffyApi.clickLink(link!);
|
|
400
|
+
|
|
401
|
+
await vi.waitFor(() => expect(liveSpan?.textContent).toBe("You are ecstatic."));
|
|
278
402
|
});
|
|
279
403
|
|
|
280
404
|
// ===== AnimatePlugin Tests =====
|
|
281
405
|
|
|
282
|
-
test
|
|
283
|
-
|
|
284
|
-
|
|
406
|
+
test("Animate plugin: creates animated span", async () => {
|
|
407
|
+
const script = `
|
|
408
|
+
{{#animate "typewriter"}}Hello **world**{{/animate}}
|
|
409
|
+
`;
|
|
410
|
+
|
|
411
|
+
const { element } = await initScript(script);
|
|
412
|
+
|
|
413
|
+
// Once the animation completes, the original content is restored
|
|
414
|
+
const animateSpan = element.querySelector("span[data-name]");
|
|
415
|
+
expect(animateSpan).not.toBeNull();
|
|
416
|
+
expect(animateSpan?.innerHTML).toBe("Hello <strong>world</strong>");
|
|
417
|
+
expect(animateSpan?.classList.contains("squiffy-animate")).toBe(false);
|
|
285
418
|
});
|
|
286
419
|
|
|
287
|
-
test
|
|
288
|
-
|
|
289
|
-
|
|
420
|
+
test("Animate plugin: sets data attributes", async () => {
|
|
421
|
+
const script = `
|
|
422
|
+
{{#animate "typewriter" trigger="link" interval=50}}[Click me]{{/animate}}
|
|
423
|
+
|
|
424
|
+
[Click me]:
|
|
425
|
+
You clicked!
|
|
426
|
+
`;
|
|
427
|
+
|
|
428
|
+
const { element } = await initScript(script);
|
|
429
|
+
|
|
430
|
+
const animateSpan = element.querySelector<HTMLElement>(".squiffy-animate");
|
|
431
|
+
expect(animateSpan).not.toBeNull();
|
|
432
|
+
expect(animateSpan?.tagName).toBe("SPAN");
|
|
433
|
+
expect(animateSpan?.getAttribute("data-name")).toBe('"typewriter"');
|
|
434
|
+
expect(JSON.parse(animateSpan!.getAttribute("data-content")!)).toContain('data-passage="Click me"');
|
|
435
|
+
expect(animateSpan?.getAttribute("data-trigger")).toBe('"link"');
|
|
436
|
+
expect(animateSpan?.getAttribute("data-interval")).toBe("50");
|
|
290
437
|
});
|
|
291
438
|
|
|
292
439
|
test("Animate plugin: trigger on link", async () => {
|
|
@@ -121,6 +121,29 @@ Destination section text.`;
|
|
|
121
121
|
expect(element.querySelector(".squiffy-output-section")?.getAttribute("data-section")).toBe("destination");
|
|
122
122
|
});
|
|
123
123
|
|
|
124
|
+
test("squiffy.story.seen() in JS reports which sections and passages have been seen", async () => {
|
|
125
|
+
// Note: the 4-space indent on the JS lines is the squiffy JS syntax - don't trim it!
|
|
126
|
+
const script = `Click [a] then [check].
|
|
127
|
+
|
|
128
|
+
[a]:
|
|
129
|
+
Passage a.
|
|
130
|
+
|
|
131
|
+
[check]:
|
|
132
|
+
squiffy.set("seen_a", squiffy.story.seen("a"));
|
|
133
|
+
squiffy.set("seen_b", squiffy.story.seen("b"));
|
|
134
|
+
squiffy.set("seen_start", squiffy.story.seen("_default"));
|
|
135
|
+
Checked.`;
|
|
136
|
+
|
|
137
|
+
const { squiffyApi, element } = await initScript(script);
|
|
138
|
+
|
|
139
|
+
await squiffyApi.clickLink(findLink(element, "passage", "a"));
|
|
140
|
+
await squiffyApi.clickLink(findLink(element, "passage", "check"));
|
|
141
|
+
|
|
142
|
+
expect(squiffyApi.get("seen_a")).toBe(true);
|
|
143
|
+
expect(squiffyApi.get("seen_b")).toBe(false);
|
|
144
|
+
expect(squiffyApi.get("seen_start")).toBe(true);
|
|
145
|
+
});
|
|
146
|
+
|
|
124
147
|
test("Click a section link", async () => {
|
|
125
148
|
const script = await fs.readFile("../examples/test/example.squiffy", "utf-8");
|
|
126
149
|
const { squiffyApi, element } = await initScript(script);
|
|
@@ -703,6 +726,20 @@ Passage in section B.`);
|
|
|
703
726
|
expect(await squiffyApi.clickLink(linkB1)).toBe(true);
|
|
704
727
|
});
|
|
705
728
|
|
|
729
|
+
test("Print an array element by index", async () => {
|
|
730
|
+
const script = `
|
|
731
|
+
[[section1]]:
|
|
732
|
+
squiffy.set("fruits", ["apple", "banana", "cherry"]);
|
|
733
|
+
|
|
734
|
+
The second fruit is {{fruits.[1]}}.
|
|
735
|
+
`;
|
|
736
|
+
|
|
737
|
+
const { element } = await initScript(script);
|
|
738
|
+
|
|
739
|
+
const output = getSectionContent(element, "section1");
|
|
740
|
+
expect(output).toBe("The second fruit is banana.");
|
|
741
|
+
});
|
|
742
|
+
|
|
706
743
|
test("Embed text from a section", async () => {
|
|
707
744
|
const script = `
|
|
708
745
|
[[section1]]:
|
package/src/squiffy.runtime.ts
CHANGED