dssoca 0.12.0 → 0.13.0
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/components/Kbd.svelte +120 -0
- package/dist/components/Kbd.svelte.d.ts +30 -0
- package/dist/components/SearchPalette.svelte +526 -0
- package/dist/components/SearchPalette.svelte.d.ts +82 -0
- package/dist/components/ShortcutsHelp.svelte +365 -0
- package/dist/components/ShortcutsHelp.svelte.d.ts +34 -0
- package/dist/components/Switch.svelte +32 -2
- package/dist/components/Switch.svelte.d.ts +5 -0
- package/dist/components/Topbar.svelte +39 -24
- package/dist/components/Topbar.svelte.d.ts +5 -2
- package/dist/dssoca.config.d.ts +1 -1
- package/dist/dssoca.config.js +2 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/shortcuts.svelte.d.ts +192 -0
- package/dist/shortcuts.svelte.js +633 -0
- package/package.json +2 -2
|
@@ -0,0 +1,633 @@
|
|
|
1
|
+
import { untrack } from 'svelte';
|
|
2
|
+
// ---------------------------------------------------------------------------
|
|
3
|
+
// Dev-only warnings
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
/**
|
|
6
|
+
* Dev gate: Vite consumers get `import.meta.env.DEV` (statically replaced in
|
|
7
|
+
* prod builds); non-Vite environments fall back to `NODE_ENV`.
|
|
8
|
+
*/
|
|
9
|
+
function isDev() {
|
|
10
|
+
try {
|
|
11
|
+
const dev = import.meta.env?.DEV;
|
|
12
|
+
if (typeof dev === 'boolean')
|
|
13
|
+
return dev;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
// no bundler-provided env — fall through
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
return typeof process !== 'undefined' && process.env?.NODE_ENV !== 'production';
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function devWarn(message) {
|
|
26
|
+
if (isDev() && typeof console !== 'undefined')
|
|
27
|
+
console.warn(`[dssoca] shortcuts: ${message}`);
|
|
28
|
+
}
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
// Combo grammar — parsed at registration time, canonical forms stored
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
const MODIFIER_ORDER = ['mod', 'ctrl', 'alt', 'shift', 'meta'];
|
|
33
|
+
const MODIFIER_SET = new Set(MODIFIER_ORDER);
|
|
34
|
+
/** Shifted printables (`?`, `!`, `/`…) already encode shift in `event.key`. */
|
|
35
|
+
function isShiftAgnostic(key) {
|
|
36
|
+
return key.length === 1 && !/[a-z0-9]/.test(key);
|
|
37
|
+
}
|
|
38
|
+
/** A combo with no ctrl/alt/meta/mod is a "character key" per WCAG 2.1.4. */
|
|
39
|
+
function isCharacterCombo(combo) {
|
|
40
|
+
return !combo.ctrl && !combo.alt && !combo.meta && !combo.mod;
|
|
41
|
+
}
|
|
42
|
+
function parseError(raw, reason) {
|
|
43
|
+
return new Error(`[dssoca] shortcuts: cannot parse "${raw}" — ${reason}`);
|
|
44
|
+
}
|
|
45
|
+
function parseCombo(raw) {
|
|
46
|
+
const combo = raw.trim();
|
|
47
|
+
if (combo === '')
|
|
48
|
+
throw parseError(raw, 'empty combo');
|
|
49
|
+
if (/\s/.test(combo)) {
|
|
50
|
+
throw parseError(raw, 'space is reserved for key sequences, which are not supported yet');
|
|
51
|
+
}
|
|
52
|
+
// Tokenize on '+'; a trailing '++' means the literal '+' key ('mod++').
|
|
53
|
+
let key;
|
|
54
|
+
let modifierTokens;
|
|
55
|
+
if (combo === '+') {
|
|
56
|
+
key = '+';
|
|
57
|
+
modifierTokens = [];
|
|
58
|
+
}
|
|
59
|
+
else if (combo.endsWith('+')) {
|
|
60
|
+
const head = combo.slice(0, -1);
|
|
61
|
+
if (!head.endsWith('+'))
|
|
62
|
+
throw parseError(raw, 'missing key after the trailing "+"');
|
|
63
|
+
const mods = head.slice(0, -1);
|
|
64
|
+
key = '+';
|
|
65
|
+
modifierTokens = mods === '' ? [] : mods.split('+');
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
const tokens = combo.split('+');
|
|
69
|
+
key = tokens.pop() ?? '';
|
|
70
|
+
modifierTokens = tokens;
|
|
71
|
+
}
|
|
72
|
+
key = key.toLowerCase();
|
|
73
|
+
if (key === '')
|
|
74
|
+
throw parseError(raw, 'missing key');
|
|
75
|
+
const flags = {
|
|
76
|
+
mod: false,
|
|
77
|
+
ctrl: false,
|
|
78
|
+
alt: false,
|
|
79
|
+
shift: false,
|
|
80
|
+
meta: false,
|
|
81
|
+
};
|
|
82
|
+
for (const token of modifierTokens) {
|
|
83
|
+
const name = token.toLowerCase();
|
|
84
|
+
if (!MODIFIER_SET.has(name)) {
|
|
85
|
+
throw parseError(raw, `unknown modifier "${token}" (expected mod|ctrl|alt|shift|meta, and exactly one key)`);
|
|
86
|
+
}
|
|
87
|
+
if (flags[name])
|
|
88
|
+
throw parseError(raw, `duplicate modifier "${name}"`);
|
|
89
|
+
flags[name] = true;
|
|
90
|
+
}
|
|
91
|
+
if (flags.shift && isShiftAgnostic(key)) {
|
|
92
|
+
devWarn(`"${combo}" — bind the produced character instead of shift + base key ` +
|
|
93
|
+
`(e.g. '?' rather than 'shift+/'); shifted printables match as typed.`);
|
|
94
|
+
}
|
|
95
|
+
const parts = [];
|
|
96
|
+
for (const m of MODIFIER_ORDER)
|
|
97
|
+
if (flags[m])
|
|
98
|
+
parts.push(m);
|
|
99
|
+
parts.push(key);
|
|
100
|
+
return { ...flags, key, canonical: parts.join('+') };
|
|
101
|
+
}
|
|
102
|
+
/** Parse a comma-separated combo list; throws on any malformed alternative. */
|
|
103
|
+
function parseKeys(keys) {
|
|
104
|
+
if (typeof keys !== 'string' || keys.trim() === '') {
|
|
105
|
+
throw parseError(String(keys), 'expected a non-empty combo string');
|
|
106
|
+
}
|
|
107
|
+
return keys.split(',').map((part) => parseCombo(part));
|
|
108
|
+
}
|
|
109
|
+
function toCanonical(combos) {
|
|
110
|
+
return combos.map((c) => c.canonical).join(', ');
|
|
111
|
+
}
|
|
112
|
+
// Combos the browser/OS or screen-reader browse mode already owns.
|
|
113
|
+
const RESERVED_COMBOS = new Set(['ctrl+t', 'ctrl+w', 'ctrl+n', 'mod+t', 'mod+w', 'mod+n']);
|
|
114
|
+
// NVDA/JAWS browse-mode quick keys — bare bindings never reach the page for SR users.
|
|
115
|
+
const SR_QUICK_KEYS = new Set(['h', 'k', 't', '1', '2', '3', '4', '5', '6']);
|
|
116
|
+
function warnReservedCombos(id, combos) {
|
|
117
|
+
for (const combo of combos) {
|
|
118
|
+
if (RESERVED_COMBOS.has(combo.canonical)) {
|
|
119
|
+
devWarn(`"${id}" binds "${combo.canonical}" — the browser reserves it (tab/window control).`);
|
|
120
|
+
}
|
|
121
|
+
else if (isCharacterCombo(combo) && !combo.shift && SR_QUICK_KEYS.has(combo.key)) {
|
|
122
|
+
devWarn(`"${id}" binds bare "${combo.key}" — screen-reader browse mode swallows it ` +
|
|
123
|
+
`(NVDA/JAWS quick key); prefer a mod+ combo for anything important.`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
// Pure modifier keydowns are a chord in progress, never a complete combo.
|
|
128
|
+
const CHORD_KEYS = new Set(['control', 'shift', 'alt', 'meta']);
|
|
129
|
+
/**
|
|
130
|
+
* Canonical combo string for a captured keydown — feeds the editable
|
|
131
|
+
* ShortcutsHelp recorder. Modifier-only presses return `null` (the user is
|
|
132
|
+
* mid-chord; keep waiting). Shifted printables keep the produced character
|
|
133
|
+
* (`?`, never `shift+/`), matching the matcher's shift rule. The result may
|
|
134
|
+
* still fail `parseKeys` (e.g. the space key) — recorders validate via
|
|
135
|
+
* `remap`. File-level export — not part of the public barrel surface.
|
|
136
|
+
*/
|
|
137
|
+
export function comboFromEvent(event) {
|
|
138
|
+
const key = (event.key ?? '').toLowerCase();
|
|
139
|
+
if (key === '' || CHORD_KEYS.has(key))
|
|
140
|
+
return null;
|
|
141
|
+
const parts = [];
|
|
142
|
+
if (event.ctrlKey)
|
|
143
|
+
parts.push('ctrl');
|
|
144
|
+
if (event.altKey)
|
|
145
|
+
parts.push('alt');
|
|
146
|
+
if (event.shiftKey && !isShiftAgnostic(key))
|
|
147
|
+
parts.push('shift');
|
|
148
|
+
if (event.metaKey)
|
|
149
|
+
parts.push('meta');
|
|
150
|
+
parts.push(key);
|
|
151
|
+
return parts.join('+');
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Whether any alternative in `keys` is a combo the browser/OS reserves for
|
|
155
|
+
* tab/window control. Registration merely dev-warns (`warnReservedCombos`);
|
|
156
|
+
* the ShortcutsHelp recorder rejects these outright. Accepts the literal
|
|
157
|
+
* `ctrl+`/`meta+` forms a recorded event produces as well as `mod+`
|
|
158
|
+
* spellings. Throws on malformed input (same grammar as registration).
|
|
159
|
+
* File-level export — not part of the public barrel surface.
|
|
160
|
+
*/
|
|
161
|
+
export function isReservedCombo(keys) {
|
|
162
|
+
return parseKeys(keys).some((c) => RESERVED_COMBOS.has(c.canonical) ||
|
|
163
|
+
RESERVED_COMBOS.has(c.canonical.replace(/^(?:ctrl|meta)\+/, 'mod+')));
|
|
164
|
+
}
|
|
165
|
+
// ---------------------------------------------------------------------------
|
|
166
|
+
// Environment probes (never at module scope — SSR-safe)
|
|
167
|
+
// ---------------------------------------------------------------------------
|
|
168
|
+
function hasWindow() {
|
|
169
|
+
return typeof window !== 'undefined';
|
|
170
|
+
}
|
|
171
|
+
/** Platform sniff for `mod` resolution and default formatting. */
|
|
172
|
+
function isMacLike() {
|
|
173
|
+
if (typeof navigator === 'undefined')
|
|
174
|
+
return false;
|
|
175
|
+
const nav = navigator;
|
|
176
|
+
const platform = nav.userAgentData?.platform ?? nav.platform ?? '';
|
|
177
|
+
if (/mac|iphone|ipad|ipod/i.test(platform))
|
|
178
|
+
return true;
|
|
179
|
+
return /mac os x|macintosh|iphone|ipad/i.test(nav.userAgent ?? '');
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Detected modifier-rendering platform. File-level export for `<Kbd>`'s
|
|
183
|
+
* client-side correction effect — not part of the public barrel surface.
|
|
184
|
+
*/
|
|
185
|
+
export function detectPlatform() {
|
|
186
|
+
return isMacLike() ? 'apple' : 'other';
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Open modal `<dialog>`s. Where `:modal` is verifiably supported we trust it;
|
|
190
|
+
* otherwise (jsdom's partial dialog support) any open dialog counts as modal.
|
|
191
|
+
*/
|
|
192
|
+
function openModalDialogs() {
|
|
193
|
+
if (typeof document === 'undefined')
|
|
194
|
+
return [];
|
|
195
|
+
const open = Array.from(document.querySelectorAll('dialog[open]'));
|
|
196
|
+
if (open.length === 0)
|
|
197
|
+
return [];
|
|
198
|
+
let modalSelectorSupported;
|
|
199
|
+
try {
|
|
200
|
+
modalSelectorSupported =
|
|
201
|
+
typeof CSS !== 'undefined' &&
|
|
202
|
+
typeof CSS.supports === 'function' &&
|
|
203
|
+
CSS.supports('selector(:modal)');
|
|
204
|
+
}
|
|
205
|
+
catch {
|
|
206
|
+
modalSelectorSupported = false;
|
|
207
|
+
}
|
|
208
|
+
if (!modalSelectorSupported)
|
|
209
|
+
return open;
|
|
210
|
+
return open.filter((dialog) => dialog.matches(':modal'));
|
|
211
|
+
}
|
|
212
|
+
/** Typing surface? `<input>`/`<textarea>`/`<select>` or contenteditable (incl. ancestors). */
|
|
213
|
+
function isEditableTarget(target) {
|
|
214
|
+
if (!(target instanceof Element))
|
|
215
|
+
return false;
|
|
216
|
+
const tag = target.tagName;
|
|
217
|
+
if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT')
|
|
218
|
+
return true;
|
|
219
|
+
const editable = target.closest('[contenteditable]');
|
|
220
|
+
return editable !== null && editable.getAttribute('contenteditable') !== 'false';
|
|
221
|
+
}
|
|
222
|
+
function comboMatchesEvent(combo, event, mac) {
|
|
223
|
+
if ((event.key ?? '').toLowerCase() !== combo.key)
|
|
224
|
+
return false;
|
|
225
|
+
const wantCtrl = combo.ctrl || (combo.mod && !mac);
|
|
226
|
+
const wantMeta = combo.meta || (combo.mod && mac);
|
|
227
|
+
if (event.ctrlKey !== wantCtrl)
|
|
228
|
+
return false;
|
|
229
|
+
if (event.metaKey !== wantMeta)
|
|
230
|
+
return false;
|
|
231
|
+
if (event.altKey !== combo.alt)
|
|
232
|
+
return false;
|
|
233
|
+
// Shifted printables ('?') encode shift in the key itself — ignore shiftKey.
|
|
234
|
+
if (!isShiftAgnostic(combo.key) && event.shiftKey !== combo.shift)
|
|
235
|
+
return false;
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
const noop = () => { };
|
|
239
|
+
/**
|
|
240
|
+
* Module-singleton keyboard-shortcut registry (Svelte 5 runes; house pattern
|
|
241
|
+
* of `toast.svelte.ts`). One lazy `window` keydown listener (bubble phase)
|
|
242
|
+
* serves every registration: attached on the first browser-side `add()`,
|
|
243
|
+
* detached when the registry empties. Exactly one shortcut fires per event —
|
|
244
|
+
* focus-scoped beats global, last-registered wins within a tier.
|
|
245
|
+
*
|
|
246
|
+
* The class is intentionally NOT exported: a second instance would break the
|
|
247
|
+
* "exactly one fires" guarantee. Use the `shortcuts` singleton.
|
|
248
|
+
*/
|
|
249
|
+
class ShortcutRegistry {
|
|
250
|
+
/** Global kill switch (WCAG 2.1.4 "turn off"). */
|
|
251
|
+
enabled = $state(true);
|
|
252
|
+
/**
|
|
253
|
+
* When `false`, modifier-less ("character key") bindings never fire — the
|
|
254
|
+
* GitHub-style WCAG 2.1.4 switch. `mod+`/`ctrl+`/`alt+` combos are unaffected.
|
|
255
|
+
*/
|
|
256
|
+
characterKeys = $state(true);
|
|
257
|
+
#registrations = $state([]);
|
|
258
|
+
#overrides = $state({});
|
|
259
|
+
/** Parsed forms of remapped combos, keyed by shortcut id (kept off $state). */
|
|
260
|
+
#overrideCombos = new Map();
|
|
261
|
+
#seq = 0;
|
|
262
|
+
#listening = false;
|
|
263
|
+
/** Reactive read-only list of registered shortcuts, registration order. */
|
|
264
|
+
items = $derived.by(() => this.#registrations.map((r) => ({
|
|
265
|
+
id: r.id,
|
|
266
|
+
label: r.label,
|
|
267
|
+
group: r.group,
|
|
268
|
+
scope: r.scope,
|
|
269
|
+
keys: this.#overrides[r.id]?.keys ?? r.defaultKeys,
|
|
270
|
+
defaultKeys: r.defaultKeys,
|
|
271
|
+
enabled: this.#effectiveEnabled(r),
|
|
272
|
+
})));
|
|
273
|
+
/**
|
|
274
|
+
* Register a shortcut; returns an idempotent disposer. Throws on a
|
|
275
|
+
* malformed `keys` string. On the server (no `window`) this is a full
|
|
276
|
+
* no-op that returns a working noop disposer.
|
|
277
|
+
*
|
|
278
|
+
* The optional `element` is supplied by the `shortcut()` attachment and
|
|
279
|
+
* anchors `scope: 'focus'` registrations.
|
|
280
|
+
*/
|
|
281
|
+
add(options, element) {
|
|
282
|
+
const combos = parseKeys(options.keys); // validate even on the server — fail loudly everywhere
|
|
283
|
+
if (!hasWindow())
|
|
284
|
+
return noop;
|
|
285
|
+
// untrack: add() runs inside the shortcut() attachment's effect — reading
|
|
286
|
+
// the registration list here must not make that effect re-run on every
|
|
287
|
+
// registry change (it would tear itself down mid-flush).
|
|
288
|
+
return untrack(() => this.#register(options, combos, element));
|
|
289
|
+
}
|
|
290
|
+
#register(options, combos, element) {
|
|
291
|
+
if (this.#registrations.some((r) => r.id === options.id)) {
|
|
292
|
+
devWarn(`duplicate id "${options.id}" — the last registration wins when combos collide.`);
|
|
293
|
+
}
|
|
294
|
+
warnReservedCombos(options.id, combos);
|
|
295
|
+
const scope = options.scope ?? 'global';
|
|
296
|
+
if (scope === 'focus' && !element) {
|
|
297
|
+
devWarn(`"${options.id}" has scope 'focus' but no element — register it through the ` +
|
|
298
|
+
`shortcut() attachment; it will never fire.`);
|
|
299
|
+
}
|
|
300
|
+
const reg = {
|
|
301
|
+
seq: ++this.#seq,
|
|
302
|
+
id: options.id,
|
|
303
|
+
label: options.label,
|
|
304
|
+
group: options.group,
|
|
305
|
+
scope,
|
|
306
|
+
defaultKeys: toCanonical(combos),
|
|
307
|
+
combos,
|
|
308
|
+
enabledOpt: options.enabled ?? true,
|
|
309
|
+
allowInInputs: options.allowInInputs ?? false,
|
|
310
|
+
allowRepeat: options.allowRepeat ?? false,
|
|
311
|
+
preventDefault: options.preventDefault ?? true,
|
|
312
|
+
onPress: options.onPress,
|
|
313
|
+
element,
|
|
314
|
+
};
|
|
315
|
+
this.#registrations.push(reg);
|
|
316
|
+
this.#attachListener();
|
|
317
|
+
let disposed = false;
|
|
318
|
+
return () => {
|
|
319
|
+
if (disposed)
|
|
320
|
+
return;
|
|
321
|
+
disposed = true;
|
|
322
|
+
untrack(() => {
|
|
323
|
+
const i = this.#registrations.findIndex((r) => r.seq === reg.seq);
|
|
324
|
+
if (i !== -1)
|
|
325
|
+
this.#registrations.splice(i, 1);
|
|
326
|
+
this.#detachListenerIfEmpty();
|
|
327
|
+
});
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
/** User-level enable/disable override for one shortcut (persists in overrides). */
|
|
331
|
+
setEnabled(id, enabled) {
|
|
332
|
+
this.#overrides[id] = { ...this.#overrides[id], enabled };
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Rebind a shortcut to new keys (parsed/validated immediately — throws on
|
|
336
|
+
* malformed input). `remap(id, null)` removes the override, restoring the
|
|
337
|
+
* registered default.
|
|
338
|
+
*/
|
|
339
|
+
remap(id, keys) {
|
|
340
|
+
if (keys === null) {
|
|
341
|
+
const existing = this.#overrides[id];
|
|
342
|
+
if (existing) {
|
|
343
|
+
delete existing.keys;
|
|
344
|
+
if (existing.enabled === undefined)
|
|
345
|
+
delete this.#overrides[id];
|
|
346
|
+
}
|
|
347
|
+
this.#overrideCombos.delete(id);
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
const combos = parseKeys(keys);
|
|
351
|
+
warnReservedCombos(id, combos);
|
|
352
|
+
this.#overrides[id] = { ...this.#overrides[id], keys: toCanonical(combos) };
|
|
353
|
+
this.#overrideCombos.set(id, combos);
|
|
354
|
+
}
|
|
355
|
+
/** Serializable snapshot of the global toggles + every per-id override. */
|
|
356
|
+
getOverrides() {
|
|
357
|
+
return {
|
|
358
|
+
enabled: this.enabled,
|
|
359
|
+
characterKeys: this.characterKeys,
|
|
360
|
+
shortcuts: $state.snapshot(this.#overrides),
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Merge a persisted overrides snapshot back in. Overrides for ids that are
|
|
365
|
+
* not (yet) registered are kept and take effect when the shortcut mounts.
|
|
366
|
+
* Malformed persisted combos are skipped with a dev warning (stale storage
|
|
367
|
+
* must not crash the app).
|
|
368
|
+
*/
|
|
369
|
+
applyOverrides(overrides) {
|
|
370
|
+
if (typeof overrides.enabled === 'boolean')
|
|
371
|
+
this.enabled = overrides.enabled;
|
|
372
|
+
if (typeof overrides.characterKeys === 'boolean')
|
|
373
|
+
this.characterKeys = overrides.characterKeys;
|
|
374
|
+
if (!overrides.shortcuts)
|
|
375
|
+
return;
|
|
376
|
+
for (const [id, override] of Object.entries(overrides.shortcuts)) {
|
|
377
|
+
if (typeof override.enabled === 'boolean')
|
|
378
|
+
this.setEnabled(id, override.enabled);
|
|
379
|
+
if (typeof override.keys === 'string') {
|
|
380
|
+
try {
|
|
381
|
+
this.remap(id, override.keys);
|
|
382
|
+
}
|
|
383
|
+
catch {
|
|
384
|
+
devWarn(`ignoring invalid persisted combo "${override.keys}" for "${id}".`);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
/** Clear every per-id override and restore the global toggles to defaults. */
|
|
390
|
+
resetOverrides() {
|
|
391
|
+
this.enabled = true;
|
|
392
|
+
this.characterKeys = true;
|
|
393
|
+
this.#overrides = {};
|
|
394
|
+
this.#overrideCombos.clear();
|
|
395
|
+
}
|
|
396
|
+
#effectiveEnabled(r) {
|
|
397
|
+
const userEnabled = this.#overrides[r.id]?.enabled ?? true;
|
|
398
|
+
if (!userEnabled)
|
|
399
|
+
return false;
|
|
400
|
+
return typeof r.enabledOpt === 'function' ? r.enabledOpt() : r.enabledOpt;
|
|
401
|
+
}
|
|
402
|
+
#attachListener() {
|
|
403
|
+
if (this.#listening || !hasWindow())
|
|
404
|
+
return;
|
|
405
|
+
window.addEventListener('keydown', this.#onKeydown);
|
|
406
|
+
this.#listening = true;
|
|
407
|
+
}
|
|
408
|
+
#detachListenerIfEmpty() {
|
|
409
|
+
if (!this.#listening || this.#registrations.length > 0 || !hasWindow())
|
|
410
|
+
return;
|
|
411
|
+
window.removeEventListener('keydown', this.#onKeydown);
|
|
412
|
+
this.#listening = false;
|
|
413
|
+
}
|
|
414
|
+
#onKeydown = (event) => {
|
|
415
|
+
if (!this.enabled)
|
|
416
|
+
return;
|
|
417
|
+
if (event.defaultPrevented)
|
|
418
|
+
return; // a component that handled the key wins over globals
|
|
419
|
+
if (event.isComposing)
|
|
420
|
+
return; // IME composition
|
|
421
|
+
const mac = isMacLike();
|
|
422
|
+
const modals = openModalDialogs();
|
|
423
|
+
// Focus-scoped tier beats global; within a tier, last-registered wins.
|
|
424
|
+
const match = this.#findMatch(event, mac, modals, 'focus') ?? this.#findMatch(event, mac, modals, 'global');
|
|
425
|
+
if (!match)
|
|
426
|
+
return;
|
|
427
|
+
if (match.preventDefault)
|
|
428
|
+
event.preventDefault();
|
|
429
|
+
match.onPress(event);
|
|
430
|
+
};
|
|
431
|
+
#findMatch(event, mac, modals, scope) {
|
|
432
|
+
const regs = this.#registrations;
|
|
433
|
+
for (let i = regs.length - 1; i >= 0; i--) {
|
|
434
|
+
const r = regs[i];
|
|
435
|
+
if (r.scope !== scope)
|
|
436
|
+
continue;
|
|
437
|
+
const combos = this.#overrideCombos.get(r.id) ?? r.combos;
|
|
438
|
+
const combo = combos.find((c) => comboMatchesEvent(c, event, mac));
|
|
439
|
+
if (!combo)
|
|
440
|
+
continue;
|
|
441
|
+
if (event.repeat && !r.allowRepeat)
|
|
442
|
+
continue;
|
|
443
|
+
if (!this.characterKeys && isCharacterCombo(combo))
|
|
444
|
+
continue;
|
|
445
|
+
if (!r.allowInInputs && isEditableTarget(event.target))
|
|
446
|
+
continue;
|
|
447
|
+
if (!this.#effectiveEnabled(r))
|
|
448
|
+
continue;
|
|
449
|
+
if (scope === 'focus') {
|
|
450
|
+
if (!r.element)
|
|
451
|
+
continue;
|
|
452
|
+
if (!(event.target instanceof Node) || !r.element.contains(event.target))
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
// SC 2.1.2: an open modal dialog makes the rest of the page inert for
|
|
456
|
+
// focus but NOT for window key handlers — suppress everything except
|
|
457
|
+
// focus-scoped shortcuts anchored inside the dialog.
|
|
458
|
+
if (modals.length > 0) {
|
|
459
|
+
const insideModal = scope === 'focus' && r.element != null && modals.some((m) => m.contains(r.element));
|
|
460
|
+
if (!insideModal)
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
return r;
|
|
464
|
+
}
|
|
465
|
+
return null;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
/** The one app-wide shortcut registry. */
|
|
469
|
+
export const shortcuts = new ShortcutRegistry();
|
|
470
|
+
/**
|
|
471
|
+
* `{@attach}` sugar over `shortcuts.add()`: registers on mount, cleans up on
|
|
472
|
+
* unmount, and re-registers reactively when a thunk's dependencies change.
|
|
473
|
+
* The attached element anchors `scope: 'focus'` shortcuts (they only fire
|
|
474
|
+
* while the event target is inside it).
|
|
475
|
+
*
|
|
476
|
+
* ```svelte
|
|
477
|
+
* <section {@attach shortcut(() => ({ id: 'app:save', label: 'Save', keys: 'mod+s', onPress: save }))}>
|
|
478
|
+
* ```
|
|
479
|
+
*/
|
|
480
|
+
export function shortcut(options) {
|
|
481
|
+
return (element) => {
|
|
482
|
+
// Resolving the thunk here (inside the attachment's effect) makes the
|
|
483
|
+
// registration re-run when any reactive state it reads changes.
|
|
484
|
+
const resolved = typeof options === 'function' ? options() : options;
|
|
485
|
+
return shortcuts.add(resolved, element);
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
// ---------------------------------------------------------------------------
|
|
489
|
+
// Pure formatters (no registry coupling)
|
|
490
|
+
// ---------------------------------------------------------------------------
|
|
491
|
+
const APPLE_GLYPHS = {
|
|
492
|
+
mod: '⌘',
|
|
493
|
+
meta: '⌘',
|
|
494
|
+
ctrl: '⌃',
|
|
495
|
+
alt: '⌥',
|
|
496
|
+
shift: '⇧',
|
|
497
|
+
};
|
|
498
|
+
const OTHER_GLYPHS = {
|
|
499
|
+
mod: 'Ctrl',
|
|
500
|
+
ctrl: 'Ctrl',
|
|
501
|
+
meta: 'Meta',
|
|
502
|
+
alt: 'Alt',
|
|
503
|
+
shift: 'Shift',
|
|
504
|
+
};
|
|
505
|
+
const APPLE_LABELS = {
|
|
506
|
+
mod: 'Command',
|
|
507
|
+
meta: 'Command',
|
|
508
|
+
ctrl: 'Control',
|
|
509
|
+
alt: 'Option',
|
|
510
|
+
shift: 'Shift',
|
|
511
|
+
};
|
|
512
|
+
const OTHER_LABELS = OTHER_GLYPHS;
|
|
513
|
+
const KEY_GLYPHS = {
|
|
514
|
+
arrowup: '↑',
|
|
515
|
+
arrowdown: '↓',
|
|
516
|
+
arrowleft: '←',
|
|
517
|
+
arrowright: '→',
|
|
518
|
+
enter: '↵',
|
|
519
|
+
escape: 'Esc',
|
|
520
|
+
backspace: '⌫',
|
|
521
|
+
delete: 'Del',
|
|
522
|
+
tab: '⇥',
|
|
523
|
+
' ': 'Space',
|
|
524
|
+
pageup: 'PgUp',
|
|
525
|
+
pagedown: 'PgDn',
|
|
526
|
+
};
|
|
527
|
+
const KEY_LABELS = {
|
|
528
|
+
arrowup: 'Up Arrow',
|
|
529
|
+
arrowdown: 'Down Arrow',
|
|
530
|
+
arrowleft: 'Left Arrow',
|
|
531
|
+
arrowright: 'Right Arrow',
|
|
532
|
+
enter: 'Enter',
|
|
533
|
+
escape: 'Escape',
|
|
534
|
+
backspace: 'Backspace',
|
|
535
|
+
delete: 'Delete',
|
|
536
|
+
tab: 'Tab',
|
|
537
|
+
' ': 'Space',
|
|
538
|
+
pageup: 'Page Up',
|
|
539
|
+
pagedown: 'Page Down',
|
|
540
|
+
};
|
|
541
|
+
function displayKey(key, format) {
|
|
542
|
+
const table = format === 'glyph' ? KEY_GLYPHS : KEY_LABELS;
|
|
543
|
+
const mapped = table[key];
|
|
544
|
+
if (mapped)
|
|
545
|
+
return mapped;
|
|
546
|
+
if (key.length === 1)
|
|
547
|
+
return key.toUpperCase();
|
|
548
|
+
return key.charAt(0).toUpperCase() + key.slice(1); // 'f1' → 'F1', 'home' → 'Home'
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Structured form behind `formatShortcut`: one keycap-string array per
|
|
552
|
+
* comma-alternative (`'?, mod+/'` on other → `[['?'], ['Ctrl', '/']]`).
|
|
553
|
+
* File-level export so `<Kbd>` can render one `<kbd>` per key without
|
|
554
|
+
* re-deriving the glyph/label tables — not part of the public barrel surface.
|
|
555
|
+
* Throws on malformed input (same grammar as registration).
|
|
556
|
+
*/
|
|
557
|
+
export function formatShortcutParts(keys, options = {}) {
|
|
558
|
+
const format = options.format ?? 'glyph';
|
|
559
|
+
const platform = options.platform ?? detectPlatform();
|
|
560
|
+
const mods = format === 'glyph'
|
|
561
|
+
? platform === 'apple'
|
|
562
|
+
? APPLE_GLYPHS
|
|
563
|
+
: OTHER_GLYPHS
|
|
564
|
+
: platform === 'apple'
|
|
565
|
+
? APPLE_LABELS
|
|
566
|
+
: OTHER_LABELS;
|
|
567
|
+
return parseKeys(keys).map((combo) => {
|
|
568
|
+
const parts = [];
|
|
569
|
+
for (const m of MODIFIER_ORDER)
|
|
570
|
+
if (combo[m])
|
|
571
|
+
parts.push(mods[m]);
|
|
572
|
+
parts.push(displayKey(combo.key, format));
|
|
573
|
+
return parts;
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Human-readable rendering of a combo string, for `<Kbd>`-style display.
|
|
578
|
+
* Alternatives are joined with ` / `. Apple glyphs concatenate (`⌘K`, the
|
|
579
|
+
* macOS convention); everything else joins with `+` (`Ctrl+K`). Throws on
|
|
580
|
+
* malformed input (same grammar as registration).
|
|
581
|
+
*/
|
|
582
|
+
export function formatShortcut(keys, options = {}) {
|
|
583
|
+
const format = options.format ?? 'glyph';
|
|
584
|
+
const platform = options.platform ?? detectPlatform();
|
|
585
|
+
return formatShortcutParts(keys, options)
|
|
586
|
+
.map((parts) => (format === 'glyph' && platform === 'apple' ? parts.join('') : parts.join('+')))
|
|
587
|
+
.join(' / ');
|
|
588
|
+
}
|
|
589
|
+
// UI Events canonical casing for `aria-keyshortcuts` values.
|
|
590
|
+
const ARIA_KEYS = {
|
|
591
|
+
escape: 'Escape',
|
|
592
|
+
enter: 'Enter',
|
|
593
|
+
tab: 'Tab',
|
|
594
|
+
backspace: 'Backspace',
|
|
595
|
+
delete: 'Delete',
|
|
596
|
+
arrowup: 'ArrowUp',
|
|
597
|
+
arrowdown: 'ArrowDown',
|
|
598
|
+
arrowleft: 'ArrowLeft',
|
|
599
|
+
arrowright: 'ArrowRight',
|
|
600
|
+
home: 'Home',
|
|
601
|
+
end: 'End',
|
|
602
|
+
pageup: 'PageUp',
|
|
603
|
+
pagedown: 'PageDown',
|
|
604
|
+
' ': 'Space',
|
|
605
|
+
'+': 'Plus',
|
|
606
|
+
};
|
|
607
|
+
/**
|
|
608
|
+
* The combo in `aria-keyshortcuts` spec syntax (`Meta+K`), with `mod`
|
|
609
|
+
* resolved for the given (default: detected) platform. Alternatives are
|
|
610
|
+
* space-separated per the spec. Throws on malformed input.
|
|
611
|
+
*/
|
|
612
|
+
export function ariaKeyshortcuts(keys, platform) {
|
|
613
|
+
const plat = platform ?? detectPlatform();
|
|
614
|
+
return parseKeys(keys)
|
|
615
|
+
.map((combo) => {
|
|
616
|
+
const parts = [];
|
|
617
|
+
if (combo.ctrl || (combo.mod && plat === 'other'))
|
|
618
|
+
parts.push('Control');
|
|
619
|
+
if (combo.alt)
|
|
620
|
+
parts.push('Alt');
|
|
621
|
+
if (combo.shift)
|
|
622
|
+
parts.push('Shift');
|
|
623
|
+
if (combo.meta || (combo.mod && plat === 'apple'))
|
|
624
|
+
parts.push('Meta');
|
|
625
|
+
const mapped = ARIA_KEYS[combo.key];
|
|
626
|
+
parts.push(mapped ??
|
|
627
|
+
(combo.key.length === 1
|
|
628
|
+
? combo.key.toUpperCase()
|
|
629
|
+
: combo.key.charAt(0).toUpperCase() + combo.key.slice(1)));
|
|
630
|
+
return parts.join('+');
|
|
631
|
+
})
|
|
632
|
+
.join(' ');
|
|
633
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dssoca",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "Svelte 5 design system — signal green on near-black, monospace-forward, zero border-radius; configured along two axes (color: dark/light, density: comfy/compact).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Rafael Passoca",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"./tokens.css": "./dist/tokens.css"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"svelte": "^5.
|
|
39
|
+
"svelte": "^5.29.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@eslint/js": "^10.0.1",
|