Skip to content

Commit

Permalink
Add ability to move overlay with arrow keys
Browse files Browse the repository at this point in the history
  • Loading branch information
mertkahyaoglu committed Feb 6, 2023
1 parent 4c1d944 commit 9f987f5
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions content/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ function createZeplinOverlay(screenImageData) {
closeButton.classList.add("zeplin-extension-button", "zeplin-extension-close-button");
closeButton.addEventListener("click", () => {
overlay.remove();

document.removeEventListener("keydown", handleKeyDown, true)
});
header.append(closeButton);
wrapper.append(header);
Expand All @@ -132,16 +134,47 @@ function createZeplinOverlay(screenImageData) {

// Overlay Events
function handleKeyDown(event) {
if (event.key === "Escape") {
event.preventDefault();

overlay.remove();

document.removeEventListener("keydown", handleKeyDown)
const dist = event.shiftKey ? 10 : 1;

switch (event.key) {
case "Escape":
event.preventDefault();

overlay.remove();
document.removeEventListener("keydown", handleKeyDown, true)
break;

case "ArrowUp":
event.preventDefault();
event.stopImmediatePropagation();

wrapper.style.top = parseInt(wrapper.style.top) - dist + "px";
break;

case "ArrowRight":
event.preventDefault();
event.stopImmediatePropagation();

wrapper.style.left = parseInt(wrapper.style.left) + dist + "px";
break;

case "ArrowDown":
event.preventDefault();
event.stopImmediatePropagation();

wrapper.style.top = parseInt(wrapper.style.top) + dist + "px";
break;

case "ArrowLeft":
event.preventDefault();
event.stopImmediatePropagation();

wrapper.style.left = parseInt(wrapper.style.left) - dist + "px";
break;
}
}

document.addEventListener("keydown", handleKeyDown)
document.addEventListener("keydown", handleKeyDown, true)

return overlay;
}
Expand Down

0 comments on commit 9f987f5

Please sign in to comment.