|
|
@@ -4,19 +4,25 @@ import {clippie} from 'clippie'; |
|
|
|
|
|
|
|
const {copy_success, copy_error} = window.config.i18n; |
|
|
|
|
|
|
|
// For all DOM elements with [data-clipboard-target] or [data-clipboard-text], |
|
|
|
// this copy-to-clipboard will work for them |
|
|
|
// Enable clipboard copy from HTML attributes. These properties are supported: |
|
|
|
// - data-clipboard-text: Direct text to copy, has highest precedence |
|
|
|
// - data-clipboard-target: Holds a selector for a <input> or <textarea> whose content is copied |
|
|
|
// - data-clipboard-text-type: When set to 'url' will convert relative to absolute urls |
|
|
|
export function initGlobalCopyToClipboardListener() { |
|
|
|
document.addEventListener('click', (e) => { |
|
|
|
let target = e.target; |
|
|
|
// in case <button data-clipboard-text><svg></button>, so we just search |
|
|
|
// In case <button data-clipboard-text><svg></button>, so we just search |
|
|
|
// up to 3 levels for performance |
|
|
|
for (let i = 0; i < 3 && target; i++) { |
|
|
|
let txt = target.getAttribute('data-clipboard-text'); |
|
|
|
if (txt && target.getAttribute('data-clipboard-text-type') === 'url') { |
|
|
|
txt = toAbsoluteUrl(txt); |
|
|
|
let text = target.getAttribute('data-clipboard-text'); |
|
|
|
|
|
|
|
if (!text && target.getAttribute('data-clipboard-target')) { |
|
|
|
text = document.querySelector(target.getAttribute('data-clipboard-target'))?.value; |
|
|
|
} |
|
|
|
|
|
|
|
if (text && target.getAttribute('data-clipboard-text-type') === 'url') { |
|
|
|
text = toAbsoluteUrl(text); |
|
|
|
} |
|
|
|
const text = txt || document.querySelector(target.getAttribute('data-clipboard-target'))?.value; |
|
|
|
|
|
|
|
if (text) { |
|
|
|
e.preventDefault(); |
|
|
@@ -28,6 +34,7 @@ export function initGlobalCopyToClipboardListener() { |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
target = target.parentElement; |
|
|
|
} |
|
|
|
}); |