Skip to content

Commit ef66a3a

Browse files
committed
fix styling a little bit
1 parent 4eb774d commit ef66a3a

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

src/pages/Content/content.styles.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
display: none;
1717
}
1818

19-
.no-select #screenshot-bbox {
19+
#screenshot-bbox.active {
2020
display: block;
2121
}

src/pages/Content/index.ts

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ const resetBBoxAnchor = () => {
2121
bboxAnchor.style.removeProperty('height');
2222
};
2323

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+
2434
window.addEventListener('mousedown', (event) => {
2535
if (event.altKey) {
2636
// Alt / Option key is down
@@ -34,26 +44,39 @@ window.addEventListener('mousedown', (event) => {
3444
bboxAnchor.style.left = `${startingX}px`;
3545
bboxAnchor.style.width = `0px`;
3646
bboxAnchor.style.height = `0px`;
47+
48+
bboxAnchor.classList.add('active');
3749
}
3850
});
3951

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+
4058
window.addEventListener('mousemove', (event) => {
4159
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);
4467

4568
resetBBoxAnchor();
4669

47-
if (startingY <= event.clientY && startingX <= event.clientX) {
70+
if (startingY <= currY && startingX <= currX) {
4871
bboxAnchor.style.top = `${startingY}px`;
4972
bboxAnchor.style.left = `${startingX}px`;
50-
} else if (startingY <= event.clientY && startingX >= event.clientX) {
73+
} else if (startingY <= currY && startingX >= currX) {
5174
bboxAnchor.style.top = `${startingY}px`;
5275
bboxAnchor.style.right = `${document.body.clientWidth - startingX}px`;
53-
} else if (startingY >= event.clientY && startingX <= event.clientX) {
76+
} else if (startingY >= currY && startingX <= currX) {
5477
bboxAnchor.style.bottom = `${window.innerHeight - startingY}px`;
5578
bboxAnchor.style.left = `${startingX}px`;
56-
} else if (startingY >= event.clientY && startingX >= event.clientX) {
79+
} else if (startingY >= currY && startingX >= currX) {
5780
bboxAnchor.style.bottom = `${window.innerHeight - startingY}px`;
5881
bboxAnchor.style.right = `${document.body.clientWidth - startingX}px`;
5982
}
@@ -68,21 +91,18 @@ window.addEventListener('mouseup', (event) => {
6891
return;
6992
}
7093

94+
const [currX, currY] = rectifyCursorCoordinates(event.clientX, event.clientY);
7195
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),
76100
};
77-
78101
const windowSize = { width: window.innerWidth, height: window.innerHeight };
79102

80-
mouseIsDown = false;
81-
startingX = undefined;
82-
startingY = undefined;
83-
document.body.classList.remove('no-select');
84-
resetBBoxAnchor();
103+
resetEverything();
85104

105+
// avoid screenshoting the bbox
86106
setTimeout(() => {
87107
chrome.runtime.sendMessage({
88108
msg: 'SCREENSHOT_WITH_COORDINATES',
@@ -94,11 +114,7 @@ window.addEventListener('mouseup', (event) => {
94114

95115
window.addEventListener('keydown', (event) => {
96116
if (event.key === 'Escape') {
97-
mouseIsDown = false;
98-
startingX = undefined;
99-
startingY = undefined;
100-
document.body.classList.remove('no-select');
101-
resetBBoxAnchor();
117+
resetEverything();
102118
} else if (event.altKey) {
103119
hotKeyIsDown = true;
104120
document.body.classList.add('no-select');

0 commit comments

Comments
 (0)