@@ -22,35 +22,37 @@ interface Props {
2222 onError : ( error : Error ) => void ;
2323}
2424
25- type Action = 'highlight' | 'hide' | '' ;
25+ type Action = 'highlight' | 'hide' ;
2626
2727interface Box {
28- action : Action ;
2928 startX : number ;
3029 startY : number ;
3130 endX : number ;
3231 endY : number ;
3332}
3433
35- interface Rect {
36- action : Action ;
34+ interface Dimensions {
3735 x : number ;
3836 y : number ;
3937 height : number ;
4038 width : number ;
4139}
4240
41+ interface Rect extends Dimensions {
42+ action : Action ;
43+ }
44+
4345const DPI = WINDOW . devicePixelRatio ;
4446
45- const constructRect = ( box : Box ) : Rect => ( {
46- action : box . action ,
47+ const constructRect = ( action : Action , box : Box ) : Rect => ( {
48+ action,
4749 x : Math . min ( box . startX , box . endX ) ,
4850 y : Math . min ( box . startY , box . endY ) ,
4951 width : Math . abs ( box . startX - box . endX ) ,
5052 height : Math . abs ( box . startY - box . endY ) ,
5153} ) ;
5254
53- const getContainedSize = ( measurementDiv : HTMLDivElement , imageSource : HTMLCanvasElement ) : Rect => {
55+ const getContainedSize = ( measurementDiv : HTMLDivElement , imageSource : HTMLCanvasElement ) : Dimensions => {
5456 const imgClientHeight = measurementDiv . clientHeight ;
5557 const imgClientWidth = measurementDiv . clientWidth ;
5658 const ratio = imageSource . width / imageSource . height ;
@@ -62,7 +64,7 @@ const getContainedSize = (measurementDiv: HTMLDivElement, imageSource: HTMLCanva
6264 }
6365 const x = ( imgClientWidth - width ) / 2 ;
6466 const y = ( imgClientHeight - height ) / 2 ;
65- return { action : '' , x : x , y : y , width : width , height : height } ;
67+ return { x : x , y : y , width : width , height : height } ;
6668} ;
6769
6870function drawRect ( rect : Rect , ctx : CanvasRenderingContext2D , color : string , scale : number = 1 ) : void {
@@ -101,7 +103,7 @@ function drawRect(rect: Rect, ctx: CanvasRenderingContext2D, color: string, scal
101103 }
102104}
103105
104- function resizeCanvas ( canvas : HTMLCanvasElement , imageDimensions : Rect ) : void {
106+ function resizeCanvas ( canvas : HTMLCanvasElement , imageDimensions : Dimensions ) : void {
105107 canvas . width = imageDimensions . width * DPI ;
106108 canvas . height = imageDimensions . height * DPI ;
107109 canvas . style . width = `${ imageDimensions . width } px` ;
@@ -126,7 +128,7 @@ export function ScreenshotEditorFactory({
126128
127129 return function ScreenshotEditor ( { onError } : Props ) : VNode {
128130 // Data for rendering:
129- const [ action , setAction ] = hooks . useState < 'highlight' | 'hide' | '' > ( '' ) ;
131+ const [ action , setAction ] = hooks . useState < Action > ( 'highlight ' ) ;
130132 const [ drawRects , setDrawRects ] = hooks . useState < Rect [ ] > ( [ ] ) ;
131133 const [ currentRect , setCurrentRect ] = hooks . useState < Rect | undefined > ( undefined ) ;
132134
@@ -314,7 +316,7 @@ export function ScreenshotEditorFactory({
314316 const handleMouseMove = ( e : MouseEvent ) : void => {
315317 const endX = e . clientX - boundingRect . left ;
316318 const endY = e . clientY - boundingRect . top ;
317- const rect = constructRect ( { action, startX, startY, endX, endY } ) ;
319+ const rect = constructRect ( action , { startX, startY, endX, endY } ) ;
318320 // prevent drawing when just clicking (not dragging) on the canvas
319321 if ( startX != endX && startY != endY ) {
320322 setCurrentRect ( rect ) ;
@@ -330,8 +332,7 @@ export function ScreenshotEditorFactory({
330332 if ( startX != endX && startY != endY ) {
331333 // scale to image buffer
332334 const scale = imageBuffer . width / annotatingCanvas . clientWidth ;
333- const rect = constructRect ( {
334- action,
335+ const rect = constructRect ( action , {
335336 startX : startX * scale ,
336337 startY : startY * scale ,
337338 endX : endX * scale ,
@@ -360,8 +361,8 @@ export function ScreenshotEditorFactory({
360361 < div class = "editor__image-container" >
361362 < div class = "editor__canvas-container" ref = { measurementRef } >
362363 < canvas ref = { screenshotRef } > </ canvas >
363- < canvas class = "editor__canvas-annotate" ref = { annotatingRef } onMouseDown = { handleMouseDown } > </ canvas >
364- < div class = "editor__rect-container" ref = { rectContainerRef } >
364+ < canvas ref = { annotatingRef } > </ canvas >
365+ < div class = "editor__rect-container" ref = { rectContainerRef } onMouseDown = { handleMouseDown } >
365366 { drawRects . map ( ( rect , index ) => (
366367 < div
367368 key = { index }
@@ -372,7 +373,6 @@ export function ScreenshotEditorFactory({
372373 width : `${ rect . width * scaleFactor } px` ,
373374 height : `${ rect . height * scaleFactor } px` ,
374375 } }
375- onMouseDown = { handleMouseDown }
376376 >
377377 < button type = "button" onClick = { ( ) => handleDeleteRect ( index ) } >
378378 < IconClose />
0 commit comments