gitea源码

clipboard.ts 1.2KB

12345678910111213141516171819202122232425262728293031323334
  1. import {showTemporaryTooltip} from '../modules/tippy.ts';
  2. import {toAbsoluteUrl} from '../utils.ts';
  3. import {clippie} from 'clippie';
  4. import type {DOMEvent} from '../utils/dom.ts';
  5. const {copy_success, copy_error} = window.config.i18n;
  6. // Enable clipboard copy from HTML attributes. These properties are supported:
  7. // - data-clipboard-text: Direct text to copy
  8. // - data-clipboard-target: Holds a selector for a <input> or <textarea> whose content is copied
  9. // - data-clipboard-text-type: When set to 'url' will convert relative to absolute urls
  10. export function initGlobalCopyToClipboardListener() {
  11. document.addEventListener('click', async (e: DOMEvent<MouseEvent>) => {
  12. const target = e.target.closest('[data-clipboard-text], [data-clipboard-target]');
  13. if (!target) return;
  14. e.preventDefault();
  15. let text = target.getAttribute('data-clipboard-text');
  16. if (!text) {
  17. text = document.querySelector<HTMLInputElement>(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. const success = await clippie(text);
  24. showTemporaryTooltip(target, success ? copy_success : copy_error);
  25. }
  26. });
  27. }