squiffy-runtime 6.0.0-alpha.10 → 6.0.0-alpha.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/src/types.ts ADDED
@@ -0,0 +1,78 @@
1
+ import {SquiffyEventHandler, SquiffyEventMap} from "./events.js";
2
+
3
+ export interface SquiffyInitOptions {
4
+ element: HTMLElement;
5
+ story: Story;
6
+ scroll?: string,
7
+ persist?: boolean,
8
+ onSet?: (attribute: string, value: any) => void,
9
+ }
10
+
11
+ export interface SquiffySettings {
12
+ scroll: string,
13
+ persist: boolean,
14
+ onSet: (attribute: string, value: any) => void,
15
+ }
16
+
17
+ export interface SquiffyApi {
18
+ restart: () => void;
19
+ get: (attribute: string) => any;
20
+ set: (attribute: string, value: any) => void;
21
+ clickLink: (link: HTMLElement) => boolean;
22
+ update: (story: Story) => void;
23
+
24
+ on<E extends keyof SquiffyEventMap>(
25
+ event: E,
26
+ handler: SquiffyEventHandler<E>
27
+ ): () => void; // returns unsubscribe
28
+
29
+ off<E extends keyof SquiffyEventMap>(
30
+ event: E,
31
+ handler: SquiffyEventHandler<E>
32
+ ): void;
33
+
34
+ once<E extends keyof SquiffyEventMap>(
35
+ event: E,
36
+ handler: SquiffyEventHandler<E>
37
+ ): () => void;
38
+ }
39
+
40
+ // Previous versions of Squiffy had "squiffy", "get" and "set" as globals - we now pass these directly into JS functions.
41
+ // We may tidy up this API at some point, though that would be a breaking change.
42
+ interface SquiffyJsFunctionApi {
43
+ get: (attribute: string) => any;
44
+ set: (attribute: string, value: any) => void;
45
+ ui: {
46
+ transition: (f: any) => void;
47
+ };
48
+ story: {
49
+ go: (section: string) => void;
50
+ };
51
+ }
52
+
53
+ export interface Story {
54
+ js: ((
55
+ squiffy: SquiffyJsFunctionApi,
56
+ get: (attribute: string) => any,
57
+ set: (attribute: string, value: any) => void
58
+ ) => void)[];
59
+ start: string;
60
+ id?: string | null;
61
+ sections: Record<string, Section>;
62
+ }
63
+
64
+ export interface Section {
65
+ text?: string;
66
+ clear?: boolean;
67
+ attributes?: string[],
68
+ jsIndex?: number;
69
+ passages?: Record<string, Passage>;
70
+ passageCount?: number;
71
+ }
72
+
73
+ export interface Passage {
74
+ text?: string;
75
+ clear?: boolean;
76
+ attributes?: string[];
77
+ jsIndex?: number;
78
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,14 @@
1
+ export function startsWith(string: string, prefix: string) {
2
+ return string.substring(0, prefix.length) === prefix;
3
+ }
4
+
5
+ export function rotate(options: string, current: string | null) {
6
+ const colon = options.indexOf(':');
7
+ if (colon == -1) {
8
+ return [options, current];
9
+ }
10
+ const next = options.substring(0, colon);
11
+ let remaining = options.substring(colon + 1);
12
+ if (current) remaining += ':' + current;
13
+ return [next, remaining];
14
+ }