@hadoku/ui-shell 0.0.0-stage → 0.1.5
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/LICENSE +21 -0
- package/dist/app-id/index.d.ts +4 -0
- package/dist/app-id/index.js +14 -0
- package/dist/chrome/AppChrome.d.ts +9 -0
- package/dist/chrome/AppChrome.js +201 -0
- package/dist/chrome/chrome.css +336 -0
- package/dist/chrome/index.d.ts +3 -0
- package/dist/chrome/index.js +3 -0
- package/dist/chrome/matchMedia.d.ts +2 -0
- package/dist/chrome/matchMedia.js +51 -0
- package/dist/chrome/shell.d.ts +17 -0
- package/dist/chrome/shell.js +47 -0
- package/dist/chrome/trim.css +334 -0
- package/dist/components/Reveal.d.ts +20 -0
- package/dist/components/Reveal.js +38 -0
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.js +7 -0
- package/dist/components/reveal.css +110 -0
- package/dist/embed/index.d.ts +1 -0
- package/dist/embed/index.js +1 -0
- package/dist/embed/styles.d.ts +4 -0
- package/dist/embed/styles.js +49 -0
- package/dist/motion/index.d.ts +6 -0
- package/dist/motion/index.js +8 -0
- package/dist/settings/index.d.ts +1 -0
- package/dist/settings/index.js +1 -0
- package/dist/settings/ui-scale.d.ts +45 -0
- package/dist/settings/ui-scale.js +142 -0
- package/dist/sound/GainSlider.d.ts +6 -0
- package/dist/sound/GainSlider.js +15 -0
- package/dist/sound/index.d.ts +65 -0
- package/dist/sound/index.js +204 -0
- package/dist/sound/mix.d.ts +69 -0
- package/dist/sound/mix.js +117 -0
- package/dist/sound/mixer.d.ts +37 -0
- package/dist/sound/mixer.js +301 -0
- package/dist/sound/mute.d.ts +62 -0
- package/dist/sound/mute.js +250 -0
- package/dist/sound/queue.d.ts +32 -0
- package/dist/sound/queue.js +80 -0
- package/dist/theme/index.d.ts +18 -0
- package/dist/theme/index.js +41 -0
- package/dist/trim/index.d.ts +2 -0
- package/dist/trim/index.js +2 -0
- package/dist/trim/siteTrim.d.ts +23 -0
- package/dist/trim/siteTrim.js +91 -0
- package/dist/trim/trim.d.ts +72 -0
- package/dist/trim/trim.js +63 -0
- package/dist/usage/index.d.ts +32 -0
- package/dist/usage/index.js +84 -0
- package/package.json +70 -4
- package/README.md +0 -3
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The player's say over sound: mute, overall volume, the music bus under
|
|
3
|
+
* everything else, and which track a music role plays when it has more
|
|
4
|
+
* than one (one at random until they choose). Drawn in the game's own
|
|
5
|
+
* settings; applied to the mixer by `./index.ts`.
|
|
6
|
+
*
|
|
7
|
+
* No "reduce sound" media query exists — `prefers-reduced-motion` is about
|
|
8
|
+
* motion, and reading it as a sound preference would silence players who
|
|
9
|
+
* never asked for that. Default: on, at a low volume.
|
|
10
|
+
*
|
|
11
|
+
* Kept in the fleet's preference service: `games` (`muted`, `volume`, the
|
|
12
|
+
* master every hadoku game shares — `GAMES_ROW`, not this game's row) and
|
|
13
|
+
* `<appId>-sound` (`music`, `musicPicks` — this game's own, so two games
|
|
14
|
+
* never collide). The three levels are per DEVICE, as `localStorage` held
|
|
15
|
+
* them; music picks are per PLAYER. Values live here in memory, read
|
|
16
|
+
* synchronously on every sound; `useSoundPrefs` connects them to the
|
|
17
|
+
* service once per page. The old `<appId>.sound.*` keys are read once, as
|
|
18
|
+
* a migration, and removed.
|
|
19
|
+
*/
|
|
20
|
+
import { useEffect } from 'react';
|
|
21
|
+
import { z } from 'zod';
|
|
22
|
+
import { createPrefsClient } from '@wolffm/prefs-client';
|
|
23
|
+
import { GAMES_DEFAULTS, GAMES_ROW } from '@wolffm/prefs-client/platform';
|
|
24
|
+
import { appId } from "../app-id/index.js";
|
|
25
|
+
/** A level nobody has set: the music starts at half, under the game's own sounds; everything else full. */
|
|
26
|
+
export const LEVEL_DEFAULTS = { volume: GAMES_DEFAULTS.volume, music: 0.5 };
|
|
27
|
+
const LevelSchema = z.number().min(0).max(1);
|
|
28
|
+
export const SoundPrefsSchema = z
|
|
29
|
+
.object({
|
|
30
|
+
music: LevelSchema.optional(),
|
|
31
|
+
/** The track per music role, by role id. */
|
|
32
|
+
musicPicks: z.record(z.string(), z.string()).optional(),
|
|
33
|
+
})
|
|
34
|
+
.strict();
|
|
35
|
+
/** The `localStorage` keys the values lived in before the service held them, built from `appId()`. Called lazily — never at module load — so importing this before `configureShell` runs does not throw. */
|
|
36
|
+
export function legacyKeys() {
|
|
37
|
+
const prefix = appId();
|
|
38
|
+
return {
|
|
39
|
+
muted: `${prefix}.sound.muted`,
|
|
40
|
+
volume: `${prefix}.sound.volume`,
|
|
41
|
+
music: `${prefix}.sound.music`,
|
|
42
|
+
musicPicks: `${prefix}.sound.music-picks`,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
let muted = GAMES_DEFAULTS.muted;
|
|
46
|
+
let musicPicks = {};
|
|
47
|
+
const levels = { ...LEVEL_DEFAULTS };
|
|
48
|
+
const listeners = new Set();
|
|
49
|
+
/** The service, once `useSoundPrefs` has connected it; until then a change is held for the page. */
|
|
50
|
+
let connection = null;
|
|
51
|
+
function notify() {
|
|
52
|
+
for (const fn of [...listeners])
|
|
53
|
+
fn();
|
|
54
|
+
}
|
|
55
|
+
/** Whether sounds are currently silenced. */
|
|
56
|
+
export function isMuted() {
|
|
57
|
+
return muted;
|
|
58
|
+
}
|
|
59
|
+
/** Set the preference, save it, and tell every subscriber. */
|
|
60
|
+
export function setMuted(value) {
|
|
61
|
+
if (muted === value)
|
|
62
|
+
return;
|
|
63
|
+
muted = value;
|
|
64
|
+
void connection?.games.save({ muted: value }, { scope: GAMES_ROW.scope });
|
|
65
|
+
notify();
|
|
66
|
+
}
|
|
67
|
+
/** A level, 0..1. */
|
|
68
|
+
export function getLevel(name) {
|
|
69
|
+
return levels[name];
|
|
70
|
+
}
|
|
71
|
+
/** Set a level, save it, and tell every subscriber. */
|
|
72
|
+
export function setLevel(name, value) {
|
|
73
|
+
const next = clamp(value);
|
|
74
|
+
if (levels[name] === next)
|
|
75
|
+
return;
|
|
76
|
+
levels[name] = next;
|
|
77
|
+
if (name === 'volume')
|
|
78
|
+
void connection?.games.save({ volume: next }, { scope: GAMES_ROW.scope });
|
|
79
|
+
else
|
|
80
|
+
void connection?.sound.save({ music: next }, { scope: 'device' });
|
|
81
|
+
notify();
|
|
82
|
+
}
|
|
83
|
+
/** The take the player chose for a music role, or `null` for none chosen. */
|
|
84
|
+
export function getMusicPick(role) {
|
|
85
|
+
return musicPicks[role] ?? null;
|
|
86
|
+
}
|
|
87
|
+
/** Choose a music role's track, save it, and tell every subscriber. */
|
|
88
|
+
export function setMusicPick(role, file) {
|
|
89
|
+
if (getMusicPick(role) === file)
|
|
90
|
+
return;
|
|
91
|
+
musicPicks = { ...musicPicks, [role]: file };
|
|
92
|
+
void connection?.sound.save({ musicPicks });
|
|
93
|
+
notify();
|
|
94
|
+
}
|
|
95
|
+
function clamp(n) {
|
|
96
|
+
return Math.min(1, Math.max(0, n));
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Subscribe to any change: mute, a level or a music pick. Returns the unsubscribe.
|
|
100
|
+
*
|
|
101
|
+
* Shaped for `useSyncExternalStore`: two controls on one page must agree,
|
|
102
|
+
* and a React component must not read a mutable module global during
|
|
103
|
+
* render without a subscription. Another tab's change arrives through the
|
|
104
|
+
* service's own broadcast and lands here.
|
|
105
|
+
*/
|
|
106
|
+
export function subscribeSound(fn) {
|
|
107
|
+
listeners.add(fn);
|
|
108
|
+
return () => {
|
|
109
|
+
listeners.delete(fn);
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/** What the service holds, adopted field by field: a field it does not hold keeps the page's value. */
|
|
113
|
+
function adopt(games, sound) {
|
|
114
|
+
const before = JSON.stringify([muted, levels, musicPicks]);
|
|
115
|
+
if (games.muted !== undefined)
|
|
116
|
+
muted = games.muted;
|
|
117
|
+
if (games.volume !== undefined)
|
|
118
|
+
levels.volume = games.volume;
|
|
119
|
+
if (sound.music !== undefined)
|
|
120
|
+
levels.music = sound.music;
|
|
121
|
+
if (sound.musicPicks !== undefined)
|
|
122
|
+
musicPicks = sound.musicPicks;
|
|
123
|
+
if (JSON.stringify([muted, levels, musicPicks]) !== before)
|
|
124
|
+
notify();
|
|
125
|
+
}
|
|
126
|
+
/** Whatever the old `<appId>.sound.*` keys still hold, parsed; anything unreadable is absent. */
|
|
127
|
+
export function readLegacy(store = globalThis.localStorage) {
|
|
128
|
+
const out = {};
|
|
129
|
+
if (store === undefined)
|
|
130
|
+
return out;
|
|
131
|
+
const raw = (key) => {
|
|
132
|
+
try {
|
|
133
|
+
return store.getItem(key);
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
// A blocked store holds nothing to move.
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
const keys = legacyKeys();
|
|
141
|
+
const m = raw(keys.muted);
|
|
142
|
+
if (m === 'true' || m === 'false')
|
|
143
|
+
out.muted = m === 'true';
|
|
144
|
+
for (const name of ['volume', 'music']) {
|
|
145
|
+
const v = raw(keys[name]);
|
|
146
|
+
const n = v === null ? NaN : Number(v);
|
|
147
|
+
if (Number.isFinite(n))
|
|
148
|
+
out[name] = clamp(n);
|
|
149
|
+
}
|
|
150
|
+
const p = raw(keys.musicPicks);
|
|
151
|
+
if (p !== null) {
|
|
152
|
+
try {
|
|
153
|
+
const parsed = SoundPrefsSchema.shape.musicPicks.safeParse(JSON.parse(p));
|
|
154
|
+
if (parsed.success && parsed.data !== undefined)
|
|
155
|
+
out.musicPicks = parsed.data;
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
// Not JSON: no picks to move.
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return out;
|
|
162
|
+
}
|
|
163
|
+
function removeLegacy(store = globalThis.localStorage) {
|
|
164
|
+
for (const key of Object.values(legacyKeys())) {
|
|
165
|
+
try {
|
|
166
|
+
store?.removeItem(key);
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
// A blocked store: the keys stay, and are read again (and found already moved) next time.
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/** Each a row of its own, built on first connect; one per app id, as the SDK promises. */
|
|
174
|
+
let clients = null;
|
|
175
|
+
function soundClients() {
|
|
176
|
+
clients ??= {
|
|
177
|
+
// No `defaults`: an unset field is how `connectSoundPrefs` knows the legacy key still has to move.
|
|
178
|
+
games: createPrefsClient({ appId: GAMES_ROW.appId, schema: GAMES_ROW.schema }),
|
|
179
|
+
sound: createPrefsClient({ appId: `${appId()}-sound`, schema: SoundPrefsSchema }),
|
|
180
|
+
};
|
|
181
|
+
return clients;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Connect the sound settings to the preference service for as long as the calling component is
|
|
185
|
+
* mounted: adopt what it holds, follow its changes (another tab, another host on the page), and
|
|
186
|
+
* save every change made here. The first time a page connects, a value only the old keys hold is
|
|
187
|
+
* saved to the service and the keys are removed. `ready` settles once the service's first answer
|
|
188
|
+
* is adopted.
|
|
189
|
+
*/
|
|
190
|
+
export function connectSoundPrefs() {
|
|
191
|
+
const c = soundClients();
|
|
192
|
+
connection = c;
|
|
193
|
+
const legacy = readLegacy();
|
|
194
|
+
adopt(legacy, legacy);
|
|
195
|
+
let games = {};
|
|
196
|
+
let sound = {};
|
|
197
|
+
const offGames = c.games.subscribe((next) => {
|
|
198
|
+
games = next;
|
|
199
|
+
adopt(games, sound);
|
|
200
|
+
});
|
|
201
|
+
const offSound = c.sound.subscribe((next) => {
|
|
202
|
+
sound = next;
|
|
203
|
+
adopt(games, sound);
|
|
204
|
+
});
|
|
205
|
+
const ready = Promise.all([c.games.read(), c.sound.read()]).then(([g, s]) => {
|
|
206
|
+
if (connection !== c)
|
|
207
|
+
return;
|
|
208
|
+
games = g;
|
|
209
|
+
sound = s;
|
|
210
|
+
const toGames = {};
|
|
211
|
+
if (g.muted === undefined && legacy.muted !== undefined)
|
|
212
|
+
toGames.muted = legacy.muted;
|
|
213
|
+
if (g.volume === undefined && legacy.volume !== undefined)
|
|
214
|
+
toGames.volume = legacy.volume;
|
|
215
|
+
if (Object.keys(toGames).length > 0)
|
|
216
|
+
void c.games.save(toGames, { scope: GAMES_ROW.scope });
|
|
217
|
+
if (s.music === undefined && legacy.music !== undefined)
|
|
218
|
+
void c.sound.save({ music: legacy.music }, { scope: 'device' });
|
|
219
|
+
if (s.musicPicks === undefined && legacy.musicPicks !== undefined)
|
|
220
|
+
void c.sound.save({ musicPicks: legacy.musicPicks });
|
|
221
|
+
removeLegacy();
|
|
222
|
+
adopt(games, sound);
|
|
223
|
+
});
|
|
224
|
+
return {
|
|
225
|
+
ready,
|
|
226
|
+
disconnect() {
|
|
227
|
+
offGames();
|
|
228
|
+
offSound();
|
|
229
|
+
if (connection === c)
|
|
230
|
+
connection = null;
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
/** `connectSoundPrefs` for the life of the component that calls it: the app's root, standalone or embedded. */
|
|
235
|
+
export function useSoundPrefs() {
|
|
236
|
+
useEffect(() => connectSoundPrefs().disconnect, []);
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Back to the defaults, disconnected, with no subscribers.
|
|
240
|
+
*
|
|
241
|
+
* For tests, which start each case from a fresh page. Production never calls it.
|
|
242
|
+
*/
|
|
243
|
+
export function resetMutedForTests() {
|
|
244
|
+
muted = GAMES_DEFAULTS.muted;
|
|
245
|
+
musicPicks = {};
|
|
246
|
+
Object.assign(levels, LEVEL_DEFAULTS);
|
|
247
|
+
listeners.clear();
|
|
248
|
+
connection = null;
|
|
249
|
+
clients = null;
|
|
250
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export interface QueueItem {
|
|
2
|
+
cell: string;
|
|
3
|
+
/** The cell already has takes: what is offered is new candidates to add beside them. */
|
|
4
|
+
filled?: boolean;
|
|
5
|
+
title: string;
|
|
6
|
+
sub: string;
|
|
7
|
+
}
|
|
8
|
+
export interface QueueTake {
|
|
9
|
+
file: string;
|
|
10
|
+
}
|
|
11
|
+
export interface QueueConfig<T extends QueueTake> {
|
|
12
|
+
/** Every cell the queue can serve, in display order. */
|
|
13
|
+
catalog: readonly QueueItem[];
|
|
14
|
+
/** Lower is a better guess; `null` means `folder` is not a candidate for `cell` at all. */
|
|
15
|
+
rankFor: (cell: string, folder: string) => number | null;
|
|
16
|
+
/** Ranked before the take it was made from. Default: never. */
|
|
17
|
+
isEdited?: (take: T) => boolean;
|
|
18
|
+
/** A pool (a stock library, say) that pads a thin choice. Default: never. */
|
|
19
|
+
isFallback?: (take: T) => boolean;
|
|
20
|
+
/** At most this many offered (default 6), at least this many before padding (default 3). */
|
|
21
|
+
maxChoices?: number;
|
|
22
|
+
minChoices?: number;
|
|
23
|
+
}
|
|
24
|
+
/** Build one queue against the game's own catalog and ranking. Pure mechanics otherwise: no cell grammar, no role or folder names. */
|
|
25
|
+
export declare function createSoundQueue<T extends QueueTake>(config: QueueConfig<T>): {
|
|
26
|
+
QUEUE_CHOICES: number;
|
|
27
|
+
emptyCells: (cells: Readonly<Record<string, readonly string[]>>) => QueueItem[];
|
|
28
|
+
queueCells: (cells: Readonly<Record<string, readonly string[]>>, takes: readonly T[]) => QueueItem[];
|
|
29
|
+
queueOrder: (cells: Readonly<Record<string, readonly string[]>>, skipped: readonly string[], seed?: number, takes?: readonly T[]) => QueueItem[];
|
|
30
|
+
freshFor: (cell: string, takes: readonly T[], placed: ReadonlySet<string>) => T[];
|
|
31
|
+
candidatesFor: (cell: string, takes: readonly T[], placed: ReadonlySet<string>) => T[];
|
|
32
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The sound picker's queue mode: which cells still need a sound, in what
|
|
3
|
+
* order, and which takes to offer for each. Pure mechanics only — the
|
|
4
|
+
* cells that exist, what a folder guesses it was made for, and what counts
|
|
5
|
+
* as a fallback take are the game's own data, injected rather than
|
|
6
|
+
* imported.
|
|
7
|
+
*
|
|
8
|
+
* Empty cells only, shuffled by a seed drawn once so the order holds still
|
|
9
|
+
* for a session and a skip goes to the back. `rankFor` (injected) says how
|
|
10
|
+
* good a guess a take's folder is for a cell — lower better, `null` rules
|
|
11
|
+
* it out; within a rank, unplaced before placed, edited before the take it
|
|
12
|
+
* was made from; a fallback pool pads a thin choice to `minChoices`.
|
|
13
|
+
*/
|
|
14
|
+
/** A seeded shuffle (mulberry32 + Fisher-Yates): the same seed, the same order. */
|
|
15
|
+
function shuffled(items, seed) {
|
|
16
|
+
let a = seed >>> 0;
|
|
17
|
+
const rand = () => {
|
|
18
|
+
a = (a + 0x6d2b79f5) >>> 0;
|
|
19
|
+
let t = a;
|
|
20
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
21
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
22
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
23
|
+
};
|
|
24
|
+
const out = [...items];
|
|
25
|
+
for (let i = out.length - 1; i > 0; i--) {
|
|
26
|
+
const j = Math.floor(rand() * (i + 1));
|
|
27
|
+
[out[i], out[j]] = [out[j], out[i]];
|
|
28
|
+
}
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
31
|
+
const folderOf = (file) => file.slice(0, file.indexOf('/'));
|
|
32
|
+
/** Build one queue against the game's own catalog and ranking. Pure mechanics otherwise: no cell grammar, no role or folder names. */
|
|
33
|
+
export function createSoundQueue(config) {
|
|
34
|
+
const { catalog, rankFor, isEdited = () => false, isFallback = () => false, maxChoices = 6, minChoices = 3 } = config;
|
|
35
|
+
function emptyCells(cells) {
|
|
36
|
+
return catalog.filter((i) => (cells[i.cell] ?? []).length === 0);
|
|
37
|
+
}
|
|
38
|
+
function ranked(cell, takes, placed) {
|
|
39
|
+
return takes
|
|
40
|
+
.map((t) => ({ t, rank: rankFor(cell, folderOf(t.file)) }))
|
|
41
|
+
.filter((x) => x.rank !== null)
|
|
42
|
+
.sort((a, b) => a.rank - b.rank ||
|
|
43
|
+
Number(isEdited(b.t)) - Number(isEdited(a.t)) ||
|
|
44
|
+
Number(placed.has(a.t.file)) - Number(placed.has(b.t.file)) ||
|
|
45
|
+
a.t.file.localeCompare(b.t.file))
|
|
46
|
+
.map((x) => x.t);
|
|
47
|
+
}
|
|
48
|
+
function freshFor(cell, takes, placed) {
|
|
49
|
+
return ranked(cell, takes, placed)
|
|
50
|
+
.filter((t) => !placed.has(t.file))
|
|
51
|
+
.slice(0, maxChoices);
|
|
52
|
+
}
|
|
53
|
+
function candidatesFor(cell, takes, placed) {
|
|
54
|
+
const out = ranked(cell, takes, placed).slice(0, maxChoices);
|
|
55
|
+
if (out.length < minChoices) {
|
|
56
|
+
const pool = takes.filter((t) => isFallback(t) && !placed.has(t.file) && !out.includes(t));
|
|
57
|
+
// A stable spread over the pool, so each cell gets its own few rather than the first ones every time.
|
|
58
|
+
const seed = [...cell].reduce((h, ch) => (h * 31 + ch.charCodeAt(0)) >>> 0, 7);
|
|
59
|
+
for (let i = 0; out.length < minChoices && i < pool.length; i++)
|
|
60
|
+
out.push(pool[(seed + i) % pool.length]);
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
function queueCells(cells, takes) {
|
|
65
|
+
const placed = new Set(Object.values(cells).flat());
|
|
66
|
+
return catalog.flatMap((i) => {
|
|
67
|
+
if ((cells[i.cell] ?? []).length === 0)
|
|
68
|
+
return [i];
|
|
69
|
+
return freshFor(i.cell, takes, placed).length > 0 ? [{ ...i, filled: true }] : [];
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
function queueOrder(cells, skipped, seed, takes) {
|
|
73
|
+
const items = takes === undefined ? emptyCells(cells) : queueCells(cells, takes);
|
|
74
|
+
const all = seed === undefined ? items : shuffled(items, seed);
|
|
75
|
+
const later = new Set(skipped);
|
|
76
|
+
const back = skipped.map((c) => all.find((i) => i.cell === c)).filter((i) => i !== undefined);
|
|
77
|
+
return [...all.filter((i) => !later.has(i.cell)), ...back];
|
|
78
|
+
}
|
|
79
|
+
return { QUEUE_CHOICES: maxChoices, emptyCells, queueCells, queueOrder, freshFor, candidatesFor };
|
|
80
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The theme, before the first paint.
|
|
3
|
+
*
|
|
4
|
+
* The stylesheet resolves every colour against `<html data-theme>`, and
|
|
5
|
+
* `@wolffm/themes`' provider only sets it once the bundle has mounted. Until
|
|
6
|
+
* then `:root` reads the light defaults, so a dark theme painted white first
|
|
7
|
+
* and then swapped — the flash on load. This script runs in `<head>`, ahead of
|
|
8
|
+
* the stylesheet, and seeds the attribute the way the provider's own
|
|
9
|
+
* `seedTheme` does: this tab's choice, then this browser's, then the system
|
|
10
|
+
* preference. A bare family name the hadoku.me host may store (`coffee`) takes
|
|
11
|
+
* the variant the system preference picks, and a name that is no theme is
|
|
12
|
+
* ignored rather than painted.
|
|
13
|
+
*
|
|
14
|
+
* Built at CONFIG TIME (like the app's own app-name.ts) from the package's own theme list, so a
|
|
15
|
+
* family added there is known here without a copy to keep in step. `@wolffm/themes`' own
|
|
16
|
+
* `seedTheme` is not public and is not a dependency-free `<head>` script, so this stays its own.
|
|
17
|
+
*/
|
|
18
|
+
export declare function themeInitScript(): string;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { THEME_FAMILIES } from '@wolffm/themes';
|
|
2
|
+
/**
|
|
3
|
+
* The theme, before the first paint.
|
|
4
|
+
*
|
|
5
|
+
* The stylesheet resolves every colour against `<html data-theme>`, and
|
|
6
|
+
* `@wolffm/themes`' provider only sets it once the bundle has mounted. Until
|
|
7
|
+
* then `:root` reads the light defaults, so a dark theme painted white first
|
|
8
|
+
* and then swapped — the flash on load. This script runs in `<head>`, ahead of
|
|
9
|
+
* the stylesheet, and seeds the attribute the way the provider's own
|
|
10
|
+
* `seedTheme` does: this tab's choice, then this browser's, then the system
|
|
11
|
+
* preference. A bare family name the hadoku.me host may store (`coffee`) takes
|
|
12
|
+
* the variant the system preference picks, and a name that is no theme is
|
|
13
|
+
* ignored rather than painted.
|
|
14
|
+
*
|
|
15
|
+
* Built at CONFIG TIME (like the app's own app-name.ts) from the package's own theme list, so a
|
|
16
|
+
* family added there is known here without a copy to keep in step. `@wolffm/themes`' own
|
|
17
|
+
* `seedTheme` is not public and is not a dependency-free `<head>` script, so this stays its own.
|
|
18
|
+
*/
|
|
19
|
+
export function themeInitScript() {
|
|
20
|
+
const families = THEME_FAMILIES.map((f) => [f.lightTheme, f.darkTheme]);
|
|
21
|
+
return `(function () {
|
|
22
|
+
var families = ${JSON.stringify(families)};
|
|
23
|
+
var dark = !!(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches);
|
|
24
|
+
function resolve(value) {
|
|
25
|
+
var raw = (value || '').trim();
|
|
26
|
+
for (var i = 0; i < families.length; i++) {
|
|
27
|
+
var f = families[i];
|
|
28
|
+
if (raw === f[0] || raw === f[1]) return raw;
|
|
29
|
+
if (raw && raw + '-light' === f[0]) return dark ? f[1] : f[0];
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
var stored = null;
|
|
34
|
+
try {
|
|
35
|
+
stored = resolve(sessionStorage.getItem('hadoku-theme')) || resolve(localStorage.getItem('hadoku-theme'));
|
|
36
|
+
} catch (err) {
|
|
37
|
+
console.warn('[theme-init] storage unavailable', err);
|
|
38
|
+
}
|
|
39
|
+
document.documentElement.setAttribute('data-theme', stored || (dark ? 'dark' : 'light'));
|
|
40
|
+
})();`;
|
|
41
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Trim } from './trim.js';
|
|
2
|
+
export interface SiteTrimConfig {
|
|
3
|
+
/** The server's trim endpoint, e.g. `/cardtable/api/trim`. */
|
|
4
|
+
apiUrl: string;
|
|
5
|
+
}
|
|
6
|
+
/** Call once, before anything here renders: the server endpoint this store reads and writes. */
|
|
7
|
+
export declare function configureSiteTrim(next: SiteTrimConfig): void;
|
|
8
|
+
export interface SiteTrim {
|
|
9
|
+
/** False until the server has answered. */
|
|
10
|
+
loaded: boolean;
|
|
11
|
+
trim: Trim;
|
|
12
|
+
/** The server's answer: an admin may write it. */
|
|
13
|
+
canEdit: boolean;
|
|
14
|
+
}
|
|
15
|
+
/** Only the families and options the catalogue has. */
|
|
16
|
+
export declare function knownTrim(value: unknown): Trim;
|
|
17
|
+
export declare function useSiteTrim(): SiteTrim;
|
|
18
|
+
/** Saves the site's trim; every screen redraws at once, and back again if the server refuses. Whether it was stored. */
|
|
19
|
+
export declare function saveSiteTrim(trim: Trim): Promise<boolean>;
|
|
20
|
+
/** The site's trim, written on `root`: the standalone page's `<html>`, or the embed's chrome root. */
|
|
21
|
+
export declare function useTrim(root: () => HTMLElement | null): void;
|
|
22
|
+
/** Tests only: forget what was read. */
|
|
23
|
+
export declare function resetSiteTrimForTests(): void;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The site's trim: one look for every player (the operator, 2026-10-21: "make it set the look for
|
|
3
|
+
* every player"), kept by the server (`/api/trim`, `packages/game-server/src/http/trim.ts`) and written only
|
|
4
|
+
* from `#/ui` by an admin.
|
|
5
|
+
*
|
|
6
|
+
* Read once per page into a module store every screen shares, so `#/ui` and the screens behind it
|
|
7
|
+
* redraw together on a save. Until the read answers, nothing is drawn rather than a default that
|
|
8
|
+
* might be wrong; a site nobody has set, or a read that fails, draws `DEFAULT_TRIM`. Names the
|
|
9
|
+
* catalogue does not know (a newer server, a hand edit) are dropped, never drawn.
|
|
10
|
+
*
|
|
11
|
+
* `configureSiteTrim`: the endpoint is the app's, so this package does not build `/<app>/api/…` itself.
|
|
12
|
+
*/
|
|
13
|
+
import { useLayoutEffect, useSyncExternalStore } from 'react';
|
|
14
|
+
import { applyTrim, DEFAULT_TRIM, isTrimOption, TRIM_FAMILY_IDS } from "./trim.js";
|
|
15
|
+
let config;
|
|
16
|
+
/** Call once, before anything here renders: the server endpoint this store reads and writes. */
|
|
17
|
+
export function configureSiteTrim(next) {
|
|
18
|
+
config = next;
|
|
19
|
+
}
|
|
20
|
+
function endpoint() {
|
|
21
|
+
if (config === undefined)
|
|
22
|
+
throw new Error('ui-shell: configureSiteTrim was never called');
|
|
23
|
+
return config.apiUrl;
|
|
24
|
+
}
|
|
25
|
+
const isRecord = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
26
|
+
/** The reply's JSON, or null when it has none (a proxy's error page, a 204). */
|
|
27
|
+
const bodyOf = (res) => res.json().catch(() => null);
|
|
28
|
+
const UNREAD = { loaded: false, trim: {}, canEdit: false };
|
|
29
|
+
let state = UNREAD;
|
|
30
|
+
let reading = null;
|
|
31
|
+
const listeners = new Set();
|
|
32
|
+
function set(next) {
|
|
33
|
+
state = next;
|
|
34
|
+
for (const l of listeners)
|
|
35
|
+
l();
|
|
36
|
+
}
|
|
37
|
+
/** Only the families and options the catalogue has. */
|
|
38
|
+
export function knownTrim(value) {
|
|
39
|
+
if (!isRecord(value))
|
|
40
|
+
return {};
|
|
41
|
+
return Object.fromEntries(TRIM_FAMILY_IDS.flatMap((f) => (typeof value[f] === 'string' && isTrimOption(f, value[f]) ? [[f, value[f]]] : [])));
|
|
42
|
+
}
|
|
43
|
+
function read() {
|
|
44
|
+
reading ??= fetch(endpoint(), { credentials: 'same-origin' })
|
|
45
|
+
.then(async (res) => {
|
|
46
|
+
const body = res.ok ? await bodyOf(res) : null;
|
|
47
|
+
const stored = isRecord(body) ? body.trim : null;
|
|
48
|
+
set({ loaded: true, trim: stored == null ? DEFAULT_TRIM : knownTrim(stored), canEdit: isRecord(body) && body.canEdit === true });
|
|
49
|
+
})
|
|
50
|
+
.catch(() => set({ loaded: true, trim: DEFAULT_TRIM, canEdit: false }));
|
|
51
|
+
return reading;
|
|
52
|
+
}
|
|
53
|
+
function subscribe(listener) {
|
|
54
|
+
listeners.add(listener);
|
|
55
|
+
void read();
|
|
56
|
+
return () => listeners.delete(listener);
|
|
57
|
+
}
|
|
58
|
+
export function useSiteTrim() {
|
|
59
|
+
return useSyncExternalStore(subscribe, () => state, () => state);
|
|
60
|
+
}
|
|
61
|
+
/** Saves the site's trim; every screen redraws at once, and back again if the server refuses. Whether it was stored. */
|
|
62
|
+
export async function saveSiteTrim(trim) {
|
|
63
|
+
const before = state;
|
|
64
|
+
set({ ...state, trim });
|
|
65
|
+
try {
|
|
66
|
+
const res = await fetch(endpoint(), { method: 'PUT', credentials: 'same-origin', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ trim }) });
|
|
67
|
+
if (res.ok)
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
// Refused below, like a non-2xx answer.
|
|
72
|
+
}
|
|
73
|
+
set(before);
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
/** The site's trim, written on `root`: the standalone page's `<html>`, or the embed's chrome root. */
|
|
77
|
+
export function useTrim(root) {
|
|
78
|
+
const { trim } = useSiteTrim();
|
|
79
|
+
useLayoutEffect(() => {
|
|
80
|
+
const el = root();
|
|
81
|
+
if (el === null)
|
|
82
|
+
return undefined;
|
|
83
|
+
applyTrim(el, trim);
|
|
84
|
+
return () => applyTrim(el, {});
|
|
85
|
+
}, [root, trim]);
|
|
86
|
+
}
|
|
87
|
+
/** Tests only: forget what was read. */
|
|
88
|
+
export function resetSiteTrimForTests() {
|
|
89
|
+
state = UNREAD;
|
|
90
|
+
reading = null;
|
|
91
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export declare const TRIM_FAMILIES: {
|
|
2
|
+
readonly frame: {
|
|
3
|
+
readonly label: "Frame";
|
|
4
|
+
readonly options: {
|
|
5
|
+
readonly glint: "Edge glint";
|
|
6
|
+
readonly chamfer: "Chamfered corners";
|
|
7
|
+
readonly hud: "HUD cut";
|
|
8
|
+
readonly step: "Stepped corners";
|
|
9
|
+
readonly scoop: "Scooped corners";
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
readonly corner: {
|
|
13
|
+
readonly label: "Corners";
|
|
14
|
+
readonly options: {
|
|
15
|
+
readonly brackets: "Brackets";
|
|
16
|
+
readonly triangles: "Triangles";
|
|
17
|
+
readonly rivets: "Rivets";
|
|
18
|
+
readonly gems: "Gems";
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
readonly title: {
|
|
22
|
+
readonly label: "Titles";
|
|
23
|
+
readonly options: {
|
|
24
|
+
readonly rail: "Rail";
|
|
25
|
+
readonly centred: "Centred";
|
|
26
|
+
readonly ribbon: "Ribbon";
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
readonly edge: {
|
|
30
|
+
readonly label: "Edge";
|
|
31
|
+
readonly options: {
|
|
32
|
+
readonly keystone: "Keystone gem";
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
readonly texture: {
|
|
36
|
+
readonly label: "Panel texture";
|
|
37
|
+
readonly options: {
|
|
38
|
+
readonly felt: "Felt";
|
|
39
|
+
readonly linen: "Linen";
|
|
40
|
+
readonly lattice: "Lattice";
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
readonly ground: {
|
|
44
|
+
readonly label: "Background";
|
|
45
|
+
readonly options: {
|
|
46
|
+
readonly glow: "Overhead glow";
|
|
47
|
+
readonly felt: "Table felt";
|
|
48
|
+
readonly dots: "Dot grid";
|
|
49
|
+
readonly grid: "HUD grid";
|
|
50
|
+
readonly triangles: "Triangle lattice";
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
readonly groundCorner: {
|
|
54
|
+
readonly label: "Background corners";
|
|
55
|
+
readonly options: {
|
|
56
|
+
readonly wedge: "Wedge glow";
|
|
57
|
+
readonly gems: "Gem cluster";
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
export type TrimFamily = keyof typeof TRIM_FAMILIES;
|
|
62
|
+
export type Trim = {
|
|
63
|
+
[F in TrimFamily]?: keyof (typeof TRIM_FAMILIES)[F]['options'];
|
|
64
|
+
};
|
|
65
|
+
/** What a site nobody has set draws (the operator's pick, 2026-10-21). */
|
|
66
|
+
export declare const DEFAULT_TRIM: Trim;
|
|
67
|
+
export declare const TRIM_FAMILY_IDS: TrimFamily[];
|
|
68
|
+
export declare function isTrimOption(family: TrimFamily, value: string): boolean;
|
|
69
|
+
/** `groundCorner` → `data-trim-ground-corner`. */
|
|
70
|
+
export declare function trimAttribute(family: TrimFamily): string;
|
|
71
|
+
/** Writes one attribute per chosen family and removes the rest. */
|
|
72
|
+
export declare function applyTrim(el: HTMLElement, trim: Trim): void;
|