code.gitea.io/gitea@v1.22.3/web_src/js/features/clipboard.js (about)

     1  import {showTemporaryTooltip} from '../modules/tippy.js';
     2  import {toAbsoluteUrl} from '../utils.js';
     3  import {clippie} from 'clippie';
     4  
     5  const {copy_success, copy_error} = window.config.i18n;
     6  
     7  // Enable clipboard copy from HTML attributes. These properties are supported:
     8  // - data-clipboard-text: Direct text to copy
     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
    11  export function initGlobalCopyToClipboardListener() {
    12    document.addEventListener('click', async (e) => {
    13      const target = e.target.closest('[data-clipboard-text], [data-clipboard-target]');
    14      if (!target) return;
    15  
    16      e.preventDefault();
    17  
    18      let text = target.getAttribute('data-clipboard-text');
    19      if (!text) {
    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);
    25      }
    26  
    27      if (text) {
    28        const success = await clippie(text);
    29        showTemporaryTooltip(target, success ? copy_success : copy_error);
    30      }
    31    });
    32  }