@estiva-app/ui 0.110.0 → 0.112.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/src/shortcuts.ts CHANGED
@@ -16,6 +16,8 @@
16
16
  * only the app knows which platform it is looking at.
17
17
  */
18
18
 
19
+ import { useEffect, useRef } from 'react'
20
+
19
21
  /** Whether this machine calls the modifier Cmd rather than Ctrl. */
20
22
  export function isApplePlatform(): boolean {
21
23
  if (typeof navigator === 'undefined') return false
@@ -38,3 +40,41 @@ export function shortcutLabel(keys: string, apple = isApplePlatform()): string {
38
40
  })
39
41
  .join('+')
40
42
  }
43
+
44
+ /** Whether a key press is the shortcut `keys` names, in the spelling above (`Mod-K`, `Mod-Shift-O`). */
45
+ export function matchesShortcut(event: Pick<KeyboardEvent, 'key' | 'metaKey' | 'ctrlKey' | 'shiftKey' | 'altKey'>, keys: string): boolean {
46
+ // The key is what follows the last joining '-', so `Mod--` is Mod and '-'.
47
+ const at = keys.lastIndexOf('-', keys.length - 2)
48
+ const key = keys.slice(at + 1)
49
+ const mods = new Set(at < 0 ? [] : keys.slice(0, at).split('-'))
50
+ // Mod is Cmd or Ctrl on any machine, as the top bar's Ctrl+K always took either. Shift and Alt are exact: Mod-O is
51
+ // not Ctrl+Shift+O, which the browser keeps.
52
+ if (mods.has('Mod') ? !(event.metaKey || event.ctrlKey) : event.metaKey || event.ctrlKey) return false
53
+ if (mods.has('Shift') !== event.shiftKey || mods.has('Alt') !== event.altKey) return false
54
+ return event.key.toLowerCase() === key.toLowerCase()
55
+ }
56
+
57
+ /**
58
+ * A key that does something from anywhere on the page while the caller is on
59
+ * screen, such as `Mod-K` for search. The browser's own meaning of the key
60
+ * (Ctrl+O, open a file) is taken over: the press is consumed. Off while
61
+ * `enabled` is false. The latest `onPress` runs, so it need not be stable.
62
+ */
63
+ export function useShortcut(keys: string, onPress: () => void, enabled = true): void {
64
+ const pressRef = useRef(onPress)
65
+ useEffect(() => {
66
+ pressRef.current = onPress
67
+ })
68
+ useEffect(() => {
69
+ if (!enabled) return
70
+ const onKey = (event: KeyboardEvent) => {
71
+ // Once per press, not per auto-repeat; and not a press something on the page already took, or an IME's.
72
+ if (event.repeat || event.defaultPrevented || event.isComposing || !matchesShortcut(event, keys)) return
73
+ event.preventDefault()
74
+ pressRef.current()
75
+ }
76
+ // @estiva-escape(no-hand-rolled-behaviour): the one listener for a page-wide shortcut, which no floating part owns; every app's shortcut goes through this hook
77
+ window.addEventListener('keydown', onKey)
78
+ return () => window.removeEventListener('keydown', onKey)
79
+ }, [keys, enabled])
80
+ }
@@ -140,6 +140,7 @@ The same conversation in every app; only its colours are each app's.
140
140
  | A link alone on its line | **LinkRow** — one 36px line for every kind; **LinkUnavailable** when it cannot be read |
141
141
  | A link's card: what a row cannot hold, with the pickers the reader may use | **LinkCard** — `onChange` on a picker only for a reader who may change it; **LinkCardLoading** at its height |
142
142
  | The one card attached to a piece of text, with the ✕ that removes it | **AttachedCard** — in a box, `CommentBox` places it with the files |
143
+ | A row or a card in a list the person arranges: Show as Row or Card, and Remove, behind a `…` on its corner | **CornerMenu** around the **LinkRow** or **AttachedCard** (no `onRemove`); no `menu` where they cannot arrange it |
143
144
  | A link's card on hover, read only | **LinkPreview** — LinkChip and LinkRow already wrap themselves in it |
144
145
  | A part of something longer, shared live | **PartCard** — the app draws the blocks |
145
146
  | A status and an owner changed in place, a conversation count with its unread dot, a ring | **LinkParts** — `LinkPickers`, `CommentCount`, `StatusMark`, `ProgressRing` |