squiffy-runtime 6.0.0-alpha.15 → 6.0.0-alpha.17
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/animation.d.ts +11 -0
- package/dist/events.d.ts +8 -3
- package/dist/import.d.ts +4 -0
- package/dist/linkHandler.d.ts +8 -0
- package/dist/pluginManager.d.ts +23 -0
- package/dist/squiffy.runtime.d.ts +2 -2
- package/dist/squiffy.runtime.global.js +126 -0
- package/dist/squiffy.runtime.global.js.map +1 -0
- package/dist/squiffy.runtime.js +8785 -487
- package/dist/squiffy.runtime.js.map +1 -0
- package/dist/state.d.ts +19 -0
- package/dist/textProcessor.d.ts +8 -13
- package/dist/types.d.ts +5 -2
- package/dist/types.plugins.d.ts +27 -0
- package/dist/updater.d.ts +2 -0
- package/dist/utils.d.ts +1 -2
- package/package.json +12 -5
- package/src/__snapshots__/squiffy.runtime.test.ts.snap +53 -19
- package/src/animation.ts +68 -0
- package/src/events.ts +9 -10
- package/src/import.ts +5 -0
- package/src/linkHandler.ts +18 -0
- package/src/pluginManager.ts +74 -0
- package/src/plugins/animate.ts +97 -0
- package/src/plugins/index.ts +13 -0
- package/src/plugins/live.ts +83 -0
- package/src/plugins/random.ts +22 -0
- package/src/plugins/replaceLabel.ts +22 -0
- package/src/plugins/rotateSequence.ts +61 -0
- package/src/squiffy.runtime.test.ts +306 -134
- package/src/squiffy.runtime.ts +460 -332
- package/src/state.ts +106 -0
- package/src/textProcessor.ts +61 -164
- package/src/types.plugins.ts +41 -0
- package/src/types.ts +5 -2
- package/src/updater.ts +77 -0
- package/src/utils.ts +15 -12
- package/vite.config.ts +36 -0
- package/vitest.config.ts +9 -0
- package/vitest.setup.ts +16 -0
- package/dist/events.js +0 -35
- package/dist/squiffy.runtime.test.js +0 -394
- package/dist/textProcessor.js +0 -166
- package/dist/types.js +0 -1
- package/dist/utils.js +0 -14
|
@@ -1,394 +0,0 @@
|
|
|
1
|
-
import { expect, test, beforeEach, afterEach, vi } from 'vitest';
|
|
2
|
-
import fs from 'fs/promises';
|
|
3
|
-
import globalJsdom from 'global-jsdom';
|
|
4
|
-
import { init } from './squiffy.runtime.js';
|
|
5
|
-
import { compile as squiffyCompile } from 'squiffy-compiler';
|
|
6
|
-
const html = `
|
|
7
|
-
<!DOCTYPE html>
|
|
8
|
-
<html>
|
|
9
|
-
<head>
|
|
10
|
-
</head>
|
|
11
|
-
<body>
|
|
12
|
-
<div id="squiffy">
|
|
13
|
-
</div>
|
|
14
|
-
<div id="test">
|
|
15
|
-
</div>
|
|
16
|
-
</body>
|
|
17
|
-
</html>
|
|
18
|
-
`;
|
|
19
|
-
const compile = async (script) => {
|
|
20
|
-
const compileResult = await squiffyCompile({
|
|
21
|
-
script: script,
|
|
22
|
-
});
|
|
23
|
-
if (!compileResult.success) {
|
|
24
|
-
throw new Error('Compile failed');
|
|
25
|
-
}
|
|
26
|
-
const story = compileResult.output.story;
|
|
27
|
-
const js = compileResult.output.js.map(jsLines => new Function('squiffy', 'get', 'set', jsLines.join('\n')));
|
|
28
|
-
return {
|
|
29
|
-
story: {
|
|
30
|
-
js: js,
|
|
31
|
-
...story,
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
};
|
|
35
|
-
const initScript = async (script) => {
|
|
36
|
-
const element = document.getElementById('squiffy');
|
|
37
|
-
if (!element) {
|
|
38
|
-
throw new Error('Element not found');
|
|
39
|
-
}
|
|
40
|
-
const compileResult = await compile(script);
|
|
41
|
-
const squiffyApi = init({
|
|
42
|
-
element: element,
|
|
43
|
-
story: compileResult.story,
|
|
44
|
-
});
|
|
45
|
-
return {
|
|
46
|
-
squiffyApi,
|
|
47
|
-
element
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
const findLink = (element, linkType, linkText, onlyEnabled = false) => {
|
|
51
|
-
const links = onlyEnabled
|
|
52
|
-
? element.querySelectorAll(`.squiffy-output-section:last-child a.squiffy-link.link-${linkType}`)
|
|
53
|
-
: element.querySelectorAll(`a.squiffy-link.link-${linkType}`);
|
|
54
|
-
return Array.from(links).find(link => link.textContent === linkText && (onlyEnabled ? !link.classList.contains("disabled") : true));
|
|
55
|
-
};
|
|
56
|
-
const getTestOutput = () => {
|
|
57
|
-
const testElement = document.getElementById('test');
|
|
58
|
-
if (!testElement) {
|
|
59
|
-
throw new Error('Test element not found');
|
|
60
|
-
}
|
|
61
|
-
return testElement.innerText;
|
|
62
|
-
};
|
|
63
|
-
let cleanup;
|
|
64
|
-
beforeEach(() => {
|
|
65
|
-
cleanup = globalJsdom(html);
|
|
66
|
-
});
|
|
67
|
-
afterEach(() => {
|
|
68
|
-
cleanup();
|
|
69
|
-
});
|
|
70
|
-
test('"Hello world" script should run', async () => {
|
|
71
|
-
const { element } = await initScript("Hello world");
|
|
72
|
-
expect(element.innerHTML).toMatchSnapshot();
|
|
73
|
-
});
|
|
74
|
-
test('Click a section link', async () => {
|
|
75
|
-
const script = await fs.readFile('../examples/test/example.squiffy', 'utf-8');
|
|
76
|
-
const { squiffyApi, element } = await initScript(script);
|
|
77
|
-
expect(element.innerHTML).toMatchSnapshot();
|
|
78
|
-
expect(element.querySelectorAll('a.squiffy-link').length).toBe(10);
|
|
79
|
-
const linkToPassage = findLink(element, 'passage', 'a link to a passage');
|
|
80
|
-
const section3Link = findLink(element, 'section', 'section 3');
|
|
81
|
-
expect(linkToPassage).not.toBeNull();
|
|
82
|
-
expect(section3Link).not.toBeNull();
|
|
83
|
-
expect(linkToPassage.classList).not.toContain('disabled');
|
|
84
|
-
const handler = vi.fn();
|
|
85
|
-
const off = squiffyApi.on('linkClick', handler);
|
|
86
|
-
const handled = squiffyApi.clickLink(section3Link);
|
|
87
|
-
expect(handled).toBe(true);
|
|
88
|
-
expect(element.innerHTML).toMatchSnapshot();
|
|
89
|
-
// passage link is from the previous section, so should be unclickable
|
|
90
|
-
expect(squiffyApi.clickLink(linkToPassage)).toBe(false);
|
|
91
|
-
// await for the event to be processed
|
|
92
|
-
await Promise.resolve();
|
|
93
|
-
expect(handler).toHaveBeenCalledTimes(1);
|
|
94
|
-
expect(handler).toHaveBeenCalledWith(expect.objectContaining({ linkType: 'section' }));
|
|
95
|
-
off();
|
|
96
|
-
});
|
|
97
|
-
test('Click a passage link', async () => {
|
|
98
|
-
const script = await fs.readFile('../examples/test/example.squiffy', 'utf-8');
|
|
99
|
-
const { squiffyApi, element } = await initScript(script);
|
|
100
|
-
expect(element.innerHTML).toMatchSnapshot();
|
|
101
|
-
expect(element.querySelectorAll('a.squiffy-link').length).toBe(10);
|
|
102
|
-
const linkToPassage = findLink(element, 'passage', 'a link to a passage');
|
|
103
|
-
const section3Link = findLink(element, 'section', 'section 3');
|
|
104
|
-
expect(linkToPassage).not.toBeNull();
|
|
105
|
-
expect(section3Link).not.toBeNull();
|
|
106
|
-
expect(linkToPassage.classList).not.toContain('disabled');
|
|
107
|
-
const handler = vi.fn();
|
|
108
|
-
const off = squiffyApi.on('linkClick', handler);
|
|
109
|
-
const handled = squiffyApi.clickLink(linkToPassage);
|
|
110
|
-
expect(handled).toBe(true);
|
|
111
|
-
expect(linkToPassage.classList).toContain('disabled');
|
|
112
|
-
expect(element.innerHTML).toMatchSnapshot();
|
|
113
|
-
// shouldn't be able to click it again
|
|
114
|
-
expect(squiffyApi.clickLink(linkToPassage)).toBe(false);
|
|
115
|
-
// await for the event to be processed
|
|
116
|
-
await Promise.resolve();
|
|
117
|
-
expect(handler).toHaveBeenCalledTimes(1);
|
|
118
|
-
expect(handler).toHaveBeenCalledWith(expect.objectContaining({ linkType: 'passage' }));
|
|
119
|
-
off();
|
|
120
|
-
});
|
|
121
|
-
test('Run JavaScript functions', async () => {
|
|
122
|
-
const script = `
|
|
123
|
-
document.getElementById('test').innerText = 'Initial JavaScript';
|
|
124
|
-
@set some_string = some_value
|
|
125
|
-
@set some_number = 5
|
|
126
|
-
|
|
127
|
-
+++Continue...
|
|
128
|
-
document.getElementById('test').innerText = "Value: " + get("some_number");
|
|
129
|
-
+++Continue...
|
|
130
|
-
document.getElementById('test').innerText = "Value: " + get("some_string");
|
|
131
|
-
set("some_number", 10);
|
|
132
|
-
+++Continue...
|
|
133
|
-
document.getElementById('test').innerText = "Value: " + get("some_number");
|
|
134
|
-
+++Continue...
|
|
135
|
-
@inc some_number
|
|
136
|
-
+++Continue...
|
|
137
|
-
document.getElementById('test').innerText = "Value: " + get("some_number");
|
|
138
|
-
+++Continue...
|
|
139
|
-
squiffy.story.go("other section");
|
|
140
|
-
[[other section]]:
|
|
141
|
-
document.getElementById('test').innerText = "In other section";
|
|
142
|
-
`;
|
|
143
|
-
const clickContinue = () => {
|
|
144
|
-
const continueLink = findLink(element, 'section', 'Continue...', true);
|
|
145
|
-
expect(continueLink).not.toBeNull();
|
|
146
|
-
const handled = squiffyApi.clickLink(continueLink);
|
|
147
|
-
expect(handled).toBe(true);
|
|
148
|
-
};
|
|
149
|
-
const { squiffyApi, element } = await initScript(script);
|
|
150
|
-
expect(getTestOutput()).toBe('Initial JavaScript');
|
|
151
|
-
clickContinue();
|
|
152
|
-
expect(getTestOutput()).toBe('Value: 5');
|
|
153
|
-
clickContinue();
|
|
154
|
-
expect(getTestOutput()).toBe('Value: some_value');
|
|
155
|
-
clickContinue();
|
|
156
|
-
expect(getTestOutput()).toBe('Value: 10');
|
|
157
|
-
clickContinue();
|
|
158
|
-
clickContinue();
|
|
159
|
-
expect(getTestOutput()).toBe('Value: 11');
|
|
160
|
-
clickContinue();
|
|
161
|
-
expect(getTestOutput()).toBe('In other section');
|
|
162
|
-
});
|
|
163
|
-
function safeQuerySelector(name) {
|
|
164
|
-
return name.replace(/'/g, "\\'");
|
|
165
|
-
}
|
|
166
|
-
function getSectionContent(element, section) {
|
|
167
|
-
return element.querySelector(`[data-source='[[${safeQuerySelector(section)}]]']`)?.textContent || null;
|
|
168
|
-
}
|
|
169
|
-
function getPassageContent(element, section, passage) {
|
|
170
|
-
return element.querySelector(`[data-source='[[${safeQuerySelector(section)}]][${safeQuerySelector(passage)}]']`)?.textContent || null;
|
|
171
|
-
}
|
|
172
|
-
test('Update default section output', async () => {
|
|
173
|
-
const { squiffyApi, element } = await initScript("Hello world");
|
|
174
|
-
let defaultOutput = getSectionContent(element, '_default');
|
|
175
|
-
expect(defaultOutput).toBe('Hello world');
|
|
176
|
-
expect(element.innerHTML).toMatchSnapshot();
|
|
177
|
-
const updated = await compile("Updated content");
|
|
178
|
-
squiffyApi.update(updated.story);
|
|
179
|
-
defaultOutput = getSectionContent(element, '_default');
|
|
180
|
-
expect(defaultOutput).toBe('Updated content');
|
|
181
|
-
expect(element.innerHTML).toMatchSnapshot();
|
|
182
|
-
});
|
|
183
|
-
test.each(['a', 'a\'1'])('Update passage output - passage name "%s"', async (name) => {
|
|
184
|
-
const { squiffyApi, element } = await initScript(`Click this: [${name}]
|
|
185
|
-
|
|
186
|
-
[${name}]:
|
|
187
|
-
Passage a content`);
|
|
188
|
-
const link = findLink(element, 'passage', name);
|
|
189
|
-
const handled = squiffyApi.clickLink(link);
|
|
190
|
-
expect(handled).toBe(true);
|
|
191
|
-
let defaultOutput = getSectionContent(element, '_default');
|
|
192
|
-
expect(defaultOutput).toBe(`Click this: ${name}`);
|
|
193
|
-
let passageOutput = getPassageContent(element, '_default', name);
|
|
194
|
-
expect(passageOutput).toBe('Passage a content');
|
|
195
|
-
expect(element.innerHTML).toMatchSnapshot();
|
|
196
|
-
const updated = await compile(`Click this: [${name}]
|
|
197
|
-
|
|
198
|
-
[${name}]:
|
|
199
|
-
Updated passage content`);
|
|
200
|
-
squiffyApi.update(updated.story);
|
|
201
|
-
defaultOutput = getSectionContent(element, '_default');
|
|
202
|
-
expect(defaultOutput).toBe(`Click this: ${name}`);
|
|
203
|
-
passageOutput = getPassageContent(element, '_default', name);
|
|
204
|
-
expect(passageOutput).toBe('Updated passage content');
|
|
205
|
-
expect(element.innerHTML).toMatchSnapshot();
|
|
206
|
-
});
|
|
207
|
-
test('Delete section', async () => {
|
|
208
|
-
const { squiffyApi, element } = await initScript(`Click this: [[a]]
|
|
209
|
-
|
|
210
|
-
[[a]]:
|
|
211
|
-
New section`);
|
|
212
|
-
const link = findLink(element, 'section', 'a');
|
|
213
|
-
const handled = squiffyApi.clickLink(link);
|
|
214
|
-
expect(handled).toBe(true);
|
|
215
|
-
let defaultOutput = getSectionContent(element, '_default');
|
|
216
|
-
expect(defaultOutput).toBe('Click this: a');
|
|
217
|
-
let sectionOutput = getSectionContent(element, 'a');
|
|
218
|
-
expect(sectionOutput).toBe('New section');
|
|
219
|
-
expect(element.innerHTML).toMatchSnapshot();
|
|
220
|
-
const updated = await compile(`Click this: [[a]]`);
|
|
221
|
-
squiffyApi.update(updated.story);
|
|
222
|
-
defaultOutput = getSectionContent(element, '_default');
|
|
223
|
-
expect(defaultOutput).toBe('Click this: a');
|
|
224
|
-
sectionOutput = getSectionContent(element, 'a');
|
|
225
|
-
expect(sectionOutput).toBeNull();
|
|
226
|
-
expect(element.innerHTML).toMatchSnapshot();
|
|
227
|
-
});
|
|
228
|
-
test('Delete passage', async () => {
|
|
229
|
-
const { squiffyApi, element } = await initScript(`Click this: [a]
|
|
230
|
-
|
|
231
|
-
[a]:
|
|
232
|
-
New passage`);
|
|
233
|
-
const link = findLink(element, 'passage', 'a');
|
|
234
|
-
const handled = squiffyApi.clickLink(link);
|
|
235
|
-
expect(handled).toBe(true);
|
|
236
|
-
let defaultOutput = getSectionContent(element, '_default');
|
|
237
|
-
expect(defaultOutput).toBe('Click this: a');
|
|
238
|
-
let passageOutput = getPassageContent(element, '_default', 'a');
|
|
239
|
-
expect(passageOutput).toBe('New passage');
|
|
240
|
-
expect(element.innerHTML).toMatchSnapshot();
|
|
241
|
-
const updated = await compile(`Click this: [a]`);
|
|
242
|
-
squiffyApi.update(updated.story);
|
|
243
|
-
defaultOutput = getSectionContent(element, '_default');
|
|
244
|
-
expect(defaultOutput).toBe('Click this: a');
|
|
245
|
-
passageOutput = getPassageContent(element, '_default', 'a');
|
|
246
|
-
expect(passageOutput).toBeNull();
|
|
247
|
-
expect(element.innerHTML).toMatchSnapshot();
|
|
248
|
-
});
|
|
249
|
-
test('Clicked passage links remain disabled after an update', async () => {
|
|
250
|
-
const { squiffyApi, element } = await initScript(`Click one of these: [a] [b]
|
|
251
|
-
|
|
252
|
-
[a]:
|
|
253
|
-
Output for passage A.
|
|
254
|
-
|
|
255
|
-
[b]:
|
|
256
|
-
Output for passage B.`);
|
|
257
|
-
// click linkA
|
|
258
|
-
let linkA = findLink(element, 'passage', 'a');
|
|
259
|
-
expect(linkA.classList).not.toContain('disabled');
|
|
260
|
-
expect(squiffyApi.clickLink(linkA)).toBe(true);
|
|
261
|
-
const updated = await compile(`Click one of these (updated): [a] [b]
|
|
262
|
-
|
|
263
|
-
[a]:
|
|
264
|
-
Output for passage A.
|
|
265
|
-
|
|
266
|
-
[b]:
|
|
267
|
-
Output for passage B.`);
|
|
268
|
-
squiffyApi.update(updated.story);
|
|
269
|
-
// linkA should still be disabled
|
|
270
|
-
linkA = findLink(element, 'passage', 'a');
|
|
271
|
-
expect(linkA.classList).toContain('disabled');
|
|
272
|
-
expect(squiffyApi.clickLink(linkA)).toBe(false);
|
|
273
|
-
// linkB should still be enabled
|
|
274
|
-
let linkB = findLink(element, 'passage', 'b');
|
|
275
|
-
expect(linkB.classList).not.toContain('disabled');
|
|
276
|
-
expect(squiffyApi.clickLink(linkB)).toBe(true);
|
|
277
|
-
});
|
|
278
|
-
test('Deleting the current section activates the previous section', async () => {
|
|
279
|
-
const { squiffyApi, element } = await initScript(`Choose a section: [[a]] [[b]], or passage [start1].
|
|
280
|
-
|
|
281
|
-
[start1]:
|
|
282
|
-
Output for passage start1.
|
|
283
|
-
|
|
284
|
-
[[a]]:
|
|
285
|
-
Output for section A.
|
|
286
|
-
|
|
287
|
-
[[b]]:
|
|
288
|
-
Output for section B.`);
|
|
289
|
-
// click linkA
|
|
290
|
-
let linkA = findLink(element, 'section', 'a');
|
|
291
|
-
let linkB = findLink(element, 'section', 'b');
|
|
292
|
-
expect(linkA.classList).not.toContain('disabled');
|
|
293
|
-
expect(squiffyApi.clickLink(linkA)).toBe(true);
|
|
294
|
-
// can't click start1 passage as we're in section [[a]] now
|
|
295
|
-
let linkStart1 = findLink(element, 'passage', 'start1');
|
|
296
|
-
expect(squiffyApi.clickLink(linkStart1)).toBe(false);
|
|
297
|
-
// can't click linkB as we're in section [[a]] now
|
|
298
|
-
expect(squiffyApi.clickLink(linkB)).toBe(false);
|
|
299
|
-
// now we delete section [[a]]
|
|
300
|
-
const updated = await compile(`Choose a section: [[a]] [[b]], or passage [start1].
|
|
301
|
-
|
|
302
|
-
[start1]:
|
|
303
|
-
Output for passage start1.
|
|
304
|
-
|
|
305
|
-
[[b]]:
|
|
306
|
-
Output for section B. Here's a passage: [b1].
|
|
307
|
-
|
|
308
|
-
[b1]:
|
|
309
|
-
Passage in section B.`);
|
|
310
|
-
squiffyApi.update(updated.story);
|
|
311
|
-
// We're in the first section, so the start1 passage should be clickable now
|
|
312
|
-
linkStart1 = findLink(element, 'passage', 'start1');
|
|
313
|
-
expect(squiffyApi.clickLink(linkStart1)).toBe(true);
|
|
314
|
-
// We're in the first section, so linkB should be clickable now
|
|
315
|
-
linkB = findLink(element, 'section', 'b');
|
|
316
|
-
expect(squiffyApi.clickLink(linkB)).toBe(true);
|
|
317
|
-
// and the passage [b1] within it should be clickable
|
|
318
|
-
const linkB1 = findLink(element, 'passage', 'b1');
|
|
319
|
-
expect(squiffyApi.clickLink(linkB1)).toBe(true);
|
|
320
|
-
});
|
|
321
|
-
test('Embed text from a section', async () => {
|
|
322
|
-
const script = `
|
|
323
|
-
[[section1]]:
|
|
324
|
-
Here is some text from the next section: {section2}
|
|
325
|
-
|
|
326
|
-
[[section2]]:
|
|
327
|
-
Text from next section.
|
|
328
|
-
`;
|
|
329
|
-
const { element } = await initScript(script);
|
|
330
|
-
let output = getSectionContent(element, 'section1');
|
|
331
|
-
expect(output).toBe('Here is some text from the next section: Text from next section.');
|
|
332
|
-
});
|
|
333
|
-
test('Embed text from a passage', async () => {
|
|
334
|
-
const script = `
|
|
335
|
-
[[section1]]:
|
|
336
|
-
Here is some text from a passage: {passage}
|
|
337
|
-
|
|
338
|
-
[passage]:
|
|
339
|
-
Text from a passage.
|
|
340
|
-
`;
|
|
341
|
-
const { element } = await initScript(script);
|
|
342
|
-
let output = getSectionContent(element, 'section1');
|
|
343
|
-
expect(output).toBe('Here is some text from a passage: Text from a passage.');
|
|
344
|
-
});
|
|
345
|
-
test('Update section with embedded text', async () => {
|
|
346
|
-
const script = `
|
|
347
|
-
[[section1]]:
|
|
348
|
-
Here is some text from the next section: {section2}
|
|
349
|
-
|
|
350
|
-
[[section2]]:
|
|
351
|
-
Text from next section.
|
|
352
|
-
`;
|
|
353
|
-
const { squiffyApi, element } = await initScript(script);
|
|
354
|
-
let output = getSectionContent(element, 'section1');
|
|
355
|
-
expect(output).toBe('Here is some text from the next section: Text from next section.');
|
|
356
|
-
const script2 = `
|
|
357
|
-
[[section1]]:
|
|
358
|
-
Here is an updated script with text from the next section: {section2}
|
|
359
|
-
|
|
360
|
-
[[section2]]:
|
|
361
|
-
Updated text from next section.
|
|
362
|
-
`;
|
|
363
|
-
const updated = await compile(script2);
|
|
364
|
-
squiffyApi.update(updated.story);
|
|
365
|
-
output = getSectionContent(element, 'section1');
|
|
366
|
-
// NOTE: The embedded text does not currently get updated. We would need to track where the embedded
|
|
367
|
-
// text has been written.
|
|
368
|
-
expect(output).toBe('Here is an updated script with text from the next section: Text from next section.');
|
|
369
|
-
});
|
|
370
|
-
test('Update passage with embedded text', async () => {
|
|
371
|
-
const script = `
|
|
372
|
-
[[section1]]:
|
|
373
|
-
Here is some text from a passage: {passage}
|
|
374
|
-
|
|
375
|
-
[passage]:
|
|
376
|
-
Text from a passage.
|
|
377
|
-
`;
|
|
378
|
-
const { squiffyApi, element } = await initScript(script);
|
|
379
|
-
let output = getSectionContent(element, 'section1');
|
|
380
|
-
expect(output).toBe('Here is some text from a passage: Text from a passage.');
|
|
381
|
-
const script2 = `
|
|
382
|
-
[[section1]]:
|
|
383
|
-
Here is an updated script with text from a passage: {passage}
|
|
384
|
-
|
|
385
|
-
[passage]:
|
|
386
|
-
Updated text from a passage.
|
|
387
|
-
`;
|
|
388
|
-
const updated = await compile(script2);
|
|
389
|
-
squiffyApi.update(updated.story);
|
|
390
|
-
output = getSectionContent(element, 'section1');
|
|
391
|
-
// NOTE: The embedded text does not currently get updated. We would need to track where the embedded
|
|
392
|
-
// text has been written.
|
|
393
|
-
expect(output).toBe('Here is an updated script with text from a passage: Text from a passage.');
|
|
394
|
-
});
|
package/dist/textProcessor.js
DELETED
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
import { startsWith, rotate } from "./utils.js";
|
|
2
|
-
export class TextProcessor {
|
|
3
|
-
constructor(get, set, story, currentSection, seen, processAttributes) {
|
|
4
|
-
this.get = get;
|
|
5
|
-
this.set = set;
|
|
6
|
-
this.story = story;
|
|
7
|
-
this.getCurrentSection = currentSection;
|
|
8
|
-
this.seen = seen;
|
|
9
|
-
this.processAttributes = processAttributes;
|
|
10
|
-
}
|
|
11
|
-
process(text, data) {
|
|
12
|
-
let containsUnprocessedSection = false;
|
|
13
|
-
const open = text.indexOf('{');
|
|
14
|
-
let close;
|
|
15
|
-
if (open > -1) {
|
|
16
|
-
let nestCount = 1;
|
|
17
|
-
let searchStart = open + 1;
|
|
18
|
-
let finished = false;
|
|
19
|
-
while (!finished) {
|
|
20
|
-
const nextOpen = text.indexOf('{', searchStart);
|
|
21
|
-
const nextClose = text.indexOf('}', searchStart);
|
|
22
|
-
if (nextClose > -1) {
|
|
23
|
-
if (nextOpen > -1 && nextOpen < nextClose) {
|
|
24
|
-
nestCount++;
|
|
25
|
-
searchStart = nextOpen + 1;
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
nestCount--;
|
|
29
|
-
searchStart = nextClose + 1;
|
|
30
|
-
if (nestCount === 0) {
|
|
31
|
-
close = nextClose;
|
|
32
|
-
containsUnprocessedSection = true;
|
|
33
|
-
finished = true;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
finished = true;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
if (containsUnprocessedSection) {
|
|
43
|
-
const section = text.substring(open + 1, close);
|
|
44
|
-
const value = this.processTextCommand(section, data);
|
|
45
|
-
text = text.substring(0, open) + value + this.process(text.substring(close + 1), data);
|
|
46
|
-
}
|
|
47
|
-
return (text);
|
|
48
|
-
}
|
|
49
|
-
processTextCommand(text, data) {
|
|
50
|
-
const currentSection = this.getCurrentSection();
|
|
51
|
-
if (startsWith(text, 'if ')) {
|
|
52
|
-
return this.processTextCommand_If(text, data);
|
|
53
|
-
}
|
|
54
|
-
else if (startsWith(text, 'else:')) {
|
|
55
|
-
return this.processTextCommand_Else(text, data);
|
|
56
|
-
}
|
|
57
|
-
else if (startsWith(text, 'label:')) {
|
|
58
|
-
return this.processTextCommand_Label(text, data);
|
|
59
|
-
}
|
|
60
|
-
else if (/^rotate[: ]/.test(text)) {
|
|
61
|
-
return this.processTextCommand_Rotate('rotate', text);
|
|
62
|
-
}
|
|
63
|
-
else if (/^sequence[: ]/.test(text)) {
|
|
64
|
-
return this.processTextCommand_Rotate('sequence', text);
|
|
65
|
-
}
|
|
66
|
-
else if (currentSection.passages && text in currentSection.passages) {
|
|
67
|
-
return this.process(currentSection.passages[text].text || '', data);
|
|
68
|
-
}
|
|
69
|
-
else if (text in this.story.sections) {
|
|
70
|
-
return this.process(this.story.sections[text].text || '', data);
|
|
71
|
-
}
|
|
72
|
-
else if (startsWith(text, '@') && !startsWith(text, '@replace')) {
|
|
73
|
-
this.processAttributes(text.substring(1).split(","));
|
|
74
|
-
return "";
|
|
75
|
-
}
|
|
76
|
-
return this.get(text);
|
|
77
|
-
}
|
|
78
|
-
processTextCommand_If(section, data) {
|
|
79
|
-
const command = section.substring(3);
|
|
80
|
-
const colon = command.indexOf(':');
|
|
81
|
-
if (colon == -1) {
|
|
82
|
-
return ('{if ' + command + '}');
|
|
83
|
-
}
|
|
84
|
-
const text = command.substring(colon + 1);
|
|
85
|
-
let condition = command.substring(0, colon);
|
|
86
|
-
condition = condition.replace("<", "<");
|
|
87
|
-
const operatorRegex = /([\w ]*)(=|<=|>=|<>|<|>)(.*)/;
|
|
88
|
-
const match = operatorRegex.exec(condition);
|
|
89
|
-
let result = false;
|
|
90
|
-
if (match) {
|
|
91
|
-
const lhs = this.get(match[1]);
|
|
92
|
-
const op = match[2];
|
|
93
|
-
let rhs = match[3];
|
|
94
|
-
if (startsWith(rhs, '@'))
|
|
95
|
-
rhs = this.get(rhs.substring(1));
|
|
96
|
-
if (op == '=' && lhs == rhs)
|
|
97
|
-
result = true;
|
|
98
|
-
if (op == '<>' && lhs != rhs)
|
|
99
|
-
result = true;
|
|
100
|
-
if (op == '>' && lhs > rhs)
|
|
101
|
-
result = true;
|
|
102
|
-
if (op == '<' && lhs < rhs)
|
|
103
|
-
result = true;
|
|
104
|
-
if (op == '>=' && lhs >= rhs)
|
|
105
|
-
result = true;
|
|
106
|
-
if (op == '<=' && lhs <= rhs)
|
|
107
|
-
result = true;
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
let checkValue = true;
|
|
111
|
-
if (startsWith(condition, 'not ')) {
|
|
112
|
-
condition = condition.substring(4);
|
|
113
|
-
checkValue = false;
|
|
114
|
-
}
|
|
115
|
-
if (startsWith(condition, 'seen ')) {
|
|
116
|
-
result = (this.seen(condition.substring(5)) == checkValue);
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
let value = this.get(condition);
|
|
120
|
-
if (value === null)
|
|
121
|
-
value = false;
|
|
122
|
-
result = (value == checkValue);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
const textResult = result ? this.process(text, data) : '';
|
|
126
|
-
data.lastIf = result;
|
|
127
|
-
return textResult;
|
|
128
|
-
}
|
|
129
|
-
processTextCommand_Else(section, data) {
|
|
130
|
-
if (!('lastIf' in data) || data.lastIf)
|
|
131
|
-
return '';
|
|
132
|
-
const text = section.substring(5);
|
|
133
|
-
return this.process(text, data);
|
|
134
|
-
}
|
|
135
|
-
processTextCommand_Label(section, data) {
|
|
136
|
-
const command = section.substring(6);
|
|
137
|
-
const eq = command.indexOf('=');
|
|
138
|
-
if (eq == -1) {
|
|
139
|
-
return ('{label:' + command + '}');
|
|
140
|
-
}
|
|
141
|
-
const text = command.substring(eq + 1);
|
|
142
|
-
const label = command.substring(0, eq);
|
|
143
|
-
return '<span class="squiffy-label-' + label + '">' + this.process(text, data) + '</span>';
|
|
144
|
-
}
|
|
145
|
-
processTextCommand_Rotate(type, section) {
|
|
146
|
-
let options;
|
|
147
|
-
let attribute = '';
|
|
148
|
-
if (section.substring(type.length, type.length + 1) == ' ') {
|
|
149
|
-
const colon = section.indexOf(':');
|
|
150
|
-
if (colon == -1) {
|
|
151
|
-
return '{' + section + '}';
|
|
152
|
-
}
|
|
153
|
-
options = section.substring(colon + 1);
|
|
154
|
-
attribute = section.substring(type.length + 1, colon);
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
options = section.substring(type.length + 1);
|
|
158
|
-
}
|
|
159
|
-
// TODO: Check - previously there was no second parameter here
|
|
160
|
-
const rotation = rotate(options.replace(/"/g, '"').replace(/'/g, '''), null);
|
|
161
|
-
if (attribute) {
|
|
162
|
-
this.set(attribute, rotation[0]);
|
|
163
|
-
}
|
|
164
|
-
return '<a class="squiffy-link" data-' + type + '="' + rotation[1] + '" data-attribute="' + attribute + '" role="link">' + rotation[0] + '</a>';
|
|
165
|
-
}
|
|
166
|
-
}
|
package/dist/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/utils.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export function startsWith(string, prefix) {
|
|
2
|
-
return string.substring(0, prefix.length) === prefix;
|
|
3
|
-
}
|
|
4
|
-
export function rotate(options, current) {
|
|
5
|
-
const colon = options.indexOf(':');
|
|
6
|
-
if (colon == -1) {
|
|
7
|
-
return [options, current];
|
|
8
|
-
}
|
|
9
|
-
const next = options.substring(0, colon);
|
|
10
|
-
let remaining = options.substring(colon + 1);
|
|
11
|
-
if (current)
|
|
12
|
-
remaining += ':' + current;
|
|
13
|
-
return [next, remaining];
|
|
14
|
-
}
|