// UI: unlock / load, section navigation, read-only view, edit mode. // All DOM is built with h() + textContent, never innerHTML, so document // content can't inject markup. (function () { const root = document.getElementById('app'); const state = { doc: null, passphrase: null, // kept in memory for saving (issue #6) current: null, // current section id editing: false, dirty: false, baseline: null, // deep copy of the document at open/save; Cancel reverts to it pwChanged: false, // passphrase changed since the last save collapsed: new Set(), // ids of records folded to just their title (view mode only) revealed: new WeakSet(), // secret field objects currently shown blob: null, // encrypted payload taken from a file the user chose to open handle: null, // File System Access handle once the user picked a save location status: '', error: '', }; function h(tag, attrs, ...kids) { const el = document.createElement(tag); for (const [k, v] of Object.entries(attrs || {})) { if (v === false || v == null) continue; if (k.startsWith('on')) el.addEventListener(k.slice(2), v); else if (k === 'class') el.className = v; else if (k in el && k !== 'list') el[k] = v; else el.setAttribute(k, v === true ? '' : v); } for (const kid of kids.flat(Infinity)) { if (kid == null || kid === false) continue; el.append(kid.nodeType ? kid : document.createTextNode(String(kid))); } return el; } const btn = (label, onclick, cls = '', extra = {}) => h('button', Object.assign({ type: 'button', class: 'btn ' + cls, onclick }, extra), label); function markDirty() { state.dirty = true; const d = document.getElementById('dirty'); if (d) d.hidden = false; state.status = ''; const st = document.getElementById('status'); if (st) st.textContent = ''; } function changed(record) { touch(state.doc, record); markDirty(); } function snapshot() { state.baseline = snapshotDoc(state.doc); state.pwChanged = false; } window.addEventListener('beforeunload', (e) => { if (state.dirty) { e.preventDefault(); e.returnValue = ''; } }); // ---------- start / unlock ---------- function payloadText() { const p = document.getElementById('payload'); return p ? p.textContent.trim() : ''; } function renderStart() { const blob = state.blob || payloadText(); const msg = h('p', { class: 'error', role: 'alert' }, state.error); if (blob) { const pw = h('input', { type: 'password', autocomplete: 'current-password', autofocus: true, 'aria-label': 'Passphrase' }); const form = h('form', { class: 'card start', onsubmit: async (e) => { e.preventDefault(); state.error = ''; try { const doc = JSON.parse(await decrypt(blob, pw.value)); assertValid(doc); state.doc = doc; snapshot(); state.passphrase = pw.value; state.blob = null; state.current = doc.sections[0] && doc.sections[0].id; } catch (err) { state.error = err instanceof DecryptError ? err.message : 'Could not open the file: ' + err.message; } render(); } }, h('h1', {}, 'Final Wishes'), h('p', {}, 'Enter the passphrase to open this document.'), pw, msg, btn('Open', null, 'primary', { type: 'submit' }), state.blob ? btn('Choose a different file', () => { state.blob = null; state.error = ''; render(); }, 'small') : null); return form; } // No encrypted data embedded yet (development / first use). const file = h('input', { type: 'file', accept: '.json,application/json', 'aria-label': 'Open a JSON file', onchange: async () => { try { const doc = JSON.parse(await file.files[0].text()); assertValid(doc); state.doc = doc; snapshot(); state.current = doc.sections[0] && doc.sections[0].id; state.error = ''; } catch (err) { state.error = 'Could not open that file: ' + err.message; } render(); } }); const existing = h('input', { type: 'file', accept: '.html,text/html', 'aria-label': 'Open an existing Final Wishes file', onchange: async () => { const m = (await existing.files[0].text()).match(/