1- import type { ScopedThreadRef } from "@t3tools/contracts " ;
1+ import { useAtomValue } from "@effect/atom-react " ;
22import {
3+ type KeybindingCommand ,
4+ type KeybindingShortcut ,
5+ MODEL_PICKER_JUMP_KEYBINDING_COMMANDS ,
6+ type ScopedThreadRef ,
7+ THREAD_JUMP_KEYBINDING_COMMANDS ,
8+ } from "@t3tools/contracts" ;
9+ import {
10+ type HtmlRenderShortcut ,
11+ htmlRenderShortcutsMessage ,
312 htmlRenderThemeFragment ,
413 htmlRenderThemeMessage ,
514 htmlRenderResult ,
615 readHtmlRenderContentHeight ,
16+ readHtmlRenderKeyDown ,
717 readHtmlRenderLinkRequest ,
818} from "@t3tools/shared/htmlRender" ;
9- import { useEffect , useEffectEvent , useLayoutEffect , useRef , useState } from "react" ;
19+ import {
20+ isMacPlatform ,
21+ matchesKeybindingShortcut ,
22+ normalizeEventKey ,
23+ } from "@t3tools/shared/keybindings" ;
24+ import { useEffect , useEffectEvent , useLayoutEffect , useMemo , useRef , useState } from "react" ;
1025
1126import { useOpenLink } from "~/browser/useOpenLink" ;
1227import { useHtmlRenderTheme } from "~/hooks/useHtmlRenderTheme" ;
28+ import { shortcutConflictKey } from "~/keybindings" ;
1329import { useOpenChangeRequestLink } from "~/lib/openPullRequestLink" ;
1430import { readLocalApi } from "~/localApi" ;
1531import { cn } from "~/lib/utils" ;
32+ import { primaryServerKeybindingsAtom , serverEnvironment } from "~/state/server" ;
1633
1734/**
1835 * Chromium's viewer opens with its own toolbar, a thumbnail rail and a small
@@ -22,6 +39,32 @@ import { cn } from "~/lib/utils";
2239 */
2340const PDF_VIEWER_FRAGMENT = "#toolbar=0&view=FitH" ;
2441
42+ /**
43+ * Shortcuts that still work while a focused HTML render holds the keyboard.
44+ * The page reports the keys, and a page script can report any key it likes,
45+ * so only commands that change what the reader sees are here, never one that
46+ * changes a thread.
47+ */
48+ const HTML_RENDER_SHORTCUT_COMMANDS : ReadonlySet < KeybindingCommand > = new Set ( [
49+ "commandPalette.toggle" ,
50+ "filePicker.toggle" ,
51+ "projectSearch.toggle" ,
52+ "chat.find" ,
53+ "sidebar.toggle" ,
54+ "rightPanel.toggle" ,
55+ "rightPanel.toggleMaximized" ,
56+ "threadPanel.toggle" ,
57+ "terminal.toggle" ,
58+ "preview.toggle" ,
59+ "navigation.back" ,
60+ "navigation.forward" ,
61+ "thread.previous" ,
62+ "thread.next" ,
63+ ...THREAD_JUMP_KEYBINDING_COMMANDS ,
64+ // Share Cmd/Ctrl+digit with thread jumps, and act only while the picker is open.
65+ ...MODEL_PICKER_JUMP_KEYBINDING_COMMANDS ,
66+ ] ) ;
67+
2568export const isPdfPreviewFile = ( path : string ) : boolean =>
2669 / \. p d f $ / i. test ( path . split ( / [ ? # ] / , 1 ) [ 0 ] ?? "" ) ;
2770
@@ -90,6 +133,63 @@ export function HtmlRenderDocument(props: {
90133 frameRef . current ?. contentWindow ?. postMessage ( htmlRenderThemeMessage ( theme ) , "*" ) ;
91134 } ;
92135 useEffect ( postTheme , [ theme ] ) ;
136+ const keybindings = useAtomValue ( primaryServerKeybindingsAtom ) ;
137+ // Chat also resolves project scripts from the thread's own environment.
138+ const environmentKeybindings = useAtomValue (
139+ serverEnvironment . configValueAtom ( props . threadRef ?. environmentId ?? null ) ,
140+ ) ?. keybindings ;
141+ // The app resolves a replayed key in its live context, so every allowed
142+ // binding counts whatever its `when`, and a key that any binding gives to
143+ // another command stays with the page. Only Cmd/Ctrl chords: a printable key
144+ // would type into the composer instead.
145+ const { shortcuts, blocked } = useMemo ( ( ) => {
146+ const mac = isMacPlatform ( navigator . platform ) ;
147+ const resolve = ( shortcut : KeybindingShortcut ) : HtmlRenderShortcut => ( {
148+ key : shortcut . key ,
149+ metaKey : shortcut . metaKey || ( shortcut . modKey && mac ) ,
150+ ctrlKey : shortcut . ctrlKey || ( shortcut . modKey && ! mac ) ,
151+ shiftKey : shortcut . shiftKey ,
152+ altKey : shortcut . altKey ,
153+ } ) ;
154+ const blocked = [ ...keybindings , ...( environmentKeybindings ?? [ ] ) ]
155+ . filter ( ( binding ) => ! HTML_RENDER_SHORTCUT_COMMANDS . has ( binding . command ) )
156+ . map ( ( binding ) => resolve ( binding . shortcut ) ) ;
157+ const shortcuts = new Map < string , HtmlRenderShortcut > ( ) ;
158+ for ( const { command, shortcut } of keybindings ) {
159+ if (
160+ HTML_RENDER_SHORTCUT_COMMANDS . has ( command ) &&
161+ ( shortcut . metaKey || shortcut . ctrlKey || shortcut . modKey )
162+ ) {
163+ shortcuts . set ( shortcutConflictKey ( shortcut ) , resolve ( shortcut ) ) ;
164+ }
165+ }
166+ return { shortcuts : [ ...shortcuts . values ( ) ] , blocked } ;
167+ } , [ environmentKeybindings , keybindings ] ) ;
168+ const postShortcuts = ( ) => {
169+ frameRef . current ?. contentWindow ?. postMessage (
170+ htmlRenderShortcutsMessage ( shortcuts , blocked ) ,
171+ "*" ,
172+ ) ;
173+ } ;
174+ useEffect ( postShortcuts , [ blocked , shortcuts ] ) ;
175+ // The page reports the key and code, and a page script can report any pair,
176+ // so a key that could match another command's binding is never replayed. A
177+ // named key (Backspace, Enter) must be the shortcut's own, since some app
178+ // handlers read `event.key` directly. A dead key matches by its code.
179+ const isShortcut = useEffectEvent (
180+ ( key : NonNullable < ReturnType < typeof readHtmlRenderKeyDown > > ) => {
181+ const matches = ( shortcut : HtmlRenderShortcut ) =>
182+ matchesKeybindingShortcut ( key , { ...shortcut , modKey : false } , navigator . platform ) ;
183+ const allowed = shortcuts . filter ( matches ) ;
184+ return (
185+ allowed . length > 0 &&
186+ ! blocked . some ( matches ) &&
187+ ( key . key . length === 1 ||
188+ key . key === "Dead" ||
189+ allowed . some ( ( shortcut ) => shortcut . key === normalizeEventKey ( key . key ) ) )
190+ ) ;
191+ } ,
192+ ) ;
93193 const openChangeRequestLink = useOpenChangeRequestLink ( props . threadRef ) ;
94194 const openLink = useOpenLink ( props . threadRef ) ;
95195 // Same rules as a chat link: a pull request opens in the right panel, any
@@ -140,6 +240,31 @@ export function HtmlRenderDocument(props: {
140240 window . addEventListener ( "message" , openLink ) ;
141241 return ( ) => window . removeEventListener ( "message" , openLink ) ;
142242 } , [ ] ) ;
243+ // A focused frame takes every key, so app shortcuts would stop until the
244+ // reader clicked out. The page passes on the chords posted to it, which replay
245+ // on the frame element and bubble to the app's window listeners. A report is
246+ // gated like a link and must be one of those chords.
247+ useEffect ( ( ) => {
248+ const replayKey = ( event : MessageEvent ) => {
249+ const frame = frameRef . current ;
250+ const key = readHtmlRenderKeyDown ( event . data ) ;
251+ if (
252+ key === undefined ||
253+ frame === null ||
254+ event . source !== frame . contentWindow ||
255+ document . activeElement !== frame ||
256+ navigator . userActivation ?. isActive === false
257+ ) {
258+ return ;
259+ }
260+ if ( ! isShortcut ( key ) ) return ;
261+ frame . dispatchEvent (
262+ new KeyboardEvent ( "keydown" , { ...key , bubbles : true , cancelable : true } ) ,
263+ ) ;
264+ } ;
265+ window . addEventListener ( "message" , replayKey ) ;
266+ return ( ) => window . removeEventListener ( "message" , replayKey ) ;
267+ } , [ ] ) ;
143268 const { onContentHeight } = props ;
144269 // A page posts its height once per change, so listen from the commit that
145270 // inserts the frame; a passive effect could run after a fast page's first post.
@@ -166,6 +291,7 @@ export function HtmlRenderDocument(props: {
166291 setLoaded ( true ) ;
167292 // Covers a theme change that landed while the page was loading.
168293 postTheme ( ) ;
294+ postShortcuts ( ) ;
169295 } }
170296 // A frame whose color scheme differs from its document's paints an opaque
171297 // canvas, so the blank document a frame starts with would flash white in
0 commit comments