@@ -15,6 +15,7 @@ import type {
1515} from "@t3tools/contracts" ;
1616
1717import { createAnnotationIdSource } from "./AnnotationIds.ts" ;
18+ import { clampToolbarPosition , type ToolbarPosition } from "./AnnotationToolbarPosition.ts" ;
1819import { resolveAnnotationSubmission } from "./AnnotationKeyboard.ts" ;
1920import { previewAnnotationStyles } from "./AnnotationStyles.generated.ts" ;
2021import { installRecordingCursor } from "./RecordingCursor.ts" ;
@@ -135,6 +136,7 @@ interface AnnotationSession {
135136}
136137
137138let activeSession : AnnotationSession | null = null ;
139+ let toolbarPosition : ToolbarPosition | null = null ;
138140let annotationTheme : DesktopPreviewAnnotationTheme | null = null ;
139141
140142const applyAnnotationTheme = (
@@ -564,10 +566,26 @@ function startAnnotation(sendEnabled: boolean): void {
564566 const toolbar = document . createElement ( "div" ) ;
565567 toolbar . setAttribute ( OVERLAY_ATTRIBUTE , "" ) ;
566568 toolbar . className =
567- "pointer-events-auto fixed top-2.5 left-1/2 flex -translate-x-1/2 gap-0.5 rounded-lg border border-border bg-popover/95 p-1 text-popover-foreground shadow-lg backdrop-blur-xl" ;
569+ "fixed flex gap-0.5 rounded-lg border border-border bg-popover/95 p-1 text-popover-foreground shadow-lg backdrop-blur-xl" ;
568570 toolbar . style . zIndex = String ( CHROME_LAYER_Z_INDEX ) ;
571+ toolbar . style . maxWidth = "100vw" ;
572+ toolbar . style . overflowX = "auto" ;
569573 root . appendChild ( toolbar ) ;
570574
575+ const toolbarGrip = createButton ( "⠿" , "Drag annotation toolbar (arrow keys to move)" ) ;
576+ toolbarGrip . setAttribute ( "aria-label" , "Move annotation toolbar" ) ;
577+ toolbarGrip . className += " h-8 px-2" ;
578+ toolbarGrip . style . pointerEvents = "auto" ;
579+ toolbarGrip . style . cursor = "grab" ;
580+ toolbarGrip . style . touchAction = "none" ;
581+ toolbar . appendChild ( toolbarGrip ) ;
582+
583+ const dock = createButton ( "↕" , "Move annotation toolbar to top or bottom" ) ;
584+ dock . setAttribute ( "aria-label" , "Move annotation toolbar to top or bottom" ) ;
585+ dock . className += " h-8 px-2" ;
586+ dock . style . pointerEvents = "auto" ;
587+ toolbar . appendChild ( dock ) ;
588+
571589 const editor = document . createElement ( "div" ) ;
572590 editor . setAttribute ( OVERLAY_ATTRIBUTE , "" ) ;
573591 editor . className =
@@ -989,9 +1007,77 @@ function startAnnotation(sendEnabled: boolean): void {
9891007 refreshToolButtons ( ) ;
9901008 } ) ;
9911009 toolButtons . set ( candidate , button ) ;
1010+ button . style . pointerEvents = "auto" ;
9921011 toolbar . appendChild ( button ) ;
9931012 }
9941013
1014+ const positionToolbar = ( position : ToolbarPosition ) : void => {
1015+ const rect = toolbar . getBoundingClientRect ( ) ;
1016+ toolbarPosition = clampToolbarPosition (
1017+ position ,
1018+ { width : document . documentElement . clientWidth , height : window . innerHeight } ,
1019+ rect ,
1020+ ) ;
1021+ toolbar . style . left = `${ toolbarPosition . left } px` ;
1022+ toolbar . style . top = `${ toolbarPosition . top } px` ;
1023+ } ;
1024+ let toolbarDrag : { pointerId : number ; offsetX : number ; offsetY : number } | null = null ;
1025+ toolbarGrip . addEventListener ( "pointerdown" , ( event ) => {
1026+ if ( event . button !== 0 ) return ;
1027+ const rect = toolbar . getBoundingClientRect ( ) ;
1028+ toolbarDrag = {
1029+ pointerId : event . pointerId ,
1030+ offsetX : event . clientX - rect . left ,
1031+ offsetY : event . clientY - rect . top ,
1032+ } ;
1033+ toolbarGrip . setPointerCapture ( event . pointerId ) ;
1034+ toolbarGrip . style . cursor = "grabbing" ;
1035+ event . preventDefault ( ) ;
1036+ event . stopPropagation ( ) ;
1037+ } ) ;
1038+ toolbarGrip . addEventListener ( "pointermove" , ( event ) => {
1039+ if ( ! toolbarDrag || toolbarDrag . pointerId !== event . pointerId ) return ;
1040+ positionToolbar ( {
1041+ left : event . clientX - toolbarDrag . offsetX ,
1042+ top : event . clientY - toolbarDrag . offsetY ,
1043+ } ) ;
1044+ event . preventDefault ( ) ;
1045+ event . stopPropagation ( ) ;
1046+ } ) ;
1047+ const endToolbarDrag = ( event : PointerEvent ) : void => {
1048+ if ( ! toolbarDrag || toolbarDrag . pointerId !== event . pointerId ) return ;
1049+ toolbarDrag = null ;
1050+ toolbarGrip . style . cursor = "grab" ;
1051+ if ( toolbarGrip . hasPointerCapture ( event . pointerId ) )
1052+ toolbarGrip . releasePointerCapture ( event . pointerId ) ;
1053+ event . stopPropagation ( ) ;
1054+ } ;
1055+ toolbarGrip . addEventListener ( "pointerup" , endToolbarDrag ) ;
1056+ toolbarGrip . addEventListener ( "pointercancel" , endToolbarDrag ) ;
1057+ toolbarGrip . addEventListener ( "keydown" , ( event ) => {
1058+ const delta = event . shiftKey ? 40 : 10 ;
1059+ const offsets : Record < string , ToolbarPosition > = {
1060+ ArrowLeft : { left : - delta , top : 0 } ,
1061+ ArrowRight : { left : delta , top : 0 } ,
1062+ ArrowUp : { left : 0 , top : - delta } ,
1063+ ArrowDown : { left : 0 , top : delta } ,
1064+ } ;
1065+ const offset = offsets [ event . key ] ;
1066+ if ( ! offset || ! toolbarPosition ) return ;
1067+ positionToolbar ( {
1068+ left : toolbarPosition . left + offset . left ,
1069+ top : toolbarPosition . top + offset . top ,
1070+ } ) ;
1071+ event . preventDefault ( ) ;
1072+ } ) ;
1073+ dock . addEventListener ( "click" , ( ) => {
1074+ const rect = toolbar . getBoundingClientRect ( ) ;
1075+ positionToolbar ( {
1076+ left : ( document . documentElement . clientWidth - rect . width ) / 2 ,
1077+ top : rect . top < window . innerHeight / 2 ? window . innerHeight - rect . height : 0 ,
1078+ } ) ;
1079+ } ) ;
1080+
9951081 const clampEditorPosition = ( left : number , top : number ) : { left : number ; top : number } => {
9961082 const margin = 8 ;
9971083 const rect = editor . getBoundingClientRect ( ) ;
@@ -1131,6 +1217,7 @@ function startAnnotation(sendEnabled: boolean): void {
11311217 const repaint = ( ) : void => {
11321218 for ( const target of selected . values ( ) ) updateSelectedVisual ( target ) ;
11331219 queueEditorLayout ( ) ;
1220+ if ( toolbarPosition ) positionToolbar ( toolbarPosition ) ;
11341221 } ;
11351222 // A scrollbar appearing narrows the viewport without a window resize.
11361223 const rootResizeObserver = new ResizeObserver ( repaint ) ;
@@ -1212,7 +1299,7 @@ function startAnnotation(sendEnabled: boolean): void {
12121299 } ;
12131300
12141301 const onPointerMove = ( event : PointerEvent ) : void => {
1215- if ( isAnnotationNode ( event . target as Element ) ) {
1302+ if ( dragStart === null && isAnnotationNode ( event . target as Element ) ) {
12161303 clearHoverOutline ( ) ;
12171304 return ;
12181305 }
@@ -1470,6 +1557,13 @@ function startAnnotation(sendEnabled: boolean): void {
14701557 inputLayer . focus ( { preventScroll : true } ) ;
14711558 }
14721559 document . documentElement . appendChild ( host ) ;
1560+ const toolbarRect = toolbar . getBoundingClientRect ( ) ;
1561+ positionToolbar (
1562+ toolbarPosition ?? {
1563+ left : ( document . documentElement . clientWidth - toolbarRect . width ) / 2 ,
1564+ top : 10 ,
1565+ } ,
1566+ ) ;
14731567 rootResizeObserver . observe ( document . documentElement ) ;
14741568 refreshToolButtons ( ) ;
14751569 updateStatus ( ) ;
0 commit comments