Skip to content

Commit 0b8b007

Browse files
GiteaBotsilverwind
andauthored
Clipboard copy enhancements (#27669) (#27681)
Backport #27669 by @silverwind 1. Do not show temporary tooltips that are triggered from within dropdowns. Previously this resulted in the tooltip being stuck to top-left of the page like seen on issue comment URL copy. I could not figure out any tippy options that prevent this, so I think it's better to just not show it. 1. Refactor `initGlobalCopyToClipboardListener` so that it does not run a often useless `document.querySelector` on every click, make `data-clipboard-text-type` work with `data-clipboard-target`. No use in current code base but still good to have. Finally some minor code cleanup in the function. Point 1 is for this copy button: <img width="229" alt="image" src="https://github.com/go-gitea/gitea/assets/115237/81f34746-8ea5-43d9-8c6f-f6f417a9e4ad"> Co-authored-by: silverwind <[email protected]>
1 parent dab40cd commit 0b8b007

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

web_src/js/features/clipboard.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@ import {clippie} from 'clippie';
44

55
const {copy_success, copy_error} = window.config.i18n;
66

7-
// For all DOM elements with [data-clipboard-target] or [data-clipboard-text],
8-
// this copy-to-clipboard will work for them
7+
// Enable clipboard copy from HTML attributes. These properties are supported:
8+
// - data-clipboard-text: Direct text to copy, has highest precedence
9+
// - data-clipboard-target: Holds a selector for a <input> or <textarea> whose content is copied
10+
// - data-clipboard-text-type: When set to 'url' will convert relative to absolute urls
911
export function initGlobalCopyToClipboardListener() {
1012
document.addEventListener('click', (e) => {
1113
let target = e.target;
12-
// in case <button data-clipboard-text><svg></button>, so we just search
14+
// In case <button data-clipboard-text><svg></button>, so we just search
1315
// up to 3 levels for performance
1416
for (let i = 0; i < 3 && target; i++) {
15-
let txt = target.getAttribute('data-clipboard-text');
16-
if (txt && target.getAttribute('data-clipboard-text-type') === 'url') {
17-
txt = toAbsoluteUrl(txt);
17+
let text = target.getAttribute('data-clipboard-text');
18+
19+
if (!text && target.getAttribute('data-clipboard-target')) {
20+
text = document.querySelector(target.getAttribute('data-clipboard-target'))?.value;
21+
}
22+
23+
if (text && target.getAttribute('data-clipboard-text-type') === 'url') {
24+
text = toAbsoluteUrl(text);
1825
}
19-
const text = txt || document.querySelector(target.getAttribute('data-clipboard-target'))?.value;
2026

2127
if (text) {
2228
e.preventDefault();
@@ -28,6 +34,7 @@ export function initGlobalCopyToClipboardListener() {
2834

2935
break;
3036
}
37+
3138
target = target.parentElement;
3239
}
3340
});

web_src/js/modules/tippy.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ export function initGlobalTooltips() {
169169
}
170170

171171
export function showTemporaryTooltip(target, content) {
172+
// if the target is inside a dropdown, don't show the tooltip because when the dropdown
173+
// closes, the tippy would be pushed unsightly to the top-left of the screen like seen
174+
// on the issue comment menu.
175+
if (target.closest('.ui.dropdown > .menu')) return;
176+
172177
const tippy = target._tippy ?? attachTooltip(target, content);
173178
tippy.setContent(content);
174179
if (!tippy.state.isShown) tippy.show();

0 commit comments

Comments
 (0)