squiffy-runtime 6.0.1 → 7.0.0-beta.2
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 +2 -0
- package/dist/assets.test.d.ts +1 -0
- package/dist/events.d.ts +4 -0
- package/dist/pluginManager.d.ts +2 -1
- package/dist/preview-host.d.ts +30 -0
- package/dist/preview-host.js +89 -0
- package/dist/preview-host.js.map +7 -0
- package/dist/preview-host.test.d.ts +1 -0
- package/dist/preview-html.d.ts +2 -0
- package/dist/preview-html.js +2 -0
- package/dist/preview-protocol.d.ts +57 -0
- package/dist/preview.d.ts +14 -0
- package/dist/preview.main.d.ts +1 -0
- package/dist/preview.test.d.ts +1 -0
- package/dist/squiffy.page.css +120 -0
- package/dist/squiffy.preview.html +477 -0
- package/dist/squiffy.runtime.css +76 -20
- package/dist/squiffy.runtime.global.js +62 -57
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +3146 -3069
- package/dist/squiffy.runtime.js.map +1 -1
- package/dist/types.d.ts +12 -1
- package/dist/types.plugins.d.ts +1 -0
- package/package.json +22 -11
- package/src/assets.test.ts +227 -0
- package/src/assets.ts +92 -0
- package/src/events.ts +1 -0
- package/src/pluginManager.ts +5 -1
- package/src/plugins/animate.ts +6 -5
- package/src/preview-host.test.ts +92 -0
- package/src/preview-host.ts +138 -0
- package/src/preview-protocol.ts +63 -0
- package/src/preview.main.ts +33 -0
- package/src/preview.test.ts +181 -0
- package/src/preview.ts +171 -0
- package/src/squiffy.page.css +120 -0
- package/src/squiffy.runtime.css +76 -20
- package/src/squiffy.runtime.ts +25 -6
- package/src/types.plugins.ts +4 -0
- package/src/types.ts +17 -1
- package/vite.config.ts +3 -4
- package/vite.preview.config.ts +76 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/* Page layout styles for an exported story. See https://squiffystory.com/styling/ for how to customise these. */
|
|
2
|
+
|
|
3
|
+
:root
|
|
4
|
+
{
|
|
5
|
+
/* The width of the story text */
|
|
6
|
+
--squiffy-max-width: 42rem;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/* Follow the reader's light or dark system setting, or what they chose with the theme button.
|
|
10
|
+
To keep a story light (or dark) whatever the setting, set ":root { color-scheme: light; }"
|
|
11
|
+
(or dark). These rules use :where() so that a :root rule like that overrides them, and the
|
|
12
|
+
theme button is hidden. */
|
|
13
|
+
:where(:root)
|
|
14
|
+
{
|
|
15
|
+
color-scheme: light dark;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
:where(:root[data-squiffy-theme="light"])
|
|
19
|
+
{
|
|
20
|
+
color-scheme: light;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
:where(:root[data-squiffy-theme="dark"])
|
|
24
|
+
{
|
|
25
|
+
color-scheme: dark;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
body
|
|
29
|
+
{
|
|
30
|
+
margin: 0;
|
|
31
|
+
background-color: var(--squiffy-background);
|
|
32
|
+
color: var(--squiffy-text);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/* Wraps the header and the story: sets the page width and font */
|
|
36
|
+
div#squiffy-container
|
|
37
|
+
{
|
|
38
|
+
max-width: var(--squiffy-max-width);
|
|
39
|
+
margin-left: auto;
|
|
40
|
+
margin-right: auto;
|
|
41
|
+
padding: 0 1.25rem 3rem;
|
|
42
|
+
font-family: Charter, "Bitstream Charter", "Sitka Text", Cambria, Georgia, serif;
|
|
43
|
+
line-height: 1.6;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/* The bar above the story containing the Restart and light/dark buttons. It stays at the top of
|
|
47
|
+
the window as the story scrolls under it. */
|
|
48
|
+
div#squiffy-header
|
|
49
|
+
{
|
|
50
|
+
position: sticky;
|
|
51
|
+
top: 0;
|
|
52
|
+
z-index: 1;
|
|
53
|
+
padding: 0.5rem 0;
|
|
54
|
+
background-color: var(--squiffy-background);
|
|
55
|
+
font-size: 0.875rem;
|
|
56
|
+
text-align: right;
|
|
57
|
+
white-space: nowrap;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/* A short fade under the header, so the story scrolls under it rather than being cut off */
|
|
61
|
+
div#squiffy-header::after
|
|
62
|
+
{
|
|
63
|
+
content: "";
|
|
64
|
+
position: absolute;
|
|
65
|
+
left: 0;
|
|
66
|
+
right: 0;
|
|
67
|
+
top: 100%;
|
|
68
|
+
height: 0.75rem;
|
|
69
|
+
background: linear-gradient(var(--squiffy-background), transparent);
|
|
70
|
+
pointer-events: none;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/* The button that switches between light and dark mode. It shows a moon in light mode and a sun
|
|
74
|
+
in dark mode. */
|
|
75
|
+
button#squiffy-theme
|
|
76
|
+
{
|
|
77
|
+
margin-left: 0.75em;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/* Centred on the capital letters of "Restart", rather than on the line, which looks too low */
|
|
81
|
+
button#squiffy-theme svg
|
|
82
|
+
{
|
|
83
|
+
width: 1.1em;
|
|
84
|
+
height: 1.1em;
|
|
85
|
+
vertical-align: -0.25em;
|
|
86
|
+
fill: none;
|
|
87
|
+
stroke: currentColor;
|
|
88
|
+
stroke-width: 2;
|
|
89
|
+
stroke-linecap: round;
|
|
90
|
+
stroke-linejoin: round;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
button#squiffy-theme:not([data-next="light"]) .squiffy-icon-light,
|
|
94
|
+
button#squiffy-theme[data-next="light"] .squiffy-icon-dark
|
|
95
|
+
{
|
|
96
|
+
display: none;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/* The story text */
|
|
100
|
+
div#squiffy
|
|
101
|
+
{
|
|
102
|
+
font-size: 1.125rem;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
div#squiffy :is(h1, h2, h3, h4, h5, h6)
|
|
106
|
+
{
|
|
107
|
+
line-height: 1.25;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
div#squiffy p
|
|
111
|
+
{
|
|
112
|
+
text-wrap: pretty;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/* Stop images overflowing the page */
|
|
116
|
+
div#squiffy img
|
|
117
|
+
{
|
|
118
|
+
max-width: 100%;
|
|
119
|
+
height: auto;
|
|
120
|
+
}
|
package/src/squiffy.runtime.css
CHANGED
|
@@ -1,15 +1,49 @@
|
|
|
1
|
-
/* Core Squiffy Runtime Styles */
|
|
1
|
+
/* Core Squiffy Runtime Styles. See https://squiffystory.com/styling/ for how to customise these. */
|
|
2
|
+
|
|
3
|
+
/* Colours. Each has a light and a dark value, and light-dark() picks one from the page's
|
|
4
|
+
color-scheme. This file doesn't set color-scheme itself, so a story embedded in a page stays
|
|
5
|
+
light unless the page opts in to dark mode. The exported story's page (squiffy.page.css) sets
|
|
6
|
+
"color-scheme: light dark", so it follows the reader's system setting. */
|
|
7
|
+
:root
|
|
8
|
+
{
|
|
9
|
+
--squiffy-text: light-dark(#1f2328, #e4e4e7);
|
|
10
|
+
--squiffy-background: light-dark(#ffffff, #18181b);
|
|
11
|
+
/* Secondary text: the Restart button, and links waiting for valid input */
|
|
12
|
+
--squiffy-muted: light-dark(#6b6f76, #9a9ca3);
|
|
13
|
+
/* The lines between sections */
|
|
14
|
+
--squiffy-border: light-dark(#e2e2e6, #34343a);
|
|
15
|
+
--squiffy-link: light-dark(#1d5fd6, #7aa7ff);
|
|
16
|
+
--squiffy-link-hover: light-dark(#0f3f99, #b0cbff);
|
|
17
|
+
--squiffy-error: light-dark(#c62828, #ff7b72);
|
|
18
|
+
--squiffy-success: light-dark(#2e7d32, #6cc070);
|
|
19
|
+
}
|
|
2
20
|
|
|
3
21
|
/* Link styles */
|
|
4
22
|
a.squiffy-link
|
|
5
23
|
{
|
|
24
|
+
color: var(--squiffy-link);
|
|
6
25
|
text-decoration: underline;
|
|
7
|
-
text-decoration-color:
|
|
8
|
-
|
|
26
|
+
text-decoration-color: currentColor;
|
|
27
|
+
text-decoration-thickness: from-font;
|
|
28
|
+
text-underline-offset: 0.15em;
|
|
9
29
|
cursor: pointer;
|
|
10
30
|
transition: color 0.4s ease-in-out, text-decoration-color 0.4s ease-in-out;
|
|
11
31
|
}
|
|
12
32
|
|
|
33
|
+
a.squiffy-link:hover
|
|
34
|
+
{
|
|
35
|
+
color: var(--squiffy-link-hover);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
a.squiffy-link:focus-visible,
|
|
39
|
+
button.squiffy-header-button:focus-visible,
|
|
40
|
+
.squiffy-continue:focus-visible
|
|
41
|
+
{
|
|
42
|
+
outline: 2px solid var(--squiffy-link);
|
|
43
|
+
outline-offset: 2px;
|
|
44
|
+
border-radius: 2px;
|
|
45
|
+
}
|
|
46
|
+
|
|
13
47
|
/* Rotate/sequence links - visually distinct as they update in-place rather than advancing the story */
|
|
14
48
|
a.squiffy-link[data-handler="rotate"],
|
|
15
49
|
a.squiffy-link[data-handler="sequence"] {
|
|
@@ -33,10 +67,9 @@ a.squiffy-link.validation-disabled
|
|
|
33
67
|
{
|
|
34
68
|
text-decoration: inherit;
|
|
35
69
|
text-decoration-color: transparent;
|
|
36
|
-
color:
|
|
70
|
+
color: var(--squiffy-muted) !important;
|
|
37
71
|
cursor: not-allowed;
|
|
38
72
|
pointer-events: none;
|
|
39
|
-
opacity: 0.6;
|
|
40
73
|
}
|
|
41
74
|
|
|
42
75
|
/* Invalid input field states */
|
|
@@ -44,8 +77,8 @@ input.squiffy-invalid,
|
|
|
44
77
|
textarea.squiffy-invalid,
|
|
45
78
|
select.squiffy-invalid
|
|
46
79
|
{
|
|
47
|
-
border-color:
|
|
48
|
-
box-shadow: 0 0 0 0.2rem
|
|
80
|
+
border-color: var(--squiffy-error) !important;
|
|
81
|
+
box-shadow: 0 0 0 0.2rem color-mix(in srgb, var(--squiffy-error) 25%, transparent);
|
|
49
82
|
}
|
|
50
83
|
|
|
51
84
|
/* Valid input field states (optional, provides positive feedback) */
|
|
@@ -53,7 +86,7 @@ input:not([disabled]):valid,
|
|
|
53
86
|
textarea:not([disabled]):valid,
|
|
54
87
|
select:not([disabled]):valid
|
|
55
88
|
{
|
|
56
|
-
border-color:
|
|
89
|
+
border-color: var(--squiffy-success);
|
|
57
90
|
}
|
|
58
91
|
|
|
59
92
|
/* Focus state for invalid inputs */
|
|
@@ -62,19 +95,29 @@ textarea.squiffy-invalid:focus,
|
|
|
62
95
|
select.squiffy-invalid:focus
|
|
63
96
|
{
|
|
64
97
|
outline: none;
|
|
65
|
-
border-color:
|
|
66
|
-
box-shadow: 0 0 0 0.2rem
|
|
98
|
+
border-color: var(--squiffy-error) !important;
|
|
99
|
+
box-shadow: 0 0 0 0.2rem color-mix(in srgb, var(--squiffy-error) 50%, transparent);
|
|
67
100
|
}
|
|
68
101
|
|
|
69
|
-
/* Header button styles */
|
|
102
|
+
/* Header button styles: the Restart button */
|
|
70
103
|
button.squiffy-header-button
|
|
71
104
|
{
|
|
72
|
-
|
|
73
|
-
color: Blue;
|
|
105
|
+
color: var(--squiffy-muted);
|
|
74
106
|
cursor: pointer;
|
|
75
107
|
background: none;
|
|
76
108
|
border: none;
|
|
77
|
-
|
|
109
|
+
padding: 0.25em 0;
|
|
110
|
+
font: inherit;
|
|
111
|
+
text-decoration: underline;
|
|
112
|
+
text-decoration-color: transparent;
|
|
113
|
+
text-underline-offset: 0.15em;
|
|
114
|
+
transition: color 0.2s ease-in-out, text-decoration-color 0.2s ease-in-out;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
button.squiffy-header-button:hover
|
|
118
|
+
{
|
|
119
|
+
color: var(--squiffy-text);
|
|
120
|
+
text-decoration-color: currentColor;
|
|
78
121
|
}
|
|
79
122
|
|
|
80
123
|
/* Output block spacing */
|
|
@@ -83,7 +126,7 @@ div.squiffy-output-passage:not(:last-child),
|
|
|
83
126
|
div.squiffy-output-section:not(:last-child)
|
|
84
127
|
{
|
|
85
128
|
margin-bottom: 1em;
|
|
86
|
-
border-bottom: 1px solid
|
|
129
|
+
border-bottom: 1px solid var(--squiffy-border);
|
|
87
130
|
}
|
|
88
131
|
|
|
89
132
|
/* Fade animations */
|
|
@@ -99,9 +142,11 @@ div.squiffy-output-section:not(:last-child)
|
|
|
99
142
|
|
|
100
143
|
/* Continue animation - click to proceed */
|
|
101
144
|
.squiffy-continue {
|
|
145
|
+
color: var(--squiffy-link);
|
|
102
146
|
text-decoration: underline;
|
|
103
|
-
text-decoration-color:
|
|
104
|
-
|
|
147
|
+
text-decoration-color: currentColor;
|
|
148
|
+
text-decoration-thickness: from-font;
|
|
149
|
+
text-underline-offset: 0.15em;
|
|
105
150
|
cursor: pointer;
|
|
106
151
|
}
|
|
107
152
|
|
|
@@ -121,8 +166,8 @@ div.squiffy-output-section:not(:last-child)
|
|
|
121
166
|
}
|
|
122
167
|
|
|
123
168
|
@keyframes squiffy-glow {
|
|
124
|
-
0%, 100% { text-shadow: 0 0 4px
|
|
125
|
-
50% { text-shadow: 0 0 12px
|
|
169
|
+
0%, 100% { text-shadow: 0 0 4px color-mix(in srgb, currentColor 50%, transparent); }
|
|
170
|
+
50% { text-shadow: 0 0 12px color-mix(in srgb, currentColor 80%, transparent), 0 0 20px color-mix(in srgb, currentColor 40%, transparent); }
|
|
126
171
|
}
|
|
127
172
|
|
|
128
173
|
/* Style: bounce - subtle vertical bounce */
|
|
@@ -139,5 +184,16 @@ div.squiffy-output-section:not(:last-child)
|
|
|
139
184
|
/* Style: wave - character spans need special handling for underline */
|
|
140
185
|
.squiffy-continue-wave {
|
|
141
186
|
text-decoration: none;
|
|
142
|
-
border-bottom: 1px solid
|
|
187
|
+
border-bottom: 1px solid currentColor;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/* Respect the reader's reduced-motion setting */
|
|
191
|
+
@media (prefers-reduced-motion: reduce)
|
|
192
|
+
{
|
|
193
|
+
.squiffy-continue-pulse,
|
|
194
|
+
.squiffy-continue-glow,
|
|
195
|
+
.squiffy-continue-bounce
|
|
196
|
+
{
|
|
197
|
+
animation: none;
|
|
198
|
+
}
|
|
143
199
|
}
|
package/src/squiffy.runtime.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SquiffyApi, SquiffyInitOptions, Story, Section, Passage } from "./types.js";
|
|
1
|
+
import { SquiffyApi, SquiffyInitOptions, Story, Section, Passage, StartAt } from "./types.js";
|
|
2
2
|
import { TextProcessor } from "./textProcessor.js";
|
|
3
3
|
import { Emitter, SquiffyEventMap } from "./events.js";
|
|
4
4
|
import { State } from "./state.js";
|
|
@@ -9,6 +9,7 @@ import { LinkHandler } from "./linkHandler.js";
|
|
|
9
9
|
import { Animation } from "./animation.js";
|
|
10
10
|
import { imports } from "./import.js";
|
|
11
11
|
import { areInputsValid, setupInputValidation } from "./inputValidation.js";
|
|
12
|
+
import { createAssetHtmlResolver } from "./assets.js";
|
|
12
13
|
|
|
13
14
|
export type { SquiffyApi } from "./types.js";
|
|
14
15
|
export type { SquiffyEventHandler, SquiffyEventMap } from "./events.js";
|
|
@@ -95,7 +96,15 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
|
|
|
95
96
|
link.removeAttribute("tabindex");
|
|
96
97
|
}
|
|
97
98
|
|
|
98
|
-
async function begin() {
|
|
99
|
+
async function begin(startAt?: StartAt) {
|
|
100
|
+
if (startAt && startAt.section in story.sections) {
|
|
101
|
+
// Start somewhere else in a fresh story, e.g. from the section being edited
|
|
102
|
+
await go(startAt.section);
|
|
103
|
+
if (startAt.passage !== undefined && get("_section") == startAt.section) {
|
|
104
|
+
await showPassage(startAt.passage);
|
|
105
|
+
}
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
99
108
|
if (!load()) {
|
|
100
109
|
await go(story.start);
|
|
101
110
|
}
|
|
@@ -209,6 +218,7 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
|
|
|
209
218
|
if (newCanGoBack != oldCanGoBack) {
|
|
210
219
|
emitter.emit("canGoBackChanged", { canGoBack: newCanGoBack });
|
|
211
220
|
}
|
|
221
|
+
emitter.emit("navigated", { section: sectionName });
|
|
212
222
|
}
|
|
213
223
|
|
|
214
224
|
function runJs(index: number, extra: any = null) {
|
|
@@ -329,6 +339,7 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
|
|
|
329
339
|
if (newCanGoBack != oldCanGoBack) {
|
|
330
340
|
emitter.emit("canGoBackChanged", { canGoBack: newCanGoBack });
|
|
331
341
|
}
|
|
342
|
+
emitter.emit("navigated", { section: get("_section"), passage: passageName });
|
|
332
343
|
}
|
|
333
344
|
|
|
334
345
|
async function processAttributes(attributes: string[]) {
|
|
@@ -373,7 +384,8 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
|
|
|
373
384
|
return false;
|
|
374
385
|
}
|
|
375
386
|
|
|
376
|
-
|
|
387
|
+
// Saved output may contain asset URLs that are no longer valid (e.g. blob: URLs), so resolve them again
|
|
388
|
+
outputElement.innerHTML = resolveAssetsInHtml(output);
|
|
377
389
|
|
|
378
390
|
setCurrentSectionElement();
|
|
379
391
|
setCurrentPassageElement();
|
|
@@ -481,7 +493,7 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
|
|
|
481
493
|
write: (text: string, source: string) => {
|
|
482
494
|
scrollPosition = outputElement.scrollHeight;
|
|
483
495
|
|
|
484
|
-
const html = ui.
|
|
496
|
+
const html = ui.renderHtml(text, false).trim();
|
|
485
497
|
|
|
486
498
|
// Previously, we skipped the rest of this if "html" came back as an empty string.
|
|
487
499
|
// But, we _do_ always want the block to be created, in the editor at least - as the
|
|
@@ -532,6 +544,10 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
|
|
|
532
544
|
processText: (text: string, inline: boolean) => {
|
|
533
545
|
return textProcessor.process(text, inline);
|
|
534
546
|
},
|
|
547
|
+
// processText, plus asset URL resolution - for text that's going to be displayed as HTML
|
|
548
|
+
renderHtml: (text: string, inline: boolean) => {
|
|
549
|
+
return resolveAssetsInHtml(ui.processText(text, inline));
|
|
550
|
+
},
|
|
535
551
|
};
|
|
536
552
|
|
|
537
553
|
function update(newStory: Story) {
|
|
@@ -554,7 +570,7 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
|
|
|
554
570
|
const previousSection = currentSection;
|
|
555
571
|
currentSection = section;
|
|
556
572
|
try {
|
|
557
|
-
return ui.
|
|
573
|
+
return ui.renderHtml(text, false);
|
|
558
574
|
} finally {
|
|
559
575
|
currentSection = previousSection;
|
|
560
576
|
}
|
|
@@ -677,6 +693,8 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
|
|
|
677
693
|
if (!canGoBack()) {
|
|
678
694
|
emitter.emit("canGoBackChanged", { canGoBack: false });
|
|
679
695
|
}
|
|
696
|
+
const passage = currentPassageElement?.getAttribute("data-passage");
|
|
697
|
+
emitter.emit("navigated", passage ? { section: get("_section"), passage } : { section: get("_section") });
|
|
680
698
|
}
|
|
681
699
|
|
|
682
700
|
// We create a separate div inside the passed-in element. This allows us to clear the text output, but
|
|
@@ -685,6 +703,7 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
|
|
|
685
703
|
const outputElement = document.createElement("div");
|
|
686
704
|
outputElementContainer.appendChild(outputElement);
|
|
687
705
|
story = options.story;
|
|
706
|
+
const resolveAssetsInHtml = createAssetHtmlResolver(options.resolveAssetUrl);
|
|
688
707
|
|
|
689
708
|
const settings = {
|
|
690
709
|
scroll: options.scroll || "body",
|
|
@@ -775,7 +794,7 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
|
|
|
775
794
|
const animation = new Animation();
|
|
776
795
|
|
|
777
796
|
const pluginManager = new PluginManager(outputElement, textProcessor, state, linkHandler,
|
|
778
|
-
getSectionText, getPassageText, ui.
|
|
797
|
+
getSectionText, getPassageText, ui.renderHtml, addTransition, animation, emitter, resolveAssetsInHtml);
|
|
779
798
|
pluginManager.add(Plugins.ReplaceLabel());
|
|
780
799
|
pluginManager.add(Plugins.RotateSequencePlugin());
|
|
781
800
|
pluginManager.add(Plugins.RandomPlugin());
|
package/src/types.plugins.ts
CHANGED
|
@@ -22,6 +22,10 @@ export interface PluginHost {
|
|
|
22
22
|
getSectionText(name: string): string | null;
|
|
23
23
|
getPassageText(name: string): string | null;
|
|
24
24
|
processText: (text: string, inline: boolean) => string;
|
|
25
|
+
// Rewrites asset URLs (src, srcset, poster, style url()) in already-rendered HTML using the
|
|
26
|
+
// resolveAssetUrl init option. Use this for HTML a plugin inserts without going through
|
|
27
|
+
// processText. Returns the HTML unchanged if no resolveAssetUrl was given.
|
|
28
|
+
resolveAssetsInHtml: (html: string) => string;
|
|
25
29
|
addTransition: (fn: () => Promise<void>) => void;
|
|
26
30
|
animation: Animation;
|
|
27
31
|
on<E extends keyof SquiffyEventMap>(
|
package/src/types.ts
CHANGED
|
@@ -7,6 +7,13 @@ export interface SquiffyInitOptions {
|
|
|
7
7
|
persist?: boolean,
|
|
8
8
|
storyId?: string,
|
|
9
9
|
onSet?: (attribute: string, value: any) => void,
|
|
10
|
+
/**
|
|
11
|
+
* Maps an asset path used in the story (e.g. "cat.png" in an img src) to the URL it should be
|
|
12
|
+
* loaded from. Applied to src, srcset and poster attributes, and url() in inline styles, of
|
|
13
|
+
* rendered output. Absolute URLs (http:, https:, data:, blob: etc. and protocol-relative) are
|
|
14
|
+
* left alone. When not provided, paths are used as they are.
|
|
15
|
+
*/
|
|
16
|
+
resolveAssetUrl?: (path: string) => string,
|
|
10
17
|
}
|
|
11
18
|
|
|
12
19
|
export interface SquiffySettings {
|
|
@@ -15,8 +22,17 @@ export interface SquiffySettings {
|
|
|
15
22
|
onSet: (attribute: string, value: any) => void,
|
|
16
23
|
}
|
|
17
24
|
|
|
25
|
+
// Where to start a story, instead of its start section or a saved game
|
|
26
|
+
export interface StartAt {
|
|
27
|
+
section: string;
|
|
28
|
+
// A passage in that section, shown after the section
|
|
29
|
+
passage?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
18
32
|
export interface SquiffyApi {
|
|
19
|
-
|
|
33
|
+
// Starts the story: from startAt if it's given (ignoring any saved game), otherwise from the
|
|
34
|
+
// saved game, or the story's start section.
|
|
35
|
+
begin: (startAt?: StartAt) => Promise<void>;
|
|
20
36
|
restart: () => void;
|
|
21
37
|
get: (attribute: string) => any;
|
|
22
38
|
set: (attribute: string, value: any) => void;
|
package/vite.config.ts
CHANGED
|
@@ -39,10 +39,9 @@ export default defineConfig({
|
|
|
39
39
|
closeBundle() {
|
|
40
40
|
try {
|
|
41
41
|
mkdirSync("dist", { recursive: true });
|
|
42
|
-
|
|
43
|
-
resolve("src
|
|
44
|
-
|
|
45
|
-
);
|
|
42
|
+
for (const file of ["squiffy.runtime.css", "squiffy.page.css"]) {
|
|
43
|
+
copyFileSync(resolve("src", file), resolve("dist", file));
|
|
44
|
+
}
|
|
46
45
|
} catch (err) {
|
|
47
46
|
console.error("Failed to copy CSS file:", err);
|
|
48
47
|
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// Builds the preview page (src/preview.main.ts) as a single self-contained HTML file,
|
|
2
|
+
// dist/squiffy.preview.html, which hosts load into a sandboxed iframe with srcdoc or use as a
|
|
3
|
+
// VS Code webview's html. It's self-contained because a sandboxed frame has an opaque origin,
|
|
4
|
+
// so it can't rely on loading anything from the host's server. Also builds the host helper,
|
|
5
|
+
// dist/preview-host.js, and dist/preview-html.js, which exports the page as a string.
|
|
6
|
+
// See preview-protocol.ts, and #307 for the spike results behind this.
|
|
7
|
+
import { defineConfig } from "vite";
|
|
8
|
+
import { build } from "esbuild";
|
|
9
|
+
import { readFileSync, rmSync, writeFileSync } from "fs";
|
|
10
|
+
import { resolve } from "path";
|
|
11
|
+
|
|
12
|
+
const template = (css: string, js: string) => `<!doctype html>
|
|
13
|
+
<html lang="en">
|
|
14
|
+
<head>
|
|
15
|
+
<meta charset="utf-8">
|
|
16
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
17
|
+
<title>Squiffy preview</title>
|
|
18
|
+
<style>
|
|
19
|
+
${css}
|
|
20
|
+
/* The preview scrolls the story within the frame, with the text centred as on the exported page */
|
|
21
|
+
html, body { height: 100%; }
|
|
22
|
+
div#squiffy-container { max-width: none; height: 100%; padding: 0; }
|
|
23
|
+
#squiffy, .squiffy-preview-story, .squiffy-preview-story > div { height: 100%; box-sizing: border-box; }
|
|
24
|
+
.squiffy-preview-story > div { padding: 1em max(1.25rem, calc((100% - var(--squiffy-max-width)) / 2)); }
|
|
25
|
+
</style>
|
|
26
|
+
</head>
|
|
27
|
+
<body>
|
|
28
|
+
<div id="squiffy-container"><div id="squiffy"></div></div>
|
|
29
|
+
<script>
|
|
30
|
+
${js.replace(/<\/(script)/gi, "<\\/$1")}
|
|
31
|
+
</script>
|
|
32
|
+
</body>
|
|
33
|
+
</html>
|
|
34
|
+
`;
|
|
35
|
+
|
|
36
|
+
export default defineConfig({
|
|
37
|
+
build: {
|
|
38
|
+
target: "es2020",
|
|
39
|
+
minify: "esbuild",
|
|
40
|
+
emptyOutDir: false,
|
|
41
|
+
outDir: "dist/preview-build",
|
|
42
|
+
lib: {
|
|
43
|
+
entry: "src/preview.main.ts",
|
|
44
|
+
name: "squiffyPreview",
|
|
45
|
+
formats: ["iife"],
|
|
46
|
+
fileName: () => "squiffy.preview.js",
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
plugins: [
|
|
50
|
+
{
|
|
51
|
+
name: "inline-preview-page",
|
|
52
|
+
async closeBundle() {
|
|
53
|
+
const js = readFileSync(resolve("dist/preview-build/squiffy.preview.js"), "utf8");
|
|
54
|
+
// The same styles as an exported story's page
|
|
55
|
+
const css = readFileSync(resolve("src/squiffy.runtime.css"), "utf8") + "\n"
|
|
56
|
+
+ readFileSync(resolve("src/squiffy.page.css"), "utf8");
|
|
57
|
+
const html = template(css, js);
|
|
58
|
+
writeFileSync(resolve("dist/squiffy.preview.html"), html);
|
|
59
|
+
writeFileSync(resolve("dist/preview-html.js"),
|
|
60
|
+
`// The Squiffy preview page, built from squiffy-runtime's src/preview.main.ts\nexport const previewHtml = ${JSON.stringify(html)};\n`);
|
|
61
|
+
writeFileSync(resolve("dist/preview-html.d.ts"),
|
|
62
|
+
"/** The Squiffy preview page, as HTML to use with setUpPreviewFrame() from squiffy-runtime/preview-host. */\nexport declare const previewHtml: string;\n");
|
|
63
|
+
rmSync(resolve("dist/preview-build"), { recursive: true, force: true });
|
|
64
|
+
|
|
65
|
+
await build({
|
|
66
|
+
entryPoints: [resolve("src/preview-host.ts")],
|
|
67
|
+
bundle: true,
|
|
68
|
+
format: "esm",
|
|
69
|
+
target: "es2020",
|
|
70
|
+
outfile: resolve("dist/preview-host.js"),
|
|
71
|
+
sourcemap: true,
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
});
|