@@ -2,7 +2,6 @@ import type { VNode } from 'preact';
22import { useCallback , useEffect , useRef , useState } from 'preact/hooks' ;
33import { GLOBAL_OBJ } from '@sentry/utils' ;
44import { h } from 'preact' ;
5- import { once } from 'events' ;
65
76// exporting a separate copy of `WINDOW` rather than exporting the one from `@sentry/browser`
87// prevents the browser package from being bundled in the CDN bundle, and avoids a
@@ -60,31 +59,43 @@ export function ScreenshotWidget({ screenshotImage, setScreenshotImage }: Props)
6059 const [ confirmCrop , setConfirmCrop ] = useState ( false ) ;
6160 const [ crop , setCrop ] = useState < Rect > ( { x : 0 , y : 0 , width : 0 , height : 0 } ) ;
6261 const imageRef = useRef < HTMLCanvasElement > ( null ) ;
63- const renderSize = getCanvasRenderSize ( screenshotImage ?. width ?? 0 , screenshotImage ?. height ?? 0 ) ;
62+ const [ image , setImage ] = useState < HTMLCanvasElement | null > ( screenshotImage ) ;
6463
6564 useEffect ( ( ) => {
6665 const imageCanvas = imageRef . current ;
6766 const ctx = imageCanvas ?. getContext ( '2d' ) ;
6867 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 ) ;
7888 }
79- } ;
80- } , [ screenshotImage ] ) ;
89+ }
90+ } , [ image ] ) ;
8191
8292 function setCanvasSize ( canvas : HTMLCanvasElement ) {
83- if ( screenshotImage ) {
93+ if ( image ) {
94+ const renderSize = getCanvasRenderSize ( image . width , image . height ) ;
8495 canvas . style . width = `${ renderSize . width } px` ;
8596 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' ;
8899
89100 setRectEnd ( { x : renderSize . width , y : renderSize . height } ) ;
90101 }
@@ -135,17 +146,6 @@ export function ScreenshotWidget({ screenshotImage, setScreenshotImage }: Props)
135146 canvasRef . current ?. addEventListener ( 'mousemove' , handleMouseMove ) ;
136147 }
137148
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-
149149 useEffect ( ( ) => {
150150 refreshCanvas ( ) ;
151151 } , [ rectStart , rectEnd ] ) ;
@@ -154,6 +154,7 @@ export function ScreenshotWidget({ screenshotImage, setScreenshotImage }: Props)
154154 const canvas = imageRef . current ;
155155 if ( ! rect ) {
156156 setScreenshotImage ( canvas ) ;
157+ setImage ( canvas ) ;
157158 return ;
158159 }
159160 // eslint-disable-next-line no-restricted-globals
@@ -166,7 +167,7 @@ export function ScreenshotWidget({ screenshotImage, setScreenshotImage }: Props)
166167 }
167168
168169 setScreenshotImage ( cutoutCanvas ) ;
169- screenshotImage = cutoutCanvas ;
170+ setImage ( cutoutCanvas ) ;
170171 }
171172
172173 return (
@@ -187,21 +188,36 @@ export function ScreenshotWidget({ screenshotImage, setScreenshotImage }: Props)
187188 < button style = { { position : 'absolute' , left : rectStart . x , top : rectEnd . y } } > </ button >
188189 < button style = { { position : 'absolute' , left : rectEnd . x , top : rectStart . y } } > </ button >
189190 < button style = { { position : 'absolute' , left : rectEnd . x , top : rectEnd . y } } > </ button >
190- < button
191+ < div
191192 style = { {
192- width : '100px' ,
193- height : '100px' ,
194- background : 'purple' ,
195193 position : 'absolute' ,
196- left : 0 ,
197- top : 0 ,
194+ left : rectEnd . x ,
195+ top : rectEnd . y ,
198196 display : confirmCrop ? 'inline' : 'none' ,
199197 } }
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 >
205221 </ div >
206222 ) ;
207223}
0 commit comments