You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

clipboard.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import {showTemporaryTooltip} from '../modules/tippy.js';
  2. import {toAbsoluteUrl} from '../utils.js';
  3. import {clippie} from 'clippie';
  4. const {copy_success, copy_error} = window.config.i18n;
  5. // Enable clipboard copy from HTML attributes. These properties are supported:
  6. // - data-clipboard-text: Direct text to copy, has highest precedence
  7. // - data-clipboard-target: Holds a selector for a <input> or <textarea> whose content is copied
  8. // - data-clipboard-text-type: When set to 'url' will convert relative to absolute urls
  9. export function initGlobalCopyToClipboardListener() {
  10. document.addEventListener('click', (e) => {
  11. let target = e.target;
  12. // In case <button data-clipboard-text><svg></button>, so we just search
  13. // up to 3 levels for performance
  14. for (let i = 0; i < 3 && target; i++) {
  15. let text = target.getAttribute('data-clipboard-text');
  16. if (!text && target.getAttribute('data-clipboard-target')) {
  17. text = document.querySelector(target.getAttribute('data-clipboard-target'))?.value;
  18. }
  19. if (text && target.getAttribute('data-clipboard-text-type') === 'url') {
  20. text = toAbsoluteUrl(text);
  21. }
  22. if (text) {
  23. e.preventDefault();
  24. (async() => {
  25. const success = await clippie(text);
  26. showTemporaryTooltip(target, success ? copy_success : copy_error);
  27. })();
  28. break;
  29. }
  30. target = target.parentElement;
  31. }
  32. });
  33. }