@@ -316,12 +316,43 @@ function createBox(color: string, fill: string): HTMLDivElement {
316316}
317317
318318function positionBox ( node : HTMLElement , rect : PreviewAnnotationRect ) : void {
319+ if ( rect . width <= 0 || rect . height <= 0 ) {
320+ node . style . display = "none" ;
321+ return ;
322+ }
319323 node . style . display = "block" ;
320324 node . style . transform = `translate(${ rect . x } px, ${ rect . y } px)` ;
321325 node . style . width = `${ rect . width } px` ;
322326 node . style . height = `${ rect . height } px` ;
323327}
324328
329+ /** Clamps a box to the viewport and clipping ancestors, so its border is never painted off-screen. */
330+ function visibleElementRect ( element : Element ) : PreviewAnnotationRect {
331+ const rect = element . getBoundingClientRect ( ) ;
332+ let left = Math . max ( 0 , rect . left ) ;
333+ let top = Math . max ( 0 , rect . top ) ;
334+ let right = Math . min ( document . documentElement . clientWidth , rect . right ) ;
335+ let bottom = Math . min ( document . documentElement . clientHeight , rect . bottom ) ;
336+ if ( getComputedStyle ( element ) . position !== "fixed" ) {
337+ for ( let ancestor = element . parentElement ; ancestor ; ancestor = ancestor . parentElement ) {
338+ if ( ancestor === document . body || ancestor === document . documentElement ) break ;
339+ const style = getComputedStyle ( ancestor ) ;
340+ const bounds = ancestor . getBoundingClientRect ( ) ;
341+ const clipLeft = bounds . left + ancestor . clientLeft ;
342+ const clipTop = bounds . top + ancestor . clientTop ;
343+ if ( style . overflowX !== "visible" ) {
344+ left = Math . max ( left , clipLeft ) ;
345+ right = Math . min ( right , clipLeft + ancestor . clientWidth ) ;
346+ }
347+ if ( style . overflowY !== "visible" ) {
348+ top = Math . max ( top , clipTop ) ;
349+ bottom = Math . min ( bottom , clipTop + ancestor . clientHeight ) ;
350+ }
351+ }
352+ }
353+ return { x : left , y : top , width : Math . max ( 0 , right - left ) , height : Math . max ( 0 , bottom - top ) } ;
354+ }
355+
325356function createLabel ( ) : HTMLDivElement {
326357 const label = document . createElement ( "div" ) ;
327358 label . setAttribute ( OVERLAY_ATTRIBUTE , "" ) ;
@@ -343,11 +374,15 @@ function updateSelectedVisual(target: SelectedElement): void {
343374 target . label . style . display = "none" ;
344375 return ;
345376 }
346- const rect = target . element . getBoundingClientRect ( ) ;
347- positionBox ( target . outline , rectFromDomRect ( rect ) ) ;
377+ const rect = visibleElementRect ( target . element ) ;
378+ positionBox ( target . outline , rect ) ;
379+ if ( rect . width === 0 || rect . height === 0 ) {
380+ target . label . style . display = "none" ;
381+ return ;
382+ }
348383 target . label . textContent = describeRawElement ( target . element ) ;
349384 target . label . style . display = "block" ;
350- target . label . style . transform = `translate(${ Math . max ( 4 , rect . left ) } px, ${ Math . max ( 4 , rect . top - 22 ) } px)` ;
385+ target . label . style . transform = `translate(${ Math . max ( 4 , rect . x ) } px, ${ Math . max ( 4 , rect . y - 22 ) } px)` ;
351386}
352387
353388function toStackFrame ( frame : {
@@ -995,8 +1030,9 @@ function startAnnotation(sendEnabled: boolean): void {
9951030 const getAnnotationBounds = ( ) : PreviewAnnotationRect | null =>
9961031 unionRects (
9971032 [
998- ...Array . from ( selected . values ( ) , ( target ) =>
999- rectFromDomRect ( target . element . getBoundingClientRect ( ) ) ,
1033+ // Follow the visible outline, so a clipped selection does not anchor the editor off-screen.
1034+ ...Array . from ( selected . values ( ) , ( target ) => visibleElementRect ( target . element ) ) . filter (
1035+ ( rect ) => rect . width > 0 && rect . height > 0 ,
10001036 ) ,
10011037 ...regions . map ( ( region ) => region . rect ) ,
10021038 ...strokes . map ( ( stroke ) => stroke . bounds ) ,
@@ -1108,6 +1144,8 @@ function startAnnotation(sendEnabled: boolean): void {
11081144 for ( const target of selected . values ( ) ) updateSelectedVisual ( target ) ;
11091145 queueEditorLayout ( ) ;
11101146 } ;
1147+ // A scrollbar appearing narrows the viewport without a window resize.
1148+ const rootResizeObserver = new ResizeObserver ( repaint ) ;
11111149
11121150 const removeTargetAtPoint = ( x : number , y : number ) : boolean => {
11131151 for ( const target of Array . from ( selected . values ( ) ) . toReversed ( ) ) {
@@ -1192,7 +1230,7 @@ function startAnnotation(sendEnabled: boolean): void {
11921230 }
11931231 if ( tool === "select" && dragStart === null ) {
11941232 const target = pickFromPoint ( event . clientX , event . clientY ) ;
1195- if ( target ) positionBox ( hoverOutline , rectFromDomRect ( target . getBoundingClientRect ( ) ) ) ;
1233+ if ( target ) positionBox ( hoverOutline , visibleElementRect ( target ) ) ;
11961234 else clearHoverOutline ( ) ;
11971235 return ;
11981236 }
@@ -1310,6 +1348,7 @@ function startAnnotation(sendEnabled: boolean): void {
13101348 const teardown = ( notifyMain : boolean ) : void => {
13111349 if ( finished ) return ;
13121350 finished = true ;
1351+ rootResizeObserver . disconnect ( ) ;
13131352 restoreStyles ( ) ;
13141353 window . removeEventListener ( "pointermove" , onPointerMove , true ) ;
13151354 window . removeEventListener ( "pointerdown" , onPointerDown , true ) ;
@@ -1445,6 +1484,7 @@ function startAnnotation(sendEnabled: boolean): void {
14451484 ipcRenderer . on ( CANCEL_PICK_CHANNEL , onCancel ) ;
14461485 ipcRenderer . on ( ANNOTATION_CAPTURED_CHANNEL , onCaptured ) ;
14471486 document . documentElement . appendChild ( host ) ;
1487+ rootResizeObserver . observe ( document . documentElement ) ;
14481488 refreshToolButtons ( ) ;
14491489 updateStatus ( ) ;
14501490 activeSession = {
0 commit comments