|
1 | 1 | import { useAtomValue } from "@effect/atom-react"; |
2 | 2 | import { |
3 | 3 | type KeybindingCommand, |
| 4 | + type KeybindingShortcut, |
| 5 | + MODEL_PICKER_JUMP_KEYBINDING_COMMANDS, |
4 | 6 | type ScopedThreadRef, |
5 | 7 | THREAD_JUMP_KEYBINDING_COMMANDS, |
6 | 8 | } from "@t3tools/contracts"; |
@@ -55,6 +57,8 @@ const HTML_RENDER_SHORTCUT_COMMANDS: ReadonlySet<KeybindingCommand> = new Set([ |
55 | 57 | "thread.previous", |
56 | 58 | "thread.next", |
57 | 59 | ...THREAD_JUMP_KEYBINDING_COMMANDS, |
| 60 | + // Share Cmd/Ctrl+digit with thread jumps, and act only while the picker is open. |
| 61 | + ...MODEL_PICKER_JUMP_KEYBINDING_COMMANDS, |
58 | 62 | ]); |
59 | 63 |
|
60 | 64 | export const isPdfPreviewFile = (path: string): boolean => |
@@ -136,42 +140,42 @@ export function HtmlRenderDocument(props: { |
136 | 140 | // would type into the composer instead. |
137 | 141 | const { shortcuts, blocked } = useMemo(() => { |
138 | 142 | const mac = isMacPlatform(navigator.platform); |
| 143 | + const resolve = (shortcut: KeybindingShortcut): HtmlRenderShortcut => ({ |
| 144 | + key: shortcut.key, |
| 145 | + metaKey: shortcut.metaKey || (shortcut.modKey && mac), |
| 146 | + ctrlKey: shortcut.ctrlKey || (shortcut.modKey && !mac), |
| 147 | + shiftKey: shortcut.shiftKey, |
| 148 | + altKey: shortcut.altKey, |
| 149 | + }); |
139 | 150 | const blocked = [...keybindings, ...(environmentKeybindings ?? [])] |
140 | 151 | .filter((binding) => !HTML_RENDER_SHORTCUT_COMMANDS.has(binding.command)) |
141 | | - .map((binding) => binding.shortcut); |
142 | | - const claimed = new Set(blocked.map((shortcut) => shortcutConflictKey(shortcut))); |
143 | | - const offered = new Map<string, HtmlRenderShortcut>(); |
| 152 | + .map((binding) => resolve(binding.shortcut)); |
| 153 | + const shortcuts = new Map<string, HtmlRenderShortcut>(); |
144 | 154 | for (const { command, shortcut } of keybindings) { |
145 | | - const conflictKey = shortcutConflictKey(shortcut); |
146 | 155 | if ( |
147 | | - !HTML_RENDER_SHORTCUT_COMMANDS.has(command) || |
148 | | - !(shortcut.metaKey || shortcut.ctrlKey || shortcut.modKey) || |
149 | | - claimed.has(conflictKey) |
| 156 | + HTML_RENDER_SHORTCUT_COMMANDS.has(command) && |
| 157 | + (shortcut.metaKey || shortcut.ctrlKey || shortcut.modKey) |
150 | 158 | ) { |
151 | | - continue; |
| 159 | + shortcuts.set(shortcutConflictKey(shortcut), resolve(shortcut)); |
152 | 160 | } |
153 | | - offered.set(conflictKey, { |
154 | | - key: shortcut.key, |
155 | | - metaKey: shortcut.metaKey || (shortcut.modKey && mac), |
156 | | - ctrlKey: shortcut.ctrlKey || (shortcut.modKey && !mac), |
157 | | - shiftKey: shortcut.shiftKey, |
158 | | - altKey: shortcut.altKey, |
159 | | - }); |
160 | 161 | } |
161 | | - return { shortcuts: [...offered.values()], blocked }; |
| 162 | + return { shortcuts: [...shortcuts.values()], blocked }; |
162 | 163 | }, [environmentKeybindings, keybindings]); |
163 | 164 | const postShortcuts = () => { |
164 | | - frameRef.current?.contentWindow?.postMessage(htmlRenderShortcutsMessage(shortcuts), "*"); |
| 165 | + frameRef.current?.contentWindow?.postMessage( |
| 166 | + htmlRenderShortcutsMessage(shortcuts, blocked), |
| 167 | + "*", |
| 168 | + ); |
165 | 169 | }; |
166 | | - useEffect(postShortcuts, [shortcuts]); |
| 170 | + useEffect(postShortcuts, [blocked, shortcuts]); |
167 | 171 | // The page reports the key and code, and a page script can report any pair, |
168 | 172 | // so a key that could match another command's binding is never replayed. |
169 | 173 | const isShortcut = useEffectEvent( |
170 | | - (key: NonNullable<ReturnType<typeof readHtmlRenderKeyDown>>) => |
171 | | - shortcuts.some((shortcut) => |
172 | | - matchesKeybindingShortcut(key, { ...shortcut, modKey: false }, navigator.platform), |
173 | | - ) && |
174 | | - !blocked.some((shortcut) => matchesKeybindingShortcut(key, shortcut, navigator.platform)), |
| 174 | + (key: NonNullable<ReturnType<typeof readHtmlRenderKeyDown>>) => { |
| 175 | + const matches = (shortcut: HtmlRenderShortcut) => |
| 176 | + matchesKeybindingShortcut(key, { ...shortcut, modKey: false }, navigator.platform); |
| 177 | + return shortcuts.some(matches) && !blocked.some(matches); |
| 178 | + }, |
175 | 179 | ); |
176 | 180 | const openChangeRequestLink = useOpenChangeRequestLink(props.threadRef); |
177 | 181 | const openLink = useOpenLink(props.threadRef); |
|
0 commit comments