Skip to content

Commit 8beded8

Browse files
committed
tune clipboard
1 parent b59f83f commit 8beded8

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

web_src/js/features/clipboard.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,26 @@ function onError(btn) {
1818

1919
export default function initGlobalCopyToClipboardListener() {
2020
document.addEventListener('click', async (e) => {
21-
const target = e.target;
22-
let text;
23-
if (target.dataset.clipboardText) {
24-
text = target.dataset.clipboardText;
25-
} else if (target.dataset.clipboardTarget) {
26-
text = document.querySelector(target.dataset.clipboardTarget)?.value;
27-
}
28-
if (!text) return;
29-
30-
try {
31-
await navigator.clipboard.writeText(text);
32-
onSuccess(target);
33-
} catch {
34-
onError(target);
21+
let target = e.target;
22+
// in case <button data-clipboard-text><svg></button>, so we just search up to 3 levels for performance.
23+
for (let i = 0; i < 3 && target; i++) {
24+
let text;
25+
if (target.dataset.clipboardText) {
26+
text = target.dataset.clipboardText;
27+
} else if (target.dataset.clipboardTarget) {
28+
text = document.querySelector(target.dataset.clipboardTarget)?.value;
29+
}
30+
if (text) {
31+
try {
32+
await navigator.clipboard.writeText(text);
33+
onSuccess(target);
34+
} catch {
35+
onError(target);
36+
}
37+
e.preventDefault();
38+
break;
39+
}
40+
target = target.parentElement;
3541
}
3642
});
3743
}

0 commit comments

Comments
 (0)