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.
- 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 +12 -0
- package/dist/every.d.ts +12 -0
- package/dist/every.test.d.ts +1 -0
- package/dist/helpers.test.d.ts +1 -0
- package/dist/numbers.d.ts +3 -0
- package/dist/panels.d.ts +14 -0
- package/dist/panels.test.d.ts +1 -0
- package/dist/paths.d.ts +15 -0
- package/dist/paths.test.d.ts +1 -0
- package/dist/persistent.test.d.ts +1 -0
- package/dist/playerSettings.d.ts +17 -0
- package/dist/preview-host.d.ts +7 -3
- package/dist/preview-host.js +47 -5
- package/dist/preview-host.js.map +2 -2
- package/dist/preview-html.js +1 -1
- package/dist/preview-protocol.d.ts +16 -0
- package/dist/preview.d.ts +1 -0
- package/dist/saveSlots.d.ts +16 -0
- package/dist/saves.test.d.ts +1 -0
- package/dist/savesDialog.d.ts +43 -0
- package/dist/savesDialog.test.d.ts +1 -0
- package/dist/scripts.test.d.ts +1 -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 +155 -0
- package/dist/squiffy.preview.html +1469 -136
- package/dist/squiffy.runtime.css +1123 -20
- package/dist/squiffy.runtime.d.ts +13 -1
- package/dist/squiffy.runtime.global.js +144 -79
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +6771 -4424
- package/dist/squiffy.runtime.js.map +1 -1
- package/dist/state.d.ts +17 -0
- package/dist/storyLists.d.ts +4 -0
- package/dist/storyLists.test.d.ts +1 -0
- package/dist/style.test.d.ts +1 -0
- package/dist/textProcessor.d.ts +27 -0
- package/dist/transitions.test.d.ts +1 -0
- package/dist/types.d.ts +125 -0
- package/dist/types.plugins.d.ts +1 -0
- package/dist/uiLinks.test.d.ts +1 -0
- package/dist/utils.d.ts +2 -1
- package/dist/utils.test.d.ts +1 -0
- package/package.json +11 -6
- package/src/__snapshots__/squiffy.runtime.test.ts.snap +22 -22
- 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 +19 -0
- package/src/every.test.ts +25 -0
- package/src/every.ts +94 -0
- package/src/helpers.test.ts +34 -0
- package/src/numbers.ts +21 -0
- package/src/panels.test.ts +225 -0
- package/src/panels.ts +187 -0
- package/src/paths.test.ts +66 -0
- package/src/paths.ts +155 -0
- package/src/persistent.test.ts +155 -0
- package/src/playerSettings.ts +128 -0
- package/src/pluginManager.ts +1 -0
- package/src/plugins/animate.ts +28 -4
- package/src/plugins/live.ts +18 -5
- package/src/plugins/plugins.test.ts +93 -6
- package/src/plugins/replaceLabel.ts +7 -3
- package/src/preview-host.test.ts +97 -1
- package/src/preview-host.ts +71 -10
- package/src/preview-protocol.ts +25 -3
- package/src/preview.main.ts +1 -0
- package/src/preview.test.ts +203 -4
- package/src/preview.ts +178 -12
- package/src/saveSlots.ts +159 -0
- package/src/saves.test.ts +199 -0
- package/src/savesDialog.test.ts +290 -0
- package/src/savesDialog.ts +499 -0
- package/src/scripts.test.ts +130 -0
- 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 +155 -0
- package/src/squiffy.runtime.css +1123 -20
- package/src/squiffy.runtime.test.ts +2806 -44
- package/src/squiffy.runtime.ts +1111 -64
- package/src/state.ts +172 -11
- package/src/storyLists.test.ts +134 -0
- package/src/storyLists.ts +57 -0
- package/src/style.test.ts +89 -0
- package/src/textProcessor.ts +739 -58
- package/src/transitions.test.ts +343 -0
- package/src/types.plugins.ts +3 -0
- package/src/types.ts +209 -1
- package/src/uiLinks.test.ts +156 -0
- package/src/updater.ts +26 -8
- package/src/utils.test.ts +60 -0
- package/src/utils.ts +46 -14
- package/vite.config.ts +3 -4
- package/vite.preview.config.ts +16 -5
- package/dist/infixExpressions.d.ts +0 -2
- package/src/infixExpressions.test.ts +0 -187
- package/src/infixExpressions.ts +0 -323
- /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,15 +28,119 @@ 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
|
+
|
|
35
|
+
// Persistent attributes (squiffy.json's "persistent", #539): the names the story declares,
|
|
36
|
+
// and their values, which are kept apart from the store. They belong to the player rather than
|
|
37
|
+
// the playthrough, so restarting, going back and loading a saved game leave them as they are.
|
|
38
|
+
persistentNames: () => string[] = () => [];
|
|
39
|
+
persistentValues: Record<string, any> = {};
|
|
40
|
+
// Where they're kept in localStorage, when persist is on
|
|
41
|
+
private persistentKey?: string;
|
|
42
|
+
|
|
43
|
+
// Starts with the values given, or else those kept in localStorage under the key, if any
|
|
44
|
+
initPersistent(key: string | undefined, values?: Record<string, unknown>) {
|
|
45
|
+
this.persistentKey = key;
|
|
46
|
+
if (values) {
|
|
47
|
+
this.persistentValues = structuredClone(values);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (!key) return;
|
|
51
|
+
try {
|
|
52
|
+
const saved = JSON.parse(localStorage.getItem(key) ?? "{}");
|
|
53
|
+
if (saved && typeof saved == "object" && !Array.isArray(saved)) this.persistentValues = saved;
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// Nothing kept, or storage that can't be read
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
isPersistent(attribute: string) {
|
|
61
|
+
return this.persistentNames().includes(attribute);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// The persistent attributes the story has set, by name
|
|
65
|
+
getPersistent(): Record<string, any> {
|
|
66
|
+
const names = this.persistentNames();
|
|
67
|
+
return structuredClone(Object.fromEntries(Object.entries(this.persistentValues).filter(([name]) => names.includes(name))));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Clears the persistent attributes named, or all of them, as {{forget}} does
|
|
71
|
+
forget(names?: string[]) {
|
|
72
|
+
if (this.readOnly) return;
|
|
73
|
+
const forgotten = (names ?? Object.keys(this.persistentValues))
|
|
74
|
+
.filter(name => this.isPersistent(name) && Object.prototype.hasOwnProperty.call(this.persistentValues, name));
|
|
75
|
+
if (!forgotten.length) return;
|
|
76
|
+
for (const name of forgotten) delete this.persistentValues[name];
|
|
77
|
+
this.storePersistent();
|
|
78
|
+
for (const name of forgotten) {
|
|
79
|
+
this.emitter.emit("set", { attribute: name, value: null });
|
|
80
|
+
this.onSet(name, null);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private storePersistent() {
|
|
85
|
+
if (!this.persistentKey) return;
|
|
86
|
+
try {
|
|
87
|
+
localStorage.setItem(this.persistentKey, JSON.stringify(this.persistentValues));
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
// Storage that's full or can't be written: they last until the page is closed
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Where an attribute's value is kept: with the persistent attributes, or in the store
|
|
95
|
+
private valuesFor(attribute: string): Record<string, any> {
|
|
96
|
+
return this.isPersistent(attribute) ? this.persistentValues : this.store;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// While this is true, set() does nothing: e.g. while a panel is rendered, so its {{set}}s
|
|
100
|
+
// don't change the story
|
|
101
|
+
readOnly = false;
|
|
102
|
+
|
|
103
|
+
// The name can be a path to a value inside an object or list, such as hero.hp (#442): see
|
|
104
|
+
// paths.ts
|
|
27
105
|
set(attribute: string, value: any) {
|
|
28
|
-
this.
|
|
106
|
+
this.setAt(parsePath(attribute), value);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Sets the value at a path: the attribute, then the names inside it. The whole attribute is
|
|
110
|
+
// set again, so going back, saves and the "set" event see the attribute that changed.
|
|
111
|
+
setAt(segments: string[], value: any) {
|
|
112
|
+
if (this.readOnly) return;
|
|
113
|
+
const [attribute, ...inside] = segments;
|
|
114
|
+
// Only the player changes their settings, in the Settings dialog
|
|
115
|
+
if (attribute == SETTINGS && this.playerSettings()) {
|
|
116
|
+
console.warn("Squiffy: the story can't change the player's settings.");
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (!inside.length) {
|
|
120
|
+
this.setInternal(attribute, value, true);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (typeof value === "undefined") value = true;
|
|
124
|
+
const root = this.has(attribute) ? this.valuesFor(attribute)[attribute] : null;
|
|
125
|
+
this.setInternal(attribute, withValueAt(root, inside, attribute, value), true);
|
|
29
126
|
}
|
|
30
127
|
|
|
31
128
|
setInternal(attribute: string, value: any, raiseEvents: boolean) {
|
|
32
129
|
if (typeof value === "undefined") value = true;
|
|
33
130
|
|
|
131
|
+
if (this.isPersistent(attribute)) {
|
|
132
|
+
// Going back and loading a saved game set attributes without raising events, and
|
|
133
|
+
// they leave persistent attributes as they are
|
|
134
|
+
if (!raiseEvents) return;
|
|
135
|
+
this.persistentValues[attribute] = structuredClone(value);
|
|
136
|
+
this.storePersistent();
|
|
137
|
+
this.emitter.emit("set", { attribute, value });
|
|
138
|
+
this.onSet(attribute, value);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
34
142
|
if (raiseEvents && this.onSetInternal) {
|
|
35
|
-
this.onSetInternal(attribute, this.
|
|
143
|
+
this.onSetInternal(attribute, this.getAt([attribute]), structuredClone(value));
|
|
36
144
|
}
|
|
37
145
|
|
|
38
146
|
this.store[attribute] = structuredClone(value);
|
|
@@ -48,12 +156,25 @@ export class State {
|
|
|
48
156
|
this.onSet(attribute, value);
|
|
49
157
|
}
|
|
50
158
|
|
|
159
|
+
// Only the store's own properties are attributes, not ones it inherits, such as valueOf
|
|
160
|
+
has(attribute: string) {
|
|
161
|
+
if (attribute == SETTINGS && this.playerSettings()) return true;
|
|
162
|
+
return Object.prototype.hasOwnProperty.call(this.valuesFor(attribute), attribute);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// The name can be a path, as for set()
|
|
51
166
|
get(attribute: string): any {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
167
|
+
return this.getAt(parsePath(attribute));
|
|
168
|
+
}
|
|
55
169
|
|
|
56
|
-
|
|
170
|
+
getAt(segments: string[]): any {
|
|
171
|
+
const [attribute, ...inside] = segments;
|
|
172
|
+
if (!this.has(attribute)) return null;
|
|
173
|
+
const settings = attribute == SETTINGS ? this.playerSettings() : null;
|
|
174
|
+
if (settings) return inside.length ? structuredClone(valueAt(settings, inside)) : { ...settings };
|
|
175
|
+
const values = this.valuesFor(attribute);
|
|
176
|
+
const value = inside.length ? valueAt(values[attribute], inside) : values[attribute];
|
|
177
|
+
return structuredClone(value);
|
|
57
178
|
}
|
|
58
179
|
|
|
59
180
|
getStore() {
|
|
@@ -61,16 +182,24 @@ export class State {
|
|
|
61
182
|
// This allows {{attribute}} in templates to reflect changes made
|
|
62
183
|
// by helpers like {{inc}} during the same render pass.
|
|
63
184
|
return new Proxy({} as Record<string, any>, {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
185
|
+
// A name that isn't an attribute is null, unless it's one every object has, like
|
|
186
|
+
// valueOf, which Handlebars can use
|
|
187
|
+
get: (target, prop) => typeof prop == "string" && (this.has(prop) || !(prop in target))
|
|
188
|
+
? this.getAt([prop])
|
|
189
|
+
: Reflect.get(target, prop),
|
|
190
|
+
has: (_, prop) => this.has(String(prop)),
|
|
191
|
+
ownKeys: () => [...new Set([
|
|
192
|
+
...Object.keys(this.store).filter(key => !this.isPersistent(key)),
|
|
193
|
+
...Object.keys(this.getPersistent()),
|
|
194
|
+
...(this.playerSettings() ? [SETTINGS] : []),
|
|
195
|
+
])],
|
|
67
196
|
getOwnPropertyDescriptor: (_, prop) => {
|
|
68
197
|
const key = String(prop);
|
|
69
|
-
if (
|
|
198
|
+
if (this.has(key)) {
|
|
70
199
|
return {
|
|
71
200
|
enumerable: true,
|
|
72
201
|
configurable: true,
|
|
73
|
-
value: this.
|
|
202
|
+
value: this.getAt([key])
|
|
74
203
|
};
|
|
75
204
|
}
|
|
76
205
|
return undefined;
|
|
@@ -87,11 +216,13 @@ export class State {
|
|
|
87
216
|
for (const key of keys) {
|
|
88
217
|
if (key.startsWith(this.storyId + "-")) {
|
|
89
218
|
const attribute = key.substring(this.storyId.length + 1);
|
|
219
|
+
if (this.isPersistent(attribute)) continue;
|
|
90
220
|
this.store[attribute] = JSON.parse(localStorage[key]);
|
|
91
221
|
}
|
|
92
222
|
}
|
|
93
223
|
}
|
|
94
224
|
|
|
225
|
+
// Persistent attributes are left as they are: forget() clears them
|
|
95
226
|
reset() {
|
|
96
227
|
this.store = {};
|
|
97
228
|
|
|
@@ -107,9 +238,19 @@ export class State {
|
|
|
107
238
|
}
|
|
108
239
|
}
|
|
109
240
|
|
|
241
|
+
// Records a visit to a section or passage: adds it to _seen_sections, counts it in _visits
|
|
242
|
+
// (#377), and keeps the turn it was on in _lastvisit (#540). They're all attributes, so going
|
|
243
|
+
// back undoes them.
|
|
110
244
|
setSeen(sectionName: string) {
|
|
111
245
|
let seenSections = this.get("_seen_sections");
|
|
112
246
|
if (!seenSections) seenSections = [];
|
|
247
|
+
const visits = this.get("_visits") || {};
|
|
248
|
+
// A save from before _visits has the name in _seen_sections with no count: that's one visit
|
|
249
|
+
visits[sectionName] = this.getVisitsFrom(visits, seenSections, sectionName) + 1;
|
|
250
|
+
this.set("_visits", visits);
|
|
251
|
+
const lastVisit = this.get("_lastvisit") || {};
|
|
252
|
+
lastVisit[sectionName] = this.get("turns") || 0;
|
|
253
|
+
this.set("_lastvisit", lastVisit);
|
|
113
254
|
if (seenSections.indexOf(sectionName) == -1) {
|
|
114
255
|
seenSections.push(sectionName);
|
|
115
256
|
this.set("_seen_sections", seenSections);
|
|
@@ -121,4 +262,24 @@ export class State {
|
|
|
121
262
|
if (!seenSections) return false;
|
|
122
263
|
return (seenSections.indexOf(sectionName) > -1);
|
|
123
264
|
}
|
|
265
|
+
|
|
266
|
+
// How many times a section or passage has been visited, or 0 if it hasn't
|
|
267
|
+
getVisits(sectionName: string): number {
|
|
268
|
+
return this.getVisitsFrom(this.get("_visits") || {}, this.get("_seen_sections") || [], sectionName);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// How many turns ago a section or passage was last visited: 0 if the player is there now, and
|
|
272
|
+
// -1 if they've never been there (#540)
|
|
273
|
+
getLastVisited(sectionName: string): number {
|
|
274
|
+
const lastVisit = this.get("_lastvisit") || {};
|
|
275
|
+
const turns = this.get("turns") || 0;
|
|
276
|
+
if (Object.prototype.hasOwnProperty.call(lastVisit, sectionName)) return turns - lastVisit[sectionName];
|
|
277
|
+
// A save from before _lastvisit only says whether it's been visited: as if it was at the start
|
|
278
|
+
return this.getSeen(sectionName) ? turns : -1;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
private getVisitsFrom(visits: Record<string, number>, seenSections: string[], sectionName: string): number {
|
|
282
|
+
if (Object.prototype.hasOwnProperty.call(visits, sectionName)) return visits[sectionName];
|
|
283
|
+
return seenSections.indexOf(sectionName) > -1 ? 1 : 0;
|
|
284
|
+
}
|
|
124
285
|
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// Listing a story's sections and passages, and their @tag tags (#527): the passages, sections
|
|
2
|
+
// and tags helpers, and squiffy.story.passages(), sections() and tags() in JavaScript
|
|
3
|
+
import { expect, test, beforeEach, afterEach, vi } from "vitest";
|
|
4
|
+
import globalJsdom from "global-jsdom";
|
|
5
|
+
import { init } from "./squiffy.runtime.js";
|
|
6
|
+
import { compile as squiffyCompile } from "squiffy-compiler";
|
|
7
|
+
|
|
8
|
+
let cleanup: () => void;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
cleanup = globalJsdom("<!DOCTYPE html><html><body><div id=\"squiffy\"></div></body></html>");
|
|
12
|
+
vi.useFakeTimers();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
vi.useRealTimers();
|
|
17
|
+
cleanup();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const start = async (script: string) => {
|
|
21
|
+
const result = await squiffyCompile({ script });
|
|
22
|
+
if (!result.success) throw new Error("Compile failed: " + JSON.stringify(result.errors));
|
|
23
|
+
const story = {
|
|
24
|
+
...result.output.story,
|
|
25
|
+
js: result.output.js.map(lines => new Function("squiffy", "get", "set", lines.join("\n"))) as any,
|
|
26
|
+
};
|
|
27
|
+
const element = document.getElementById("squiffy")!;
|
|
28
|
+
const api = await init({ element, story, persist: false, storyId: "test", scroll: "none" });
|
|
29
|
+
await api.begin();
|
|
30
|
+
await vi.runAllTimersAsync();
|
|
31
|
+
return { api, element };
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const text = (element: HTMLElement) => element.textContent!.replace(/\s+/g, " ").trim();
|
|
35
|
+
|
|
36
|
+
// The story the passengers are in, from Moquette, where the issue came from
|
|
37
|
+
const passengers = `
|
|
38
|
+
[[]]:
|
|
39
|
+
|
|
40
|
+
[a man (Kurt Cobain)]:
|
|
41
|
+
@tag passenger
|
|
42
|
+
A man.
|
|
43
|
+
|
|
44
|
+
[a woman and child]:
|
|
45
|
+
@tag passenger settled
|
|
46
|
+
A woman and child.
|
|
47
|
+
|
|
48
|
+
[a busker]:
|
|
49
|
+
@tag passenger
|
|
50
|
+
A busker.
|
|
51
|
+
|
|
52
|
+
[@last]:
|
|
53
|
+
The end of the line.
|
|
54
|
+
|
|
55
|
+
[[carriage]]:
|
|
56
|
+
@tag train
|
|
57
|
+
CONTENT
|
|
58
|
+
|
|
59
|
+
[a newspaper]:
|
|
60
|
+
A newspaper.
|
|
61
|
+
|
|
62
|
+
[]:
|
|
63
|
+
Every passage.
|
|
64
|
+
|
|
65
|
+
[@1]:
|
|
66
|
+
Once.
|
|
67
|
+
|
|
68
|
+
[[platform]]:
|
|
69
|
+
@tag station underground
|
|
70
|
+
The platform.
|
|
71
|
+
`;
|
|
72
|
+
|
|
73
|
+
const story = (content: string) => passengers.replace("CONTENT", content);
|
|
74
|
+
|
|
75
|
+
test("passages lists the current section's passages and the master section's, without the special ones", async () => {
|
|
76
|
+
const { element } = await start(story("{{join (passages) \" / \"}}"));
|
|
77
|
+
expect(text(element)).toBe("a newspaper / a man (Kurt Cobain) / a woman and child / a busker");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("passages lists a section's passages, with \"\" for the master section", async () => {
|
|
81
|
+
const { element } = await start(story("{{join (passages \"carriage\")}} | {{join (passages \"\")}} | {{length (passages \"platform\")}}"));
|
|
82
|
+
expect(text(element)).toBe("a newspaper | a man (Kurt Cobain), a woman and child, a busker | 0");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("passages tagged= and untagged= choose by tag", async () => {
|
|
86
|
+
const { element } = await start(story("{{join (passages tagged=\"passenger\" untagged=\"settled\")}}. {{join (passages tagged=\"passenger settled\")}}."));
|
|
87
|
+
expect(text(element)).toBe("a man (Kurt Cobain), a busker. a woman and child.");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("passages gives a list to choose a passage from, which links to it", async () => {
|
|
91
|
+
const { element } = await start(story("{{#each (passages tagged=\"passenger\")}}{{passage this}} {{/each}}"));
|
|
92
|
+
const link = [...element.querySelectorAll<HTMLElement>("a.squiffy-link")].find(l => l.textContent == "a busker")!;
|
|
93
|
+
link.click();
|
|
94
|
+
await vi.runAllTimersAsync();
|
|
95
|
+
expect(text(element)).toContain("A busker.");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("random chooses from the list", async () => {
|
|
99
|
+
const { element } = await start(story("{{random (passages tagged=\"passenger\" untagged=\"settled\")}}"));
|
|
100
|
+
expect(["a man (Kurt Cobain)", "a busker"]).toContain(text(element));
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("sections lists the story's sections, apart from the master section", async () => {
|
|
104
|
+
const { element } = await start(story("{{join (sections)}}. {{join (sections tagged=\"station\")}}. {{join (sections untagged=\"train\")}}."));
|
|
105
|
+
expect(text(element)).toBe("carriage, platform. platform. platform.");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("tags gives a passage's or section's tags, or the current section's", async () => {
|
|
109
|
+
const { element } = await start(story("{{join (tags)}}. {{join (tags \"a woman and child\")}}. {{join (tags \"platform\")}}. {{#if (contains (tags \"a busker\") \"settled\")}}settled{{else}}not settled{{/if}}. {{#if (tags \"a woman and child\") contains \"settled\"}}settled{{/if}}."));
|
|
110
|
+
expect(text(element)).toBe("train. passenger, settled. station, underground. not settled. settled.");
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("an attribute with the same name is still shown", async () => {
|
|
114
|
+
const { element } = await start(story("@set tags = mine\n{{tags}} {{join (tags \"platform\")}}"));
|
|
115
|
+
expect(text(element)).toBe("mine station, underground");
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("a section that doesn't exist is an error", async () => {
|
|
119
|
+
const { element } = await start(story("{{passages \"nowhere\"}}"));
|
|
120
|
+
expect(text(element)).toContain("There's no section called \"nowhere\"");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("squiffy.story.passages, sections and tags in JavaScript", async () => {
|
|
124
|
+
const { element } = await start(story(` var s = squiffy.story;
|
|
125
|
+
set("result", [
|
|
126
|
+
s.passages().length,
|
|
127
|
+
s.passages("", { tagged: "passenger", untagged: ["settled"] }).join("/"),
|
|
128
|
+
s.sections({ tagged: "underground" }).join("/"),
|
|
129
|
+
s.tags().join("/"),
|
|
130
|
+
s.tags("a woman and child").join("/"),
|
|
131
|
+
].join(" | "));
|
|
132
|
+
{{result}}`));
|
|
133
|
+
expect(text(element)).toBe("4 | a man (Kurt Cobain)/a busker | platform | train | passenger/settled");
|
|
134
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Lists of a story's sections and passages, and their @tag tags (#527), for the passages,
|
|
2
|
+
// sections and tags helpers and squiffy.story in JavaScript. This is how a story chooses among
|
|
3
|
+
// its passages without listing their names a second time.
|
|
4
|
+
import type { Passage, Section, Story, TagFilter } from "./types.js";
|
|
5
|
+
|
|
6
|
+
const tagList = (tags: string | string[] | undefined): string[] =>
|
|
7
|
+
tags === undefined ? [] : (Array.isArray(tags) ? tags : String(tags).split(/\s+/)).map(String).filter(t => t);
|
|
8
|
+
|
|
9
|
+
function matches(item: Section | Passage, filter: TagFilter) {
|
|
10
|
+
const tags = item.tags ?? [];
|
|
11
|
+
return tagList(filter.tagged).every(t => tags.includes(t))
|
|
12
|
+
&& !tagList(filter.untagged).some(t => tags.includes(t));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// The master passage, and passages such as @1, @last and @after, run by themselves, so they
|
|
16
|
+
// aren't listed
|
|
17
|
+
const isSpecial = (name: string) => name === "" || name.startsWith("@");
|
|
18
|
+
|
|
19
|
+
function sectionNamed(story: Story, name: string): Section {
|
|
20
|
+
const section = story.sections[name];
|
|
21
|
+
if (!section) throw new Error(`There's no section called "${name}"`);
|
|
22
|
+
return section;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// The passages of the named section ("" for the master section), in the order they're written,
|
|
26
|
+
// apart from the special ones.
|
|
27
|
+
// With no section, the current section's passages, then the master section's, which links in
|
|
28
|
+
// the current section can also reach.
|
|
29
|
+
export function listPassages(story: Story, sectionName: string | undefined, currentSection: string, filter: TagFilter = {}): string[] {
|
|
30
|
+
const sections = sectionName === undefined
|
|
31
|
+
? [story.sections[currentSection], story.sections[""]]
|
|
32
|
+
: [sectionNamed(story, sectionName)];
|
|
33
|
+
const names: string[] = [];
|
|
34
|
+
for (const section of sections) {
|
|
35
|
+
for (const [name, passage] of Object.entries(section?.passages ?? {})) {
|
|
36
|
+
if (!isSpecial(name) && !names.includes(name) && matches(passage, filter)) names.push(name);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return names;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// The story's sections, in the order they're written, apart from the master section
|
|
43
|
+
export function listSections(story: Story, filter: TagFilter = {}): string[] {
|
|
44
|
+
return Object.entries(story.sections)
|
|
45
|
+
.filter(([name, section]) => name !== "" && matches(section, filter))
|
|
46
|
+
.map(([name]) => name);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// A passage's tags, or a section's: a passage in the current section, then one in the master
|
|
50
|
+
// section, then a section, as a passage link would find it
|
|
51
|
+
export function tagsOf(story: Story, name: string, currentSection: string): string[] {
|
|
52
|
+
const item = story.sections[currentSection]?.passages?.[name]
|
|
53
|
+
?? story.sections[""]?.passages?.[name]
|
|
54
|
+
?? story.sections[name];
|
|
55
|
+
if (!item) throw new Error(`There's no section or passage called "${name}"`);
|
|
56
|
+
return [...(item.tags ?? [])];
|
|
57
|
+
}
|
|
@@ -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
|
+
});
|