squiffy-runtime 7.0.0-beta.5 → 7.0.0-beta.6

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.
Files changed (45) hide show
  1. package/dist/events.d.ts +1 -0
  2. package/dist/panels.d.ts +14 -0
  3. package/dist/panels.test.d.ts +1 -0
  4. package/dist/preview-host.d.ts +2 -1
  5. package/dist/preview-host.js +4 -1
  6. package/dist/preview-host.js.map +2 -2
  7. package/dist/preview-html.js +1 -1
  8. package/dist/preview-protocol.d.ts +4 -0
  9. package/dist/saveSlots.d.ts +16 -0
  10. package/dist/saves.test.d.ts +1 -0
  11. package/dist/savesDialog.d.ts +43 -0
  12. package/dist/savesDialog.test.d.ts +1 -0
  13. package/dist/squiffy.page.css +46 -22
  14. package/dist/squiffy.preview.html +604 -108
  15. package/dist/squiffy.runtime.css +468 -8
  16. package/dist/squiffy.runtime.d.ts +9 -1
  17. package/dist/squiffy.runtime.global.js +113 -72
  18. package/dist/squiffy.runtime.global.js.map +1 -1
  19. package/dist/squiffy.runtime.js +4154 -3534
  20. package/dist/squiffy.runtime.js.map +1 -1
  21. package/dist/state.d.ts +2 -0
  22. package/dist/textProcessor.d.ts +1 -0
  23. package/dist/types.d.ts +30 -0
  24. package/package.json +3 -3
  25. package/src/__snapshots__/squiffy.runtime.test.ts.snap +10 -10
  26. package/src/events.ts +1 -0
  27. package/src/panels.test.ts +201 -0
  28. package/src/panels.ts +187 -0
  29. package/src/preview-host.test.ts +5 -1
  30. package/src/preview-host.ts +9 -3
  31. package/src/preview-protocol.ts +6 -2
  32. package/src/preview.test.ts +68 -3
  33. package/src/preview.ts +74 -13
  34. package/src/saveSlots.ts +159 -0
  35. package/src/saves.test.ts +199 -0
  36. package/src/savesDialog.test.ts +290 -0
  37. package/src/savesDialog.ts +499 -0
  38. package/src/squiffy.page.css +46 -22
  39. package/src/squiffy.runtime.css +468 -8
  40. package/src/squiffy.runtime.test.ts +375 -4
  41. package/src/squiffy.runtime.ts +227 -10
  42. package/src/state.ts +18 -4
  43. package/src/textProcessor.ts +50 -9
  44. package/src/types.ts +54 -1
  45. package/vite.preview.config.ts +7 -0
@@ -147,7 +147,7 @@ Checked.`;
147
147
  });
148
148
 
149
149
  test("Click a section link", async () => {
150
- const script = await fs.readFile("../examples/test/example.squiffy", "utf-8");
150
+ const script = await fs.readFile("../compiler/test/fixtures/test/example.squiffy", "utf-8");
151
151
  const { squiffyApi, element } = await initScript(script);
152
152
  expect(element.innerHTML).toMatchSnapshot();
153
153
 
@@ -181,7 +181,7 @@ test("Click a section link", async () => {
181
181
  });
182
182
 
183
183
  test("Click a section link and go back", async () => {
184
- const script = await fs.readFile("../examples/test/example.squiffy", "utf-8");
184
+ const script = await fs.readFile("../compiler/test/fixtures/test/example.squiffy", "utf-8");
185
185
  const { squiffyApi, element } = await initScript(script);
186
186
 
187
187
  const linkToPassage = findLink(element, "passage", "a link to a passage");
@@ -245,7 +245,7 @@ test("Going back is saved, so a saved game doesn't bring back what was undone",
245
245
  });
246
246
 
247
247
  test("Click a passage link", async () => {
248
- const script = await fs.readFile("../examples/test/example.squiffy", "utf-8");
248
+ const script = await fs.readFile("../compiler/test/fixtures/test/example.squiffy", "utf-8");
249
249
  const { squiffyApi, element } = await initScript(script);
250
250
  expect(element.innerHTML).toMatchSnapshot();
251
251
 
@@ -280,7 +280,7 @@ test("Click a passage link", async () => {
280
280
  });
281
281
 
282
282
  test("Click a passage link and go back", async () => {
283
- const script = await fs.readFile("../examples/test/example.squiffy", "utf-8");
283
+ const script = await fs.readFile("../compiler/test/fixtures/test/example.squiffy", "utf-8");
284
284
  const { squiffyApi, element } = await initScript(script);
285
285
 
286
286
  const linkToPassage = findLink(element, "passage", "a link to a passage");
@@ -337,6 +337,132 @@ Last turn.
337
337
  expectSingleBlock("c", ["You chose c.", "Last turn."]);
338
338
  });
339
339
 
340
+ test("A passage with @repeat can be clicked again, and doesn't count towards @1 or @last", async () => {
341
+ // https://github.com/textadventures/squiffy/issues/104
342
+ const script = `
343
+ [[start]]:
344
+ Talk to the [guard], or look at the [a] or the [b].
345
+
346
+ [guard]:
347
+ @repeat
348
+ @inc talks
349
+ Talk {{talks}}.
350
+
351
+ [a]:
352
+ You look at a.
353
+
354
+ [b]:
355
+ You look at b.
356
+
357
+ [@1]:
358
+ Turn one.
359
+
360
+ [@last]:
361
+ Last turn.
362
+ `;
363
+ const { squiffyApi, element } = await initScript(script);
364
+ const passageTexts = () => Array.from(element.querySelectorAll(".squiffy-output-passage"))
365
+ .map(p => p.textContent!.trim());
366
+
367
+ const guard = findLink(element, "passage", "guard");
368
+ expect(await squiffyApi.clickLink(guard)).toBe(true);
369
+ expect(await squiffyApi.clickLink(guard)).toBe(true);
370
+ expect(guard.classList).not.toContain("disabled");
371
+ expect(squiffyApi.get("talks")).toBe(2);
372
+ // Neither click was a turn in the section, so @1 hasn't run
373
+ expect(passageTexts()).toEqual(["Talk 1.", "Talk 2."]);
374
+
375
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
376
+ expect(passageTexts().at(-1)).toBe("You look at a.Turn one.");
377
+
378
+ // @1 doesn't run again after another click on the guard
379
+ await squiffyApi.clickLink(guard);
380
+ expect(passageTexts().at(-1)).toBe("Talk 3.");
381
+
382
+ // @last doesn't wait for the guard, and doesn't run again for it
383
+ await squiffyApi.clickLink(findLink(element, "passage", "b"));
384
+ expect(passageTexts().at(-1)).toBe("You look at b.Last turn.");
385
+ await squiffyApi.clickLink(guard);
386
+ expect(passageTexts().at(-1)).toBe("Talk 4.");
387
+ expect(squiffyApi.get("turns")).toBe(6);
388
+ });
389
+
390
+ test("{{deactivate}} with no name stops a passage with @repeat, and going back undoes it", async () => {
391
+ // https://github.com/textadventures/squiffy/issues/13
392
+ const script = `
393
+ [[start]]:
394
+ Talk to the [guard]. Or [[leave]].
395
+
396
+ [guard]:
397
+ @repeat
398
+ @inc talks
399
+ {{#if talks < 2}}Move along.{{else}}The password is swordfish.{{deactivate}}{{/if}}
400
+
401
+ [[leave]]:
402
+ Bye.
403
+ `;
404
+ const { squiffyApi, element } = await initScript(script);
405
+ const guard = findLink(element, "passage", "guard");
406
+
407
+ await squiffyApi.clickLink(guard);
408
+ expect(guard.classList).not.toContain("disabled");
409
+ await squiffyApi.clickLink(guard);
410
+ expect(element.textContent).toContain("The password is swordfish.");
411
+ expect(guard.classList).toContain("disabled");
412
+ expect(await squiffyApi.clickLink(guard)).toBe(false);
413
+
414
+ squiffyApi.goBack();
415
+ expect(guard.classList).not.toContain("disabled");
416
+ expect(guard.hasAttribute("data-deactivated-by")).toBe(false);
417
+ expect(await squiffyApi.clickLink(guard)).toBe(true);
418
+ expect(guard.classList).toContain("disabled");
419
+ });
420
+
421
+ test("{{deactivate}} disables links to other passages, and going back only enables the ones it disabled", async () => {
422
+ // https://github.com/textadventures/squiffy/issues/13
423
+ const script = `
424
+ [[start]]:
425
+ You can see [a], [b], [c] and [d].
426
+
427
+ [a]:
428
+ {{deactivate "b" "c"}}A rules out B and C.
429
+
430
+ [b]:
431
+ B.
432
+
433
+ [c]:
434
+ C.
435
+
436
+ [d]:
437
+ D.
438
+ `;
439
+ const { squiffyApi, element } = await initScript(script);
440
+ const link = (name: string) => findLink(element, "passage", name);
441
+
442
+ // C is clicked first, so it's disabled already
443
+ await squiffyApi.clickLink(link("c"));
444
+ await squiffyApi.clickLink(link("a"));
445
+ expect(["a", "b", "c", "d"].map(n => link(n).classList.contains("disabled"))).toEqual([true, true, true, false]);
446
+
447
+ squiffyApi.goBack();
448
+ // A was undone, so B is back, but C stays clicked
449
+ expect(["a", "b", "c", "d"].map(n => link(n).classList.contains("disabled"))).toEqual([false, false, true, false]);
450
+ });
451
+
452
+ test("{{deactivate}} shows an error for a passage that isn't in the section, or with no name in a section", async () => {
453
+ const script = `
454
+ [[start]]:
455
+ {{deactivate}}See [a].
456
+
457
+ [a]:
458
+ {{deactivate "nowhere"}}A.
459
+ `;
460
+ const { squiffyApi, element } = await initScript(script);
461
+ expect(element.textContent).toContain("{{deactivate}} with no passage name only works in a passage");
462
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
463
+ expect(element.textContent).toContain('there is no passage called "nowhere" in this section');
464
+ });
465
+
340
466
  test("Going back to a section re-enables its inputs", async () => {
341
467
  // https://github.com/textadventures/squiffy/issues/123
342
468
  const script = `
@@ -3043,6 +3169,94 @@ Length is {{length (array "x" "y")}}.
3043
3169
  expect(element.textContent).toContain("Length is 2.");
3044
3170
  });
3045
3171
 
3172
+ // {{#each}} loops
3173
+
3174
+ test("{{#each}} lists an array, with @index, @first and {{else}}", async () => {
3175
+ const script = `
3176
+ [[start]]:
3177
+ @set gold = 3
3178
+ {{set "inventory" (array "sword" "key" "lamp")}}
3179
+ {{set "empty" (array)}}
3180
+ A: {{#each inventory}}{{#unless @first}}, {{/unless}}{{@index}} {{this}}{{/each}}.
3181
+ B: {{#each empty}}{{this}}{{else}}nothing{{/each}}.
3182
+ `;
3183
+
3184
+ const { element } = await initScript(script);
3185
+ expect(element.textContent).toContain("A: 0 sword, 1 key, 2 lamp.");
3186
+ expect(element.textContent).toContain("B: nothing.");
3187
+ });
3188
+
3189
+ test("Attributes can be read inside {{#each}}", async () => {
3190
+ const script = `
3191
+ [[start]]:
3192
+ @set gold = 3
3193
+ {{set "inventory" (array "sword" "key")}}
3194
+ A: {{#each inventory}}{{this}} {{gold}}; {{/each}}
3195
+ B: {{#each inventory}}{{../gold}}{{/each}}
3196
+ C: {{#each inventory}}{{#if gold > 2 and this = "key"}}yes{{/if}}{{/each}}
3197
+ D: {{#each inventory}}{{inc "gold"}}{{/each}}{{gold}}
3198
+ `;
3199
+
3200
+ const { element } = await initScript(script, true);
3201
+ expect(element.textContent).toContain("A: sword 3; key 3;");
3202
+ expect(element.textContent).toContain("B: 33");
3203
+ expect(element.textContent).toContain("C: yes");
3204
+ expect(element.textContent).toContain("D: 5");
3205
+ });
3206
+
3207
+ test("Links inside {{#each}} can use the item in their text and setters", async () => {
3208
+ const script = `
3209
+ [[start]]:
3210
+ @set gold = 3
3211
+ {{set "inventory" (array "sword" "key")}}
3212
+ {{#each inventory}}
3213
+ - [[Drop the {{this}}]](drop, item = "{{this}}", gold += 1)
3214
+ - [Look at the {{this}}](look, looking = "{{this}}")
3215
+ {{/each}}
3216
+
3217
+ [look]:
3218
+ You look at the {{looking}}.
3219
+
3220
+ [[drop]]:
3221
+ You dropped the {{item}}, and have {{gold}} gold.
3222
+ `;
3223
+
3224
+ const { squiffyApi, element } = await initScript(script, true);
3225
+ expect(findLink(element, "section", "Drop the sword")).toBeDefined();
3226
+
3227
+ await squiffyApi.clickLink(findLink(element, "passage", "Look at the sword"));
3228
+ expect(element.textContent).toContain("You look at the sword.");
3229
+
3230
+ await squiffyApi.clickLink(findLink(element, "section", "Drop the key"));
3231
+ expect(squiffyApi.get("item")).toBe("key");
3232
+ expect(element.textContent).toContain("You dropped the key, and have 4 gold.");
3233
+ });
3234
+
3235
+ test("A link's text can show an attribute", async () => {
3236
+ const script = `
3237
+ [[start]]:
3238
+ @set gold = 3
3239
+ [[Spend {{gold}} gold]](shop)
3240
+
3241
+ [[shop]]:
3242
+ Spent.
3243
+ `;
3244
+
3245
+ const { element } = await initScript(script, true);
3246
+ expect(findLink(element, "section", "Spend 3 gold")).toBeDefined();
3247
+ });
3248
+
3249
+ test("Names every object has, like valueOf, aren't attributes", async () => {
3250
+ const script = `
3251
+ [[start]]:
3252
+ {{#if constructor}}yes{{else}}no{{/if}}
3253
+ `;
3254
+
3255
+ const { squiffyApi, element } = await initScript(script);
3256
+ expect(element.textContent).toContain("no");
3257
+ expect(squiffyApi.get("valueOf")).toBeNull();
3258
+ });
3259
+
3046
3260
  test("Array helpers work together for game round tracking", async () => {
3047
3261
  const script = `
3048
3262
  [[start]]:
@@ -3633,3 +3847,160 @@ Start {{visits "start"}}, cave {{visits "cave"}}, lake {{visits "lake"}}, turns
3633
3847
  localStorage.clear();
3634
3848
  });
3635
3849
 
3850
+
3851
+ test("previous and previous_section say where the player was before (#56)", async () => {
3852
+ const script = `
3853
+ [[start]]:
3854
+ Start, previous "{{previous}}". [a] [b] [[cave]]
3855
+
3856
+ [a]:
3857
+ A after {{previous}}.
3858
+
3859
+ [b]:
3860
+ B after {{previous}}.
3861
+
3862
+ [[cave]]:
3863
+ Cave after {{previous}}, from section {{previous_section}}. [c]
3864
+
3865
+ [c]:
3866
+ C after {{previous}}, from section {{previous_section}}.
3867
+ `;
3868
+ const { squiffyApi, element } = await initScript(script);
3869
+ expect(visibleText(element)).toContain("Start, previous \"\".");
3870
+ expect(squiffyApi.get("previous")).toBeNull();
3871
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
3872
+ expect(visibleText(element)).toContain("A after start.");
3873
+ await squiffyApi.clickLink(findLink(element, "passage", "b"));
3874
+ expect(visibleText(element)).toContain("B after a.");
3875
+ await squiffyApi.clickLink(findLink(element, "section", "cave"));
3876
+ expect(visibleText(element)).toContain("Cave after b, from section start.");
3877
+ await squiffyApi.clickLink(findLink(element, "passage", "c"));
3878
+ expect(visibleText(element)).toContain("C after cave, from section start.");
3879
+ });
3880
+
3881
+ test("previous works in conditions", async () => {
3882
+ const script = `
3883
+ [[start]]:
3884
+ [[river]] [[forest]]
3885
+
3886
+ [[river]]:
3887
+ [[cave]]
3888
+
3889
+ [[forest]]:
3890
+ [[cave]]
3891
+
3892
+ [[cave]]:
3893
+ {{#if previous_section = "river"}}You squeeze out of the water.{{else}}You step in from the trees.{{/if}}
3894
+ `;
3895
+ const { squiffyApi, element } = await initScript(script, true);
3896
+ await squiffyApi.clickLink(findLink(element, "section", "river"));
3897
+ await squiffyApi.clickLink(findLink(element, "section", "cave"));
3898
+ expect(visibleText(element)).toContain("You squeeze out of the water.");
3899
+ });
3900
+
3901
+ test("A section reached with {{goto}} has the section before it as previous", async () => {
3902
+ const script = `
3903
+ [[start]]:
3904
+ [[fight]]
3905
+
3906
+ [[fight]]:
3907
+ {{goto "death"}}
3908
+
3909
+ [[death]]:
3910
+ Died after {{previous}}.
3911
+ `;
3912
+ const { squiffyApi, element } = await initScript(script);
3913
+ await squiffyApi.clickLink(findLink(element, "section", "fight"));
3914
+ expect(visibleText(element)).toContain("Died after fight.");
3915
+ expect(squiffyApi.get("previous_section")).toBe("fight");
3916
+ });
3917
+
3918
+ test("Going back undoes previous and previous_section", async () => {
3919
+ const script = `
3920
+ [[start]]:
3921
+ [a] [[next]]
3922
+
3923
+ [a]:
3924
+ A.
3925
+
3926
+ [[next]]:
3927
+ Next.
3928
+ `;
3929
+ const { squiffyApi, element } = await initScript(script);
3930
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
3931
+ await squiffyApi.clickLink(findLink(element, "section", "next"));
3932
+ expect(squiffyApi.get("previous")).toBe("a");
3933
+ expect(squiffyApi.get("previous_section")).toBe("start");
3934
+
3935
+ squiffyApi.goBack();
3936
+ expect(squiffyApi.get("previous")).toBe("start");
3937
+ expect(squiffyApi.get("previous_section")).toBeNull();
3938
+
3939
+ squiffyApi.goBack();
3940
+ expect(squiffyApi.get("previous")).toBeNull();
3941
+ });
3942
+
3943
+ test("squiffy.ui.afterWrite runs once the text and its {{...}} are on the page", async () => {
3944
+ // https://github.com/textadventures/squiffy/issues/106
3945
+ // Note: the 4-space indent on the JS lines is the squiffy JS syntax - don't trim it!
3946
+ const script = `
3947
+ [[start]]:
3948
+ @set n = 5
3949
+ set("order", "before");
3950
+ squiffy.ui.afterWrite(() => {
3951
+ set("shown", document.getElementById("count").textContent);
3952
+ set("order", get("order") + ",after");
3953
+ });
3954
+ set("order", get("order") + ",js");
3955
+ Count: <span id="count">{{n}}</span> [a] [[next]]
3956
+
3957
+ [a]:
3958
+ squiffy.ui.afterWrite(() => set("passage_shown", document.getElementById("passage").textContent));
3959
+ <span id="passage">{{n}} again</span>
3960
+
3961
+ [[next]]:
3962
+ Next.
3963
+ `;
3964
+ const { squiffyApi, element } = await initScript(script);
3965
+ expect(squiffyApi.get("shown")).toBe("5");
3966
+ expect(squiffyApi.get("order")).toBe("before,js,after");
3967
+
3968
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
3969
+ expect(squiffyApi.get("passage_shown")).toBe("5 again");
3970
+
3971
+ // What an afterWrite function changes is part of the turn, so going back undoes it
3972
+ squiffyApi.goBack();
3973
+ expect(squiffyApi.get("passage_shown")).toBeNull();
3974
+ expect(squiffyApi.get("shown")).toBe("5");
3975
+ });
3976
+
3977
+ test("squiffy.story.go() in squiffy.ui.afterWrite goes there once the section has finished", async () => {
3978
+ // https://github.com/textadventures/squiffy/issues/106
3979
+ // Note: the 4-space indent on the JS lines is the squiffy JS syntax - don't trim it!
3980
+ const script = `
3981
+ [[start]]:
3982
+ @set x = 1
3983
+ Start. [[next]]
3984
+
3985
+ [[next]]:
3986
+ @set x = 2
3987
+ squiffy.ui.afterWrite(() => squiffy.story.go("other"));
3988
+ squiffy.ui.afterWrite(() => set("ran", true));
3989
+ Next.
3990
+
3991
+ [[other]]:
3992
+ @set x = 3
3993
+ Other.
3994
+ `;
3995
+ const { squiffyApi, element } = await initScript(script);
3996
+ await squiffyApi.clickLink(findLink(element, "section", "next"));
3997
+ expect(squiffyApi.get("_section")).toBe("other");
3998
+ expect(squiffyApi.get("ran")).toBe(true);
3999
+ expect(visibleText(element)).toBe("Start. next Next. Other.");
4000
+
4001
+ // Going back steps back through both sections, as with {{goto}}
4002
+ squiffyApi.goBack();
4003
+ expect(squiffyApi.get("_section")).toBe("start");
4004
+ expect(squiffyApi.get("x")).toBe(1);
4005
+ expect(visibleText(element)).toBe("Start. next");
4006
+ });