Add viewer/editor UI

Passphrase unlock (or open a plain JSON file while no payload is
embedded), sectioned navigation, read-only cards with masked secret
fields and a reveal toggle, and an edit mode with add/remove/reorder for
sections, records, fields and items. Pure edit operations live in
ops.js with tests. DOM is built with textContent only.

Closes #4

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 08:10:21 -05:00
parent 2c713f3a9b
commit d882debb9b
5 changed files with 400 additions and 7 deletions

View File

@@ -1,5 +1,233 @@
// App entry point. Crypto (#2), data model (#3), UI (#4) and print (#5) land here.
// 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 app = document.getElementById('app');
app.textContent = 'Final Wishes: scaffold loaded.';
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,
revealed: new WeakSet(), // secret field objects currently shown
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;
}
function changed(record) {
touch(state.doc, record);
markDirty();
}
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 = 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;
state.passphrase = pw.value;
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' }));
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;
state.current = doc.sections[0] && doc.sections[0].id;
state.error = '';
} catch (err) { state.error = 'Could not open that file: ' + err.message; }
render();
} });
return h('div', { class: 'card start' },
h('h1', {}, 'Final Wishes'),
h('p', {}, 'No encrypted data is built into this file yet.'),
h('p', {}, 'Open a JSON file:'), file, msg,
h('p', {}, 'or'),
btn('Start a new document', () => {
state.doc = emptyDocument('');
state.editing = true;
render();
}, 'primary'));
}
// ---------- read-only view ----------
function statusBadge(status) {
return status === 'tbd' ? h('span', { class: 'badge tbd' }, 'TBD') : status === 'done' ? h('span', { class: 'badge done' }, 'Done') : null;
}
function viewField(f) {
let value;
if (f.secret) {
const shown = state.revealed.has(f);
const span = h('span', { class: 'secret' }, !f.value ? '(not set)' : shown ? f.value : '••••••••');
value = h('span', {}, span, f.value && btn(shown ? 'Hide' : 'Show', () => {
state.revealed[shown ? 'delete' : 'add'](f);
render();
}, 'link', { 'aria-label': (shown ? 'Hide ' : 'Show ') + f.label }));
} else {
value = f.value || h('span', { class: 'muted' }, '(blank)');
}
return [h('dt', {}, f.label), h('dd', {}, value)];
}
function viewRecord(r) {
return h('article', { class: 'card' },
h('h3', {}, r.title, ' ', statusBadge(r.status)),
r.fields.length ? h('dl', {}, r.fields.map(viewField)) : null,
r.notes ? h('p', { class: 'notes' }, r.notes) : null,
h('p', { class: 'muted small' }, 'Updated ' + r.updated));
}
function viewItem(it, ordered) {
return h('li', { class: 'item ' + it.status },
h('div', {}, h('strong', {}, it.text), ' ', statusBadge(it.status)),
it.detail ? h('p', { class: 'notes' }, it.detail) : null);
}
function viewSection(s) {
return [
s.intro ? h('p', { class: 'intro' }, s.intro) : null,
s.kind === 'text' ? h('p', { class: 'body' }, s.body) : null,
s.kind === 'records' ? s.records.map(viewRecord) : null,
s.kind === 'steps' ? h('ol', { class: 'items' }, s.items.map((i) => viewItem(i))) : null,
s.kind === 'checklist' ? h('ul', { class: 'items' }, s.items.map((i) => viewItem(i))) : null,
];
}
// ---------- edit mode ----------
function input(obj, key, opts = {}, record) {
const tag = opts.multiline ? 'textarea' : 'input';
const el = h(tag, { value: obj[key] ?? '', 'aria-label': opts.label || key, rows: opts.multiline ? 3 : undefined,
oninput: () => { obj[key] = el.value; changed(record); } });
return el;
}
function select(obj, key, values, label, record) {
const el = h('select', { 'aria-label': label, onchange: () => { obj[key] = el.value; changed(record); } },
values.map((v) => h('option', { value: v, selected: obj[key] === v }, v)));
return el;
}
// Up / down / delete controls for element i of arr.
function rowTools(arr, i, what, record) {
const act = (fn) => () => { fn(); changed(record); render(); };
return h('span', { class: 'tools' },
btn('↑', act(() => move(arr, i, -1)), 'icon', { 'aria-label': 'Move ' + what + ' up', disabled: i === 0 }),
btn('↓', act(() => move(arr, i, 1)), 'icon', { 'aria-label': 'Move ' + what + ' down', disabled: i === arr.length - 1 }),
btn('✕', () => { if (confirm('Delete this ' + what + '?')) { removeAt(arr, i); changed(record); render(); } }, 'icon danger', { 'aria-label': 'Delete ' + what }));
}
function editField(r, f, i) {
const secret = h('input', { type: 'checkbox', checked: !!f.secret, 'aria-label': 'Secret',
onchange: () => { f.secret = secret.checked || undefined; if (!f.secret) delete f.secret; changed(r); } });
return h('div', { class: 'row field' },
input(f, 'label', { label: 'Field label' }, r), input(f, 'value', { label: 'Field value' }, r),
h('label', { class: 'check' }, secret, ' secret'), rowTools(r.fields, i, 'field', r));
}
function editRecord(s, r, i) {
return h('article', { class: 'card editing' },
h('div', { class: 'row' }, input(r, 'title', { label: 'Record title' }, r), select(r, 'status', ['ok', 'tbd'], 'Record status', r), rowTools(s.records, i, 'record')),
r.fields.map((f, k) => editField(r, f, k)),
btn('+ Add field', () => { addField(r); changed(r); render(); }, 'small'),
h('label', {}, 'Notes', input(r, 'notes', { multiline: true, label: 'Notes' }, r)));
}
function editItem(s, it, i) {
return h('li', { class: 'card editing' },
h('div', { class: 'row' }, input(it, 'text', { label: 'Item text' }), select(it, 'status', ['open', 'done', 'tbd'], 'Item status'), rowTools(s.items, i, 'item')),
h('label', {}, 'Detail', input(it, 'detail', { multiline: true, label: 'Detail' })));
}
function editSection(s) {
const secIdx = state.doc.sections.indexOf(s);
return [
h('div', { class: 'card editing' },
h('div', { class: 'row' }, input(s, 'title', { label: 'Section title' }), rowTools(state.doc.sections, secIdx, 'section')),
h('label', {}, 'Intro', input(s, 'intro', { multiline: true, label: 'Section intro' }))),
s.kind === 'text' ? h('label', {}, 'Text', input(s, 'body', { multiline: true, label: 'Body' })) : null,
s.kind === 'records' ? [s.records.map((r, i) => editRecord(s, r, i)), btn('+ Add record', () => { addRecord(state.doc, s); markDirty(); render(); }, 'primary')] : null,
s.kind === 'steps' || s.kind === 'checklist' ? [h('ol', { class: 'items' }, s.items.map((it, i) => editItem(s, it, i))), btn('+ Add item', () => { addItem(state.doc, s); markDirty(); render(); }, 'primary')] : null,
];
}
// ---------- shell ----------
function renderNav() {
const nav = h('nav', { 'aria-label': 'Sections' },
h('ul', {}, state.doc.sections.map((s) => h('li', {},
h('a', { href: '#' + s.id, class: s.id === state.current ? 'current' : '', onclick: (e) => { e.preventDefault(); state.current = s.id; render(); } }, s.title)))));
if (state.editing) {
const kind = h('select', { 'aria-label': 'New section type' }, SECTION_KINDS.map((k) => h('option', { value: k }, k)));
const title = h('input', { placeholder: 'New section title', 'aria-label': 'New section title' });
nav.append(h('div', { class: 'addsec' }, title, kind, btn('+ Add section', () => {
if (!title.value.trim()) return title.focus();
state.current = addSection(state.doc, kind.value, title.value.trim()).id;
markDirty(); render();
}, 'small')));
}
return nav;
}
function render() {
root.replaceChildren();
if (!state.doc) return root.append(renderStart());
const sec = state.doc.sections.find((s) => s.id === state.current) || state.doc.sections[0];
if (sec) state.current = sec.id;
root.append(
h('header', { class: 'top' },
h('h1', {}, state.doc.meta.title || 'Final Wishes'),
h('span', { class: 'muted' }, 'Last update: ' + state.doc.meta.lastUpdated),
h('span', { id: 'dirty', class: 'badge tbd', hidden: !state.dirty }, 'Unsaved changes'),
btn(state.editing ? 'Done editing' : 'Edit', () => { state.editing = !state.editing; render(); }, state.editing ? 'primary' : '')),
h('div', { class: 'layout' }, renderNav(),
h('main', {}, sec ? [h('h2', {}, sec.title), state.editing ? editSection(sec) : viewSection(sec)]
: h('p', { class: 'muted' }, 'No sections yet. Choose Edit to add one.'))));
}
render();
})();