@@ -21,6 +21,16 @@ const resetBBoxAnchor = () => {
21
21
bboxAnchor . style . removeProperty ( 'height' ) ;
22
22
} ;
23
23
24
+ const resetEverything = ( ) => {
25
+ hotKeyIsDown = false ;
26
+ mouseIsDown = false ;
27
+ startingX = undefined ;
28
+ startingY = undefined ;
29
+ document . body . classList . remove ( 'no-select' ) ;
30
+ resetBBoxAnchor ( ) ;
31
+ bboxAnchor . classList . remove ( 'active' ) ;
32
+ } ;
33
+
24
34
window . addEventListener ( 'mousedown' , ( event ) => {
25
35
if ( event . altKey ) {
26
36
// Alt / Option key is down
@@ -34,26 +44,39 @@ window.addEventListener('mousedown', (event) => {
34
44
bboxAnchor . style . left = `${ startingX } px` ;
35
45
bboxAnchor . style . width = `0px` ;
36
46
bboxAnchor . style . height = `0px` ;
47
+
48
+ bboxAnchor . classList . add ( 'active' ) ;
37
49
}
38
50
} ) ;
39
51
52
+ const rectifyCursorCoordinates = ( clientX : number , clientY : number ) => {
53
+ let currX = clientX < 0 ? 0 : clientX ,
54
+ currY = clientY < 0 ? 0 : clientY ;
55
+ return [ currX , currY ] ;
56
+ } ;
57
+
40
58
window . addEventListener ( 'mousemove' , ( event ) => {
41
59
if ( mouseIsDown && startingX !== undefined && startingY !== undefined ) {
42
- let width = Math . abs ( event . clientX - startingX ) ,
43
- height = Math . abs ( event . clientY - startingY ) ;
60
+ const [ currX , currY ] = rectifyCursorCoordinates (
61
+ event . clientX ,
62
+ event . clientY
63
+ ) ;
64
+
65
+ const width = Math . abs ( currX - startingX ) ,
66
+ height = Math . abs ( currY - startingY ) ;
44
67
45
68
resetBBoxAnchor ( ) ;
46
69
47
- if ( startingY <= event . clientY && startingX <= event . clientX ) {
70
+ if ( startingY <= currY && startingX <= currX ) {
48
71
bboxAnchor . style . top = `${ startingY } px` ;
49
72
bboxAnchor . style . left = `${ startingX } px` ;
50
- } else if ( startingY <= event . clientY && startingX >= event . clientX ) {
73
+ } else if ( startingY <= currY && startingX >= currX ) {
51
74
bboxAnchor . style . top = `${ startingY } px` ;
52
75
bboxAnchor . style . right = `${ document . body . clientWidth - startingX } px` ;
53
- } else if ( startingY >= event . clientY && startingX <= event . clientX ) {
76
+ } else if ( startingY >= currY && startingX <= currX ) {
54
77
bboxAnchor . style . bottom = `${ window . innerHeight - startingY } px` ;
55
78
bboxAnchor . style . left = `${ startingX } px` ;
56
- } else if ( startingY >= event . clientY && startingX >= event . clientX ) {
79
+ } else if ( startingY >= currY && startingX >= currX ) {
57
80
bboxAnchor . style . bottom = `${ window . innerHeight - startingY } px` ;
58
81
bboxAnchor . style . right = `${ document . body . clientWidth - startingX } px` ;
59
82
}
@@ -68,21 +91,18 @@ window.addEventListener('mouseup', (event) => {
68
91
return ;
69
92
}
70
93
94
+ const [ currX , currY ] = rectifyCursorCoordinates ( event . clientX , event . clientY ) ;
71
95
const rect = {
72
- x : Math . min ( startingX as number , event . clientX ) ,
73
- y : Math . min ( startingY as number , event . clientY ) ,
74
- width : Math . abs ( ( startingX as number ) - event . clientX ) ,
75
- height : Math . abs ( ( startingY as number ) - event . clientY ) ,
96
+ x : Math . min ( startingX , currX ) ,
97
+ y : Math . min ( startingY , currY ) ,
98
+ width : Math . abs ( startingX - currX ) ,
99
+ height : Math . abs ( startingY - currY ) ,
76
100
} ;
77
-
78
101
const windowSize = { width : window . innerWidth , height : window . innerHeight } ;
79
102
80
- mouseIsDown = false ;
81
- startingX = undefined ;
82
- startingY = undefined ;
83
- document . body . classList . remove ( 'no-select' ) ;
84
- resetBBoxAnchor ( ) ;
103
+ resetEverything ( ) ;
85
104
105
+ // avoid screenshoting the bbox
86
106
setTimeout ( ( ) => {
87
107
chrome . runtime . sendMessage ( {
88
108
msg : 'SCREENSHOT_WITH_COORDINATES' ,
@@ -94,11 +114,7 @@ window.addEventListener('mouseup', (event) => {
94
114
95
115
window . addEventListener ( 'keydown' , ( event ) => {
96
116
if ( event . key === 'Escape' ) {
97
- mouseIsDown = false ;
98
- startingX = undefined ;
99
- startingY = undefined ;
100
- document . body . classList . remove ( 'no-select' ) ;
101
- resetBBoxAnchor ( ) ;
117
+ resetEverything ( ) ;
102
118
} else if ( event . altKey ) {
103
119
hotKeyIsDown = true ;
104
120
document . body . classList . add ( 'no-select' ) ;
0 commit comments