MediaWiki:Common.js: Porovnání verzí
MediaWiki interface page
More actions
Bez shrnutí editace |
Bez shrnutí editace |
||
| Řádek 6: | Řádek 6: | ||
}); | }); | ||
(function () { function | // Mini toolbar pro formátování parametru `text` v dialogu šablony (VE) – UPGRADE | ||
(function () { | |||
// --- helpers --- | |||
function toggleWrap(input, before, after) { | |||
const s = input.selectionStart, e = input.selectionEnd; | |||
const v = input.value; | |||
const hasBefore = v.slice(s - before.length, s) === before; | |||
const hasAfter = v.slice(e, e + after.length) === after; | |||
if (hasBefore && hasAfter) { | |||
// rozbalit | |||
input.value = v.slice(0, s - before.length) + v.slice(s, e) + v.slice(e + after.length); | |||
const posStart = s - before.length; | |||
const posEnd = e - before.length; | |||
input.focus(); input.setSelectionRange(posStart, posEnd); | |||
} else { | |||
// zabalit | |||
const sel = v.slice(s, e); | |||
input.value = v.slice(0, s) + before + sel + 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; | |||
const 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 => { | |||
if (!l.trim()) return l; | |||
return 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) + ' '; | |||
const close = ' ' + '='.repeat(level); | |||
const s = input.selectionStart, e = input.selectionEnd; | |||
const v = input.value, sel = v.slice(s, e); | |||
const lines = sel.split('\n'); | |||
function stripAnyHeading(line){ | |||
// odeber libovolné =…= okraje a vrátí čistý text | |||
const m = line.match(/^\s*(=+)\s*(.*?)\s*(=+)\s*$/); | |||
if (m) return m[2]; | |||
return line.replace(/^\s+|\s+$/g,''); | |||
} | |||
// toggle: když všechny vybrané řádky už mají přesně Hn, zruš | |||
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 out = lines.map(l => { | |||
if (!l.trim()) return l; | |||
if (isAllSame) return stripAnyHeading(l); | |||
return open + stripAnyHeading(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; | |||
const 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; // za ]] (není kritické) | |||
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); | |||
// zvětšit a povolit resize | |||
$textarea.addClass('pmt-textarea'); | |||
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é (toggle)', el => toggleWrap(el, "'''", "'''")); | |||
btn('I', 'Kurzíva (toggle)', el => toggleWrap(el, "''", "''")); | |||
btn('H2', 'Nadpis H2 (toggle)', el => headingOnLines(el, 2)); | |||
btn('H3', 'Nadpis H3 (toggle)', el => headingOnLines(el, 3)); | |||
btn('H4', 'Nadpis H4 (toggle)', el => headingOnLines(el, 4)); | |||
btn('H5', 'Nadpis H5 (toggle)', el => headingOnLines(el, 5)); | |||
btn('•', 'Odrážky (toggle)', el => toggleLinesPrefix(el, '* ')); | |||
btn('[]', 'Odkaz [[…]]', insertLink); | |||
// 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)); | |||
} | |||
function init() { | |||
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); | |||
} | |||
})(); | |||
Verze z 9. 10. 2025, 23:20
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) – UPGRADE
(function () {
// --- helpers ---
function toggleWrap(input, before, after) {
const s = input.selectionStart, e = input.selectionEnd;
const v = input.value;
const hasBefore = v.slice(s - before.length, s) === before;
const hasAfter = v.slice(e, e + after.length) === after;
if (hasBefore && hasAfter) {
// rozbalit
input.value = v.slice(0, s - before.length) + v.slice(s, e) + v.slice(e + after.length);
const posStart = s - before.length;
const posEnd = e - before.length;
input.focus(); input.setSelectionRange(posStart, posEnd);
} else {
// zabalit
const sel = v.slice(s, e);
input.value = v.slice(0, s) + before + sel + 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;
const 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 => {
if (!l.trim()) return l;
return 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) + ' ';
const close = ' ' + '='.repeat(level);
const s = input.selectionStart, e = input.selectionEnd;
const v = input.value, sel = v.slice(s, e);
const lines = sel.split('\n');
function stripAnyHeading(line){
// odeber libovolné =…= okraje a vrátí čistý text
const m = line.match(/^\s*(=+)\s*(.*?)\s*(=+)\s*$/);
if (m) return m[2];
return line.replace(/^\s+|\s+$/g,'');
}
// toggle: když všechny vybrané řádky už mají přesně Hn, zruš
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 out = lines.map(l => {
if (!l.trim()) return l;
if (isAllSame) return stripAnyHeading(l);
return open + stripAnyHeading(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;
const 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; // za ]] (není kritické)
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);
// zvětšit a povolit resize
$textarea.addClass('pmt-textarea');
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é (toggle)', el => toggleWrap(el, "'''", "'''"));
btn('I', 'Kurzíva (toggle)', el => toggleWrap(el, "''", "''"));
btn('H2', 'Nadpis H2 (toggle)', el => headingOnLines(el, 2));
btn('H3', 'Nadpis H3 (toggle)', el => headingOnLines(el, 3));
btn('H4', 'Nadpis H4 (toggle)', el => headingOnLines(el, 4));
btn('H5', 'Nadpis H5 (toggle)', el => headingOnLines(el, 5));
btn('•', 'Odrážky (toggle)', el => toggleLinesPrefix(el, '* '));
btn('[]', 'Odkaz [[…]]', insertLink);
// 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));
}
function init() {
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);
}
})();