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.2KB

1234567891011121314151617181920212223242526272829303132
  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
  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', async (e) => {
  11. const target = e.target.closest('[data-clipboard-text], [data-clipboard-target]');
  12. if (!target) return;
  13. e.preventDefault();
  14. let text = target.getAttribute('data-clipboard-text');
  15. if (!text) {
  16. text = document.querySelector(target.getAttribute('data-clipboard-target'))?.value;
  17. }
  18. if (text && target.getAttribute('data-clipboard-text-type') === 'url') {
  19. text = toAbsoluteUrl(text);
  20. }
  21. if (text) {
  22. const success = await clippie(text);
  23. showTemporaryTooltip(target, success ? copy_success : copy_error);
  24. }
  25. });
  26. }