// 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 tabs: {}, // section id -> id of the sub-tab being shown ('' = General); not saved collapsed: new Set(), // ids of records folded to just their title (view mode only) walk: null, // { id, k }: the visual sequence shown as a one-step-at-a-time walkthrough (not saved) 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: '', }; // ?reset=1 (or #reset=1): after unlock, offer to erase every secret value. // Read live: typing #reset=1 into an already-open page only fires hashchange, it does not reload. const resetRequested = () => /[?#&]reset=1(?![0-9])/.test(location.search + location.hash); const resetBanner = () => { const n = countSecrets(state.doc); const done = () => { state.resetOffered = true; render(); }; return h('div', { class: 'card', role: 'alert' }, h('h2', {}, 'Reset: erase secret values'), h('p', {}, n ? 'This will erase ' + n + ' secret value' + (n === 1 ? '' : 's') + ' (passwords, PINs, secret images and steps) from this document in memory. Labels and everything else stay.' : 'This document has no secret values to erase.'), h('p', {}, 'Nothing is written until you Save. Make a Backup copy first if you may need the secrets again, because saving over this file removes them for good.'), h('div', { class: 'row' }, n ? btn('Erase ' + n + ' secret value' + (n === 1 ? '' : 's'), () => { const erased = clearSecrets(state.doc); state.revealed = new WeakSet(); state.resetOffered = true; markDirty(); state.status = 'Erased ' + erased + ' secret value' + (erased === 1 ? '' : 's') + '. Save to keep this.'; render(); }, 'primary') : null, btn(n ? 'Keep them' : 'Dismiss', done))); }; 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(/