Skip to content

Commit

Permalink
Close clipboard if clicked outside
Browse files Browse the repository at this point in the history
  • Loading branch information
manics committed Jul 12, 2024
1 parent 53c67fe commit f337437
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
15 changes: 14 additions & 1 deletion js/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import "./clipboard.css";
* @param {Element} trigger
* @param {Element} clipboard
*/
export function setupClipboard(trigger, clipboard) {
export function setupClipboard(trigger, clipboard, canvas) {
const arrowElement = clipboard.querySelector(".arrow");
function updatePosition() {
computePosition(trigger, clipboard, {
Expand Down Expand Up @@ -53,5 +53,18 @@ export function setupClipboard(trigger, clipboard) {
trigger.classList.toggle("active");
updatePosition();
e.preventDefault();
e.stopPropagation();
});

// If the clipboard is clicked this should not be passed to the desktop
clipboard.addEventListener("click", (e) => {
e.stopPropagation();
});
// Close the popup if we click outside it
canvas.addEventListener("click", () => {
if (trigger.classList.contains("active")) {
clipboard.classList.toggle("hidden");
trigger.classList.toggle("active");
}
});
}
1 change: 1 addition & 0 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function connect() {
setupClipboard(
document.getElementById("clipboard-button"),
document.getElementById("clipboard-container"),
document.getElementsByTagName("canvas")[0],
);
}

Expand Down
6 changes: 4 additions & 2 deletions tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ def test_desktop(browser):
# Open clipboard, enter random text, close clipboard
clipboard_text = str(uuid4())
page1.get_by_role("link", name="Remote Clipboard").click()
page1.wait_for_selector("#clipboard-text")
expect(page1.locator("#clipboard-text")).to_be_visible()
page1.locator("#clipboard-text").click()
page1.locator("#clipboard-text").fill(clipboard_text)
page1.get_by_role("link", name="Remote Clipboard").click()
# Click outside clipboard, it should be closed
page1.locator("canvas").click(position={"x": 969, "y": 273})
expect(page1.locator("#clipboard-text")).not_to_be_visible()

# Exec into container to check clipboard contents
for engine in ["docker", "podman"]:
Expand Down

0 comments on commit f337437

Please sign in to comment.