squiffy-runtime 7.0.0-beta.6 → 7.0.0-beta.8
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/assets.d.ts +1 -0
- package/dist/classes.test.d.ts +1 -0
- package/dist/confirmDialog.d.ts +8 -0
- package/dist/events.d.ts +11 -0
- package/dist/every.d.ts +12 -0
- package/dist/every.test.d.ts +1 -0
- package/dist/paths.d.ts +15 -0
- package/dist/paths.test.d.ts +1 -0
- package/dist/playerSettings.d.ts +17 -0
- package/dist/preview-host.d.ts +2 -0
- package/dist/preview-host.js +10 -0
- package/dist/preview-host.js.map +2 -2
- package/dist/preview-html.js +1 -1
- package/dist/preview-protocol.d.ts +6 -0
- package/dist/settings.test.d.ts +1 -0
- package/dist/settingsDialog.d.ts +17 -0
- package/dist/skill.test.d.ts +1 -0
- package/dist/sound.d.ts +35 -0
- package/dist/sound.test.d.ts +1 -0
- package/dist/squiffy.page.css +14 -3
- package/dist/squiffy.preview.html +680 -82
- package/dist/squiffy.runtime.css +531 -1
- package/dist/squiffy.runtime.d.ts +5 -1
- package/dist/squiffy.runtime.global.js +117 -96
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +5728 -4520
- package/dist/squiffy.runtime.js.map +1 -1
- package/dist/state.d.ts +4 -1
- package/dist/style.test.d.ts +1 -0
- package/dist/textProcessor.d.ts +16 -0
- package/dist/transitions.test.d.ts +1 -0
- package/dist/types.d.ts +54 -0
- package/dist/uiLinks.test.d.ts +1 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.test.d.ts +1 -0
- package/package.json +3 -3
- package/src/__snapshots__/squiffy.runtime.test.ts.snap +10 -10
- package/src/animation.test.ts +44 -0
- package/src/animation.ts +9 -5
- package/src/assets.test.ts +1 -3
- package/src/assets.ts +1 -1
- package/src/classes.test.ts +201 -0
- package/src/confirmDialog.ts +66 -0
- package/src/events.ts +18 -0
- package/src/every.test.ts +25 -0
- package/src/every.ts +94 -0
- package/src/panels.test.ts +24 -0
- package/src/paths.test.ts +66 -0
- package/src/paths.ts +155 -0
- package/src/playerSettings.ts +128 -0
- package/src/plugins/animate.ts +16 -3
- package/src/plugins/live.ts +10 -4
- package/src/plugins/plugins.test.ts +51 -6
- package/src/preview-host.test.ts +22 -0
- package/src/preview-host.ts +15 -0
- package/src/preview-protocol.ts +9 -1
- package/src/preview.test.ts +114 -3
- package/src/preview.ts +97 -3
- package/src/settings.test.ts +233 -0
- package/src/settingsDialog.ts +160 -0
- package/src/skill.test.ts +67 -0
- package/src/sound.test.ts +273 -0
- package/src/sound.ts +207 -0
- package/src/squiffy.page.css +14 -3
- package/src/squiffy.runtime.css +531 -1
- package/src/squiffy.runtime.test.ts +1130 -17
- package/src/squiffy.runtime.ts +600 -38
- package/src/state.ts +45 -10
- package/src/style.test.ts +89 -0
- package/src/textProcessor.ts +484 -35
- package/src/transitions.test.ts +302 -0
- package/src/types.ts +79 -0
- package/src/uiLinks.test.ts +156 -0
- package/src/updater.ts +26 -8
- package/src/utils.test.ts +60 -0
- package/src/utils.ts +44 -14
- package/dist/infixExpressions.d.ts +0 -6
- package/src/infixExpressions.test.ts +0 -379
- package/src/infixExpressions.ts +0 -488
- /package/dist/{infixExpressions.test.d.ts → animation.test.d.ts} +0 -0
package/src/state.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { Emitter, SquiffyEventMap } from "./events.js";
|
|
2
|
+
import { parsePath, valueAt, withValueAt } from "./paths.js";
|
|
3
|
+
|
|
4
|
+
// The attribute the story reads the player's settings from
|
|
5
|
+
const SETTINGS = "settings";
|
|
2
6
|
|
|
3
7
|
export class State {
|
|
4
8
|
persist: boolean;
|
|
@@ -24,20 +28,44 @@ export class State {
|
|
|
24
28
|
return this.persist && window.localStorage && this.storyId;
|
|
25
29
|
}
|
|
26
30
|
|
|
31
|
+
// The player's settings (#452), which the story reads as the attribute "settings", e.g.
|
|
32
|
+
// settings.textSize, or null if the story has none, when "settings" is an attribute like any other
|
|
33
|
+
playerSettings: () => Record<string, unknown> | null = () => null;
|
|
34
|
+
|
|
27
35
|
// While this is true, set() does nothing: e.g. while a panel is rendered, so its {{set}}s
|
|
28
36
|
// don't change the story
|
|
29
37
|
readOnly = false;
|
|
30
38
|
|
|
39
|
+
// The name can be a path to a value inside an object or list, such as hero.hp (#442): see
|
|
40
|
+
// paths.ts
|
|
31
41
|
set(attribute: string, value: any) {
|
|
42
|
+
this.setAt(parsePath(attribute), value);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Sets the value at a path: the attribute, then the names inside it. The whole attribute is
|
|
46
|
+
// set again, so going back, saves and the "set" event see the attribute that changed.
|
|
47
|
+
setAt(segments: string[], value: any) {
|
|
32
48
|
if (this.readOnly) return;
|
|
33
|
-
|
|
49
|
+
const [attribute, ...inside] = segments;
|
|
50
|
+
// Only the player changes their settings, in the Settings dialog
|
|
51
|
+
if (attribute == SETTINGS && this.playerSettings()) {
|
|
52
|
+
console.warn("Squiffy: the story can't change the player's settings.");
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (!inside.length) {
|
|
56
|
+
this.setInternal(attribute, value, true);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (typeof value === "undefined") value = true;
|
|
60
|
+
const root = this.has(attribute) ? this.store[attribute] : null;
|
|
61
|
+
this.setInternal(attribute, withValueAt(root, inside, attribute, value), true);
|
|
34
62
|
}
|
|
35
63
|
|
|
36
64
|
setInternal(attribute: string, value: any, raiseEvents: boolean) {
|
|
37
65
|
if (typeof value === "undefined") value = true;
|
|
38
66
|
|
|
39
67
|
if (raiseEvents && this.onSetInternal) {
|
|
40
|
-
this.onSetInternal(attribute, this.
|
|
68
|
+
this.onSetInternal(attribute, this.getAt([attribute]), structuredClone(value));
|
|
41
69
|
}
|
|
42
70
|
|
|
43
71
|
this.store[attribute] = structuredClone(value);
|
|
@@ -54,16 +82,23 @@ export class State {
|
|
|
54
82
|
}
|
|
55
83
|
|
|
56
84
|
// Only the store's own properties are attributes, not ones it inherits, such as valueOf
|
|
57
|
-
|
|
85
|
+
has(attribute: string) {
|
|
86
|
+
if (attribute == SETTINGS && this.playerSettings()) return true;
|
|
58
87
|
return Object.prototype.hasOwnProperty.call(this.store, attribute);
|
|
59
88
|
}
|
|
60
89
|
|
|
90
|
+
// The name can be a path, as for set()
|
|
61
91
|
get(attribute: string): any {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
92
|
+
return this.getAt(parsePath(attribute));
|
|
93
|
+
}
|
|
65
94
|
|
|
66
|
-
|
|
95
|
+
getAt(segments: string[]): any {
|
|
96
|
+
const [attribute, ...inside] = segments;
|
|
97
|
+
if (!this.has(attribute)) return null;
|
|
98
|
+
const settings = attribute == SETTINGS ? this.playerSettings() : null;
|
|
99
|
+
if (settings) return inside.length ? structuredClone(valueAt(settings, inside)) : { ...settings };
|
|
100
|
+
const value = inside.length ? valueAt(this.store[attribute], inside) : this.store[attribute];
|
|
101
|
+
return structuredClone(value);
|
|
67
102
|
}
|
|
68
103
|
|
|
69
104
|
getStore() {
|
|
@@ -74,17 +109,17 @@ export class State {
|
|
|
74
109
|
// A name that isn't an attribute is null, unless it's one every object has, like
|
|
75
110
|
// valueOf, which Handlebars can use
|
|
76
111
|
get: (target, prop) => typeof prop == "string" && (this.has(prop) || !(prop in target))
|
|
77
|
-
? this.
|
|
112
|
+
? this.getAt([prop])
|
|
78
113
|
: Reflect.get(target, prop),
|
|
79
114
|
has: (_, prop) => this.has(String(prop)),
|
|
80
|
-
ownKeys: () => Object.keys(this.store),
|
|
115
|
+
ownKeys: () => [...new Set([...Object.keys(this.store), ...(this.playerSettings() ? [SETTINGS] : [])])],
|
|
81
116
|
getOwnPropertyDescriptor: (_, prop) => {
|
|
82
117
|
const key = String(prop);
|
|
83
118
|
if (this.has(key)) {
|
|
84
119
|
return {
|
|
85
120
|
enumerable: true,
|
|
86
121
|
configurable: true,
|
|
87
|
-
value: this.
|
|
122
|
+
value: this.getAt([key])
|
|
88
123
|
};
|
|
89
124
|
}
|
|
90
125
|
return undefined;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// {{#style}} (#453): gives some text a class, for the story's CSS or the built-in effects
|
|
2
|
+
import { expect, test, beforeEach, afterEach } from "vitest";
|
|
3
|
+
import globalJsdom from "global-jsdom";
|
|
4
|
+
import { init } from "./squiffy.runtime.js";
|
|
5
|
+
import { compile as squiffyCompile } from "squiffy-compiler";
|
|
6
|
+
|
|
7
|
+
let cleanup: () => void;
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
cleanup = globalJsdom("<!DOCTYPE html><html><body><div id=\"squiffy\"></div></body></html>");
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
localStorage.clear();
|
|
15
|
+
cleanup();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const start = async (script: string) => {
|
|
19
|
+
const result = await squiffyCompile({ script });
|
|
20
|
+
if (!result.success) throw new Error("Compile failed");
|
|
21
|
+
const story = {
|
|
22
|
+
...result.output.story,
|
|
23
|
+
js: result.output.js.map(lines => new Function("squiffy", "get", "set", lines.join("\n"))) as any,
|
|
24
|
+
};
|
|
25
|
+
const element = document.getElementById("squiffy")!;
|
|
26
|
+
const api = await init({ element, story, persist: false, storyId: "test", scroll: "none" });
|
|
27
|
+
await api.begin();
|
|
28
|
+
return element;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
test("{{#style}} wraps inline text in a span with the class", async () => {
|
|
32
|
+
const element = await start("A {{#style \"warning\"}}**loud** noise{{/style}} here.");
|
|
33
|
+
const span = element.querySelector(".squiffy-style")!;
|
|
34
|
+
expect(span.tagName).toBe("SPAN");
|
|
35
|
+
expect([...span.classList]).toEqual(["squiffy-style", "warning"]);
|
|
36
|
+
expect(span.innerHTML).toBe("<strong>loud</strong> noise");
|
|
37
|
+
expect(span.closest("p")?.textContent).toBe("A loud noise here.");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("{{#style}} takes several classes, from strings or attributes", async () => {
|
|
41
|
+
const element = await start(`@set mood = gloomy
|
|
42
|
+
{{#style "big shudder" mood}}Text{{/style}}`);
|
|
43
|
+
expect([...element.querySelector(".squiffy-style")!.classList]).toEqual(["squiffy-style", "big", "shudder", "gloomy"]);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("{{#style}} puts each effect after the first on an element of its own, so they all apply", async () => {
|
|
47
|
+
const element = await start("{{#style \"big shudder fade-in-out mirror\"}}**Text**{{/style}}");
|
|
48
|
+
const p = element.querySelector(".squiffy-output-block p")!;
|
|
49
|
+
expect(p.innerHTML).toBe("<span class=\"squiffy-style big shudder\"><span class=\"squiffy-style fade-in-out\">"
|
|
50
|
+
+ "<span class=\"squiffy-style mirror\"><strong>Text</strong></span></span></span>");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("{{#style}} nests divs for several effects around paragraphs", async () => {
|
|
54
|
+
const element = await start("{{#style \"letter sway blur\"}}\nDear *you*,\n\nGoodbye.\n{{/style}}");
|
|
55
|
+
const outer = element.querySelector(".squiffy-style")!;
|
|
56
|
+
expect(outer.tagName).toBe("DIV");
|
|
57
|
+
expect([...outer.classList]).toEqual(["squiffy-style", "letter", "sway"]);
|
|
58
|
+
const inner = outer.querySelector(":scope > .squiffy-style")!;
|
|
59
|
+
expect(inner.tagName).toBe("DIV");
|
|
60
|
+
expect([...inner.classList]).toEqual(["squiffy-style", "blur"]);
|
|
61
|
+
expect([...inner.querySelectorAll("p")].map(p => p.innerHTML)).toEqual(["Dear <em>you</em>,", "Goodbye."]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("{{#style}} around paragraphs makes a div, with Markdown rendered inside", async () => {
|
|
65
|
+
const element = await start(`{{#style "letter"}}
|
|
66
|
+
Dear *you*,
|
|
67
|
+
|
|
68
|
+
Goodbye.
|
|
69
|
+
{{/style}}`);
|
|
70
|
+
const div = element.querySelector(".squiffy-style")!;
|
|
71
|
+
expect(div.tagName).toBe("DIV");
|
|
72
|
+
expect(div.classList.contains("letter")).toBe(true);
|
|
73
|
+
expect([...div.querySelectorAll("p")].map(p => p.innerHTML)).toEqual(["Dear <em>you</em>,", "Goodbye."]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("{{#style}} escapes the class names, and shows helpers inside it", async () => {
|
|
77
|
+
const element = await start(`@set name = Ann
|
|
78
|
+
{{#style "a\\"b"}}Hi {{name}}{{/style}}`);
|
|
79
|
+
const span = element.querySelector(".squiffy-style")!;
|
|
80
|
+
expect(span.getAttribute("class")).toBe("squiffy-style a\"b");
|
|
81
|
+
expect(span.textContent).toBe("Hi Ann");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("{{#style}} can wrap an {{#every}}", async () => {
|
|
85
|
+
const element = await start("The wheel: {{#style \"wheel\"}}{{#every 100 times=2}}spin{{/every}}{{/style}}");
|
|
86
|
+
expect(element.querySelector(".squiffy-error")).toBeNull();
|
|
87
|
+
const span = element.querySelector(".squiffy-style.wheel")!;
|
|
88
|
+
expect(span.querySelector(".squiffy-every")).not.toBeNull();
|
|
89
|
+
});
|