@@ -35,6 +35,7 @@ import {
3535 START_PICK_CHANNEL ,
3636} from "./GuestProtocol.ts" ;
3737const OVERLAY_ATTRIBUTE = "data-t3code-annotation-ui" ;
38+ const INPUT_LAYER_ATTRIBUTE = "data-t3code-annotation-input" ;
3839const Z_INDEX_OVERLAY = 2147483646 ;
3940const PRIMARY = "var(--t3-primary)" ;
4041const PRIMARY_FILL = "color-mix(in srgb, var(--t3-primary) 10%, transparent)" ;
@@ -268,7 +269,7 @@ function isAnnotationNode(element: Element): boolean {
268269function pickFromPoint ( clientX : number , clientY : number ) : Element | null {
269270 for ( const candidate of document . elementsFromPoint ( clientX , clientY ) ) {
270271 if ( ! ( candidate instanceof Element ) ) continue ;
271- if ( isAnnotationNode ( candidate ) ) continue ;
272+ if ( isAnnotationNode ( candidate ) || candidate . hasAttribute ( INPUT_LAYER_ATTRIBUTE ) ) continue ;
272273 if ( candidate === document . documentElement || candidate === document . body ) continue ;
273274 return candidate ;
274275 }
@@ -636,7 +637,18 @@ function startAnnotation(sendEnabled: boolean): void {
636637 const strokes : PreviewAnnotationStrokeTarget [ ] = [ ] ;
637638 const styleChanges = new Map < string , PreviewAnnotationStyleChange > ( ) ;
638639 const toolButtons = new Map < AnnotationTool , HTMLButtonElement > ( ) ;
639- let tool : AnnotationTool = "select" ;
640+ // Chromium draws a PDF in its own viewer frame, so pointer and key input over it
641+ // never reaches this document. A transparent layer above the viewer catches the
642+ // pointer and takes focus for Escape and the tool keys. A PDF has no elements to
643+ // select, so it starts on Region.
644+ const isPdf = document . contentType === "application/pdf" ;
645+ const inputLayer = isPdf ? document . createElement ( "div" ) : null ;
646+ if ( inputLayer ) {
647+ inputLayer . setAttribute ( INPUT_LAYER_ATTRIBUTE , "" ) ;
648+ inputLayer . tabIndex = - 1 ;
649+ inputLayer . style . cssText = `position:fixed;inset:0;z-index:${ Z_INDEX_OVERLAY - 1 } ;cursor:crosshair;outline:none` ;
650+ }
651+ let tool : AnnotationTool = isPdf ? "marquee" : "select" ;
640652 let dragStart : PreviewAnnotationPoint | null = null ;
641653 let activeStroke : { target : PreviewAnnotationStrokeTarget ; path : SVGPathElement } | null = null ;
642654 let pendingCapture = false ;
@@ -1356,6 +1368,7 @@ function startAnnotation(sendEnabled: boolean): void {
13561368 ipcRenderer . off ( ANNOTATION_CAPTURED_CHANNEL , onCaptured ) ;
13571369 document . documentElement . removeAttribute ( "data-t3code-annotation-tool" ) ;
13581370 cursorStyle . remove ( ) ;
1371+ inputLayer ?. remove ( ) ;
13591372 host . remove ( ) ;
13601373 activeSession = null ;
13611374 if ( notifyMain ) ipcRenderer . send ( ELEMENT_PICKED_CHANNEL , null ) ;
@@ -1467,6 +1480,10 @@ function startAnnotation(sendEnabled: boolean): void {
14671480 window . addEventListener ( "resize" , repaint , { passive : true } ) ;
14681481 ipcRenderer . on ( CANCEL_PICK_CHANNEL , onCancel ) ;
14691482 ipcRenderer . on ( ANNOTATION_CAPTURED_CHANNEL , onCaptured ) ;
1483+ if ( inputLayer ) {
1484+ document . documentElement . appendChild ( inputLayer ) ;
1485+ inputLayer . focus ( { preventScroll : true } ) ;
1486+ }
14701487 document . documentElement . appendChild ( host ) ;
14711488 rootResizeObserver . observe ( document . documentElement ) ;
14721489 refreshToolButtons ( ) ;
0 commit comments