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)
// Mini toolbar pro formátování textu v parametrech šablon (VisualEditor)
(function () {
(function () {
   function wrapSelection(input, before, after) {
   function wrapSelection(input, before, after) {
Řádek 19: Řádek 19:
     const s = input.selectionStart, e = input.selectionEnd;
     const s = input.selectionStart, e = input.selectionEnd;
     const val = input.value, sel = val.slice(s, e);
     const val = input.value, sel = val.slice(s, e);
     const wrapped = sel.split('\n').map(l => (l ? prefix + l : l)).join('\n');
     const wrapped = (sel || '').split('\n').map(l => (l ? prefix + l : l)).join('\n');
     input.value = val.slice(0, s) + wrapped + val.slice(e);
     input.value = val.slice(0, s) + wrapped + val.slice(e);
     const pos = s + wrapped.length;
     const pos = s + wrapped.length;
Řádek 25: Řádek 25:
     input.setSelectionRange(pos, pos);
     input.setSelectionRange(pos, pos);
   }
   }
   function addToolbarForPage(pageEl) {
   function addToolbarNear(input) {
     const $page = $(pageEl);
     const $input = $(input);
     if ($page.data('privateToolbar')) return;
 
    const $textarea = $page.find('textarea.oo-ui-inputWidget-input').first();
     // najdi pole (field) – co nejvýš, ale v rámci dialogu
     if (!$textarea.length) return;
    const $field =
    const input = $textarea.get(0);
      $input.closest('.ve-ui-mwParameterPage-field').length ?
      $input.closest('.ve-ui-mwParameterPage-field') :
      $input.closest('.oo-ui-layout'); // fallback
 
     if (!$field.length || $field.data('privateToolbar')) return;


     const $bar = $('<div class="private-mini-toolbar" />');
     const $bar = $('<div class="private-mini-toolbar" />');
Řádek 40: Řádek 44:
     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 H2',         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 s = el.selectionStart, e = el.selectionEnd;
       const val = el.value, sel = val.slice(s, e) || 'Cíl';
       const val = el.value, sel = val.slice(s, e) || 'Cíl';
       wrapSelection(el, '[[' + sel + '|', ']]');
       // [[Cíl|text]] pokud je něco vybráno; jinak jen [[Cíl]]
      if (sel && sel !== 'Cíl') wrapSelection(el, '[[', '|' + sel + ']]');
      else wrapSelection(el, '[[Cíl]]', '');
     });
     });


     // vlož lištu před textarea (nad popisek pole)
     // vlož lištu těsně před widget s textareou
     $textarea.closest('.oo-ui-widget').before($bar);
     const $widget = $input.closest('.oo-ui-widget');
     $page.data('privateToolbar', true);
     ($widget.length ? $widget : $field).before($bar);
  }
 
  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
     $field.data('privateToolbar', true);
  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)
   // Delegovaný listener: když zaostří textarea v dialogu šablony, přidej lištu
   if (mw.loader.getState('ext.visualEditor.desktopArticleTarget.init')) {
   $(document).on(
     init();
    'focusin',
  } else {
    '.ve-ui-mwTemplateDialog textarea.oo-ui-inputWidget-input:not(.oo-ui-element-hidden)',
    mw.loader.using('ext.visualEditor.desktopArticleTarget.init').then(init);
     function () { addToolbarNear(this); }
   }
   );
})();
})();

Verze z 9. 10. 2025, 23:04

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í textu v parametrech šablon (VisualEditor)
(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 addToolbarNear(input) {
    const $input = $(input);

    // najdi pole (field) – co nejvýš, ale v rámci dialogu
    const $field =
      $input.closest('.ve-ui-mwParameterPage-field').length ?
      $input.closest('.ve-ui-mwParameterPage-field') :
      $input.closest('.oo-ui-layout'); // fallback

    if (!$field.length || $field.data('privateToolbar')) return;

    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 H2',          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';
      // [[Cíl|text]] pokud je něco vybráno; jinak jen [[Cíl]]
      if (sel && sel !== 'Cíl') wrapSelection(el, '[[', '|' + sel + ']]');
      else wrapSelection(el, '[[Cíl]]', '');
    });

    // vlož lištu těsně před widget s textareou
    const $widget = $input.closest('.oo-ui-widget');
    ($widget.length ? $widget : $field).before($bar);

    $field.data('privateToolbar', true);
  }

  // Delegovaný listener: když zaostří textarea v dialogu šablony, přidej lištu
  $(document).on(
    'focusin',
    '.ve-ui-mwTemplateDialog textarea.oo-ui-inputWidget-input:not(.oo-ui-element-hidden)',
    function () { addToolbarNear(this); }
  );
})();