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: Porovnání verzí

MediaWiki interface page
Bez shrnutí editace
Bez shrnutí editace
Řádek 6: Řádek 6:
});
});


// Mini toolbar pro formátování parametru `text` v dialogu šablony (VE)
(function () {
(function () {
   function wrapSelection(input, before, after) {
   function wrapSelection(input, before, after) {
     const start = input.selectionStart, end = input.selectionEnd;
     const s = input.selectionStart, e = input.selectionEnd;
     const val = input.value;
     const val = input.value, sel = val.slice(s, e);
    const sel = val.slice(start, end);
     input.value = val.slice(0, s) + before + sel + after + val.slice(e);
     input.value = val.slice(0, start) + before + sel + after + val.slice(end);
     const pos = s + before.length + sel.length + after.length;
 
     const pos = start + before.length + sel.length + after.length;
     input.focus();
     input.focus();
     input.setSelectionRange(pos, pos);
     input.setSelectionRange(pos, pos);
   }
   }
   function wrapEachLine(input, prefix) {
   function wrapEachLine(input, prefix) {
     const start = input.selectionStart, end = input.selectionEnd;
     const s = input.selectionStart, e = input.selectionEnd;
     const val = input.value;
     const val = input.value, sel = val.slice(s, e);
    const sel = val.slice(start, end);
     const wrapped = sel.split('\n').map(l => (l ? prefix + l : l)).join('\n');
     const wrapped = sel.split('\n').map(line => (line.length ? prefix + line : line)).join('\n');
     input.value = val.slice(0, s) + wrapped + val.slice(e);
     input.value = val.slice(0, start) + wrapped + val.slice(end);
     const pos = s + wrapped.length;
     const pos = start + wrapped.length;
     input.focus();
     input.focus();
     input.setSelectionRange(pos, pos);
     input.setSelectionRange(pos, pos);
   }
   }
 
   function addToolbarForPage(pageEl) {
   function addToolbar($container) {
    const $page = $(pageEl);
     if ($container.data('privateToolbar')) return; // už jednou přidané
     if ($page.data('privateToolbar')) return;
     const $textarea = $container.find('textarea.oo-ui-inputWidget-input').first();
     const $textarea = $page.find('textarea.oo-ui-inputWidget-input').first();
     if (!$textarea.length) return;
     if (!$textarea.length) return;
     const input = $textarea.get(0);
     const input = $textarea.get(0);


     const $bar = $('<div class="private-mini-toolbar" />');
     const $bar = $('<div class="private-mini-toolbar" />');
     const btn = (label, title, handler) => {
     function btn(label, title, handler) {
       const $b = $('<button type="button" class="pmt-btn" />').text(label).attr('title', title);
       $('<button type="button" class="pmt-btn" />')
      $b.on('click', () => handler(input));
        .text(label).attr('title', title).on('click', () => handler(input))
      $bar.append($b);
        .appendTo($bar);
     };
     }
 
     btn('B',   "Tučné ('''...''')",   el => wrapSelection(el, "'''", "'''"));
     btn('B','Tučné (\'\'\')', el => wrapSelection(el, "'''", "'''"));
     btn('I',   "Kurzíva (''...'' )", el => wrapSelection(el, "''",  "''"));
     btn('I','Kurzíva (\'\')',   el => wrapSelection(el, "''",  "''"));
     btn('H2', 'Nadpis úrovně 2',     el => wrapSelection(el, '== ', ' =='));
     btn('H2','Nadpis úrovně 2', el => wrapSelection(el, '== ', ' =='));
     btn('•',   'Seznam (odrážky)',   el => wrapEachLine(el, '* '));
     btn('•','Seznam (odrážky)', el => wrapEachLine(el, '* '));
     btn('[]', 'Odkaz [[...]]',       el => {
     btn('[]','Odkaz [[...]]',   el => {
       const s = el.selectionStart, e = el.selectionEnd;
       const start = el.selectionStart, end = el.selectionEnd;
       const val = el.value, sel = val.slice(s, e) || 'Cíl';
       const val = el.value, sel = val.slice(start, end) || 'Cíl odkazu';
       wrapSelection(el, '[[' + sel + '|', ']]');
      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);
    // vlož lištu před textarea (nad popisek pole)
     $container.data('privateToolbar', true);
     $textarea.closest('.oo-ui-widget').before($bar);
     $page.data('privateToolbar', true);
   }
   }


   function tryEnhance(node) {
   function scan(root) {
     const $pages = $(node).find('.ve-ui-mwParameterPage[data-param-name="text"]');
     // Stránka parametru v dialogu šablony s datovým atributem parametru
    $pages.each(function () {
    $(root).find('.ve-ui-mwTemplateDialog .ve-ui-mwParameterPage[data-param-name="text"]')
      const $page = $(this);
      .each((_, el) => addToolbarForPage(el));
      const $dialog = $page.closest('.ve-ui-mwTemplateDialog');
  }
      if (!$dialog.length) return;


      const isSoukrome = $dialog.find('.ve-ui-mwTemplateInfoWidget-title').text().trim().toLowerCase() === 'soukrome';
  // Když se aktivuje VisualEditor, připoj pozorovatele
      if (isSoukrome) addToolbar($page);
  function init() {
     });
    const mo = new MutationObserver(muts => muts.forEach(m => scan(m.target)));
    mo.observe(document.body, { childList: true, subtree: true });
     scan(document); // pro případ, že dialog už je otevřený
   }
   }


   mw.hook('ve.activationComplete').add(function () {
   // Počkej, až je VE načtený (ale funguje i kdyby už byl)
     const root = document.body;
  if (mw.loader.getState('ext.visualEditor.desktopArticleTarget.init')) {
     const mo = new MutationObserver(muts => muts.forEach(m => tryEnhance(m.target)));
     init();
    mo.observe(root, { childList: true, subtree: true });
  } else {
    tryEnhance(document);
     mw.loader.using('ext.visualEditor.desktopArticleTarget.init').then(init);
   });
   }
})();
})();

Verze z 9. 10. 2025, 22:59

mw.hook('wikipage.content').add(function ($c) {
  var $ph = $c.find('.private-placeholder');
  if ($ph.length > 1) {
    $ph.slice(1).remove();
  }
});

// Mini toolbar pro formátování parametru `text` v dialogu šablony (VE)
(function () {
  function wrapSelection(input, before, after) {
    const s = input.selectionStart, e = input.selectionEnd;
    const val = input.value, sel = val.slice(s, e);
    input.value = val.slice(0, s) + before + sel + after + val.slice(e);
    const pos = s + before.length + sel.length + after.length;
    input.focus();
    input.setSelectionRange(pos, pos);
  }
  function wrapEachLine(input, prefix) {
    const s = input.selectionStart, e = input.selectionEnd;
    const val = input.value, sel = val.slice(s, e);
    const wrapped = sel.split('\n').map(l => (l ? prefix + l : l)).join('\n');
    input.value = val.slice(0, s) + wrapped + val.slice(e);
    const pos = s + wrapped.length;
    input.focus();
    input.setSelectionRange(pos, pos);
  }
  function addToolbarForPage(pageEl) {
    const $page = $(pageEl);
    if ($page.data('privateToolbar')) return;
    const $textarea = $page.find('textarea.oo-ui-inputWidget-input').first();
    if (!$textarea.length) return;
    const input = $textarea.get(0);

    const $bar = $('<div class="private-mini-toolbar" />');
    function btn(label, title, handler) {
      $('<button type="button" class="pmt-btn" />')
        .text(label).attr('title', title).on('click', () => handler(input))
        .appendTo($bar);
    }
    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 s = el.selectionStart, e = el.selectionEnd;
      const val = el.value, sel = val.slice(s, e) || 'Cíl';
      wrapSelection(el, '[[' + sel + '|', ']]');
    });

    // vlož lištu před textarea (nad popisek pole)
    $textarea.closest('.oo-ui-widget').before($bar);
    $page.data('privateToolbar', true);
  }

  function scan(root) {
    // Stránka parametru v dialogu šablony s datovým atributem parametru
    $(root).find('.ve-ui-mwTemplateDialog .ve-ui-mwParameterPage[data-param-name="text"]')
      .each((_, el) => addToolbarForPage(el));
  }

  // Když se aktivuje VisualEditor, připoj pozorovatele
  function init() {
    const mo = new MutationObserver(muts => muts.forEach(m => scan(m.target)));
    mo.observe(document.body, { childList: true, subtree: true });
    scan(document); // pro případ, že dialog už je otevřený
  }

  // Počkej, až je VE načtený (ale funguje i kdyby už byl)
  if (mw.loader.getState('ext.visualEditor.desktopArticleTarget.init')) {
    init();
  } else {
    mw.loader.using('ext.visualEditor.desktopArticleTarget.init').then(init);
  }
})();