Toggle menu
Toggle preferences menu
Toggle personal menu
Nejste přihlášen(a)
Your IP address will be publicly visible if you make any edits.

MediaWiki:Common.js

MediaWiki interface page

Poznámka: Po zveřejnění musíte vyprázdnit cache vašeho prohlížeče, jinak změny neuvidíte.

  • Firefox / Safari: Při kliknutí na Aktualizovat držte Shift nebo stiskněte Ctrl-F5 nebo Ctrl-R (na Macu ⌘-R)
  • Google Chrome: Stiskněte Ctrl-Shift-R (na Macu ⌘-Shift-R)
  • Edge: Při kliknutí na Aktualizovat držte Ctrl nebo stiskněte Ctrl-F5.
mw.hook('wikipage.content').add(function ($c) {
  var $ph = $c.find('.private-placeholder');
  if ($ph.length > 1) {
    $ph.slice(1).remove();
  }
});

(function () {
  function wrapSelection(input, before, after) {
    const start = input.selectionStart, end = input.selectionEnd;
    const val = input.value;
    const sel = val.slice(start, end);
    input.value = val.slice(0, start) + before + sel + after + val.slice(end);

    const pos = start + before.length + sel.length + after.length;
    input.focus();
    input.setSelectionRange(pos, pos);
  }

  function wrapEachLine(input, prefix) {
    const start = input.selectionStart, end = input.selectionEnd;
    const val = input.value;
    const sel = val.slice(start, end);
    const wrapped = sel.split('\n').map(line => (line.length ? prefix + line : line)).join('\n');
    input.value = val.slice(0, start) + wrapped + val.slice(end);
    const pos = start + wrapped.length;
    input.focus();
    input.setSelectionRange(pos, pos);
  }

  function addToolbar($container) {
    if ($container.data('privateToolbar')) return; // už jednou přidané
    const $textarea = $container.find('textarea.oo-ui-inputWidget-input').first();
    if (!$textarea.length) return;
    const input = $textarea.get(0);

    const $bar = $('<div class="private-mini-toolbar" />');
    const btn = (label, title, handler) => {
      const $b = $('<button type="button" class="pmt-btn" />').text(label).attr('title', title);
      $b.on('click', () => handler(input));
      $bar.append($b);
    };

    btn('B','Tučné (\'\'\')', el => wrapSelection(el, "'''", "'''"));
    btn('I','Kurzíva (\'\')',   el => wrapSelection(el, "''",  "''"));
    btn('H2','Nadpis úrovně 2', el => wrapSelection(el, '== ', ' =='));
    btn('•','Seznam (odrážky)', el => wrapEachLine(el, '* '));
    btn('[]','Odkaz [[...]]',   el => {
      const start = el.selectionStart, end = el.selectionEnd;
      const val = el.value, sel = val.slice(start, end) || 'Cíl odkazu';
      const target = prompt('Cíl odkazu (název stránky nebo URL):', sel);
      if (target === null) return;
      wrapSelection(el, '[[', '|' + sel + ']]'.replace('|Cíl odkazu', sel));
    });

    $container.prepend($bar);
    $container.data('privateToolbar', true);
  }

  function tryEnhance(node) {
    const $pages = $(node).find('.ve-ui-mwParameterPage[data-param-name="text"]');
    $pages.each(function () {
      const $page = $(this);
      const $dialog = $page.closest('.ve-ui-mwTemplateDialog');
      if (!$dialog.length) return;

      const isSoukrome = $dialog.find('.ve-ui-mwTemplateInfoWidget-title').text().trim().toLowerCase() === 'soukrome';
      if (isSoukrome) addToolbar($page);
    });
  }

  mw.hook('ve.activationComplete').add(function () {
    const root = document.body;
    const mo = new MutationObserver(muts => muts.forEach(m => tryEnhance(m.target)));
    mo.observe(root, { childList: true, subtree: true });
    tryEnhance(document);
  });
})();