147 lines
6.2 KiB
JavaScript
147 lines
6.2 KiB
JavaScript
// Export dialog: PDF, Word (.docx) or wikitext. PDF and Word can hold the rendered preview or the markup.
|
|
(function () {
|
|
'use strict';
|
|
|
|
const SKIP = { SCRIPT: 1, STYLE: 1, LINK: 1, NOSCRIPT: 1, META: 1, TITLE: 1 };
|
|
|
|
// Word cannot read stylesheets, so the look of the preview is copied onto each element as inline style.
|
|
function inlineBody(doc) {
|
|
const win = doc.defaultView;
|
|
const body = doc.body;
|
|
const clone = body.cloneNode(true);
|
|
const a = doc.createTreeWalker(body, NodeFilter.SHOW_ELEMENT);
|
|
const b = doc.createTreeWalker(clone, NodeFilter.SHOW_ELEMENT);
|
|
const kill = [];
|
|
let o = a.currentNode;
|
|
let c = b.currentNode;
|
|
do {
|
|
const tag = o.tagName;
|
|
const cs = win.getComputedStyle(o);
|
|
if (SKIP[tag] || cs.display === 'none' || /\b(mw-editsection|noprint|mw-empty-elt)\b/.test(o.className || '')) {
|
|
kill.push(c);
|
|
} else {
|
|
const st = [];
|
|
const col = cs.color;
|
|
if (col && col !== 'rgb(0, 0, 0)' && col !== 'rgb(32, 33, 34)') st.push('color:' + col);
|
|
const bg = cs.backgroundColor;
|
|
if (bg && bg !== 'rgba(0, 0, 0, 0)' && bg !== 'transparent' && bg !== 'rgb(255, 255, 255)' && tag !== 'BODY') st.push('background-color:' + bg);
|
|
if (parseInt(cs.fontWeight, 10) >= 600 && !/^(H[1-6]|B|STRONG|TH)$/.test(tag)) st.push('font-weight:bold');
|
|
if (cs.fontStyle === 'italic' && !/^(I|EM|CITE)$/.test(tag)) st.push('font-style:italic');
|
|
if (/^(P|DIV|TD|TH|H[1-6]|LI|BLOCKQUOTE)$/.test(tag) && /^(center|right|justify)$/.test(cs.textAlign)) st.push('text-align:' + cs.textAlign);
|
|
if (/^(TABLE|TD|TH)$/.test(tag)) {
|
|
const w = parseFloat(cs.borderTopWidth);
|
|
if (w > 0 && cs.borderTopStyle !== 'none') st.push('border:1px solid ' + cs.borderTopColor);
|
|
}
|
|
if (st.length) c.setAttribute('style', (c.getAttribute('style') ? c.getAttribute('style') + ';' : '') + st.join(';'));
|
|
c.removeAttribute('data-swm');
|
|
}
|
|
o = a.nextNode();
|
|
c = b.nextNode();
|
|
} while (o && c);
|
|
kill.forEach((k) => { if (k.parentNode) k.parentNode.removeChild(k); });
|
|
return clone.innerHTML;
|
|
}
|
|
|
|
function cleanHtml(doc) {
|
|
return '<!DOCTYPE html>' + doc.documentElement.outerHTML.replace(/\sdata-swm="[^"]*"/g, '');
|
|
}
|
|
|
|
function init(opts) {
|
|
const wn = window.wn;
|
|
const toast = opts.toast;
|
|
const btn = document.getElementById('btnExport');
|
|
if (!btn) return;
|
|
btn.addEventListener('click', openDialog);
|
|
|
|
function openDialog() {
|
|
const wrap = document.createElement('div');
|
|
const form = { format: 'pdf', content: 'preview', paper: 'Letter' };
|
|
const rows = [];
|
|
|
|
function group(title, name, choices, onchange) {
|
|
const g = document.createElement('div');
|
|
g.className = 'expGroup';
|
|
const t = document.createElement('div');
|
|
t.className = 'expTitle';
|
|
t.textContent = title;
|
|
g.appendChild(t);
|
|
const inputs = [];
|
|
choices.forEach((ch) => {
|
|
const l = document.createElement('label');
|
|
l.className = 'expOpt';
|
|
const i = document.createElement('input');
|
|
i.type = 'radio';
|
|
i.name = 'exp-' + name;
|
|
i.value = ch[0];
|
|
i.checked = form[name] === ch[0];
|
|
i.addEventListener('change', () => { form[name] = ch[0]; refresh(); });
|
|
l.appendChild(i);
|
|
const s = document.createElement('span');
|
|
const b = document.createElement('b');
|
|
b.textContent = ch[1];
|
|
s.appendChild(b);
|
|
if (ch[2]) { const d = document.createElement('small'); d.textContent = ch[2]; s.appendChild(d); }
|
|
l.appendChild(s);
|
|
g.appendChild(l);
|
|
inputs.push(i);
|
|
});
|
|
wrap.appendChild(g);
|
|
rows.push({ g, inputs });
|
|
return g;
|
|
}
|
|
|
|
group('Format', 'format', [
|
|
['pdf', 'PDF', 'A print-ready document.'],
|
|
['docx', 'Word document (.docx)', 'Opens in Word, LibreOffice and Google Docs.'],
|
|
['wiki', 'Wikitext (.wiki)', 'The raw markup, ready to paste into a wiki.']
|
|
]);
|
|
const contentG = group('What goes in the file', 'content', [
|
|
['preview', 'The preview', 'The page as it looks rendered, with your wiki\'s styling when the preview is live.'],
|
|
['markup', 'The wiki markup', 'The source text itself, in a monospace font.']
|
|
]);
|
|
const paperRow = document.createElement('div');
|
|
paperRow.className = 'formRow';
|
|
const pl = document.createElement('label');
|
|
pl.textContent = 'Paper size';
|
|
const ps = document.createElement('select');
|
|
[['Letter', 'Letter (8.5 x 11 in)'], ['A4', 'A4']].forEach((p) => { const o = document.createElement('option'); o.value = p[0]; o.textContent = p[1]; ps.appendChild(o); });
|
|
ps.addEventListener('change', () => { form.paper = ps.value; });
|
|
paperRow.appendChild(pl);
|
|
paperRow.appendChild(ps);
|
|
wrap.appendChild(paperRow);
|
|
|
|
function refresh() {
|
|
const wiki = form.format === 'wiki';
|
|
contentG.classList.toggle('dim', wiki);
|
|
contentG.querySelectorAll('input').forEach((i) => { i.disabled = wiki; });
|
|
ps.disabled = wiki;
|
|
paperRow.classList.toggle('dim', wiki);
|
|
}
|
|
refresh();
|
|
|
|
const m = opts.openModal({
|
|
title: 'Export "' + opts.getTitle() + '"',
|
|
content: wrap,
|
|
buttons: [{ label: 'Cancel', value: null }, { label: 'Export...', value: 'go', primary: true }]
|
|
});
|
|
m.promise.then(async (v) => {
|
|
if (v !== 'go') return;
|
|
const req = { format: form.format, content: form.content, paper: form.paper, title: opts.getTitle(), text: opts.getText() };
|
|
if (form.format !== 'wiki' && form.content === 'preview') {
|
|
toast('Rendering the preview...');
|
|
const doc = await opts.snapshot();
|
|
if (!doc) { toast('The preview is not ready. Try again in a moment.'); return; }
|
|
if (form.format === 'pdf') req.html = cleanHtml(doc);
|
|
else req.body = inlineBody(doc);
|
|
}
|
|
toast('Exporting...', 8000);
|
|
const r = await wn.exportFile(req);
|
|
if (r.canceled) { toast('Export canceled.'); return; }
|
|
toast(r.ok ? 'Saved ' + r.path : 'Export failed: ' + r.error, r.ok ? 8000 : 8000);
|
|
});
|
|
}
|
|
}
|
|
|
|
window.SwiftExport = { init, inlineBody };
|
|
})();
|