// Snippets menu: common wikitext pieces, grouped, with a filter box. (function () { 'use strict'; const NEEDS = { Cite: 'This needs the Cite extension (the references app). If your wiki does not have it installed, the tags will show up as plain text instead of working footnotes.', SyntaxHighlight: 'This needs the SyntaxHighlight extension on your wiki.', Math: 'This needs the Math extension on your wiki.', Poem: 'This needs the Poem feature (built in on recent MediaWiki, an extension on old ones).', ParserFunctions: 'This needs the ParserFunctions extension on your wiki.', Gallery: 'Gallery tags are built in, but the files you list must exist on your wiki.' }; const W = (g, label, a, b, ph, extra) => Object.assign({ g, label, kind: 'wrap', a, b, ph }, extra || {}); const T = (g, label, text, extra) => Object.assign({ g, label, kind: 'text', text }, extra || {}); const B = (g, label, text, extra) => Object.assign({ g, label, kind: 'block', text }, extra || {}); const ITEMS = [ W('Escaping', 'nowiki', '', '', 'plain text'), W('Escaping', 'pre (preformatted)', '
', '
', 'preformatted text'), W('Escaping', 'Comment', '', 'a note only editors see'), W('Escaping', 'kbd (keyboard key)', '', '', 'Ctrl'), W('Escaping', 'code', '', '', 'code'), T('Escaping', 'Non-breaking space', ' '), T('Escaping', 'Line break', '
'), T('Escaping', 'Clear floats', '
'), W('References', 'ref (footnote)', '', '', 'Reference text', { needs: 'Cite' }), W('References', 'Named ref', '', '', 'Reference text', { needs: 'Cite' }), T('References', 'Reuse a named ref', '', { needs: 'Cite' }), W('References', 'Grouped ref', '', '', 'Note text', { needs: 'Cite' }), B('References', 'references list', '', { needs: 'Cite' }), B('References', 'references for a group', '', { needs: 'Cite' }), B('References', 'Reflist template', '{{Reflist}}', { needs: 'Cite', hint: 'Needs the Reflist template as well.' }), W('Page control', 'noinclude', '', '', 'only on this page'), W('Page control', 'includeonly', '', '', 'only where transcluded'), W('Page control', 'onlyinclude', '', '', 'the part that is transcluded'), B('Page control', '#REDIRECT', '#REDIRECT [[Target page]]'), B('Page control', 'Category', '[[Category:Name]]'), T('Page control', 'Link to a category', '[[:Category:Name]]'), B('Page control', 'DEFAULTSORT', '{{DEFAULTSORT:Sort key}}'), B('Page control', '__TOC__', '__TOC__'), B('Page control', '__NOTOC__', '__NOTOC__'), B('Page control', '__FORCETOC__', '__FORCETOC__'), B('Page control', '__NOEDITSECTION__', '__NOEDITSECTION__'), T('Magic words', 'Signature and date', '~~~~'), T('Magic words', 'PAGENAME', '{{PAGENAME}}'), T('Magic words', 'SITENAME', '{{SITENAME}}'), T('Magic words', 'CURRENTYEAR', '{{CURRENTYEAR}}'), T('Magic words', 'Anchor', ''), T('Magic words', 'Link to a section', '[[#Section name|link text]]'), T('Magic words', 'Link to a page section', '[[Page name#Section name|link text]]'), B('Layout', 'Definition list', '; Term\n: Definition'), B('Layout', 'Collapsible box', '{| class="wikitable mw-collapsible mw-collapsed"\n! Click to expand\n|-\n| Hidden content\n|}'), B('Layout', 'Gallery', '\nFile:Example1.jpg|Caption one\nFile:Example2.jpg|Caption two\n', { needs: 'Gallery' }), B('Layout', 'Two columns', '{| style="width:100%"\n| style="width:50%; vertical-align:top" |\nLeft column\n| style="width:50%; vertical-align:top" |\nRight column\n|}'), B('Layout', 'Side box (floats right)', '
\nSide box text\n
'), B('Layout', 'Note box', '
\n\'\'\'Note:\'\'\' Text of the note.\n
'), B('Layout', 'Warning box', '
\n\'\'\'Warning:\'\'\' Text of the warning.\n
'), W('Extensions', 'syntaxhighlight', '', '', 'code', { needs: 'SyntaxHighlight' }), W('Extensions', 'math', '', '', 'x^2 + y^2 = z^2', { needs: 'Math' }), W('Extensions', 'poem', '', '', 'Line one\nLine two', { needs: 'Poem' }), T('Extensions', '#if', '{{#if: condition | then | else }}', { needs: 'ParserFunctions' }), T('Extensions', '#switch', '{{#switch: value\n | a = result a\n | b = result b\n | #default = other\n}}', { needs: 'ParserFunctions' }), T('Extensions', '#expr', '{{#expr: 1 + 2 }}', { needs: 'ParserFunctions' }) ]; function init(opts) { const api = opts.api; const toast = opts.toast; const btn = document.getElementById('btnSnippets'); if (!btn) return; const pop = document.createElement('div'); pop.className = 'pop snippets'; pop.hidden = true; document.body.appendChild(pop); function close() { pop.hidden = true; } function insertItem(it) { close(); if (it.kind === 'wrap') api.toggleWrap(it.a, it.b, it.ph); else if (it.kind === 'block') api.insertBlock(it.text); else api.insert(it.text); if (it.needs) toast(NEEDS[it.needs], 7000); } function build(filter) { const list = pop.querySelector('.snList'); list.innerHTML = ''; const f = filter.trim().toLowerCase(); let group = null; ITEMS.forEach((it) => { const hay = (it.label + ' ' + it.g + ' ' + (it.needs || '') + ' ' + (it.text || it.a || '')).toLowerCase(); if (f && hay.indexOf(f) < 0) return; if (it.g !== group) { group = it.g; const h = document.createElement('div'); h.className = 'snGroup'; h.textContent = group; list.appendChild(h); } const b = document.createElement('button'); b.className = 'tm'; b.textContent = it.label + (it.needs ? ' *' : ''); b.title = it.needs ? NEEDS[it.needs] : (it.hint || ''); b.addEventListener('mousedown', (e) => { if (e.target === b) e.preventDefault(); }); b.addEventListener('click', () => insertItem(it)); list.appendChild(b); }); if (!list.children.length) { const n = document.createElement('div'); n.className = 'snGroup'; n.textContent = 'Nothing matches.'; list.appendChild(n); } } function open() { pop.innerHTML = ''; const t = document.createElement('div'); t.className = 'popTitle'; t.textContent = 'Snippets'; pop.appendChild(t); const q = document.createElement('input'); q.type = 'search'; q.placeholder = 'Filter snippets'; q.className = 'snFilter'; q.addEventListener('input', () => build(q.value)); pop.appendChild(q); const list = document.createElement('div'); list.className = 'snList'; pop.appendChild(list); const note = document.createElement('div'); note.className = 'snNote'; note.textContent = '* Needs an extension on your wiki. Footnotes ( and ) need the Cite extension, the references app. If your wiki does not have it installed, they will not work and show as plain text.'; pop.appendChild(note); build(''); pop.hidden = false; const r = btn.getBoundingClientRect(); pop.style.left = Math.max(6, Math.min(r.left, window.innerWidth - pop.offsetWidth - 8)) + 'px'; pop.style.top = (r.bottom + 4) + 'px'; q.focus(); } btn.addEventListener('mousedown', (e) => e.preventDefault()); btn.addEventListener('click', () => { if (pop.hidden) open(); else close(); }); document.addEventListener('mousedown', (e) => { if (!pop.hidden && !pop.contains(e.target) && e.target !== btn) close(); }); document.addEventListener('keydown', (e) => { if (e.key === 'Escape' && !pop.hidden) close(); }); } window.SwiftSnippets = { init, ITEMS }; })();