@@ -2,7 +2,6 @@ import type { VNode } from 'preact';
2
2
import { useCallback , useEffect , useRef , useState } from 'preact/hooks' ;
3
3
import { GLOBAL_OBJ } from '@sentry/utils' ;
4
4
import { h } from 'preact' ;
5
- import { once } from 'events' ;
6
5
7
6
// exporting a separate copy of `WINDOW` rather than exporting the one from `@sentry/browser`
8
7
// prevents the browser package from being bundled in the CDN bundle, and avoids a
@@ -60,31 +59,43 @@ export function ScreenshotWidget({ screenshotImage, setScreenshotImage }: Props)
60
59
const [ confirmCrop , setConfirmCrop ] = useState ( false ) ;
61
60
const [ crop , setCrop ] = useState < Rect > ( { x : 0 , y : 0 , width : 0 , height : 0 } ) ;
62
61
const imageRef = useRef < HTMLCanvasElement > ( null ) ;
63
- const renderSize = getCanvasRenderSize ( screenshotImage ?. width ?? 0 , screenshotImage ?. height ?? 0 ) ;
62
+ const [ image , setImage ] = useState < HTMLCanvasElement | null > ( screenshotImage ) ;
64
63
65
64
useEffect ( ( ) => {
66
65
const imageCanvas = imageRef . current ;
67
66
const ctx = imageCanvas ?. getContext ( '2d' ) ;
68
67
const img = new Image ( ) ;
69
- if ( screenshotImage ) {
70
- img . src = screenshotImage . toDataURL ( ) ;
71
- }
72
- img . onload = ( ) => {
73
- if ( imageCanvas && ctx ) {
74
- imageCanvas . width = img . width ;
75
- imageCanvas . height = img . height ;
76
- setCanvasSize ( imageCanvas ) ;
77
- ctx . drawImage ( img , 0 , 0 ) ;
68
+
69
+ if ( image ) {
70
+ img . src = image . toDataURL ( ) ;
71
+ const renderSize = getCanvasRenderSize ( image . width , image . height ) ;
72
+
73
+ img . onload = ( ) => {
74
+ if ( imageCanvas && ctx ) {
75
+ imageCanvas . width = img . width ;
76
+ imageCanvas . height = img . height ;
77
+ setCanvasSize ( imageCanvas ) ;
78
+ ctx . drawImage ( img , 0 , 0 ) ;
79
+ }
80
+ } ;
81
+
82
+ const canvas = canvasRef . current ;
83
+ if ( canvas ) {
84
+ canvas . width = renderSize . width ;
85
+ canvas . height = renderSize . height ;
86
+ setRectStart ( { x : 0 , y : 0 } ) ;
87
+ setCanvasSize ( canvas ) ;
78
88
}
79
- } ;
80
- } , [ screenshotImage ] ) ;
89
+ }
90
+ } , [ image ] ) ;
81
91
82
92
function setCanvasSize ( canvas : HTMLCanvasElement ) {
83
- if ( screenshotImage ) {
93
+ if ( image ) {
94
+ const renderSize = getCanvasRenderSize ( image . width , image . height ) ;
84
95
canvas . style . width = `${ renderSize . width } px` ;
85
96
canvas . style . height = `${ renderSize . height } px` ;
86
- canvas . style . top = ` ${ ( WINDOW . innerHeight - renderSize . height ) / 2 } px` ;
87
- canvas . style . left = ` ${ ( WINDOW . innerWidth - renderSize . width ) / 2 } px` ;
97
+ canvas . style . top = '0px' ;
98
+ canvas . style . left = '0px' ;
88
99
89
100
setRectEnd ( { x : renderSize . width , y : renderSize . height } ) ;
90
101
}
@@ -135,17 +146,6 @@ export function ScreenshotWidget({ screenshotImage, setScreenshotImage }: Props)
135
146
canvasRef . current ?. addEventListener ( 'mousemove' , handleMouseMove ) ;
136
147
}
137
148
138
- // canvasRef.current?.addEventListener('mousedown', onGrabButton);
139
-
140
- useEffect ( ( ) => {
141
- const canvas = canvasRef . current ;
142
- if ( canvas ) {
143
- canvas . width = renderSize . width ;
144
- canvas . height = renderSize . height ;
145
- setCanvasSize ( canvas ) ;
146
- }
147
- } , [ screenshotImage ] ) ;
148
-
149
149
useEffect ( ( ) => {
150
150
refreshCanvas ( ) ;
151
151
} , [ rectStart , rectEnd ] ) ;
@@ -154,6 +154,7 @@ export function ScreenshotWidget({ screenshotImage, setScreenshotImage }: Props)
154
154
const canvas = imageRef . current ;
155
155
if ( ! rect ) {
156
156
setScreenshotImage ( canvas ) ;
157
+ setImage ( canvas ) ;
157
158
return ;
158
159
}
159
160
// eslint-disable-next-line no-restricted-globals
@@ -166,7 +167,7 @@ export function ScreenshotWidget({ screenshotImage, setScreenshotImage }: Props)
166
167
}
167
168
168
169
setScreenshotImage ( cutoutCanvas ) ;
169
- screenshotImage = cutoutCanvas ;
170
+ setImage ( cutoutCanvas ) ;
170
171
}
171
172
172
173
return (
@@ -187,21 +188,36 @@ export function ScreenshotWidget({ screenshotImage, setScreenshotImage }: Props)
187
188
< button style = { { position : 'absolute' , left : rectStart . x , top : rectEnd . y } } > </ button >
188
189
< button style = { { position : 'absolute' , left : rectEnd . x , top : rectStart . y } } > </ button >
189
190
< button style = { { position : 'absolute' , left : rectEnd . x , top : rectEnd . y } } > </ button >
190
- < button
191
+ < div
191
192
style = { {
192
- width : '100px' ,
193
- height : '100px' ,
194
- background : 'purple' ,
195
193
position : 'absolute' ,
196
- left : 0 ,
197
- top : 0 ,
194
+ left : rectEnd . x ,
195
+ top : rectEnd . y ,
198
196
display : confirmCrop ? 'inline' : 'none' ,
199
197
} }
200
- onClick = { e => {
201
- e . preventDefault ( ) ;
202
- submit ( crop ) ;
203
- } }
204
- > </ button >
198
+ >
199
+ < button
200
+ onClick = { e => {
201
+ e . preventDefault ( ) ;
202
+ setRectStart ( { x : 0 , y : 0 } ) ;
203
+ setConfirmCrop ( false ) ;
204
+ } }
205
+ >
206
+ Cancel
207
+ </ button >
208
+ < button
209
+ style = { {
210
+ background : 'purple' ,
211
+ } }
212
+ onClick = { e => {
213
+ e . preventDefault ( ) ;
214
+ submit ( crop ) ;
215
+ setConfirmCrop ( false ) ;
216
+ } }
217
+ >
218
+ Submit
219
+ </ button >
220
+ </ div >
205
221
</ div >
206
222
) ;
207
223
}
0 commit comments