| 1 |
(d => { |
| 2 |
function new_req(url, data, callback) { |
| 3 |
const req = new XMLHttpRequest(); |
| 4 |
req.open('POST', url); |
| 5 |
|
| 6 |
req.setRequestHeader('litedown-data', data); |
| 7 |
req.send(); |
| 8 |
req.onload = callback; |
| 9 |
} |
| 10 |
function new_dialog(text, error, html, action = el => el.close(), callback = (el, b) => {}) { |
| 11 |
let el = d.querySelector('dialog.litedown-dialog'); |
| 12 |
if (!el) { |
| 13 |
el = d.createElement('dialog'); |
| 14 |
el.className = 'litedown-dialog'; |
| 15 |
el.innerHTML = '<div></div><p><button>OK</button></p>'; |
| 16 |
d.body.append(el); |
| 17 |
} |
| 18 |
const b = el.querySelector('button'); |
| 19 |
b.onclick = e => action(el, b); |
| 20 |
const p = el.firstElementChild; |
| 21 |
p[(html && !error) ? 'innerHTML' : 'innerText'] = text.toLowerCase() === 'connection refused' ? |
| 22 |
(text + '; please re-run litedown::roam()') : text; |
| 23 |
if (error) p.className = 'error'; |
| 24 |
callback(el, b); |
| 25 |
el.showModal(); |
| 26 |
} |
| 27 |
function show_dialog(resp) { |
| 28 |
new_dialog(resp.responseText, resp.status !== 200); |
| 29 |
} |
| 30 |
|
| 31 |
function new_file() { |
| 32 |
new_req(location.href, 'new', e => { |
| 33 |
new_dialog(e.target.responseText, e.target.status !== 200, true, (el, b) => { |
| 34 |
const file = el.querySelector('#filename-input').value; |
| 35 |
if (!file || b.innerText === 'Cancel') return el.close(); |
| 36 |
const o = location.origin, p = location.pathname, |
| 37 |
u = o + p + (/\/$/.test(p) ? '' : '/../') + file, features = []; |
| 38 |
el.querySelectorAll('input[type="checkbox"]').forEach(input => { |
| 39 |
if (input.checked) features.push(input.name); |
| 40 |
}); |
| 41 |
new_req(u, 'new:' + features.join(','), e => { |
| 42 |
const text = e.target.responseText; |
| 43 |
switch (text) { |
| 44 |
case 'view': location.href = u + '?preview=2'; break; |
| 45 |
case 'true': break; |
| 46 |
case 'false': new_dialog('Failed to create the file', true); break; |
| 47 |
default: new_dialog(text, true); |
| 48 |
} |
| 49 |
}); |
| 50 |
}, (el, b) => { |
| 51 |
b.innerText = 'Cancel'; |
| 52 |
const input = el.querySelector('#filename-input'), |
| 53 |
options = [...el.querySelector('#file-list').options].map(o => o.value); |
| 54 |
input.onchange = e => { |
| 55 |
const v = input.value.trim(); |
| 56 |
if (!/.+\.[a-zA-Z0-9]+$/.test(v)) { |
| 57 |
input.value = v + '.Rmd'; input.oninput(); |
| 58 |
} |
| 59 |
}; |
| 60 |
input.oninput = e => { |
| 61 |
const v = input.value.trim(), X = '❌'; |
| 62 |
b.innerText = (v === '' || v.startsWith(X) || options.includes(v) || options.includes(X + ' ' + v)) ? 'Cancel' : 'OK'; |
| 63 |
}; |
| 64 |
}); |
| 65 |
}); |
| 66 |
} |
| 67 |
|
| 68 |
const fm = d.querySelector('.frontmatter'); |
| 69 |
fm && !fm.innerText.trim() && fm.remove(); |
| 70 |
const chapters = d.querySelectorAll('div[data-source]'); |
| 71 |
|
| 72 |
chapters.length > 1 && chapters.forEach((el, i) => { |
| 73 |
const u = el.dataset.source; |
| 74 |
if (!u || el.querySelector('.buttons')) return; |
| 75 |
const u2 = Array((u.match(/\/+/g) || []).length).fill('../').join(''), |
| 76 |
a2 = i === 0 ? '' : `<a href="${u2}${u}?preview=2" class="btn-lite run" title="Preview single chapter">⏵</a>`; |
| 77 |
el.insertAdjacentHTML('afterbegin', `<span class="buttons">${a2}<a href="?path=${u}" class="btn-lite open pencil" title="Open ${u}">✎</a></span>`); |
| 78 |
}); |
| 79 |
const nav = d.querySelector('.nav-path, .title h1'); |
| 80 |
const btn = nav.querySelector('.buttons') || d.createElement('span'); |
| 81 |
btn.className = 'buttons'; |
| 82 |
['back', 'forward', 'new', 'refresh', 'print'].forEach((action, i) => { |
| 83 |
if (btn.querySelector(`.${action}`)) return; |
| 84 |
const a = d.createElement('a'); |
| 85 |
a.href = '#'; a.title = action[0].toUpperCase() + action.slice(1); |
| 86 |
const k = ({ |
| 87 |
back: 'Alt + LeftArrow', forward: 'Alt + RightArrow', |
| 88 |
refresh: 'Ctrl + R / Command + R' |
| 89 |
})[action]; |
| 90 |
if (k) a.title += ` (${k})`; |
| 91 |
a.className = action + ' btn-lite'; |
| 92 |
a.innerText = ['←', '→', '+', '⟳', '⎙'][i]; |
| 93 |
a.onclick = e => btnAction(e, action); |
| 94 |
btn.append(a); |
| 95 |
}); |
| 96 |
if (nav) { |
| 97 |
nav.querySelectorAll('a.btn-lite').forEach(a => btn.append(a)); |
| 98 |
nav.append(btn); |
| 99 |
} |
| 100 |
function btnAction(e, action) { |
| 101 |
if (!action) return; |
| 102 |
e.preventDefault(); |
| 103 |
switch (action) { |
| 104 |
case 'back': history.back(); break; |
| 105 |
case 'forward': history.forward(); break; |
| 106 |
case 'new': new_file(); break; |
| 107 |
case 'refresh': location.reload(); break; |
| 108 |
case 'print': window.print(); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
d.querySelectorAll('a[href]').forEach(a => { |
| 113 |
if (a.innerText !== '✎' || a.onclick) return; |
| 114 |
a.classList.add('pencil'); if (!a.title) a.title = 'Open'; |
| 115 |
a.onclick = e => { |
| 116 |
e.preventDefault(); |
| 117 |
new_req(a.href, 'open'); |
| 118 |
}; |
| 119 |
}); |
| 120 |
function btnSave(e, a) { |
| 121 |
e.preventDefault(); |
| 122 |
const cls = d.body.classList; |
| 123 |
if (cls.contains('waiting')) return; |
| 124 |
cls.add('waiting'); |
| 125 |
new_req(a ? a.href : location.href, 'save', e => { |
| 126 |
show_dialog(e.target); |
| 127 |
cls.remove('waiting'); |
| 128 |
}); |
| 129 |
}; |
| 130 |
|
| 131 |
d.querySelectorAll('a.save[href]').forEach(a => { |
| 132 |
if (a.innerText !== '↯') return; |
| 133 |
a.title = 'Render to disk'; |
| 134 |
a.getAttribute('href') === '#' && (a.title += '(Ctrl + K / Command + K)'); |
| 135 |
a.onclick = e => btnSave(e, a); |
| 136 |
}); |
| 137 |
function check_one(q, a, s) { |
| 138 |
(q ? d.querySelectorAll(q) : [d.body]).forEach(el => { |
| 139 |
let u = a ? el[a] : location.href; |
| 140 |
|
| 141 |
if (a && u && !(u.startsWith(location.origin) && u.includes('/custom/litedown/'))) |
| 142 |
return; |
| 143 |
|
| 144 |
if (u && u.includes('__files/')) return; |
| 145 |
if (el.dataset.wait) return; |
| 146 |
el.dataset.wait = 1; |
| 147 |
new_req(u, q ? (a ? 'asset' : `book:${el.dataset[s]}`) : 'page', e => { |
| 148 |
const res = e.target.responseText; |
| 149 |
if (e.target.status !== 200) { |
| 150 |
if (res.toLowerCase() != 'connection reset by peer') show_dialog(e.target); |
| 151 |
} else if (res !== '') { |
| 152 |
if (a) { |
| 153 |
el[a] = `${u.replace(/[?].*/, '')}?timestamp=${+new Date()}`; |
| 154 |
el.tagName ==='SCRIPT' && update_script(el); |
| 155 |
} else if (s) { |
| 156 |
|
| 157 |
update_chapter(el, res); |
| 158 |
window.mermaid && mermaid?.init(); |
| 159 |
|
| 160 |
d.querySelectorAll('script[src]').forEach(update_script); |
| 161 |
} else { |
| 162 |
|
| 163 |
res === '1' && location.reload(); |
| 164 |
} |
| 165 |
} |
| 166 |
el.dataset.wait = ''; |
| 167 |
}); |
| 168 |
}); |
| 169 |
} |
| 170 |
function update_chapter(el, html) { |
| 171 |
const w = d.createElement('div'); w.innerHTML = html; |
| 172 |
w.querySelector('#TOC')?.remove(); |
| 173 |
w.firstElementChild.className = el.className; |
| 174 |
|
| 175 |
let q = '[class^="ref-number-"]'; |
| 176 |
w.querySelector(q) && d.querySelectorAll(q).forEach(el => { |
| 177 |
}); |
| 178 |
|
| 179 |
w.querySelectorAll('a[class^="cross-ref-"][href^="#"]').forEach(el => { |
| 180 |
const n = d.getElementById(el.getAttribute('href').replace(/^#/, '')) |
| 181 |
?.querySelector('.section-number,[class^="ref-number-"]') |
| 182 |
?.innerText; |
| 183 |
if (n) el.innerText = n; |
| 184 |
}); |
| 185 |
el.outerHTML = w.innerHTML; |
| 186 |
|
| 187 |
q = 'a[id^="fnref-"]'; |
| 188 |
w.querySelector(q) && d.querySelectorAll(q).forEach((el, i) => { |
| 189 |
el.innerText = i + 1; |
| 190 |
}); |
| 191 |
w.remove(); |
| 192 |
} |
| 193 |
|
| 194 |
function update_script(el) { |
| 195 |
const s = d.createElement('script'); |
| 196 |
el.after(s); |
| 197 |
for (const a of el.attributes) { |
| 198 |
s.setAttribute(a.name, a.value); |
| 199 |
} |
| 200 |
el.remove(); |
| 201 |
} |
| 202 |
function check_all() { |
| 203 |
|
| 204 |
chapters.length <= 1 ? check_one() : check_one('div[data-source]', false, 'source'); |
| 205 |
check_one('[src]', 'src'); |
| 206 |
check_one('link[href]', 'href'); |
| 207 |
} |
| 208 |
|
| 209 |
const live = d.querySelector('meta[name="live-previewer"]')?.content === 'litedown::roam'; |
| 210 |
function new_interval() { |
| 211 |
if (live) d.body.dataset.timerId = setInterval(check_all, 2000); |
| 212 |
|
| 213 |
chapters.length <= 1 ? open_line(d) : |
| 214 |
chapters.forEach(el => open_line(el, el.dataset.source)); |
| 215 |
} |
| 216 |
function open_line(container, path) { |
| 217 |
container.querySelectorAll('.auto-numbers span[data-line-number]').forEach(el => { |
| 218 |
const n = +el.dataset.lineNumber; |
| 219 |
if (n) el.onclick = e => { |
| 220 |
const u = `${location.href.replace(/[?#].*/, '')}?line=${n}`; |
| 221 |
new_req(path ? `${u}&path=${path}` : u, 'open'); |
| 222 |
}; |
| 223 |
}); |
| 224 |
} |
| 225 |
window.addEventListener('load', e => { |
| 226 |
d.onkeydown = e => { |
| 227 |
const k = e.key, ctrl = e.metaKey || e.ctrlKey; let a; |
| 228 |
k === 'r' && ctrl && (a = 'refresh'); |
| 229 |
e.altKey && (a = ({'ArrowLeft': 'back', 'ArrowRight': 'forward'})[k]); |
| 230 |
btnAction(e, a); |
| 231 |
k == 'k' && ctrl && btnSave(e); |
| 232 |
}; |
| 233 |
new_interval(); |
| 234 |
}); |
| 235 |
|
| 236 |
window.addEventListener('beforeunload', e => new_req(location.href, 'cleanup')); |
| 237 |
|
| 238 |
|
| 239 |
if (d.body.dataset.timerId) { |
| 240 |
clearInterval(d.body.dataset.timerId); |
| 241 |
new_interval(); |
| 242 |
} |
| 243 |
})(document); |