@rapidmx/web-client 0.9.0 → 0.10.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/README.md +192 -88
- package/apps/shared/auth/accountUrl.ts +9 -0
- package/apps/shared/auth/adminAccess.ts +99 -0
- package/apps/shared/components/admin/settings/DomainDnsSetup.tsx +2 -1
- package/apps/shared/components/calendar/layout/CalendarShell.tsx +8 -2
- package/apps/shared/components/contacts/ContactsToolbar.tsx +125 -115
- package/apps/shared/components/contacts/layout/ContactsShell.tsx +15 -3
- package/apps/shared/components/layout/AppShell.tsx +411 -288
- package/apps/shared/components/layout/KeyEnrollmentGate.tsx +398 -389
- package/apps/shared/components/layout/MailboxProvisioning.tsx +168 -164
- package/apps/shared/components/layout/UserMenu.tsx +303 -185
- package/apps/shared/components/mail/ConversationList.tsx +269 -262
- package/apps/shared/components/mail/ConversationThreadPane.tsx +475 -434
- package/apps/shared/components/mail/LazyReadingPane.tsx +115 -0
- package/apps/shared/components/mail/MailSelectionBar.tsx +235 -222
- package/apps/shared/components/mail/MessageDetailPane.tsx +1458 -1387
- package/apps/shared/components/mail/NewMailToasts.tsx +141 -0
- package/apps/shared/components/mail/compose/ComposeContext.tsx +279 -160
- package/apps/shared/components/mail/compose/ComposeToolbar.tsx +422 -418
- package/apps/shared/components/mail/compose/ComposeWindow.tsx +1842 -1736
- package/apps/shared/components/mail/compose/ComposeWindowPlaceholder.tsx +111 -0
- package/apps/shared/components/mail/compose/RichTextEditor.tsx +147 -118
- package/apps/shared/components/mail/compose/composePerf.ts +46 -0
- package/apps/shared/components/mail/compose/quotedBody.ts +161 -100
- package/apps/shared/components/mail/layout/MailShell.tsx +476 -472
- package/apps/shared/components/mail/unreadStyle.tsx +75 -0
- package/apps/shared/components/settings/layout/SettingsShell.tsx +252 -240
- package/apps/shared/components/tasks/layout/TasksShell.tsx +15 -3
- package/apps/shared/keyboard/GlobalShortcuts.tsx +51 -0
- package/apps/shared/keyboard/ShortcutProvider.tsx +62 -0
- package/apps/shared/keyboard/ShortcutsDialog.tsx +84 -0
- package/apps/shared/keyboard/dispatch.ts +124 -0
- package/apps/shared/keyboard/format.ts +89 -0
- package/apps/shared/keyboard/keymap.ts +114 -0
- package/apps/shared/keyboard/match.ts +44 -0
- package/apps/shared/keyboard/parse.ts +136 -0
- package/apps/shared/keyboard/platform.ts +34 -0
- package/apps/shared/keyboard/registry.ts +65 -0
- package/apps/shared/keyboard/targets.ts +79 -0
- package/apps/shared/keyboard/useShortcut.ts +50 -0
- package/apps/shared/keyboard/useShortcutProps.ts +17 -0
- package/apps/shared/mail/folderCounts.ts +302 -0
- package/apps/shared/mail/listSnapshots.ts +87 -0
- package/apps/shared/mail/messageReadState.ts +85 -0
- package/apps/shared/mail/newMailNotifications.ts +183 -0
- package/apps/shared/mail/useMailConnection.ts +150 -0
- package/apps/shared/mail/useMailLiveUpdates.ts +232 -224
- package/apps/shared/mail/useMarkMessageRead.ts +47 -0
- package/apps/shared/mail/useNewMailNotifications.ts +163 -0
- package/apps/shared/mail/useUnreadTitle.ts +42 -0
- package/apps/shared/navigation/AppRouter.tsx +300 -0
- package/apps/shared/navigation/appHrefs.ts +23 -0
- package/apps/shared/navigation/frameContext.tsx +35 -0
- package/apps/shared/navigation/idle.ts +45 -0
- package/apps/shared/navigation/routerContext.tsx +83 -0
- package/apps/shared/navigation/routes.ts +72 -0
- package/apps/shared/search/LocalIndexLifecycle.tsx +114 -98
- package/apps/shared/styles/app.css +28 -10
- package/apps/www/_routedPage.tsx +24 -0
- package/apps/www/_routes.ts +35 -0
- package/apps/www/calendar/index.tsx +55 -19
- package/apps/www/contacts/[uid].tsx +112 -107
- package/apps/www/contacts/index.tsx +584 -567
- package/apps/www/index.tsx +2280 -1917
- package/apps/www/messages/[uid].tsx +106 -101
- package/apps/www/settings/auto-reply/index.tsx +4 -1
- package/apps/www/settings/encryption/index.tsx +1252 -1249
- package/apps/www/settings/filters/[uid].tsx +4 -1
- package/apps/www/settings/filters/index.tsx +102 -99
- package/apps/www/settings/filters/new/index.tsx +138 -133
- package/apps/www/settings/labels/index.tsx +204 -201
- package/apps/www/settings/privacy/index.tsx +4 -1
- package/apps/www/settings/read-receipts/index.tsx +4 -1
- package/apps/www/settings/sharing/index.tsx +277 -274
- package/apps/www/settings/signatures/[uid].tsx +170 -167
- package/apps/www/settings/signatures/index.tsx +88 -85
- package/apps/www/settings/signatures/new/index.tsx +134 -129
- package/apps/www/tasks/index.tsx +18 -2
- package/dist/apps/shared/auth/accountUrl.d.ts +2 -0
- package/dist/apps/shared/auth/accountUrl.js +8 -0
- package/dist/apps/shared/auth/adminAccess.d.ts +30 -0
- package/dist/apps/shared/auth/adminAccess.js +89 -0
- package/dist/apps/shared/components/admin/settings/DomainDnsSetup.js +1 -1
- package/dist/apps/shared/components/calendar/layout/CalendarShell.d.ts +1 -1
- package/dist/apps/shared/components/calendar/layout/CalendarShell.js +8 -4
- package/dist/apps/shared/components/contacts/ContactsToolbar.d.ts +3 -1
- package/dist/apps/shared/components/contacts/ContactsToolbar.js +7 -4
- package/dist/apps/shared/components/contacts/layout/ContactsShell.d.ts +1 -1
- package/dist/apps/shared/components/contacts/layout/ContactsShell.js +15 -5
- package/dist/apps/shared/components/layout/AppShell.d.ts +29 -4
- package/dist/apps/shared/components/layout/AppShell.js +73 -14
- package/dist/apps/shared/components/layout/KeyEnrollmentGate.js +10 -9
- package/dist/apps/shared/components/layout/MailboxProvisioning.js +3 -1
- package/dist/apps/shared/components/layout/UserMenu.d.ts +19 -3
- package/dist/apps/shared/components/layout/UserMenu.js +52 -5
- package/dist/apps/shared/components/mail/ConversationList.js +4 -11
- package/dist/apps/shared/components/mail/ConversationThreadPane.d.ts +4 -1
- package/dist/apps/shared/components/mail/ConversationThreadPane.js +52 -27
- package/dist/apps/shared/components/mail/LazyReadingPane.d.ts +9 -0
- package/dist/apps/shared/components/mail/LazyReadingPane.js +82 -0
- package/dist/apps/shared/components/mail/MailSelectionBar.d.ts +4 -1
- package/dist/apps/shared/components/mail/MailSelectionBar.js +11 -3
- package/dist/apps/shared/components/mail/MessageDetailPane.d.ts +7 -1
- package/dist/apps/shared/components/mail/MessageDetailPane.js +83 -34
- package/dist/apps/shared/components/mail/NewMailToasts.d.ts +19 -0
- package/dist/apps/shared/components/mail/NewMailToasts.js +55 -0
- package/dist/apps/shared/components/mail/compose/ComposeContext.d.ts +30 -0
- package/dist/apps/shared/components/mail/compose/ComposeContext.js +71 -5
- package/dist/apps/shared/components/mail/compose/ComposeToolbar.js +4 -3
- package/dist/apps/shared/components/mail/compose/ComposeWindow.d.ts +1 -1
- package/dist/apps/shared/components/mail/compose/ComposeWindow.js +98 -22
- package/dist/apps/shared/components/mail/compose/ComposeWindowPlaceholder.d.ts +19 -0
- package/dist/apps/shared/components/mail/compose/ComposeWindowPlaceholder.js +33 -0
- package/dist/apps/shared/components/mail/compose/RichTextEditor.d.ts +9 -1
- package/dist/apps/shared/components/mail/compose/RichTextEditor.js +20 -2
- package/dist/apps/shared/components/mail/compose/composePerf.d.ts +16 -0
- package/dist/apps/shared/components/mail/compose/composePerf.js +41 -0
- package/dist/apps/shared/components/mail/compose/quotedBody.d.ts +13 -0
- package/dist/apps/shared/components/mail/compose/quotedBody.js +67 -11
- package/dist/apps/shared/components/mail/layout/MailShell.d.ts +12 -4
- package/dist/apps/shared/components/mail/layout/MailShell.js +76 -96
- package/dist/apps/shared/components/mail/unreadStyle.d.ts +45 -0
- package/dist/apps/shared/components/mail/unreadStyle.js +61 -0
- package/dist/apps/shared/components/settings/layout/SettingsShell.d.ts +1 -1
- package/dist/apps/shared/components/settings/layout/SettingsShell.js +15 -5
- package/dist/apps/shared/components/tasks/layout/TasksShell.d.ts +1 -1
- package/dist/apps/shared/components/tasks/layout/TasksShell.js +15 -5
- package/dist/apps/shared/keyboard/GlobalShortcuts.d.ts +14 -0
- package/dist/apps/shared/keyboard/GlobalShortcuts.js +37 -0
- package/dist/apps/shared/keyboard/ShortcutProvider.d.ts +20 -0
- package/dist/apps/shared/keyboard/ShortcutProvider.js +52 -0
- package/dist/apps/shared/keyboard/ShortcutsDialog.d.ts +14 -0
- package/dist/apps/shared/keyboard/ShortcutsDialog.js +42 -0
- package/dist/apps/shared/keyboard/dispatch.d.ts +16 -0
- package/dist/apps/shared/keyboard/dispatch.js +109 -0
- package/dist/apps/shared/keyboard/format.d.ts +12 -0
- package/dist/apps/shared/keyboard/format.js +74 -0
- package/dist/apps/shared/keyboard/keymap.d.ts +294 -0
- package/dist/apps/shared/keyboard/keymap.js +84 -0
- package/dist/apps/shared/keyboard/match.d.ts +20 -0
- package/dist/apps/shared/keyboard/match.js +31 -0
- package/dist/apps/shared/keyboard/parse.d.ts +31 -0
- package/dist/apps/shared/keyboard/parse.js +107 -0
- package/dist/apps/shared/keyboard/platform.d.ts +15 -0
- package/dist/apps/shared/keyboard/platform.js +22 -0
- package/dist/apps/shared/keyboard/registry.d.ts +39 -0
- package/dist/apps/shared/keyboard/registry.js +33 -0
- package/dist/apps/shared/keyboard/targets.d.ts +14 -0
- package/dist/apps/shared/keyboard/targets.js +67 -0
- package/dist/apps/shared/keyboard/useShortcut.d.ts +19 -0
- package/dist/apps/shared/keyboard/useShortcut.js +35 -0
- package/dist/apps/shared/keyboard/useShortcutProps.d.ts +10 -0
- package/dist/apps/shared/keyboard/useShortcutProps.js +15 -0
- package/dist/apps/shared/mail/folderCounts.d.ts +78 -0
- package/dist/apps/shared/mail/folderCounts.js +212 -0
- package/dist/apps/shared/mail/listSnapshots.d.ts +46 -0
- package/dist/apps/shared/mail/listSnapshots.js +43 -0
- package/dist/apps/shared/mail/messageReadState.d.ts +32 -0
- package/dist/apps/shared/mail/messageReadState.js +63 -0
- package/dist/apps/shared/mail/newMailNotifications.d.ts +62 -0
- package/dist/apps/shared/mail/newMailNotifications.js +138 -0
- package/dist/apps/shared/mail/useMailConnection.d.ts +55 -0
- package/dist/apps/shared/mail/useMailConnection.js +96 -0
- package/dist/apps/shared/mail/useMailLiveUpdates.d.ts +13 -6
- package/dist/apps/shared/mail/useMailLiveUpdates.js +28 -26
- package/dist/apps/shared/mail/useMarkMessageRead.d.ts +12 -0
- package/dist/apps/shared/mail/useMarkMessageRead.js +44 -0
- package/dist/apps/shared/mail/useNewMailNotifications.d.ts +41 -0
- package/dist/apps/shared/mail/useNewMailNotifications.js +110 -0
- package/dist/apps/shared/mail/useUnreadTitle.d.ts +18 -0
- package/dist/apps/shared/mail/useUnreadTitle.js +31 -0
- package/dist/apps/shared/navigation/AppRouter.d.ts +52 -0
- package/dist/apps/shared/navigation/AppRouter.js +242 -0
- package/dist/apps/shared/navigation/appHrefs.d.ts +14 -0
- package/dist/apps/shared/navigation/appHrefs.js +20 -0
- package/dist/apps/shared/navigation/frameContext.d.ts +19 -0
- package/dist/apps/shared/navigation/frameContext.js +24 -0
- package/dist/apps/shared/navigation/idle.d.ts +14 -0
- package/dist/apps/shared/navigation/idle.js +43 -0
- package/dist/apps/shared/navigation/routerContext.d.ts +37 -0
- package/dist/apps/shared/navigation/routerContext.js +56 -0
- package/dist/apps/shared/navigation/routes.d.ts +32 -0
- package/dist/apps/shared/navigation/routes.js +37 -0
- package/dist/apps/shared/search/LocalIndexLifecycle.js +17 -3
- package/dist/apps/shared/styles/app.css +28 -10
- package/dist/apps/www/_routedPage.d.ts +12 -0
- package/dist/apps/www/_routedPage.js +19 -0
- package/dist/apps/www/_routes.d.ts +11 -0
- package/dist/apps/www/_routes.js +29 -0
- package/dist/apps/www/calendar/index.d.ts +2 -2
- package/dist/apps/www/calendar/index.js +39 -8
- package/dist/apps/www/contacts/[uid].d.ts +3 -9
- package/dist/apps/www/contacts/[uid].js +6 -2
- package/dist/apps/www/contacts/index.d.ts +2 -2
- package/dist/apps/www/contacts/index.js +17 -4
- package/dist/apps/www/index.d.ts +2 -2
- package/dist/apps/www/index.js +301 -35
- package/dist/apps/www/messages/[uid].d.ts +3 -7
- package/dist/apps/www/messages/[uid].js +7 -4
- package/dist/apps/www/settings/auto-reply/index.d.ts +2 -2
- package/dist/apps/www/settings/auto-reply/index.js +3 -1
- package/dist/apps/www/settings/encryption/index.d.ts +2 -2
- package/dist/apps/www/settings/encryption/index.js +3 -1
- package/dist/apps/www/settings/filters/[uid].d.ts +2 -2
- package/dist/apps/www/settings/filters/[uid].js +3 -1
- package/dist/apps/www/settings/filters/index.d.ts +2 -2
- package/dist/apps/www/settings/filters/index.js +3 -1
- package/dist/apps/www/settings/filters/new/index.d.ts +2 -2
- package/dist/apps/www/settings/filters/new/index.js +6 -2
- package/dist/apps/www/settings/labels/index.d.ts +2 -2
- package/dist/apps/www/settings/labels/index.js +3 -1
- package/dist/apps/www/settings/privacy/index.d.ts +2 -2
- package/dist/apps/www/settings/privacy/index.js +3 -1
- package/dist/apps/www/settings/read-receipts/index.d.ts +2 -2
- package/dist/apps/www/settings/read-receipts/index.js +3 -1
- package/dist/apps/www/settings/sharing/index.d.ts +2 -2
- package/dist/apps/www/settings/sharing/index.js +3 -1
- package/dist/apps/www/settings/signatures/[uid].d.ts +2 -2
- package/dist/apps/www/settings/signatures/[uid].js +3 -1
- package/dist/apps/www/settings/signatures/index.d.ts +2 -2
- package/dist/apps/www/settings/signatures/index.js +3 -1
- package/dist/apps/www/settings/signatures/new/index.d.ts +2 -2
- package/dist/apps/www/settings/signatures/new/index.js +6 -2
- package/dist/apps/www/tasks/index.d.ts +2 -2
- package/dist/apps/www/tasks/index.js +15 -3
- package/package.json +2 -2
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2026 Jean-Philippe Steinmetz
|
|
3
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5
|
+
import type { KeyEnvironment } from "./platform.js";
|
|
6
|
+
|
|
7
|
+
/** A shortcut such as `ctrl+shift+a`, `alt+n`, `Delete` or `?`, broken into what a key event is compared with. */
|
|
8
|
+
export interface ParsedSpec {
|
|
9
|
+
/** The spec as written. */
|
|
10
|
+
spec: string;
|
|
11
|
+
/** The key, lower-case: a letter or digit, a symbol (`?`, `/`, `.`, `,`), or a named key (`enter`, `delete`, `arrowup`, ...). */
|
|
12
|
+
key: string;
|
|
13
|
+
ctrl: boolean;
|
|
14
|
+
shift: boolean;
|
|
15
|
+
alt: boolean;
|
|
16
|
+
meta: boolean;
|
|
17
|
+
/** Ctrl on Windows and Linux, Cmd on macOS - resolved against the environment by `resolveModifiers()`. */
|
|
18
|
+
mod: boolean;
|
|
19
|
+
/** The `KeyboardEvent.code` of the key on a QWERTY keyboard, for layouts where `event.key` is not the letter (see `match.ts`). */
|
|
20
|
+
code?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const MODIFIERS: Record<string, "ctrl" | "shift" | "alt" | "meta" | "mod"> = {
|
|
24
|
+
ctrl: "ctrl",
|
|
25
|
+
control: "ctrl",
|
|
26
|
+
shift: "shift",
|
|
27
|
+
alt: "alt",
|
|
28
|
+
option: "alt",
|
|
29
|
+
opt: "alt",
|
|
30
|
+
meta: "meta",
|
|
31
|
+
cmd: "meta",
|
|
32
|
+
command: "meta",
|
|
33
|
+
mod: "mod",
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** Spec spellings of keys whose `event.key` is something else. */
|
|
37
|
+
const KEY_ALIASES: Record<string, string> = {
|
|
38
|
+
esc: "escape",
|
|
39
|
+
del: "delete",
|
|
40
|
+
ins: "insert",
|
|
41
|
+
return: "enter",
|
|
42
|
+
up: "arrowup",
|
|
43
|
+
down: "arrowdown",
|
|
44
|
+
left: "arrowleft",
|
|
45
|
+
right: "arrowright",
|
|
46
|
+
space: " ",
|
|
47
|
+
spacebar: " ",
|
|
48
|
+
plus: "+",
|
|
49
|
+
pgup: "pageup",
|
|
50
|
+
pgdn: "pagedown",
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** The named keys a spec may use besides a single character and `F1`-`F12`. */
|
|
54
|
+
const NAMED_KEYS = new Set([
|
|
55
|
+
"escape",
|
|
56
|
+
"enter",
|
|
57
|
+
"tab",
|
|
58
|
+
"backspace",
|
|
59
|
+
"delete",
|
|
60
|
+
"insert",
|
|
61
|
+
"home",
|
|
62
|
+
"end",
|
|
63
|
+
"pageup",
|
|
64
|
+
"pagedown",
|
|
65
|
+
"arrowup",
|
|
66
|
+
"arrowdown",
|
|
67
|
+
"arrowleft",
|
|
68
|
+
"arrowright",
|
|
69
|
+
]);
|
|
70
|
+
|
|
71
|
+
const SYMBOL_CODES: Record<string, string> = { "/": "Slash", ".": "Period", ",": "Comma" };
|
|
72
|
+
|
|
73
|
+
/** The `code` a key has on a QWERTY keyboard, for the keys where one is worth falling back to. */
|
|
74
|
+
function codeFor(key: string): string | undefined {
|
|
75
|
+
if (/^[a-z]$/.test(key)) {
|
|
76
|
+
return `Key${key.toUpperCase()}`;
|
|
77
|
+
}
|
|
78
|
+
if (/^[0-9]$/.test(key)) {
|
|
79
|
+
return `Digit${key}`;
|
|
80
|
+
}
|
|
81
|
+
return SYMBOL_CODES[key];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Parses a shortcut spec: modifiers and one key joined by `+`, in any order and any case - `ctrl+shift+a`, `Alt+N`, `mod+enter`,
|
|
86
|
+
* `Delete`, `?`, `ctrl+/`, `ctrl++`. `mod` is Ctrl on Windows/Linux and Cmd on macOS. Throws on a spec that cannot be understood, so a
|
|
87
|
+
* typo in a key map fails at once rather than silently never matching.
|
|
88
|
+
*/
|
|
89
|
+
export function parseSpec(spec: string): ParsedSpec {
|
|
90
|
+
// A trailing `+` is the plus key itself (`ctrl++`), which a plain split would lose.
|
|
91
|
+
const plusKey = spec.length > 1 && spec.endsWith("++");
|
|
92
|
+
const tokens = (plusKey ? spec.slice(0, -2) : spec).split("+").map((token) => token.trim());
|
|
93
|
+
if (plusKey) {
|
|
94
|
+
tokens.push("+");
|
|
95
|
+
}
|
|
96
|
+
const parsed: ParsedSpec = { spec, key: "", ctrl: false, shift: false, alt: false, meta: false, mod: false };
|
|
97
|
+
for (const token of tokens) {
|
|
98
|
+
if (token === "") {
|
|
99
|
+
throw new Error(`Invalid shortcut "${spec}": empty part.`);
|
|
100
|
+
}
|
|
101
|
+
const lower = token.toLowerCase();
|
|
102
|
+
const modifier = MODIFIERS[lower];
|
|
103
|
+
if (modifier) {
|
|
104
|
+
parsed[modifier] = true;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (parsed.key !== "") {
|
|
108
|
+
throw new Error(`Invalid shortcut "${spec}": more than one key.`);
|
|
109
|
+
}
|
|
110
|
+
const key = KEY_ALIASES[lower] ?? lower;
|
|
111
|
+
if ([...key].length > 1 && !NAMED_KEYS.has(key) && !/^f([1-9]|1[0-2])$/.test(key)) {
|
|
112
|
+
throw new Error(`Invalid shortcut "${spec}": unknown key "${token}".`);
|
|
113
|
+
}
|
|
114
|
+
parsed.key = key;
|
|
115
|
+
parsed.code = codeFor(key);
|
|
116
|
+
}
|
|
117
|
+
if (parsed.key === "") {
|
|
118
|
+
throw new Error(`Invalid shortcut "${spec}": no key.`);
|
|
119
|
+
}
|
|
120
|
+
return parsed;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The modifiers a spec needs on a given platform, with `mod` turned into Cmd (macOS) or Ctrl. */
|
|
124
|
+
export function resolveModifiers(parsed: ParsedSpec, env: KeyEnvironment): { ctrl: boolean; shift: boolean; alt: boolean; meta: boolean } {
|
|
125
|
+
return {
|
|
126
|
+
ctrl: parsed.ctrl || (parsed.mod && !env.mac),
|
|
127
|
+
shift: parsed.shift,
|
|
128
|
+
alt: parsed.alt,
|
|
129
|
+
meta: parsed.meta || (parsed.mod && env.mac),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Whether the key is a printable symbol whose shifted-ness depends on the layout (`?` is Shift+/ on a US keyboard, not on others). */
|
|
134
|
+
export function isSymbolKey(key: string): boolean {
|
|
135
|
+
return [...key].length === 1 && !/[a-z0-9]/.test(key);
|
|
136
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2026 Jean-Philippe Steinmetz
|
|
3
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5
|
+
|
|
6
|
+
/** What the keyboard layer needs to know about where it runs. */
|
|
7
|
+
export interface KeyEnvironment {
|
|
8
|
+
/** macOS (and iOS/iPadOS): `mod` is Cmd there, and Option is a text-entry modifier. */
|
|
9
|
+
mac: boolean;
|
|
10
|
+
/** The desktop client (`electron-client`), which exposes `window.rapidmx` from its preload script. */
|
|
11
|
+
electron: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** What the server render and the first client render use, so the two agree; the real environment is read after mount. */
|
|
15
|
+
export const SERVER_ENVIRONMENT: KeyEnvironment = { mac: false, electron: false };
|
|
16
|
+
|
|
17
|
+
/** Whether this is an Apple platform. Prefers `navigator.userAgentData` (Chromium), then the older `navigator.platform`. */
|
|
18
|
+
export function isMacPlatform(): boolean {
|
|
19
|
+
if (typeof navigator === "undefined") {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
const uaData = (navigator as { userAgentData?: { platform?: string } }).userAgentData;
|
|
23
|
+
return /mac|iphone|ipad|ipod/i.test(uaData?.platform || navigator.platform || "");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Whether the page is running inside the desktop client, which is what `window.rapidmx` (its preload's `contextBridge`) tells. */
|
|
27
|
+
export function isElectronClient(): boolean {
|
|
28
|
+
return typeof window !== "undefined" && (window as { rapidmx?: unknown }).rapidmx !== undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The environment as it is right now. Only meaningful in a browser: on the server it is `SERVER_ENVIRONMENT`. */
|
|
32
|
+
export function currentEnvironment(): KeyEnvironment {
|
|
33
|
+
return { mac: isMacPlatform(), electron: isElectronClient() };
|
|
34
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2026 Jean-Philippe Steinmetz
|
|
3
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5
|
+
import type { RefObject } from "react";
|
|
6
|
+
import type { ShortcutDef, ShortcutScope } from "./keymap.js";
|
|
7
|
+
import { ParsedSpec } from "./parse.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* What a handler does with its key. Returning `false` says it did not handle it (nothing selected, a button that is disabled right now)
|
|
11
|
+
* - the key is then left alone, and a handler of a lower scope may take it. Anything else means it did, and the key is consumed.
|
|
12
|
+
*/
|
|
13
|
+
export type ShortcutHandler = (event: KeyboardEvent) => boolean | void;
|
|
14
|
+
|
|
15
|
+
/** One mounted view's claim on a shortcut. */
|
|
16
|
+
export interface Registration {
|
|
17
|
+
/** Unique per registration. */
|
|
18
|
+
id: number;
|
|
19
|
+
shortcut: ShortcutDef;
|
|
20
|
+
scope: ShortcutScope;
|
|
21
|
+
/** Called at event time; reads whatever the component holds now. */
|
|
22
|
+
run: ShortcutHandler;
|
|
23
|
+
/** Applies only to events whose target is inside this element (a compose window). */
|
|
24
|
+
container?: RefObject<Element | null>;
|
|
25
|
+
keys: ParsedSpec[];
|
|
26
|
+
electronKeys: ParsedSpec[];
|
|
27
|
+
/** Later registrations win over earlier ones in the same scope. */
|
|
28
|
+
order: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The shortcuts that are mounted right now. Components add and remove their claims as they mount and unmount, so a page switch changes
|
|
33
|
+
* what a key does with nothing to re-wire; the dispatcher (`dispatchKeyEvent()`) looks handlers up here at the time of the key press,
|
|
34
|
+
* and the help dialog lists what is here.
|
|
35
|
+
*/
|
|
36
|
+
export class ShortcutRegistry {
|
|
37
|
+
private registrations: readonly Registration[] = [];
|
|
38
|
+
private counter = 0;
|
|
39
|
+
private listeners = new Set<() => void>();
|
|
40
|
+
|
|
41
|
+
/** Adds a claim; returns what removes it. */
|
|
42
|
+
add(claim: Omit<Registration, "id" | "order">): () => void {
|
|
43
|
+
const registration: Registration = { ...claim, id: ++this.counter, order: this.counter };
|
|
44
|
+
this.registrations = [...this.registrations, registration];
|
|
45
|
+
this.publish();
|
|
46
|
+
return () => {
|
|
47
|
+
this.registrations = this.registrations.filter((existing) => existing.id !== registration.id);
|
|
48
|
+
this.publish();
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The current claims, oldest first. A new array whenever they change (so it can be a `useSyncExternalStore()` snapshot). */
|
|
53
|
+
getSnapshot = (): readonly Registration[] => this.registrations;
|
|
54
|
+
|
|
55
|
+
subscribe = (listener: () => void): (() => void) => {
|
|
56
|
+
this.listeners.add(listener);
|
|
57
|
+
return () => {
|
|
58
|
+
this.listeners.delete(listener);
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
private publish(): void {
|
|
63
|
+
this.listeners.forEach((listener) => listener());
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2026 Jean-Philippe Steinmetz
|
|
3
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5
|
+
|
|
6
|
+
/** `<input>` types that take no typed text, so a letter pressed on one is a shortcut, not input. (Radios and ranges use the arrows.) */
|
|
7
|
+
const NON_TEXT_INPUT_TYPES = new Set(["button", "checkbox", "submit", "reset", "image", "file", "color"]);
|
|
8
|
+
|
|
9
|
+
/** Roles whose members have keys of their own (arrows, Enter, Escape, type-ahead): a bare key pressed inside one belongs to the widget. */
|
|
10
|
+
const WIDGET_SELECTOR = [
|
|
11
|
+
"menu",
|
|
12
|
+
"menubar",
|
|
13
|
+
"listbox",
|
|
14
|
+
"tablist",
|
|
15
|
+
"tree",
|
|
16
|
+
"grid",
|
|
17
|
+
"radiogroup",
|
|
18
|
+
"slider",
|
|
19
|
+
"spinbutton",
|
|
20
|
+
"combobox",
|
|
21
|
+
]
|
|
22
|
+
.map((role) => `[role="${role}"]`)
|
|
23
|
+
.join(",");
|
|
24
|
+
|
|
25
|
+
/** Elements Enter (and Space) already does something on. */
|
|
26
|
+
const ACTIVATABLE_SELECTOR = 'a[href], button, summary, [role="button"], [role="link"], [role="menuitem"], [role="menuitemcheckbox"], [role="menuitemradio"], [role="tab"], [role="option"]';
|
|
27
|
+
|
|
28
|
+
/** An open popup's trigger: Escape closes the popup, so it is not a shortcut then. */
|
|
29
|
+
const OPEN_POPUP_TRIGGER = '[aria-haspopup][aria-expanded="true"], [role="combobox"][aria-expanded="true"]';
|
|
30
|
+
|
|
31
|
+
function elementOf(target: EventTarget | null): Element | null {
|
|
32
|
+
return target instanceof Element ? target : null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Whether the event target takes typed text: a text field, a select, a textarea, or anything contenteditable (the rich-text editor). */
|
|
36
|
+
export function isTextEntry(target: EventTarget | null): boolean {
|
|
37
|
+
const element = elementOf(target);
|
|
38
|
+
if (!element) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
const tag = element.tagName;
|
|
42
|
+
if (tag === "TEXTAREA" || tag === "SELECT") {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
if (tag === "INPUT") {
|
|
46
|
+
return !NON_TEXT_INPUT_TYPES.has((element as HTMLInputElement).type);
|
|
47
|
+
}
|
|
48
|
+
return !!element.closest('[contenteditable]:not([contenteditable="false"]), [role="textbox"], [role="searchbox"]');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Whether the target is inside a widget that reads bare keys itself (an open menu, a listbox, a tab list, ...). */
|
|
52
|
+
export function isInWidget(target: EventTarget | null): boolean {
|
|
53
|
+
return !!elementOf(target)?.closest(WIDGET_SELECTOR);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Whether Enter/Space on the target already activates it (a button, a link, a menu item). */
|
|
57
|
+
export function isActivatable(target: EventTarget | null): boolean {
|
|
58
|
+
return !!elementOf(target)?.closest(ACTIVATABLE_SELECTOR);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Whether the target is the trigger of a popup that is open right now (or the combobox that owns an open list). */
|
|
62
|
+
export function isPopupOpenAt(target: EventTarget | null): boolean {
|
|
63
|
+
return !!elementOf(target)?.closest(OPEN_POPUP_TRIGGER);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Whether a modal dialog (`aria-modal="true"`: react-shared's `Modal` and `Drawer`, the unlock prompts, ...) is on screen. */
|
|
67
|
+
export function isModalOpen(doc: Document): boolean {
|
|
68
|
+
return doc.querySelector('[aria-modal="true"]') !== null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Whether the target is inside a popover that is a dialog of its own (the emoji and GIF pickers, the send-later picker): Escape is theirs, not a view's. A region that declares a shortcut scope (a compose window) is not one. */
|
|
72
|
+
export function isInPopoverDialog(target: EventTarget | null): boolean {
|
|
73
|
+
return !!elementOf(target)?.closest('[role="dialog"]:not([data-shortcut-scope])');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The `data-shortcut-scope` of the region the target is in (a compose window), if any. */
|
|
77
|
+
export function regionScopeOf(target: EventTarget | null): string | undefined {
|
|
78
|
+
return elementOf(target)?.closest("[data-shortcut-scope]")?.getAttribute("data-shortcut-scope") ?? undefined;
|
|
79
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2026 Jean-Philippe Steinmetz
|
|
3
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5
|
+
import { RefObject, useEffect, useMemo, useRef } from "react";
|
|
6
|
+
import type { ShortcutDef, ShortcutScope } from "./keymap.js";
|
|
7
|
+
import { parseSpec } from "./parse.js";
|
|
8
|
+
import type { ShortcutHandler } from "./registry.js";
|
|
9
|
+
import { useShortcutRegistry } from "./ShortcutProvider.js";
|
|
10
|
+
|
|
11
|
+
export interface UseShortcutOptions {
|
|
12
|
+
/** Overrides the scope the shortcut is defined with - required for a bare spec string, which has none. Default `global`. */
|
|
13
|
+
scope?: ShortcutScope;
|
|
14
|
+
/** Registers only while true: a view offers a shortcut only when it can really do the thing. Default true. */
|
|
15
|
+
enabled?: boolean;
|
|
16
|
+
/** Restricts it to events whose target is inside this element (a compose window). */
|
|
17
|
+
container?: RefObject<Element | null>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Claims a shortcut for as long as the calling component is mounted (and `enabled`). `shortcut` is an entry of `SHORTCUTS` - which is what
|
|
22
|
+
* the help dialog and tooltips describe - or a bare spec such as `"alt+n"` for a one-off. The handler is read at the time of the key press,
|
|
23
|
+
* so it always sees the component's current state without the registration being redone; return `false` from it to decline the key.
|
|
24
|
+
*
|
|
25
|
+
* Does nothing outside a `ShortcutProvider`.
|
|
26
|
+
*/
|
|
27
|
+
export function useShortcut(shortcut: ShortcutDef | string, handler: ShortcutHandler, options: UseShortcutOptions = {}): void {
|
|
28
|
+
const { scope, enabled = true, container } = options;
|
|
29
|
+
const registry = useShortcutRegistry();
|
|
30
|
+
const handlerRef = useRef(handler);
|
|
31
|
+
handlerRef.current = handler;
|
|
32
|
+
const def = useMemo<ShortcutDef>(
|
|
33
|
+
() => (typeof shortcut === "string" ? { id: `custom:${shortcut}`, keys: [shortcut], label: shortcut, scope: "global" } : shortcut),
|
|
34
|
+
[shortcut],
|
|
35
|
+
);
|
|
36
|
+
const resolvedScope = scope ?? def.scope;
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
if (!registry || !enabled) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
return registry.add({
|
|
42
|
+
shortcut: def,
|
|
43
|
+
scope: resolvedScope,
|
|
44
|
+
run: (event) => handlerRef.current(event),
|
|
45
|
+
container,
|
|
46
|
+
keys: def.keys.map(parseSpec),
|
|
47
|
+
electronKeys: (def.electronKeys ?? []).map(parseSpec),
|
|
48
|
+
});
|
|
49
|
+
}, [registry, enabled, def, resolvedScope, container]);
|
|
50
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2026 Jean-Philippe Steinmetz
|
|
3
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5
|
+
import { ariaKeyShortcuts, withHint } from "./format.js";
|
|
6
|
+
import type { ShortcutDef } from "./keymap.js";
|
|
7
|
+
import { useKeyEnvironment } from "./ShortcutProvider.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* What a control that a keyboard shortcut also triggers gets: a tooltip naming the key - `Reply (Ctrl+R)`, in this platform's own key names -
|
|
11
|
+
* and `aria-keyshortcuts` for assistive technology. The accessible name is left alone (the control keeps its own label), so a tooltip suffix never
|
|
12
|
+
* changes what a screen reader or a test finds it by. With `active` false - the shortcut isn't registered right now - it is just the plain label.
|
|
13
|
+
*/
|
|
14
|
+
export function useShortcutProps(label: string, shortcut: ShortcutDef, active = true): { title: string; "aria-keyshortcuts"?: string } {
|
|
15
|
+
const env = useKeyEnvironment();
|
|
16
|
+
return active ? { title: withHint(label, shortcut, env), "aria-keyshortcuts": ariaKeyShortcuts(shortcut, env) } : { title: label };
|
|
17
|
+
}
|