squiffy-runtime 7.0.0-beta.4 → 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.
- package/dist/events.d.ts +1 -0
- package/dist/panels.d.ts +14 -0
- package/dist/panels.test.d.ts +1 -0
- package/dist/preview-host.d.ts +2 -1
- package/dist/preview-host.js +4 -1
- 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/saveSlots.d.ts +16 -0
- package/dist/saves.test.d.ts +1 -0
- package/dist/savesDialog.d.ts +43 -0
- package/dist/savesDialog.test.d.ts +1 -0
- package/dist/squiffy.page.css +46 -22
- package/dist/squiffy.preview.html +612 -109
- package/dist/squiffy.runtime.css +476 -9
- package/dist/squiffy.runtime.d.ts +9 -1
- package/dist/squiffy.runtime.global.js +113 -72
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +4146 -3460
- package/dist/squiffy.runtime.js.map +1 -1
- package/dist/state.d.ts +4 -0
- package/dist/textProcessor.d.ts +5 -0
- package/dist/types.d.ts +32 -0
- package/package.json +3 -3
- package/src/__snapshots__/squiffy.runtime.test.ts.snap +21 -21
- package/src/events.ts +1 -0
- package/src/panels.test.ts +201 -0
- package/src/panels.ts +187 -0
- package/src/plugins/plugins.test.ts +42 -0
- package/src/plugins/replaceLabel.ts +6 -2
- package/src/preview-host.test.ts +5 -1
- package/src/preview-host.ts +9 -3
- package/src/preview-protocol.ts +6 -2
- package/src/preview.test.ts +71 -3
- package/src/preview.ts +74 -13
- package/src/saveSlots.ts +159 -0
- package/src/saves.test.ts +199 -0
- package/src/savesDialog.test.ts +290 -0
- package/src/savesDialog.ts +499 -0
- package/src/squiffy.page.css +46 -22
- package/src/squiffy.runtime.css +476 -9
- package/src/squiffy.runtime.test.ts +890 -4
- package/src/squiffy.runtime.ts +322 -17
- package/src/state.ts +34 -4
- package/src/textProcessor.ts +76 -9
- package/src/types.ts +57 -1
- package/vite.preview.config.ts +7 -0
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
import type { SavedGame, SquiffyApi } from "./types.js";
|
|
2
|
+
import { indexedDbSaveSlots, memorySaveSlots, type SaveSlot, type SaveSlots } from "./saveSlots.js";
|
|
3
|
+
|
|
4
|
+
// The dialog's text in English. The published page passes in squiffy.json's "strings", which
|
|
5
|
+
// use the same names (see PAGE_STRINGS in the compiler). [name] and [time] are filled in.
|
|
6
|
+
export const SAVES_STRINGS = {
|
|
7
|
+
saves: "Saved games",
|
|
8
|
+
saveName: "Name (optional)",
|
|
9
|
+
save: "Save",
|
|
10
|
+
noSaves: "No saved games yet.",
|
|
11
|
+
savedGame: "Saved game",
|
|
12
|
+
savedToday: "Today, [time]",
|
|
13
|
+
savedYesterday: "Yesterday, [time]",
|
|
14
|
+
load: "Load",
|
|
15
|
+
loadConfirm: "Load \"[name]\"? You'll lose anything you've done since you last saved.",
|
|
16
|
+
saveOptions: "Options for [name]",
|
|
17
|
+
overwrite: "Overwrite",
|
|
18
|
+
overwriteConfirm: "Overwrite \"[name]\"?",
|
|
19
|
+
rename: "Rename",
|
|
20
|
+
newName: "New name",
|
|
21
|
+
saveToFile: "Save to file",
|
|
22
|
+
loadFromFile: "Load from file",
|
|
23
|
+
delete: "Delete",
|
|
24
|
+
deleteConfirm: "Delete \"[name]\"? This can't be undone.",
|
|
25
|
+
cancel: "Cancel",
|
|
26
|
+
close: "Close",
|
|
27
|
+
wrongStory: "This saved game is for a different story.",
|
|
28
|
+
notASave: "This file isn't a saved game.",
|
|
29
|
+
cantSave: "The game couldn't be saved. Try Save to file instead.",
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type SavesStringName = keyof typeof SAVES_STRINGS;
|
|
33
|
+
|
|
34
|
+
export interface SavesDialogOptions {
|
|
35
|
+
// Where the player's saves are kept. By default, in the browser's IndexedDB under `key`.
|
|
36
|
+
slots?: SaveSlots;
|
|
37
|
+
// Which story the saves are for, when `slots` isn't given: the page's path by default
|
|
38
|
+
key?: string;
|
|
39
|
+
// The dialog's text, for a story in another language. Anything left out is in English.
|
|
40
|
+
strings?: Partial<Record<string, string>>;
|
|
41
|
+
// The name Save to file gives the file, before ".squiffy-save": the page's title by default
|
|
42
|
+
fileName?: string;
|
|
43
|
+
// Where the dialog goes in the page: the end of the body by default
|
|
44
|
+
container?: HTMLElement;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface SavesDialog {
|
|
48
|
+
element: HTMLDialogElement;
|
|
49
|
+
open: () => Promise<void>;
|
|
50
|
+
close: () => void;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const SAVE_FILE_EXTENSION = ".squiffy-save";
|
|
54
|
+
|
|
55
|
+
const ICON_CLOSE = "<svg viewBox=\"0 0 24 24\" aria-hidden=\"true\"><path d=\"M18 6 6 18M6 6l12 12\"/></svg>";
|
|
56
|
+
const ICON_OPTIONS = "<svg viewBox=\"0 0 24 24\" aria-hidden=\"true\"><circle cx=\"5\" cy=\"12\" r=\"1\"/><circle cx=\"12\" cy=\"12\" r=\"1\"/><circle cx=\"19\" cy=\"12\" r=\"1\"/></svg>";
|
|
57
|
+
const ICON_DOWNLOAD = "<svg viewBox=\"0 0 24 24\" aria-hidden=\"true\"><path d=\"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4M7 10l5 5 5-5M12 15V3\"/></svg>";
|
|
58
|
+
const ICON_UPLOAD = "<svg viewBox=\"0 0 24 24\" aria-hidden=\"true\"><path d=\"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4M17 8l-5-5-5 5M12 3v12\"/></svg>";
|
|
59
|
+
|
|
60
|
+
let dialogCount = 0;
|
|
61
|
+
|
|
62
|
+
// "Today, 12:57", "Yesterday, 21:14", or the date and time for anything older
|
|
63
|
+
export function formatSaveTime(savedAt: string, strings: Record<SavesStringName, string> = SAVES_STRINGS, now = new Date()) {
|
|
64
|
+
const date = new Date(savedAt);
|
|
65
|
+
if (isNaN(date.getTime())) return "";
|
|
66
|
+
const time = date.toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit" });
|
|
67
|
+
const startOfDay = (d: Date) => new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
|
|
68
|
+
const daysAgo = Math.round((startOfDay(now) - startOfDay(date)) / 86400000);
|
|
69
|
+
if (daysAgo === 0) return strings.savedToday.replace("[time]", time);
|
|
70
|
+
if (daysAgo === 1) return strings.savedYesterday.replace("[time]", time);
|
|
71
|
+
const day = date.toLocaleDateString(undefined, {
|
|
72
|
+
day: "numeric", month: "short",
|
|
73
|
+
...(date.getFullYear() !== now.getFullYear() ? { year: "numeric" } : {}),
|
|
74
|
+
});
|
|
75
|
+
return `${day}, ${time}`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// The Saved games dialog: saves the game into a list of saves, which the player can load,
|
|
79
|
+
// overwrite, rename, save to a file or delete, and saves to and loads from a file. It's added to
|
|
80
|
+
// the page hidden, and open() shows it.
|
|
81
|
+
export function createSavesDialog(api: SquiffyApi, options: SavesDialogOptions = {}): SavesDialog {
|
|
82
|
+
const strings: Record<SavesStringName, string> = { ...SAVES_STRINGS };
|
|
83
|
+
for (const name of Object.keys(SAVES_STRINGS) as SavesStringName[]) {
|
|
84
|
+
const value = options.strings?.[name];
|
|
85
|
+
if (typeof value == "string") strings[name] = value;
|
|
86
|
+
}
|
|
87
|
+
const slots = options.slots ?? (typeof indexedDB != "undefined"
|
|
88
|
+
? indexedDbSaveSlots(options.key ?? window.location.pathname)
|
|
89
|
+
: memorySaveSlots());
|
|
90
|
+
const id = `squiffy-saves-${++dialogCount}`;
|
|
91
|
+
|
|
92
|
+
const dialog = document.createElement("dialog");
|
|
93
|
+
dialog.className = "squiffy-saves";
|
|
94
|
+
dialog.setAttribute("aria-labelledby", `${id}-heading`);
|
|
95
|
+
dialog.innerHTML = `
|
|
96
|
+
<div class="squiffy-saves-header">
|
|
97
|
+
<h2 id="${id}-heading"></h2>
|
|
98
|
+
<button type="button" class="squiffy-saves-icon-button" data-close>${ICON_CLOSE}</button>
|
|
99
|
+
</div>
|
|
100
|
+
<p class="squiffy-saves-error" role="alert" hidden></p>
|
|
101
|
+
<form class="squiffy-saves-new">
|
|
102
|
+
<input type="text" autocomplete="off">
|
|
103
|
+
<button type="submit" class="squiffy-saves-button squiffy-saves-primary"></button>
|
|
104
|
+
</form>
|
|
105
|
+
<ul class="squiffy-saves-list"></ul>
|
|
106
|
+
<div class="squiffy-saves-footer">
|
|
107
|
+
<button type="button" class="squiffy-saves-link" data-save-to-file>${ICON_DOWNLOAD}<span></span></button>
|
|
108
|
+
<button type="button" class="squiffy-saves-link" data-load-from-file>${ICON_UPLOAD}<span></span></button>
|
|
109
|
+
<input type="file" accept="${SAVE_FILE_EXTENSION},.json,application/json" hidden>
|
|
110
|
+
</div>
|
|
111
|
+
<div class="squiffy-saves-footer-confirm"></div>`;
|
|
112
|
+
|
|
113
|
+
const $ = <T extends Element>(selector: string) => dialog.querySelector<T>(selector)!;
|
|
114
|
+
$("h2").textContent = strings.saves;
|
|
115
|
+
const closeButton = $<HTMLButtonElement>("[data-close]");
|
|
116
|
+
closeButton.title = strings.close;
|
|
117
|
+
closeButton.setAttribute("aria-label", strings.close);
|
|
118
|
+
const error = $<HTMLParagraphElement>(".squiffy-saves-error");
|
|
119
|
+
const form = $<HTMLFormElement>("form");
|
|
120
|
+
const nameInput = $<HTMLInputElement>("form input");
|
|
121
|
+
nameInput.placeholder = strings.saveName;
|
|
122
|
+
nameInput.setAttribute("aria-label", strings.saveName);
|
|
123
|
+
$("form button").textContent = strings.save;
|
|
124
|
+
const list = $<HTMLUListElement>(".squiffy-saves-list");
|
|
125
|
+
$("[data-save-to-file] span").textContent = strings.saveToFile;
|
|
126
|
+
$("[data-load-from-file] span").textContent = strings.loadFromFile;
|
|
127
|
+
const fileInput = $<HTMLInputElement>("input[type=file]");
|
|
128
|
+
const footerConfirm = $<HTMLDivElement>(".squiffy-saves-footer-confirm");
|
|
129
|
+
|
|
130
|
+
(options.container ?? document.body).appendChild(dialog);
|
|
131
|
+
|
|
132
|
+
// Whether the player has done anything since they last saved or loaded, which loading a save
|
|
133
|
+
// would lose. Squiffy saves automatically too, but that doesn't count: loading replaces it.
|
|
134
|
+
// That's anything since the start of the story (which may be a game restored from earlier),
|
|
135
|
+
// unless they've saved or loaded since. It's worked out when it's needed, as the dialog can be
|
|
136
|
+
// made before the story begins.
|
|
137
|
+
let savedOrLoaded = false;
|
|
138
|
+
api.on("navigated", () => { savedOrLoaded = false; });
|
|
139
|
+
const unsaved = () => !savedOrLoaded && api.canGoBack();
|
|
140
|
+
|
|
141
|
+
let saves: SaveSlot[] = [];
|
|
142
|
+
// Closes whatever's open inside the dialog (a question, a save's menu, or a rename), so
|
|
143
|
+
// Escape can close that before it closes the dialog
|
|
144
|
+
let cancelOpen: (() => void) | null = null;
|
|
145
|
+
|
|
146
|
+
const showError = (message: string) => {
|
|
147
|
+
error.textContent = message;
|
|
148
|
+
error.hidden = false;
|
|
149
|
+
};
|
|
150
|
+
const hideError = () => {
|
|
151
|
+
error.hidden = true;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const titleOf = (slot: SaveSlot) => slot.name || slot.save.summary || strings.savedGame;
|
|
155
|
+
|
|
156
|
+
const metaOf = (slot: SaveSlot) =>
|
|
157
|
+
[formatSaveTime(slot.save.savedAt, strings), slot.name && slot.save.summary].filter(Boolean).join(" · ");
|
|
158
|
+
|
|
159
|
+
const rowOf = (slotId: number) => list.querySelector<HTMLLIElement>(`[data-slot="${slotId}"]`);
|
|
160
|
+
|
|
161
|
+
function render() {
|
|
162
|
+
list.replaceChildren();
|
|
163
|
+
if (!saves.length) {
|
|
164
|
+
const empty = document.createElement("li");
|
|
165
|
+
empty.className = "squiffy-saves-empty";
|
|
166
|
+
empty.textContent = strings.noSaves;
|
|
167
|
+
list.appendChild(empty);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
for (const slot of saves) {
|
|
171
|
+
const title = titleOf(slot);
|
|
172
|
+
const row = document.createElement("li");
|
|
173
|
+
row.className = "squiffy-saves-row";
|
|
174
|
+
row.dataset.slot = String(slot.id);
|
|
175
|
+
row.innerHTML = `
|
|
176
|
+
<div class="squiffy-saves-line">
|
|
177
|
+
<div class="squiffy-saves-info">
|
|
178
|
+
<div class="squiffy-saves-title" id="${id}-title-${slot.id}"></div>
|
|
179
|
+
<div class="squiffy-saves-meta"></div>
|
|
180
|
+
</div>
|
|
181
|
+
<button type="button" class="squiffy-saves-button" data-action="load" aria-describedby="${id}-title-${slot.id}"></button>
|
|
182
|
+
<div class="squiffy-saves-options">
|
|
183
|
+
<button type="button" class="squiffy-saves-icon-button" data-action="options" aria-haspopup="menu" aria-expanded="false" aria-controls="${id}-menu-${slot.id}">${ICON_OPTIONS}</button>
|
|
184
|
+
<div class="squiffy-saves-menu" id="${id}-menu-${slot.id}" role="menu" hidden>
|
|
185
|
+
<button type="button" role="menuitem" data-action="overwrite"></button>
|
|
186
|
+
<button type="button" role="menuitem" data-action="rename"></button>
|
|
187
|
+
<button type="button" role="menuitem" data-action="download"></button>
|
|
188
|
+
<button type="button" role="menuitem" data-action="delete" class="squiffy-saves-danger"></button>
|
|
189
|
+
</div>
|
|
190
|
+
</div>
|
|
191
|
+
</div>`;
|
|
192
|
+
const q = (selector: string) => row.querySelector<HTMLElement>(selector)!;
|
|
193
|
+
q(".squiffy-saves-title").textContent = title;
|
|
194
|
+
const meta = metaOf(slot);
|
|
195
|
+
if (meta) q(".squiffy-saves-meta").textContent = meta;
|
|
196
|
+
else q(".squiffy-saves-meta").remove();
|
|
197
|
+
q("[data-action=load]").textContent = strings.load;
|
|
198
|
+
const options = q("[data-action=options]");
|
|
199
|
+
options.title = strings.saveOptions.replace("[name]", title);
|
|
200
|
+
options.setAttribute("aria-label", options.title);
|
|
201
|
+
q("[data-action=overwrite]").textContent = strings.overwrite;
|
|
202
|
+
q("[data-action=rename]").textContent = strings.rename;
|
|
203
|
+
q("[data-action=download]").textContent = strings.saveToFile;
|
|
204
|
+
q("[data-action=delete]").textContent = strings.delete;
|
|
205
|
+
list.appendChild(row);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async function refresh() {
|
|
210
|
+
try {
|
|
211
|
+
saves = await slots.list();
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
saves = [];
|
|
215
|
+
showError(strings.cantSave);
|
|
216
|
+
}
|
|
217
|
+
render();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Asks a question inside the dialog, under `container`, rather than with confirm(). Resolves
|
|
221
|
+
// true if the player goes ahead.
|
|
222
|
+
function confirmInDialog(container: HTMLElement, message: string, actionLabel: string, danger = false) {
|
|
223
|
+
cancelOpen?.();
|
|
224
|
+
const box = document.createElement("div");
|
|
225
|
+
box.className = "squiffy-saves-confirm";
|
|
226
|
+
box.setAttribute("role", "group");
|
|
227
|
+
box.innerHTML = `<p id="${id}-confirm"></p>
|
|
228
|
+
<div class="squiffy-saves-confirm-buttons">
|
|
229
|
+
<button type="button" class="squiffy-saves-button" data-answer="no"></button>
|
|
230
|
+
<button type="button" class="squiffy-saves-button ${danger ? "squiffy-saves-danger-button" : "squiffy-saves-primary"}" data-answer="yes" aria-describedby="${id}-confirm"></button>
|
|
231
|
+
</div>`;
|
|
232
|
+
box.setAttribute("aria-labelledby", `${id}-confirm`);
|
|
233
|
+
box.querySelector("p")!.textContent = message;
|
|
234
|
+
const no = box.querySelector<HTMLButtonElement>("[data-answer=no]")!;
|
|
235
|
+
const yes = box.querySelector<HTMLButtonElement>("[data-answer=yes]")!;
|
|
236
|
+
no.textContent = strings.cancel;
|
|
237
|
+
yes.textContent = actionLabel;
|
|
238
|
+
container.appendChild(box);
|
|
239
|
+
return new Promise<boolean>(resolve => {
|
|
240
|
+
const finish = (answer: boolean) => {
|
|
241
|
+
cancelOpen = null;
|
|
242
|
+
box.remove();
|
|
243
|
+
resolve(answer);
|
|
244
|
+
};
|
|
245
|
+
cancelOpen = () => finish(false);
|
|
246
|
+
no.addEventListener("click", () => finish(false));
|
|
247
|
+
yes.addEventListener("click", () => finish(true));
|
|
248
|
+
yes.focus();
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// A save's menu goes in the top layer as a popover, where the browser has them, so the list
|
|
253
|
+
// scrolling doesn't cut it off. It's under its ⋯ button, or above it if there's no room below.
|
|
254
|
+
const popovers = typeof HTMLElement.prototype.showPopover == "function";
|
|
255
|
+
|
|
256
|
+
function showMenu(menu: HTMLElement, button: HTMLElement) {
|
|
257
|
+
if (!popovers) {
|
|
258
|
+
menu.hidden = false;
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
menu.hidden = false;
|
|
262
|
+
menu.popover = "manual";
|
|
263
|
+
menu.showPopover();
|
|
264
|
+
const rect = button.getBoundingClientRect();
|
|
265
|
+
const { width, height } = menu.getBoundingClientRect();
|
|
266
|
+
const below = rect.bottom + 4;
|
|
267
|
+
menu.style.top = `${below + height > window.innerHeight - 8 ? Math.max(8, rect.top - 4 - height) : below}px`;
|
|
268
|
+
menu.style.left = `${Math.max(8, Math.min(rect.right - width, window.innerWidth - 8 - width))}px`;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function hideMenu(menu: HTMLElement) {
|
|
272
|
+
if (popovers && menu.matches(":popover-open")) menu.hidePopover();
|
|
273
|
+
menu.hidden = true;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// A save's menu, opened by its ⋯ button
|
|
277
|
+
function openMenu(button: HTMLElement) {
|
|
278
|
+
const menu = document.getElementById(button.getAttribute("aria-controls")!)!;
|
|
279
|
+
const wasOpen = button.getAttribute("aria-expanded") == "true";
|
|
280
|
+
cancelOpen?.();
|
|
281
|
+
if (wasOpen) return;
|
|
282
|
+
showMenu(menu, button);
|
|
283
|
+
button.setAttribute("aria-expanded", "true");
|
|
284
|
+
const items = [...menu.querySelectorAll<HTMLElement>("[role=menuitem]")];
|
|
285
|
+
items[0].focus();
|
|
286
|
+
|
|
287
|
+
const close = (focusButton: boolean) => {
|
|
288
|
+
cancelOpen = null;
|
|
289
|
+
hideMenu(menu);
|
|
290
|
+
button.setAttribute("aria-expanded", "false");
|
|
291
|
+
menu.removeEventListener("keydown", onKey);
|
|
292
|
+
dialog.removeEventListener("pointerdown", onPointerDown);
|
|
293
|
+
if (focusButton) button.focus();
|
|
294
|
+
};
|
|
295
|
+
const onKey = (e: KeyboardEvent) => {
|
|
296
|
+
const index = items.indexOf(document.activeElement as HTMLElement);
|
|
297
|
+
const next = ({ ArrowDown: index + 1, ArrowUp: index - 1, Home: 0, End: items.length - 1 } as Record<string, number>)[e.key];
|
|
298
|
+
if (next !== undefined) {
|
|
299
|
+
e.preventDefault();
|
|
300
|
+
items[(next + items.length) % items.length].focus();
|
|
301
|
+
}
|
|
302
|
+
else if (e.key == "Tab") {
|
|
303
|
+
close(false);
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
const onPointerDown = (e: PointerEvent) => {
|
|
307
|
+
const target = e.target as Node;
|
|
308
|
+
if (!menu.contains(target) && !button.contains(target)) close(false);
|
|
309
|
+
};
|
|
310
|
+
menu.addEventListener("keydown", onKey);
|
|
311
|
+
dialog.addEventListener("pointerdown", onPointerDown);
|
|
312
|
+
cancelOpen = () => close(true);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Turns a save's title into a text box. Enter or leaving the box keeps the new name (an empty
|
|
316
|
+
// one makes the save unnamed again), and Escape keeps the old one.
|
|
317
|
+
function startRename(slot: SaveSlot) {
|
|
318
|
+
const title = rowOf(slot.id)?.querySelector(".squiffy-saves-title");
|
|
319
|
+
if (!title) return;
|
|
320
|
+
const input = document.createElement("input");
|
|
321
|
+
input.type = "text";
|
|
322
|
+
input.className = "squiffy-saves-rename";
|
|
323
|
+
input.autocomplete = "off";
|
|
324
|
+
input.value = slot.name ?? "";
|
|
325
|
+
input.placeholder = titleOf(slot);
|
|
326
|
+
input.setAttribute("aria-label", strings.newName);
|
|
327
|
+
title.replaceWith(input);
|
|
328
|
+
input.focus();
|
|
329
|
+
input.select();
|
|
330
|
+
|
|
331
|
+
let finished = false;
|
|
332
|
+
const finish = async (keep: boolean) => {
|
|
333
|
+
if (finished) return;
|
|
334
|
+
finished = true;
|
|
335
|
+
cancelOpen = null;
|
|
336
|
+
const name = input.value.trim() || null;
|
|
337
|
+
if (keep && name !== slot.name) {
|
|
338
|
+
await slots.rename(slot.id, name).catch(() => showError(strings.cantSave));
|
|
339
|
+
}
|
|
340
|
+
await refresh();
|
|
341
|
+
rowOf(slot.id)?.querySelector<HTMLElement>("[data-action=load]")?.focus();
|
|
342
|
+
};
|
|
343
|
+
cancelOpen = () => { finish(false); };
|
|
344
|
+
input.addEventListener("keydown", e => {
|
|
345
|
+
if (e.key == "Enter") {
|
|
346
|
+
e.preventDefault();
|
|
347
|
+
finish(true);
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
input.addEventListener("blur", () => finish(true));
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function fileName() {
|
|
354
|
+
const name = (options.fileName ?? document.title).replace(/[\\/:*?"<>|]+/g, "").replace(/\s+/g, " ").trim();
|
|
355
|
+
return (name || "story") + SAVE_FILE_EXTENSION;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function download(save: SavedGame) {
|
|
359
|
+
const url = URL.createObjectURL(new Blob([JSON.stringify(save)], { type: "application/json" }));
|
|
360
|
+
const link = document.createElement("a");
|
|
361
|
+
link.href = url;
|
|
362
|
+
link.download = fileName();
|
|
363
|
+
dialog.appendChild(link);
|
|
364
|
+
link.click();
|
|
365
|
+
link.remove();
|
|
366
|
+
setTimeout(() => URL.revokeObjectURL(url), 0);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
async function load(save: unknown) {
|
|
370
|
+
const result = await api.loadSave(save as SavedGame);
|
|
371
|
+
if (!result.success) {
|
|
372
|
+
showError(strings[result.error]);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
savedOrLoaded = true;
|
|
376
|
+
dialog.close();
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// Escape closes whatever's open in the dialog first
|
|
380
|
+
dialog.addEventListener("cancel", e => {
|
|
381
|
+
if (cancelOpen) {
|
|
382
|
+
e.preventDefault();
|
|
383
|
+
cancelOpen();
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
closeButton.addEventListener("click", () => dialog.close());
|
|
388
|
+
|
|
389
|
+
form.addEventListener("submit", async e => {
|
|
390
|
+
e.preventDefault();
|
|
391
|
+
hideError();
|
|
392
|
+
cancelOpen?.();
|
|
393
|
+
try {
|
|
394
|
+
await slots.add(api.getSave(), nameInput.value);
|
|
395
|
+
savedOrLoaded = true;
|
|
396
|
+
nameInput.value = "";
|
|
397
|
+
}
|
|
398
|
+
catch {
|
|
399
|
+
showError(strings.cantSave);
|
|
400
|
+
}
|
|
401
|
+
await refresh();
|
|
402
|
+
nameInput.focus();
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
list.addEventListener("click", async e => {
|
|
406
|
+
const button = (e.target as Element).closest<HTMLElement>("[data-action]");
|
|
407
|
+
const row = button?.closest<HTMLLIElement>("[data-slot]");
|
|
408
|
+
if (!button || !row) return;
|
|
409
|
+
const slot = saves.find(s => s.id === Number(row.dataset.slot));
|
|
410
|
+
if (!slot) return;
|
|
411
|
+
const action = button.dataset.action;
|
|
412
|
+
if (action == "options") {
|
|
413
|
+
openMenu(button);
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if (button.getAttribute("role") == "menuitem") cancelOpen?.();
|
|
417
|
+
hideError();
|
|
418
|
+
const title = titleOf(slot);
|
|
419
|
+
|
|
420
|
+
if (action == "load") {
|
|
421
|
+
if (unsaved() && !await confirmInDialog(row, strings.loadConfirm.replace("[name]", title), strings.load)) {
|
|
422
|
+
button.focus();
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
const latest = await slots.get(slot.id).catch(() => null);
|
|
426
|
+
if (latest) await load(latest.save);
|
|
427
|
+
}
|
|
428
|
+
else if (action == "overwrite") {
|
|
429
|
+
if (!await confirmInDialog(row, strings.overwriteConfirm.replace("[name]", title), strings.overwrite)) return;
|
|
430
|
+
try {
|
|
431
|
+
await slots.overwrite(slot.id, api.getSave());
|
|
432
|
+
savedOrLoaded = true;
|
|
433
|
+
}
|
|
434
|
+
catch {
|
|
435
|
+
showError(strings.cantSave);
|
|
436
|
+
}
|
|
437
|
+
await refresh();
|
|
438
|
+
rowOf(slot.id)?.querySelector<HTMLElement>("[data-action=load]")?.focus();
|
|
439
|
+
}
|
|
440
|
+
else if (action == "rename") {
|
|
441
|
+
startRename(slot);
|
|
442
|
+
}
|
|
443
|
+
else if (action == "download") {
|
|
444
|
+
download(slot.save);
|
|
445
|
+
}
|
|
446
|
+
else if (action == "delete") {
|
|
447
|
+
if (!await confirmInDialog(row, strings.deleteConfirm.replace("[name]", title), strings.delete, true)) return;
|
|
448
|
+
await slots.delete(slot.id).catch(() => showError(strings.cantSave));
|
|
449
|
+
await refresh();
|
|
450
|
+
nameInput.focus();
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
$("[data-save-to-file]").addEventListener("click", () => {
|
|
455
|
+
hideError();
|
|
456
|
+
download(api.getSave());
|
|
457
|
+
savedOrLoaded = true;
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
$("[data-load-from-file]").addEventListener("click", () => {
|
|
461
|
+
hideError();
|
|
462
|
+
fileInput.click();
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
fileInput.addEventListener("change", async () => {
|
|
466
|
+
const file = fileInput.files?.[0];
|
|
467
|
+
fileInput.value = "";
|
|
468
|
+
if (!file) return;
|
|
469
|
+
let save: unknown;
|
|
470
|
+
try {
|
|
471
|
+
save = JSON.parse(await file.text());
|
|
472
|
+
}
|
|
473
|
+
catch {
|
|
474
|
+
showError(strings.notASave);
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
if (unsaved() && !await confirmInDialog(footerConfirm, strings.loadConfirm.replace("[name]", file.name), strings.load)) {
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
await load(save);
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
return {
|
|
484
|
+
element: dialog,
|
|
485
|
+
async open() {
|
|
486
|
+
hideError();
|
|
487
|
+
cancelOpen?.();
|
|
488
|
+
nameInput.value = "";
|
|
489
|
+
// Shown straight away, so the player can type a name while the saves are listed
|
|
490
|
+
if (!dialog.open) dialog.showModal();
|
|
491
|
+
nameInput.focus();
|
|
492
|
+
await refresh();
|
|
493
|
+
},
|
|
494
|
+
close() {
|
|
495
|
+
cancelOpen?.();
|
|
496
|
+
dialog.close();
|
|
497
|
+
},
|
|
498
|
+
};
|
|
499
|
+
}
|
package/src/squiffy.page.css
CHANGED
|
@@ -4,6 +4,12 @@
|
|
|
4
4
|
{
|
|
5
5
|
/* The width of the story text */
|
|
6
6
|
--squiffy-max-width: 42rem;
|
|
7
|
+
/* The story's font. Charter, Sitka Text, Cambria or Georgia, whichever the reader has */
|
|
8
|
+
--squiffy-font-family: Charter, "Bitstream Charter", "Sitka Text", Cambria, Georgia, serif;
|
|
9
|
+
/* The size of the story text. The header's buttons are smaller, and don't change with it */
|
|
10
|
+
--squiffy-font-size: 1.125rem;
|
|
11
|
+
/* The space between lines of text */
|
|
12
|
+
--squiffy-line-height: 1.6;
|
|
7
13
|
}
|
|
8
14
|
|
|
9
15
|
/* Follow the reader's light or dark system setting, or what they chose with the theme button.
|
|
@@ -39,12 +45,12 @@ div#squiffy-container
|
|
|
39
45
|
margin-left: auto;
|
|
40
46
|
margin-right: auto;
|
|
41
47
|
padding: 0 1.25rem 3rem;
|
|
42
|
-
font-family:
|
|
43
|
-
line-height:
|
|
48
|
+
font-family: var(--squiffy-font-family);
|
|
49
|
+
line-height: var(--squiffy-line-height);
|
|
44
50
|
}
|
|
45
51
|
|
|
46
|
-
/* The bar above the story containing the Restart and light/dark buttons. It stays at the
|
|
47
|
-
the window as the story scrolls under it. */
|
|
52
|
+
/* The bar above the story containing the Back, Restart and light/dark buttons. It stays at the
|
|
53
|
+
top of the window as the story scrolls under it. */
|
|
48
54
|
div#squiffy-header
|
|
49
55
|
{
|
|
50
56
|
position: sticky;
|
|
@@ -70,36 +76,54 @@ div#squiffy-header::after
|
|
|
70
76
|
pointer-events: none;
|
|
71
77
|
}
|
|
72
78
|
|
|
73
|
-
/*
|
|
74
|
-
|
|
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
|
|
79
|
+
/* Space between the Back, Restart and light/dark buttons */
|
|
80
|
+
button.squiffy-header-button + button.squiffy-header-button
|
|
82
81
|
{
|
|
83
|
-
|
|
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;
|
|
82
|
+
margin-left: 0.25em;
|
|
91
83
|
}
|
|
92
84
|
|
|
85
|
+
/* The button that switches between light and dark mode. It shows a moon in light mode and a sun
|
|
86
|
+
in dark mode. */
|
|
93
87
|
button#squiffy-theme:not([data-next="light"]) .squiffy-icon-light,
|
|
94
88
|
button#squiffy-theme[data-next="light"] .squiffy-icon-dark
|
|
95
89
|
{
|
|
96
90
|
display: none;
|
|
97
91
|
}
|
|
98
92
|
|
|
93
|
+
/* With a side panel (squiffy.json's "panels"), the page is wider, with the panel to the right of
|
|
94
|
+
the story. It stays in view as the story scrolls, under the header. On a narrower screen, it's a
|
|
95
|
+
button in the header instead. */
|
|
96
|
+
@media (min-width: 64rem)
|
|
97
|
+
{
|
|
98
|
+
div#squiffy-container.squiffy-has-side
|
|
99
|
+
{
|
|
100
|
+
display: grid;
|
|
101
|
+
grid-template-columns: minmax(0, 1fr) var(--squiffy-side-width);
|
|
102
|
+
column-gap: 2.5rem;
|
|
103
|
+
align-items: start;
|
|
104
|
+
max-width: calc(var(--squiffy-max-width) + var(--squiffy-side-width) + 2.5rem);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
div#squiffy-container.squiffy-has-side > div#squiffy-header
|
|
108
|
+
{
|
|
109
|
+
grid-column: 1 / -1;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
div#squiffy-container.squiffy-has-side > aside.squiffy-panels-side
|
|
113
|
+
{
|
|
114
|
+
position: sticky;
|
|
115
|
+
top: 3.5rem;
|
|
116
|
+
padding-left: 1.25rem;
|
|
117
|
+
border-left: 1px solid var(--squiffy-border);
|
|
118
|
+
max-height: calc(100vh - 4.5rem);
|
|
119
|
+
overflow-y: auto;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
99
123
|
/* The story text */
|
|
100
124
|
div#squiffy
|
|
101
125
|
{
|
|
102
|
-
font-size:
|
|
126
|
+
font-size: var(--squiffy-font-size);
|
|
103
127
|
}
|
|
104
128
|
|
|
105
129
|
div#squiffy :is(h1, h2, h3, h4, h5, h6)
|