squiffy-runtime 7.0.0-beta.1 → 7.0.0-beta.3
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/preview-host.d.ts +3 -2
- package/dist/preview-host.js +6 -0
- package/dist/preview-host.js.map +2 -2
- package/dist/preview-html.js +1 -1
- package/dist/preview-protocol.d.ts +4 -0
- package/dist/squiffy.page.css +120 -0
- package/dist/squiffy.preview.html +275 -93
- package/dist/squiffy.runtime.css +76 -20
- package/dist/squiffy.runtime.global.js +67 -64
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +3250 -3157
- package/dist/squiffy.runtime.js.map +1 -1
- package/package.json +8 -6
- package/src/preview-host.test.ts +17 -0
- package/src/preview-host.ts +11 -2
- package/src/preview-protocol.ts +7 -1
- package/src/preview.test.ts +14 -1
- package/src/preview.ts +6 -0
- package/src/squiffy.page.css +120 -0
- package/src/squiffy.runtime.css +76 -20
- package/src/squiffy.runtime.test.ts +30 -0
- package/src/squiffy.runtime.ts +18 -0
- package/vite.config.ts +3 -4
- package/vite.preview.config.ts +9 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "squiffy-runtime",
|
|
3
|
-
"version": "7.0.0-beta.
|
|
3
|
+
"version": "7.0.0-beta.3",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/textadventures/squiffy.git",
|
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
"./squiffy.runtime.css": "./dist/squiffy.runtime.css",
|
|
18
18
|
"./dist/squiffy.runtime.global.js": "./dist/squiffy.runtime.global.js",
|
|
19
19
|
"./dist/squiffy.runtime.css": "./dist/squiffy.runtime.css",
|
|
20
|
+
"./squiffy.page.css": "./dist/squiffy.page.css",
|
|
21
|
+
"./dist/squiffy.page.css": "./dist/squiffy.page.css",
|
|
20
22
|
"./preview-host": {
|
|
21
23
|
"types": "./dist/preview-host.d.ts",
|
|
22
24
|
"default": "./dist/preview-host.js"
|
|
@@ -46,16 +48,16 @@
|
|
|
46
48
|
"css.escape": "^1.5.1",
|
|
47
49
|
"esbuild": "^0.28.2",
|
|
48
50
|
"global-jsdom": "^31.0.0",
|
|
49
|
-
"jsdom": "^31.0.
|
|
50
|
-
"squiffy-compiler": "^7.0.0-beta.
|
|
51
|
+
"jsdom": "^31.0.1",
|
|
52
|
+
"squiffy-compiler": "^7.0.0-beta.3",
|
|
51
53
|
"typescript": "^6.0.3",
|
|
52
|
-
"vite": "^8.3.
|
|
54
|
+
"vite": "^8.3.8",
|
|
53
55
|
"vite-plugin-dts": "^5.1.2"
|
|
54
56
|
},
|
|
55
57
|
"dependencies": {
|
|
56
58
|
"animejs": "^4.5.0",
|
|
57
59
|
"handlebars": "^4.7.10",
|
|
58
|
-
"marked": "^18.
|
|
60
|
+
"marked": "^18.2.0"
|
|
59
61
|
},
|
|
60
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "016f53cc52dc2cf504787ef8329740ca2cf8d481"
|
|
61
63
|
}
|
package/src/preview-host.test.ts
CHANGED
|
@@ -40,6 +40,23 @@ describe("preview host", () => {
|
|
|
40
40
|
]);
|
|
41
41
|
});
|
|
42
42
|
|
|
43
|
+
test("sends the theme when the page is ready, and again if it reloads", () => {
|
|
44
|
+
const { channel, sent, receive } = fakeChannel();
|
|
45
|
+
const host = createPreviewHost(channel);
|
|
46
|
+
host.setTheme("light");
|
|
47
|
+
host.setTheme("dark");
|
|
48
|
+
expect(sent).toEqual([]);
|
|
49
|
+
|
|
50
|
+
receive({ channel: PREVIEW_CHANNEL, type: "ready" });
|
|
51
|
+
expect(sent).toEqual([{ channel: PREVIEW_CHANNEL, type: "theme", theme: "dark" }]);
|
|
52
|
+
|
|
53
|
+
host.setTheme("system");
|
|
54
|
+
expect(sent.slice(1)).toEqual([{ channel: PREVIEW_CHANNEL, type: "theme", theme: "system" }]);
|
|
55
|
+
|
|
56
|
+
receive({ channel: PREVIEW_CHANNEL, type: "ready" });
|
|
57
|
+
expect(sent.slice(2)).toEqual([{ channel: PREVIEW_CHANNEL, type: "theme", theme: "system" }]);
|
|
58
|
+
});
|
|
59
|
+
|
|
43
60
|
test("passes messages from the page to handlers, ignoring anything else", () => {
|
|
44
61
|
const { channel, receive } = fakeChannel();
|
|
45
62
|
const handlers = {
|
package/src/preview-host.ts
CHANGED
|
@@ -12,10 +12,10 @@
|
|
|
12
12
|
// await preview.load({ js: await result.getJs() }, { startAt: { section: "kitchen" } });
|
|
13
13
|
|
|
14
14
|
import {
|
|
15
|
-
Envelope, HostMessage, isEnvelope, PREVIEW_CHANNEL, PreviewAssets, PreviewMessage, PreviewStory, StartAt,
|
|
15
|
+
Envelope, HostMessage, isEnvelope, PREVIEW_CHANNEL, PreviewAssets, PreviewMessage, PreviewStory, PreviewTheme, StartAt,
|
|
16
16
|
} from "./preview-protocol.js";
|
|
17
17
|
|
|
18
|
-
export type { HostMessage, PreviewAssets, PreviewMessage, PreviewStory, StartAt } from "./preview-protocol.js";
|
|
18
|
+
export type { HostMessage, PreviewAssets, PreviewMessage, PreviewStory, PreviewTheme, StartAt } from "./preview-protocol.js";
|
|
19
19
|
export { PREVIEW_CHANNEL } from "./preview-protocol.js";
|
|
20
20
|
|
|
21
21
|
// How the host talks to the preview page
|
|
@@ -45,6 +45,8 @@ export interface PreviewHost {
|
|
|
45
45
|
back(): void;
|
|
46
46
|
// Sets the assets the story can use. Send these before load().
|
|
47
47
|
setAssets(assets: PreviewAssets): void;
|
|
48
|
+
// Shows the story in light or dark mode. It's kept if the page reloads.
|
|
49
|
+
setTheme(theme: PreviewTheme): void;
|
|
48
50
|
dispose(): void;
|
|
49
51
|
}
|
|
50
52
|
|
|
@@ -73,6 +75,7 @@ export function iframeChannel(iframe: HTMLIFrameElement): PreviewChannel {
|
|
|
73
75
|
export function createPreviewHost(channel: PreviewChannel, handlers: PreviewHandlers = {}): PreviewHost {
|
|
74
76
|
let isReady = false;
|
|
75
77
|
let queue: HostMessage[] = [];
|
|
78
|
+
let theme: PreviewTheme | undefined;
|
|
76
79
|
let resolveReady: () => void;
|
|
77
80
|
const ready = new Promise<void>(resolve => { resolveReady = resolve; });
|
|
78
81
|
|
|
@@ -89,6 +92,7 @@ export function createPreviewHost(channel: PreviewChannel, handlers: PreviewHand
|
|
|
89
92
|
// Sent again if the page reloads, which loses its story, so onReady is
|
|
90
93
|
// where a host can load it again
|
|
91
94
|
isReady = true;
|
|
95
|
+
if (theme) send({ type: "theme", theme });
|
|
92
96
|
const queued = queue;
|
|
93
97
|
queue = [];
|
|
94
98
|
for (const m of queued) send(m);
|
|
@@ -121,6 +125,11 @@ export function createPreviewHost(channel: PreviewChannel, handlers: PreviewHand
|
|
|
121
125
|
restart: () => send({ type: "restart" }),
|
|
122
126
|
back: () => send({ type: "back" }),
|
|
123
127
|
setAssets: assets => send({ type: "assets", assets }),
|
|
128
|
+
setTheme: value => {
|
|
129
|
+
theme = value;
|
|
130
|
+
// Sent when the page is ready, and again if it reloads
|
|
131
|
+
if (isReady) send({ type: "theme", theme });
|
|
132
|
+
},
|
|
124
133
|
dispose: () => {
|
|
125
134
|
stop();
|
|
126
135
|
queue = [];
|
package/src/preview-protocol.ts
CHANGED
|
@@ -27,6 +27,9 @@ export interface PreviewAssets {
|
|
|
27
27
|
urls?: Record<string, string>;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
// "system" follows the reader's system setting, like an exported story
|
|
31
|
+
export type PreviewTheme = "light" | "dark" | "system";
|
|
32
|
+
|
|
30
33
|
export type HostMessage =
|
|
31
34
|
// Starts a story, replacing any that's running. Send assets first.
|
|
32
35
|
| { type: "load"; story: PreviewStory; startAt?: StartAt }
|
|
@@ -35,7 +38,10 @@ export type HostMessage =
|
|
|
35
38
|
| { type: "restart" }
|
|
36
39
|
| { type: "back" }
|
|
37
40
|
// Replaces the assets the story can use
|
|
38
|
-
| { type: "assets"; assets: PreviewAssets }
|
|
41
|
+
| { type: "assets"; assets: PreviewAssets }
|
|
42
|
+
// Shows the story in light or dark mode, e.g. to match the host's own theme. A story's own
|
|
43
|
+
// CSS can still override it, e.g. with ":root { color-scheme: light; }".
|
|
44
|
+
| { type: "theme"; theme: PreviewTheme };
|
|
39
45
|
|
|
40
46
|
export type PreviewMessage =
|
|
41
47
|
// The page has loaded and is listening
|
package/src/preview.test.ts
CHANGED
|
@@ -144,7 +144,20 @@ describe("preview", () => {
|
|
|
144
144
|
const js = await compileJs(script);
|
|
145
145
|
await preview.receive({ type: "load", story: { js, css: ["body { color: red; }"] } });
|
|
146
146
|
await preview.receive({ type: "load", story: { js, css: ["body { color: blue; }"] } });
|
|
147
|
-
|
|
147
|
+
// After the preview's own (empty) theme style
|
|
148
|
+
expect([...document.head.querySelectorAll("style")].map(s => s.textContent)).toEqual(["", "body { color: blue; }"]);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("sets the theme before the story's stylesheets, so they can override it", async () => {
|
|
152
|
+
const { preview } = setUp();
|
|
153
|
+
const js = await compileJs(script);
|
|
154
|
+
await preview.receive({ type: "load", story: { js, css: [":root { color-scheme: light; }"] } });
|
|
155
|
+
await preview.receive({ type: "theme", theme: "dark" });
|
|
156
|
+
const styles = () => [...document.head.querySelectorAll("style")].map(s => s.textContent);
|
|
157
|
+
expect(styles()).toEqual([":root { color-scheme: dark; }", ":root { color-scheme: light; }"]);
|
|
158
|
+
|
|
159
|
+
await preview.receive({ type: "theme", theme: "system" });
|
|
160
|
+
expect(styles()[0]).toBe("");
|
|
148
161
|
});
|
|
149
162
|
|
|
150
163
|
test("reports errors", async () => {
|
package/src/preview.ts
CHANGED
|
@@ -52,6 +52,9 @@ export function createPreview(options: PreviewOptions) {
|
|
|
52
52
|
const assetUrls = new Map<string, string>();
|
|
53
53
|
const objectUrls: string[] = [];
|
|
54
54
|
const injected: Element[] = [];
|
|
55
|
+
// Sets the theme. It's added before any of the story's own stylesheets, so they can override it.
|
|
56
|
+
const themeStyle = document.createElement("style");
|
|
57
|
+
document.head.appendChild(themeStyle);
|
|
55
58
|
|
|
56
59
|
const send = (message: PreviewMessage) => {
|
|
57
60
|
try {
|
|
@@ -152,6 +155,9 @@ export function createPreview(options: PreviewOptions) {
|
|
|
152
155
|
case "assets":
|
|
153
156
|
setAssets(message.assets);
|
|
154
157
|
break;
|
|
158
|
+
case "theme":
|
|
159
|
+
themeStyle.textContent = message.theme == "system" ? "" : `:root { color-scheme: ${message.theme}; }`;
|
|
160
|
+
break;
|
|
155
161
|
}
|
|
156
162
|
}
|
|
157
163
|
catch (error) {
|
|
@@ -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
|
}
|
|
@@ -470,6 +470,36 @@ Three.
|
|
|
470
470
|
expect(visibleText(element)).toBe("Two. a three");
|
|
471
471
|
});
|
|
472
472
|
|
|
473
|
+
test("Clearing the screen scrolls up to the top of the output", async () => {
|
|
474
|
+
// https://github.com/textadventures/squiffy/issues/54
|
|
475
|
+
const script = `
|
|
476
|
+
[[start]]:
|
|
477
|
+
Start. [[two]]
|
|
478
|
+
|
|
479
|
+
[[two]]:
|
|
480
|
+
Two. [[three]]
|
|
481
|
+
|
|
482
|
+
[[three]]:
|
|
483
|
+
@clear
|
|
484
|
+
Three.
|
|
485
|
+
`;
|
|
486
|
+
const { squiffyApi, element } = await initScript(script);
|
|
487
|
+
|
|
488
|
+
// The reader has scrolled down to 500px, and the output starts 20px from the top of the page
|
|
489
|
+
Object.defineProperty(document.documentElement, "scrollTop", { configurable: true, get: () => 500 });
|
|
490
|
+
(element.firstElementChild as HTMLElement).getBoundingClientRect = () => ({ top: -480 }) as DOMRect;
|
|
491
|
+
const scrollTo = vi.fn();
|
|
492
|
+
window.scrollTo = scrollTo;
|
|
493
|
+
|
|
494
|
+
// No clear, so no scrolling up
|
|
495
|
+
await squiffyApi.clickLink(findLink(element, "section", "two"));
|
|
496
|
+
expect(scrollTo).not.toHaveBeenCalled();
|
|
497
|
+
|
|
498
|
+
await squiffyApi.clickLink(findLink(element, "section", "three"));
|
|
499
|
+
expect(scrollTo).toHaveBeenCalledTimes(1);
|
|
500
|
+
expect(scrollTo).toHaveBeenCalledWith({ top: 20, behavior: "smooth" });
|
|
501
|
+
});
|
|
502
|
+
|
|
473
503
|
test("Run JavaScript functions", async () => {
|
|
474
504
|
const script = `
|
|
475
505
|
document.getElementById('test').innerText = 'Initial JavaScript';
|
package/src/squiffy.runtime.ts
CHANGED
|
@@ -21,6 +21,8 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
|
|
|
21
21
|
let currentPassageElement: HTMLElement | null;
|
|
22
22
|
let currentBlockOutputElement: HTMLElement | null;
|
|
23
23
|
let scrollPosition = 0;
|
|
24
|
+
// Set when the screen is cleared, so the next scroll goes up to the top of the new screen (#54)
|
|
25
|
+
let scrollToTop = false;
|
|
24
26
|
const emitter = new Emitter<SquiffyEventMap>();
|
|
25
27
|
const transitions: (() => Promise<void>)[] = [];
|
|
26
28
|
let runningTransitions = false;
|
|
@@ -461,6 +463,7 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
|
|
|
461
463
|
|
|
462
464
|
const clearStackItem = document.createElement("div");
|
|
463
465
|
clearStack.appendChild(clearStackItem);
|
|
466
|
+
scrollToTop = true;
|
|
464
467
|
|
|
465
468
|
// Move everything in the outputElement (except the clearStack itself) into the new clearStackItem
|
|
466
469
|
for (const child of [...outputElement.children]) {
|
|
@@ -528,6 +531,21 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
|
|
|
528
531
|
if (settings.scroll === "none") {
|
|
529
532
|
// do nothing
|
|
530
533
|
}
|
|
534
|
+
else if (scrollToTop) {
|
|
535
|
+
// The screen was cleared, so the new text starts at the top of the output, which may be
|
|
536
|
+
// above where the reader has scrolled to.
|
|
537
|
+
scrollToTop = false;
|
|
538
|
+
if (settings.scroll === "element") {
|
|
539
|
+
currentSectionElement.scrollIntoView({ block: "start", inline: "nearest", behavior: "smooth" });
|
|
540
|
+
}
|
|
541
|
+
else {
|
|
542
|
+
const currentScrollTop = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
|
|
543
|
+
const outputTop = outputElement.getBoundingClientRect().top + currentScrollTop;
|
|
544
|
+
if (outputTop < currentScrollTop) {
|
|
545
|
+
window.scrollTo({ top: outputTop, behavior: "smooth" });
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
531
549
|
else if (settings.scroll === "element") {
|
|
532
550
|
outputElement.lastElementChild?.scrollIntoView({ block: "end", inline: "nearest", behavior: "smooth" });
|
|
533
551
|
}
|
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
|
}
|
package/vite.preview.config.ts
CHANGED
|
@@ -16,14 +16,16 @@ const template = (css: string, js: string) => `<!doctype html>
|
|
|
16
16
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
17
17
|
<title>Squiffy preview</title>
|
|
18
18
|
<style>
|
|
19
|
-
html, body { height: 100%; margin: 0; }
|
|
20
|
-
#squiffy, .squiffy-preview-story, .squiffy-preview-story > div { height: 100%; box-sizing: border-box; }
|
|
21
|
-
.squiffy-preview-story > div { padding: 1em; }
|
|
22
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)); }
|
|
23
25
|
</style>
|
|
24
26
|
</head>
|
|
25
27
|
<body>
|
|
26
|
-
<div id="squiffy"></div>
|
|
28
|
+
<div id="squiffy-container"><div id="squiffy"></div></div>
|
|
27
29
|
<script>
|
|
28
30
|
${js.replace(/<\/(script)/gi, "<\\/$1")}
|
|
29
31
|
</script>
|
|
@@ -49,7 +51,9 @@ export default defineConfig({
|
|
|
49
51
|
name: "inline-preview-page",
|
|
50
52
|
async closeBundle() {
|
|
51
53
|
const js = readFileSync(resolve("dist/preview-build/squiffy.preview.js"), "utf8");
|
|
52
|
-
|
|
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");
|
|
53
57
|
const html = template(css, js);
|
|
54
58
|
writeFileSync(resolve("dist/squiffy.preview.html"), html);
|
|
55
59
|
writeFileSync(resolve("dist/preview-html.js"),
|