Skip to content

Close clipboard if clicked outside #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion js/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import "./clipboard.css";
* Setup trigger element to toggle showing / hiding clipboard element
* @param {Element} trigger
* @param {Element} clipboard
* @param {Array[Element]} closers array of elements that should close the clipboard if clicked
*/
export function setupClipboard(trigger, clipboard) {
export function setupClipboard(trigger, clipboard, closers) {
const arrowElement = clipboard.querySelector(".arrow");
function updatePosition() {
computePosition(trigger, clipboard, {
Expand Down Expand Up @@ -53,5 +54,20 @@ 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
closers.forEach((el) => {
el.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.body, 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