@@ -20,6 +20,13 @@ import type {
2020
2121import { createAnnotationIdSource } from "./AnnotationIds.ts" ;
2222import { resolveAnnotationSubmission } from "./AnnotationKeyboard.ts" ;
23+ import {
24+ addStrokePoint ,
25+ openStrokePath ,
26+ startStroke ,
27+ strokeBounds ,
28+ type StrokeGeometry ,
29+ } from "./AnnotationStroke.ts" ;
2330import { previewAnnotationStyles } from "./AnnotationStyles.generated.ts" ;
2431import { installRecordingCursor } from "./RecordingCursor.ts" ;
2532import { DEFAULT_RECORDING_INPUT_OPTIONS } from "./RecordingInput.ts" ;
@@ -546,34 +553,6 @@ function createUnitInput(unit: string, placeholder = "0"): HTMLInputElement {
546553 return input ;
547554}
548555
549- function pathFromPoints ( points : ReadonlyArray < PreviewAnnotationPoint > ) : string {
550- if ( points . length === 0 ) return "" ;
551- if ( points . length === 1 ) return `M ${ points [ 0 ] ! . x } ${ points [ 0 ] ! . y } l 0.01 0.01` ;
552- let path = `M ${ points [ 0 ] ! . x } ${ points [ 0 ] ! . y } ` ;
553- for ( let index = 1 ; index < points . length - 1 ; index += 1 ) {
554- const current = points [ index ] ! ;
555- const next = points [ index + 1 ] ! ;
556- path += ` Q ${ current . x } ${ current . y } ${ ( current . x + next . x ) / 2 } ${ ( current . y + next . y ) / 2 } ` ;
557- }
558- const last = points [ points . length - 1 ] ! ;
559- path += ` L ${ last . x } ${ last . y } ` ;
560- return path ;
561- }
562-
563- function strokeBounds (
564- points : ReadonlyArray < PreviewAnnotationPoint > ,
565- width : number ,
566- ) : PreviewAnnotationRect {
567- const xs = points . map ( ( point ) => point . x ) ;
568- const ys = points . map ( ( point ) => point . y ) ;
569- const padding = width + 3 ;
570- const left = Math . min ( ...xs ) - padding ;
571- const top = Math . min ( ...ys ) - padding ;
572- const right = Math . max ( ...xs ) + padding ;
573- const bottom = Math . max ( ...ys ) + padding ;
574- return { x : left , y : top , width : right - left , height : bottom - top } ;
575- }
576-
577556function startAnnotation (
578557 sendEnabled : boolean ,
579558 voice : DesktopPreviewAnnotationVoiceConfig | undefined ,
@@ -910,7 +889,12 @@ function startAnnotation(
910889 }
911890 let tool : AnnotationTool = isPdf ? "marquee" : "select" ;
912891 let dragStart : PreviewAnnotationPoint | null = null ;
913- let activeStroke : { target : PreviewAnnotationStrokeTarget ; path : SVGPathElement } | null = null ;
892+ let activeStroke : {
893+ target : PreviewAnnotationStrokeTarget ;
894+ geometry : StrokeGeometry ;
895+ group : SVGGElement ;
896+ path : SVGPathElement ;
897+ } | null = null ;
914898 let pendingCapture = false ;
915899 let editorExpanded = false ;
916900 let editorWasShown = false ;
@@ -1510,15 +1494,18 @@ function startAnnotation(
15101494 return ;
15111495 }
15121496 if ( tool === "draw" && activeStroke ) {
1513- activeStroke . target . points = [
1514- ...activeStroke . target . points ,
1515- { x : event . clientX , y : event . clientY } ,
1516- ] ;
1517- activeStroke . target . bounds = strokeBounds (
1518- activeStroke . target . points ,
1519- activeStroke . target . width ,
1520- ) ;
1521- activeStroke . path . setAttribute ( "d" , pathFromPoints ( activeStroke . target . points ) ) ;
1497+ const closedPath = addStrokePoint ( activeStroke . geometry , {
1498+ x : event . clientX ,
1499+ y : event . clientY ,
1500+ } ) ;
1501+ activeStroke . target . bounds = strokeBounds ( activeStroke . geometry , activeStroke . target . width ) ;
1502+ if ( closedPath !== null ) {
1503+ activeStroke . path . setAttribute ( "d" , closedPath ) ;
1504+ activeStroke . path = activeStroke . group . appendChild (
1505+ document . createElementNS ( "http://www.w3.org/2000/svg" , "path" ) ,
1506+ ) ;
1507+ }
1508+ activeStroke . path . setAttribute ( "d" , openStrokePath ( activeStroke . geometry ) ) ;
15221509 }
15231510 } ;
15241511
@@ -1537,23 +1524,27 @@ function startAnnotation(
15371524 }
15381525 dragStart = { x : event . clientX , y : event . clientY } ;
15391526 if ( tool === "draw" ) {
1527+ const geometry = startStroke ( dragStart ) ;
15401528 const stroke : PreviewAnnotationStrokeTarget = {
15411529 id : nextId ( "stroke" ) ,
15421530 color : annotationTheme ?. primary ?? "#2563eb" ,
15431531 width : 4 ,
1544- points : [ dragStart ] ,
1532+ points : geometry . points ,
15451533 bounds : { x : dragStart . x , y : dragStart . y , width : 1 , height : 1 } ,
15461534 } ;
1547- const path = document . createElementNS ( "http://www.w3.org/2000/svg" , "path" ) ;
1548- path . setAttribute ( OVERLAY_ATTRIBUTE , "" ) ;
1549- path . setAttribute ( "data-stroke-id" , stroke . id ) ;
1550- path . setAttribute ( "fill" , "none" ) ;
1551- path . setAttribute ( "stroke" , stroke . color ) ;
1552- path . setAttribute ( "stroke-width" , String ( stroke . width ) ) ;
1553- path . setAttribute ( "stroke-linecap" , "round" ) ;
1554- path . setAttribute ( "stroke-linejoin" , "round" ) ;
1555- svg . appendChild ( path ) ;
1556- activeStroke = { target : stroke , path } ;
1535+ const group = document . createElementNS ( "http://www.w3.org/2000/svg" , "g" ) ;
1536+ group . setAttribute ( OVERLAY_ATTRIBUTE , "" ) ;
1537+ group . setAttribute ( "data-stroke-id" , stroke . id ) ;
1538+ group . setAttribute ( "fill" , "none" ) ;
1539+ group . setAttribute ( "stroke" , stroke . color ) ;
1540+ group . setAttribute ( "stroke-width" , String ( stroke . width ) ) ;
1541+ group . setAttribute ( "stroke-linecap" , "round" ) ;
1542+ group . setAttribute ( "stroke-linejoin" , "round" ) ;
1543+ const path = group . appendChild (
1544+ document . createElementNS ( "http://www.w3.org/2000/svg" , "path" ) ,
1545+ ) ;
1546+ svg . appendChild ( group ) ;
1547+ activeStroke = { target : stroke , geometry, group, path } ;
15571548 }
15581549 } ;
15591550
@@ -1580,7 +1571,7 @@ function startAnnotation(
15801571 }
15811572 } else if ( tool === "draw" && activeStroke ) {
15821573 if ( activeStroke . target . points . length > 1 ) strokes . push ( activeStroke . target ) ;
1583- else activeStroke . path . remove ( ) ;
1574+ else activeStroke . group . remove ( ) ;
15841575 activeStroke = null ;
15851576 }
15861577 dragStart = null ;
@@ -1686,7 +1677,7 @@ function startAnnotation(
16861677 return ;
16871678 }
16881679 }
1689- if ( event . key === "Escape" ) {
1680+ if ( event . key === "Escape" && ! event . isComposing ) {
16901681 event . preventDefault ( ) ;
16911682 event . stopPropagation ( ) ;
16921683 teardown ( true ) ;
0 commit comments