|
| 1 | +import { useEffect, type RefObject } from 'react' |
| 2 | +import { keyboardShortcutsStore } from '@/stores/keyboardShortcuts' |
| 3 | +import { formatShortcutKey } from '@/features/shortcuts/utils' |
| 4 | +import { isMacintosh } from '@/utils/livekit' |
| 5 | + |
| 6 | +/** |
| 7 | + * Mirror the main-window keyboard shortcuts inside the PiP document. |
| 8 | + * |
| 9 | + * The central `useKeyboardShortcuts` hook listens on `window`, which is the |
| 10 | + * main document's window. Keydown events from the PiP document never reach |
| 11 | + * it. This hook attaches the same dispatch logic to the PiP document so that |
| 12 | + * Ctrl+D (mic), Ctrl+E (cam), etc. work identically in both contexts. |
| 13 | + */ |
| 14 | +export const usePipKeyboardShortcuts = ( |
| 15 | + containerRef: RefObject<HTMLElement | null> |
| 16 | +) => { |
| 17 | + useEffect(() => { |
| 18 | + const doc = containerRef.current?.ownerDocument |
| 19 | + if (!doc || doc === document) return |
| 20 | + |
| 21 | + const onKeyDown = (e: KeyboardEvent) => { |
| 22 | + const { key, metaKey, ctrlKey, shiftKey, altKey } = e |
| 23 | + if (!key) return |
| 24 | + |
| 25 | + const shortcutKey = formatShortcutKey({ |
| 26 | + key, |
| 27 | + ctrlKey: ctrlKey || (isMacintosh() && metaKey), |
| 28 | + shiftKey, |
| 29 | + altKey, |
| 30 | + }) |
| 31 | + |
| 32 | + let handler = keyboardShortcutsStore.shortcuts.get(shortcutKey) |
| 33 | + if (!handler && shortcutKey === 'ctrl+shift+?') { |
| 34 | + handler = keyboardShortcutsStore.shortcuts.get('ctrl+shift+/') |
| 35 | + } |
| 36 | + if (!handler) return |
| 37 | + |
| 38 | + e.preventDefault() |
| 39 | + handler() |
| 40 | + } |
| 41 | + |
| 42 | + doc.addEventListener('keydown', onKeyDown) |
| 43 | + return () => doc.removeEventListener('keydown', onKeyDown) |
| 44 | + }, [containerRef]) |
| 45 | +} |
0 commit comments