MediaWiki:Common.js
MediaWiki interface page
More actions
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();
}
});
// Mini-toolbar pro parametr `text` v dialogu šablony (VE) – robustní verze
(function () {
// ===== helpers =====
function toggleWrap(input, before, after) {
const s = input.selectionStart, e = input.selectionEnd, v = input.value;
const hasBefore = v.slice(s - before.length, s) === before;
const hasAfter = v.slice(e, e + after.length) === after;
if (hasBefore && hasAfter) {
input.value = v.slice(0, s - before.length) + v.slice(s, e) + v.slice(e + after.length);
input.focus(); 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.focus(); input.setSelectionRange(pos, pos);
}
}
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('^' + prefix.replace(/[.*+?^${}()|[\]\\]/g,'\\$&')), '') : 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';
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 makeBar(input){
const $bar = $('<div class="private-mini-toolbar" />');
const add = (label, title, fn) => $('<button type="button" class="pmt-btn" />')
.text(label).attr('title', title).on('click', () => fn(input)).appendTo($bar);
add('B','Tučné (toggle)', el=>toggleWrap(el,"'''","'''"));
add('I','Kurzíva (toggle)', el=>toggleWrap(el,"''","''"));
add('H2','Nadpis H2', el=>headingOnLines(el,2));
add('H3','Nadpis H3', el=>headingOnLines(el,3));
add('H4','Nadpis H4', el=>headingOnLines(el,4));
add('H5','Nadpis H5', el=>headingOnLines(el,5));
add('•','Odrážky (toggle)', el=>toggleLinesPrefix(el,'* '));
add('[]','Odkaz [[…]]', insertLink);
return $bar;
}
function ensureToolbar($ta){
const input = $ta.get(0);
const $field = $ta.closest('.ve-ui-mwParameterPage-field');
if ($field.find('> .private-mini-toolbar').length) return;
// zvětši a označ jen viditelný textarea (bez aria-hidden)
$ta.addClass('pmt-textarea');
$field.prepend(makeBar(input));
}
function scan(root){
// Pouze parametr se jménem 'text', a jen viditelný textarea
$(root)
.find('.ve-ui-mwTemplateDialog .ve-ui-mwParameterPage[data-param-name="text"] textarea.oo-ui-inputWidget-input:not([aria-hidden="true"])')
.each((_, el) => ensureToolbar($(el)));
}
function init(){
// delegace: když pole získá focus, doruč toolbar i kdyby DOM mezitím přerenderoval
$(document).on('focus', '.ve-ui-mwTemplateDialog .ve-ui-mwParameterPage[data-param-name="text"] textarea.oo-ui-inputWidget-input:not([aria-hidden="true"])',
function(){ ensureToolbar($(this)); });
const mo = new MutationObserver(muts => muts.forEach(m => scan(m.target)));
mo.observe(document.body, { childList:true, subtree:true });
scan(document);
}
if (mw.loader.getState('ext.visualEditor.desktopArticleTarget.init')) init();
else mw.loader.using('ext.visualEditor.desktopArticleTarget.init').then(init);
})();