1
+ // eslint-disable max-lines
1
2
import type { ComponentType , VNode , h as hType } from 'preact' ;
2
- // biome-ignore lint/nursery/noUnusedImports: reason
3
3
import { h } from 'preact' ; // eslint-disable-line @typescript-eslint/no-unused-vars
4
4
import { useCallback , useEffect , useMemo , useRef , useState } from 'preact/hooks' ;
5
5
import { DOCUMENT , WINDOW } from '../../constants' ;
@@ -40,7 +40,7 @@ const constructRect = (box: Box): Rect => {
40
40
} ;
41
41
} ;
42
42
43
- const containedImage = ( img : HTMLCanvasElement ) : Box => {
43
+ const getContainedSize = ( img : HTMLCanvasElement ) : Box => {
44
44
const ratio = img . width / img . height ;
45
45
let width = img . clientHeight * ratio ;
46
46
let height = img . clientHeight ;
@@ -70,7 +70,7 @@ export function makeScreenshotEditorComponent({ h, imageBuffer, dialog }: Factor
70
70
71
71
function resizeCropper ( ) : void {
72
72
const cropper = croppingRef . current ;
73
- const imageDimensions = constructRect ( containedImage ( imageBuffer ) ) ;
73
+ const imageDimensions = constructRect ( getContainedSize ( imageBuffer ) ) ;
74
74
if ( cropper ) {
75
75
cropper . width = imageDimensions . width ;
76
76
cropper . height = imageDimensions . height ;
@@ -88,10 +88,6 @@ export function makeScreenshotEditorComponent({ h, imageBuffer, dialog }: Factor
88
88
}
89
89
90
90
useEffect ( ( ) => {
91
- refreshCroppingBox ( ) ;
92
- } , [ croppingRect ] ) ;
93
-
94
- function refreshCroppingBox ( ) : void {
95
91
const cropper = croppingRef . current ;
96
92
if ( ! cropper ) {
97
93
return ;
@@ -101,7 +97,7 @@ export function makeScreenshotEditorComponent({ h, imageBuffer, dialog }: Factor
101
97
if ( ! ctx ) {
102
98
return ;
103
99
}
104
- const imageDimensions = constructRect ( containedImage ( imageBuffer ) ) ;
100
+ const imageDimensions = constructRect ( getContainedSize ( imageBuffer ) ) ;
105
101
const croppingBox = constructRect ( croppingRect ) ;
106
102
107
103
ctx . clearRect ( 0 , 0 , imageDimensions . width , imageDimensions . height ) ;
@@ -115,6 +111,19 @@ export function makeScreenshotEditorComponent({ h, imageBuffer, dialog }: Factor
115
111
ctx . strokeStyle = 'purple' ;
116
112
ctx . lineWidth = 3 ;
117
113
ctx . strokeRect ( croppingBox . x , croppingBox . y , croppingBox . width , croppingBox . height ) ;
114
+ } , [ croppingRect ] ) ;
115
+
116
+ function onGrabButton ( e : Event , corner : string ) : void {
117
+ setConfirmCrop ( false ) ;
118
+ const handleMouseMove = makeHandleMouseMove ( corner ) ;
119
+ const handleMouseUp = ( ) : void => {
120
+ croppingRef . current && croppingRef . current . removeEventListener ( 'mousemove' , handleMouseMove ) ;
121
+ DOCUMENT . removeEventListener ( 'mouseup' , handleMouseUp ) ;
122
+ setConfirmCrop ( true ) ;
123
+ } ;
124
+
125
+ DOCUMENT . addEventListener ( 'mouseup' , handleMouseUp ) ;
126
+ croppingRef . current && croppingRef . current . addEventListener ( 'mousemove' , handleMouseMove ) ;
118
127
}
119
128
120
129
const makeHandleMouseMove = useCallback ( ( corner : string ) => {
@@ -152,22 +161,9 @@ export function makeScreenshotEditorComponent({ h, imageBuffer, dialog }: Factor
152
161
} ;
153
162
} , [ ] ) ;
154
163
155
- function onGrabButton ( e : Event , corner : string ) : void {
156
- setConfirmCrop ( false ) ;
157
- const handleMouseMove = makeHandleMouseMove ( corner ) ;
158
- const handleMouseUp = ( ) : void => {
159
- croppingRef . current && croppingRef . current . removeEventListener ( 'mousemove' , handleMouseMove ) ;
160
- DOCUMENT . removeEventListener ( 'mouseup' , handleMouseUp ) ;
161
- setConfirmCrop ( true ) ;
162
- } ;
163
-
164
- DOCUMENT . addEventListener ( 'mouseup' , handleMouseUp ) ;
165
- croppingRef . current && croppingRef . current . addEventListener ( 'mousemove' , handleMouseMove ) ;
166
- }
167
-
168
164
function submit ( ) : void {
169
165
const cutoutCanvas = DOCUMENT . createElement ( 'canvas' ) ;
170
- const imageBox = constructRect ( containedImage ( imageBuffer ) ) ;
166
+ const imageBox = constructRect ( getContainedSize ( imageBuffer ) ) ;
171
167
const croppingBox = constructRect ( croppingRect ) ;
172
168
cutoutCanvas . width = croppingBox . width ;
173
169
cutoutCanvas . height = croppingBox . height ;
@@ -257,7 +253,6 @@ export function makeScreenshotEditorComponent({ h, imageBuffer, dialog }: Factor
257
253
> </ CropCorner >
258
254
< div
259
255
style = { {
260
- position : 'absolute' ,
261
256
left : Math . max ( 0 , croppingRect . endx - 191 ) ,
262
257
top : Math . max ( 0 , croppingRect . endy + 8 ) ,
263
258
display : confirmCrop ? 'flex' : 'none' ,
@@ -312,13 +307,10 @@ function CropCorner({
312
307
} ) : VNode {
313
308
return (
314
309
< button
310
+ class = "crop-btn"
315
311
style = { {
316
- width : 30 ,
317
- height : 30 ,
318
- position : 'absolute' ,
319
312
top : top ,
320
313
left : left ,
321
- background : 'none' ,
322
314
borderTop : corner === 'topleft' || corner === 'topright' ? 'solid purple' : 'none' ,
323
315
borderLeft : corner === 'topleft' || corner === 'bottomleft' ? 'solid purple' : 'none' ,
324
316
borderRight : corner === 'topright' || corner === 'bottomright' ? 'solid purple' : 'none' ,
0 commit comments