Skip to content

Build your personal knowledge base with Trilium Notes

fix(clipboard): copy text from inside a popup over HTTP (closes #12321)

Swastik Tripathi520ac99936fb
diff --git a/apps/client/src/services/clipboard_ext.spec.ts b/apps/client/src/services/clipboard_ext.spec.ts
index 88745d444a..3f82f46553 100644
--- a/apps/client/src/services/clipboard_ext.spec.ts
+++ b/apps/client/src/services/clipboard_ext.spec.ts
@@ -63,6 +63,42 @@ describe("copyText", () => {
expect(document.querySelector("textarea")).toBeNull();
});
+ it("keeps the fallback textarea inside an open modal that traps focus", () => {
+ setClipboard(undefined);
+
+ document.body.innerHTML = `
+ <input class="quick-search">
+ <div class="modal show" tabindex="-1"><input class="first"><button class="copy">Copy</button></div>`;
+ const modal = document.querySelector<HTMLElement>(".modal");
+ const first = document.querySelector<HTMLInputElement>(".first");
+ const button = document.querySelector<HTMLButtonElement>(".copy");
+ if (!modal || !first || !button) throw new Error("fixture missing");
+ // Bootstrap's `FocusTrap` pulls focus that lands outside the modal back into it.
+ const trap = (e: FocusEvent) => {
+ if (!modal.contains(e.target as Node)) {
+ first.focus();
+ }
+ };
+ document.addEventListener("focusin", trap);
+ button.focus();
+
+ let copiedValue: string | undefined;
+ (document as unknown as { execCommand: () => boolean }).execCommand = () => {
+ const active = document.activeElement;
+ copiedValue = active instanceof HTMLTextAreaElement ? active.value : undefined;
+ return true;
+ };
+
+ try {
+ expect(copyText("in-modal")).toBe(true);
+ } finally {
+ document.removeEventListener("focusin", trap);
+ }
+ expect(copiedValue).toBe("in-modal");
+ expect(document.querySelector("textarea")).toBeNull();
+ expect(document.activeElement).toBe(button);
+ });
+
it("returns false and warns when the fallback throws", () => {
setClipboard(undefined);
diff --git a/apps/client/src/services/clipboard_ext.ts b/apps/client/src/services/clipboard_ext.ts
index 4a75202f87..a417414458 100644
--- a/apps/client/src/services/clipboard_ext.ts
+++ b/apps/client/src/services/clipboard_ext.ts
@@ -15,13 +15,21 @@ export function copyText(text: string) {
// Fallback method: https://stackoverflow.com/a/72239825
const textArea = document.createElement("textarea");
textArea.value = text;
+ textArea.style.position = "fixed";
+ textArea.style.opacity = "0";
+ // An open Bootstrap modal traps focus, so the textarea goes inside it to keep its selection.
+ const previouslyFocused = document.activeElement;
+ const container = previouslyFocused?.closest(".modal.show") ?? document.body;
try {
- document.body.appendChild(textArea);
+ container.appendChild(textArea);
textArea.focus();
textArea.select();
return document.execCommand('copy');
} finally {
- document.body.removeChild(textArea);
+ textArea.remove();
+ if (previouslyFocused instanceof HTMLElement) {
+ previouslyFocused.focus();
+ }
}
} catch (e) {