MediaWiki:Common.js: Difference between revisions

From ChronoRo WIKI
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Copy-to-clipboard for .wiki-copy-btn inside <code> */
document.addEventListener('click', function (e) {
(function () {
    var el = e.target.closest('.wiki-copy-btn');
  function copyText(text) {
     if (!el) return;
    if (navigator.clipboard && navigator.clipboard.writeText) {
      return navigator.clipboard.writeText(text);
    }
    return new Promise(function (resolve) {
      var ta = document.createElement('textarea');
      ta.value = text;
      ta.setAttribute('readonly', '');
      ta.style.position = 'fixed';
      ta.style.left = '-9999px';
      document.body.appendChild(ta);
      ta.select();
      try { document.execCommand('copy'); } catch (e) {}
      document.body.removeChild(ta);
      resolve();
     });
  }


     var text = el.getAttribute('data-copy');
  function showTip(targetEl, msg) {
     if (!text) return;
     // создаём tooltip не внутри <code>, а в body (чтобы не обрезался)
     var rect = targetEl.getBoundingClientRect();


     navigator.clipboard.writeText(text);
     var tip = document.createElement('div');
    tip.className = 'copy-tooltip-floating';
    tip.textContent = msg || 'Copied!';


     // remove old tooltip if exists
     document.body.appendChild(tip);
    var old = el.querySelector('.copy-tooltip');
    if (old) old.remove();


     // create tooltip
     // позиционируем над элементом
     var tip = document.createElement('span');
     var tipRect = tip.getBoundingClientRect();
     tip.className = 'copy-tooltip';
     var top = window.scrollY + rect.top - tipRect.height - 8;
     tip.textContent = 'Copied!';
     var left = window.scrollX + rect.left + (rect.width / 2) - (tipRect.width / 2);
    el.appendChild(tip);


     el.classList.add('copied');
     tip.style.top = Math.max(window.scrollY + 8, top) + 'px';
    tip.style.left = Math.max(window.scrollX + 8, left) + 'px';


     setTimeout(function () {
     setTimeout(function () {
        el.classList.remove('copied');
      if (tip && tip.parentNode) tip.remove();
        tip.remove();
    }, 1100);
     }, 1000);
  }
});
 
  document.addEventListener('click', function (e) {
    var el = e.target.closest ? e.target.closest('.wiki-copy-btn') : null;
    if (!el) return;
 
    var text = el.getAttribute('data-copy');
    if (!text) return;
 
    e.preventDefault();
 
    copyText(text).then(function () {
      el.classList.add('copied');
      showTip(el, 'Copied!');
      setTimeout(function () { el.classList.remove('copied'); }, 1000);
    }).catch(function () {
      showTip(el, 'Copy failed');
     });
  }, true);
})();

Revision as of 15:20, 21 January 2026

/* Copy-to-clipboard for .wiki-copy-btn inside <code> */
(function () {
  function copyText(text) {
    if (navigator.clipboard && navigator.clipboard.writeText) {
      return navigator.clipboard.writeText(text);
    }
    return new Promise(function (resolve) {
      var ta = document.createElement('textarea');
      ta.value = text;
      ta.setAttribute('readonly', '');
      ta.style.position = 'fixed';
      ta.style.left = '-9999px';
      document.body.appendChild(ta);
      ta.select();
      try { document.execCommand('copy'); } catch (e) {}
      document.body.removeChild(ta);
      resolve();
    });
  }

  function showTip(targetEl, msg) {
    // создаём tooltip не внутри <code>, а в body (чтобы не обрезался)
    var rect = targetEl.getBoundingClientRect();

    var tip = document.createElement('div');
    tip.className = 'copy-tooltip-floating';
    tip.textContent = msg || 'Copied!';

    document.body.appendChild(tip);

    // позиционируем над элементом
    var tipRect = tip.getBoundingClientRect();
    var top = window.scrollY + rect.top - tipRect.height - 8;
    var left = window.scrollX + rect.left + (rect.width / 2) - (tipRect.width / 2);

    tip.style.top = Math.max(window.scrollY + 8, top) + 'px';
    tip.style.left = Math.max(window.scrollX + 8, left) + 'px';

    setTimeout(function () {
      if (tip && tip.parentNode) tip.remove();
    }, 1100);
  }

  document.addEventListener('click', function (e) {
    var el = e.target.closest ? e.target.closest('.wiki-copy-btn') : null;
    if (!el) return;

    var text = el.getAttribute('data-copy');
    if (!text) return;

    e.preventDefault();

    copyText(text).then(function () {
      el.classList.add('copied');
      showTip(el, 'Copied!');
      setTimeout(function () { el.classList.remove('copied'); }, 1000);
    }).catch(function () {
      showTip(el, 'Copy failed');
    });
  }, true);
})();