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
 
(Není zobrazeno 12 mezilehlých verzí od stejného uživatele.)
Řádek 5: Řádek 5:
   }
   }
});
});


(function () {
(function () {
   function replaceRange(input, start, end, str) {
   function escRe(s){ return s.replace(/[.*+?^${}()|[\]\\]/g,'\\$&'); }
    const v = input.value;
    input.value = v.slice(0, start) + str + v.slice(end);
    const pos = start + str.length;
    input.focus();
    input.setSelectionRange(pos, pos);
  }
 
   function toggleWrap(input, before, after) {
   function toggleWrap(input, before, after) {
     const s = input.selectionStart, e = input.selectionEnd;
     const s = input.selectionStart, e = input.selectionEnd, v = input.value;
    const v = input.value;
     const hasB = v.slice(Math.max(0, s - before.length), s) === before;
 
     const hasA = v.slice(e, e + after.length) === after;
     const hasBefore = s >= before.length && v.slice(s - before.length, s) === before;
     if (hasB && hasA) {
     const hasAfter  = v.slice(e, e + after.length) === after;
       input.value = v.slice(0, s - before.length) + v.slice(s, e) + v.slice(e + after.length);
 
       input.setSelectionRange(s - before.length, e - before.length);
     if (hasBefore && hasAfter) {
      const inner = v.slice(s, e);
       input.value = v.slice(0, s - before.length) + inner + v.slice(e + after.length);
       const newS = s - before.length;
      const newE = newS + inner.length;
      input.focus();
      input.setSelectionRange(newS, newE);
     } else {
     } else {
       input.value = v.slice(0, s) + before + v.slice(s, e) + after + v.slice(e);
       input.value = v.slice(0, s) + before + v.slice(s, e) + after + v.slice(e);
       const pos = e + before.length + after.length;
       const pos = e + before.length + after.length;
      input.focus();
       input.setSelectionRange(pos, pos);
       input.setSelectionRange(pos, pos);
     }
     }
    input.focus();
  }
  function toggleLinesPrefix(input, prefix) {
    const s = input.selectionStart, e = input.selectionEnd, v = input.value, sel = v.slice(s, e);
    const lines = sel.split('\n');
    const allHave = lines.filter(l => l.trim()).every(l => l.startsWith(prefix));
    const out = lines.map(l => !l.trim() ? l :
      (allHave ? l.replace(new RegExp('^' + escRe(prefix)), '') : prefix + l)
    ).join('\n');
    input.value = v.slice(0, s) + out + v.slice(e);
    const pos = s + out.length;
    input.focus(); input.setSelectionRange(pos, pos);
   }
   }
 
   function headingOnLines(input, level) {
   function getLineRange(input) {
     const open = '='.repeat(level) + ' ', close = ' ' + '='.repeat(level);
     const v = input.value;
     const s = input.selectionStart, e = input.selectionEnd, v = input.value, sel = v.slice(s, e);
     const s = input.selectionStart, e = input.selectionEnd;
     const lines = sel.split('\n');
     const start = v.lastIndexOf('\n', s - 1) + 1;
    const isAllSame = lines.filter(l => l.trim()).every(l => {
     const end = (v.indexOf('\n', e) + 1) || v.length;
      const m = l.match(/^\s*(=+)\s*(.*?)\s*(=+)\s*$/); return m && m[1].length === level && m[3].length === level;
     return { start, end, text: v.slice(start, end) };
    });
    const strip = l => { const m = l.match(/^\s*(=+)\s*(.*?)\s*(=+)\s*$/); return m ? m[2] : l.trim(); };
     const out = lines.map(l => !l.trim() ? l : (isAllSame ? strip(l) : open + strip(l) + close)).join('\n');
    input.value = v.slice(0, s) + out + v.slice(e);
    const pos = s + out.length;
     input.focus(); input.setSelectionRange(pos, pos);
   }
   }
   function setLines(input, range, newText) {
   function insertLink(input){
     replaceRange(input, range.start, range.end, newText);
    const s = input.selectionStart, e = input.selectionEnd, v = input.value, sel = v.slice(s, e) || 'Cíl';
    const w = '[[' + sel + '|' + sel + ']]';
     input.value = v.slice(0, s) + w + v.slice(e);
    const pos = s + w.length;
    input.focus(); input.setSelectionRange(pos, pos);
   }
   }


   function toggleBullets(input) {
   function makeBar(input){
    const r = getLineRange(input);
  const $bar = $('<div class="private-mini-toolbar" />').hide();
    const lines = r.text.replace(/\n$/, '').split('\n');
  let hold = false;
    const allBulleted = lines.filter(l => l.trim() !== '').every(l => l.startsWith('* '));
 
    const next = lines.map(l => {
  $bar.on('mousedown', () => { hold = true; });
      if (l.trim() === '') return l;
  $(document).on('mouseup.privateMiniTb', () => { setTimeout(() => { hold = false; }, 0); });
      return allBulleted ? l.replace(/^\* /, '') : ('* ' + l);
    }).join('\n') + (r.text.endsWith('\n') ? '\n' : '');
    setLines(input, r, next);
  }


   function stripAnyHeading(line) {
   const btn = (label, title, fn) => $('<button type="button" class="pmt-btn" />')
    const m = line.match(/^\s*(=+)\s*(.*?)\s*(=+)\s*$/);
     .text(label).attr('title', title).on('click', () => fn(input)).appendTo($bar);
     if (m && m[1] && m[3]) return m[2];
    return line;
  }
  function toggleHeading(input, level) {
    const r = getLineRange(input);
    const open = '='.repeat(level) + ' ';
    const close = ' ' + '='.repeat(level);


     const lines = r.text.replace(/\n$/, '').split('\n');
  btn('B','Tučné (toggle)',     el=>toggleWrap(el,"'''","'''"));
    const next = lines.map(l => {
  btn('I','Kurzíva (toggle)',  el=>toggleWrap(el,"''","''"));
      if (l.trim() === '') return l;
  btn('H2','Nadpis H2',        el=>headingOnLines(el,2));
      const isThis =
  btn('H3','Nadpis H3',         el=>headingOnLines(el,3));
        l.startsWith(open) && l.trimEnd().endsWith(close);
  btn('H4','Nadpis H4',        el=>headingOnLines(el,4));
      if (isThis) {
  btn('H5','Nadpis H5',        el=>headingOnLines(el,5));
         return stripAnyHeading(l);
  btn('•','Odrážky (toggle)',  el=>toggleLinesPrefix(el,'* '));
      }
  btn('[]','Odkaz [[…]]',       insertLink);
      return open + stripAnyHeading(l) + close;
    }).join('\n') + (r.text.endsWith('\n') ? '\n' : '');
    setLines(input, r, next);
  }


   function insertLink(input) {
   const $ta = $(input);
    const s = input.selectionStart, e = input.selectionEnd;
    const v = input.value;
    const sel = v.slice(s, e) || 'Cíl';
    input.value = v.slice(0, s) + '[[' + sel + '|' + sel + ']]' + v.slice(e);
    const pos = s + 4 + sel.length + 1 + sel.length + 2;
    input.focus();
    input.setSelectionRange(pos, pos);
  }


   function addToolbarForPage(pageEl) {
   $ta.on('focus.privateMiniTb', () => {
     const $page = $(pageEl);
     $ta.addClass('pmt-textarea');
     if ($page.data('privateToolbar')) return;
     $bar.show();
  });


    const $textarea = $page.find('textarea.oo-ui-inputWidget-input').first();
  $ta.on('blur.privateMiniTb', () => {
    if (!$textarea.length) return;
    setTimeout(() => {
    const input = $textarea.get(0);
      if (!hold && !$(document.activeElement).closest($bar).length) {
        $bar.hide();
        $ta.removeClass('pmt-textarea');
      }
    }, 0);
  });


    $page.addClass('private-wikitext');
  return $bar;
}


    const $bar = $('<div class="private-mini-toolbar" />');
function addToolbarForParamPage(pageEl){
  const $page = $(pageEl);
  if ($page.data('privateToolbar')) return;


    function btn(label, title, handler) {
  const $ta = $page.find('textarea.oo-ui-inputWidget-input:not(.oo-ui-element-hidden):visible').first();
      $('<button type="button" class="pmt-btn" />')
  if (!$ta.length) return;
        .text(label)
        .attr('title', title)
        .on('click', () => handler(input))
        .appendTo($bar);
    }


    btn('B',  "Tučné (toggle: '''…''')",  el => toggleWrap(el, "'''", "'''"));
  if ($ta.prev('.private-mini-toolbar').length) return;
    btn('I',  "Kurzíva (toggle: ''…'')", el => toggleWrap(el, "''", "''"));
    btn('H2', 'Nadpis H2 (toggle)',      el => toggleHeading(el, 2));
    btn('H3', 'Nadpis H3 (toggle)',      el => toggleHeading(el, 3));
    btn('H4', 'Nadpis H4 (toggle)',      el => toggleHeading(el, 4));
    btn('H5', 'Nadpis H5 (toggle)',      el => toggleHeading(el, 5));
    btn('•',  'Seznam odrážek (toggle)', el => toggleBullets(el));
    btn('[]', 'Odkaz [[…]]',              el => insertLink(el));


    $textarea.closest('.oo-ui-widget').before($bar);
  const bar = makeBar($ta.get(0));
  $ta.before(bar);


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


   function scan(root) {
   function scan(root){
     $(root)
     $(root).find('.ve-ui-mwTemplateDialog .ve-ui-mwParameterPage')
      .find('.ve-ui-mwTemplateDialog .ve-ui-mwParameterPage[data-param-name="text"]')
      .filter(function(){
       .each((_, el) => addToolbarForPage(el));
        const n = this.getAttribute('data-param-name') || this.getAttribute('data-name') || '';
        return n === 'text' || n === '1';
      })
       .each((_, el) => addToolbarForParamPage(el));
   }
   }


   function init() {
   function init(){
     const mo = new MutationObserver(muts => muts.forEach(m => scan(m.target)));
     const mo = new MutationObserver(muts => muts.forEach(m => scan(m.target)));
     mo.observe(document.body, { childList: true, subtree: true });
     mo.observe(document.body, { childList:true, subtree:true });
 
    $(document).on('focus', '.ve-ui-mwTemplateDialog textarea.oo-ui-inputWidget-input', function(){
      const page = $(this).closest('.ve-ui-mwParameterPage')[0];
      if (page) addToolbarForParamPage(page);
    });
 
     scan(document);
     scan(document);
   }
   }


   if (mw.loader.getState('ext.visualEditor.desktopArticleTarget.init')) {
  mw.util.addCSS(`
    init();
    .private-mini-toolbar { display:inline-flex; gap:.25rem; flex-wrap:wrap; margin: .25rem 0; }
   } else {
    .private-mini-toolbar .pmt-btn {
    mw.loader.using('ext.visualEditor.desktopArticleTarget.init').then(init);
      border:1px solid #ccc; border-radius:4px; padding:.2rem .4rem; font:inherit; cursor:pointer;
  }
    }
    .private-mini-toolbar .pmt-btn:hover { background:#f3f3f3; }
  `);
 
   if (mw.loader.getState('ext.visualEditor.desktopArticleTarget.init')) init();
   else mw.loader.using('ext.visualEditor.desktopArticleTarget.init').then(init);
})();
})();
mw.hook('wikipage.content').add(function ($content) {
  $content.find('section.citizen-section').each(function () {
    const $sec = $(this);
    for (;;) {
      let $p = $sec.children('p:last-child');
      if (!$p.length) break;
      if ($p.hasClass('mw-empty-elt')) { $p.remove(); continue; }
      const html = ($p.html() || '').trim();
      if (!html) { $p.remove(); continue; }
      if (/^<br\s*\/?>$/i.test(html) || html === '&nbsp;' || html === '&#160;') {
        $p.remove(); continue;
      }
      break;
    }
  });
});

Aktuální verze z 10. 10. 2025, 14:26

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



(function () {
  function escRe(s){ return s.replace(/[.*+?^${}()|[\]\\]/g,'\\$&'); }
  function toggleWrap(input, before, after) {
    const s = input.selectionStart, e = input.selectionEnd, v = input.value;
    const hasB = v.slice(Math.max(0, s - before.length), s) === before;
    const hasA = v.slice(e, e + after.length) === after;
    if (hasB && hasA) {
      input.value = v.slice(0, s - before.length) + v.slice(s, e) + v.slice(e + after.length);
      input.setSelectionRange(s - before.length, e - before.length);
    } else {
      input.value = v.slice(0, s) + before + v.slice(s, e) + after + v.slice(e);
      const pos = e + before.length + after.length;
      input.setSelectionRange(pos, pos);
    }
    input.focus();
  }
  function toggleLinesPrefix(input, prefix) {
    const s = input.selectionStart, e = input.selectionEnd, v = input.value, sel = v.slice(s, e);
    const lines = sel.split('\n');
    const allHave = lines.filter(l => l.trim()).every(l => l.startsWith(prefix));
    const out = lines.map(l => !l.trim() ? l :
      (allHave ? l.replace(new RegExp('^' + escRe(prefix)), '') : prefix + l)
    ).join('\n');
    input.value = v.slice(0, s) + out + v.slice(e);
    const pos = s + out.length;
    input.focus(); input.setSelectionRange(pos, pos);
  }
  function headingOnLines(input, level) {
    const open = '='.repeat(level) + ' ', close = ' ' + '='.repeat(level);
    const s = input.selectionStart, e = input.selectionEnd, v = input.value, sel = v.slice(s, e);
    const lines = sel.split('\n');
    const isAllSame = lines.filter(l => l.trim()).every(l => {
      const m = l.match(/^\s*(=+)\s*(.*?)\s*(=+)\s*$/); return m && m[1].length === level && m[3].length === level;
    });
    const strip = l => { const m = l.match(/^\s*(=+)\s*(.*?)\s*(=+)\s*$/); return m ? m[2] : l.trim(); };
    const out = lines.map(l => !l.trim() ? l : (isAllSame ? strip(l) : open + strip(l) + close)).join('\n');
    input.value = v.slice(0, s) + out + v.slice(e);
    const pos = s + out.length;
    input.focus(); input.setSelectionRange(pos, pos);
  }
  function insertLink(input){
    const s = input.selectionStart, e = input.selectionEnd, v = input.value, sel = v.slice(s, e) || 'Cíl';
    const w = '[[' + sel + '|' + sel + ']]';
    input.value = v.slice(0, s) + w + v.slice(e);
    const pos = s + w.length;
    input.focus(); input.setSelectionRange(pos, pos);
  }

  function makeBar(input){
  const $bar = $('<div class="private-mini-toolbar" />').hide();
  let hold = false;

  $bar.on('mousedown', () => { hold = true; });
  $(document).on('mouseup.privateMiniTb', () => { setTimeout(() => { hold = false; }, 0); });

  const btn = (label, title, fn) => $('<button type="button" class="pmt-btn" />')
    .text(label).attr('title', title).on('click', () => fn(input)).appendTo($bar);

  btn('B','Tučné (toggle)',     el=>toggleWrap(el,"'''","'''"));
  btn('I','Kurzíva (toggle)',   el=>toggleWrap(el,"''","''"));
  btn('H2','Nadpis H2',         el=>headingOnLines(el,2));
  btn('H3','Nadpis H3',         el=>headingOnLines(el,3));
  btn('H4','Nadpis H4',         el=>headingOnLines(el,4));
  btn('H5','Nadpis H5',         el=>headingOnLines(el,5));
  btn('•','Odrážky (toggle)',   el=>toggleLinesPrefix(el,'* '));
  btn('[]','Odkaz [[…]]',       insertLink);

  const $ta = $(input);

  $ta.on('focus.privateMiniTb', () => {
    $ta.addClass('pmt-textarea');
    $bar.show();
  });

  $ta.on('blur.privateMiniTb', () => {
    setTimeout(() => {
      if (!hold && !$(document.activeElement).closest($bar).length) {
        $bar.hide();
        $ta.removeClass('pmt-textarea');
      }
    }, 0);
  });

  return $bar;
}

function addToolbarForParamPage(pageEl){
  const $page = $(pageEl);
  if ($page.data('privateToolbar')) return;

  const $ta = $page.find('textarea.oo-ui-inputWidget-input:not(.oo-ui-element-hidden):visible').first();
  if (!$ta.length) return;

  if ($ta.prev('.private-mini-toolbar').length) return;

  const bar = makeBar($ta.get(0));
  $ta.before(bar);

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

  function scan(root){
    $(root).find('.ve-ui-mwTemplateDialog .ve-ui-mwParameterPage')
      .filter(function(){
        const n = this.getAttribute('data-param-name') || this.getAttribute('data-name') || '';
        return n === 'text' || n === '1';
      })
      .each((_, el) => addToolbarForParamPage(el));
  }

  function init(){
    const mo = new MutationObserver(muts => muts.forEach(m => scan(m.target)));
    mo.observe(document.body, { childList:true, subtree:true });

    $(document).on('focus', '.ve-ui-mwTemplateDialog textarea.oo-ui-inputWidget-input', function(){
      const page = $(this).closest('.ve-ui-mwParameterPage')[0];
      if (page) addToolbarForParamPage(page);
    });

    scan(document);
  }

  mw.util.addCSS(`
    .private-mini-toolbar { display:inline-flex; gap:.25rem; flex-wrap:wrap; margin: .25rem 0; }
    .private-mini-toolbar .pmt-btn {
      border:1px solid #ccc; border-radius:4px; padding:.2rem .4rem; font:inherit; cursor:pointer;
    }
    .private-mini-toolbar .pmt-btn:hover { background:#f3f3f3; }
  `);

  if (mw.loader.getState('ext.visualEditor.desktopArticleTarget.init')) init();
  else mw.loader.using('ext.visualEditor.desktopArticleTarget.init').then(init);
})();



mw.hook('wikipage.content').add(function ($content) {
  $content.find('section.citizen-section').each(function () {
    const $sec = $(this);

    for (;;) {
      let $p = $sec.children('p:last-child');
      if (!$p.length) break;

      if ($p.hasClass('mw-empty-elt')) { $p.remove(); continue; }

      const html = ($p.html() || '').trim();

      if (!html) { $p.remove(); continue; }

      if (/^<br\s*\/?>$/i.test(html) || html === '&nbsp;' || html === '&#160;') {
        $p.remove(); continue;
      }

      break;
    }
  });
});