Add print layout

Print dialog with withhold/include secrets modes, cover page with
contents, one section per page, non-splitting record blocks, TBD
highlighting that survives black and white, tick boxes on steps and
checklist items, and a per-page footer. Print content is built on demand
and cleared after printing. Pure display helpers live in print.js with
tests.

Closes #5

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 08:13:11 -05:00
parent d882debb9b
commit e291ee75b6
6 changed files with 148 additions and 1 deletions

View File

@@ -196,6 +196,68 @@
];
}
// ---------- print ----------
const printRoot = document.getElementById('printdoc');
const box = (status) => h('span', { class: 'cb ' + status, 'aria-hidden': 'true' }, status === 'done' ? '✓' : '');
function printRecord(r, includeSecrets) {
return h('div', { class: 'prec' + (r.status === 'tbd' ? ' tbd' : '') },
h('h3', {}, r.title, r.status === 'tbd' ? h('span', { class: 'ptag' }, 'TBD') : null),
r.fields.length ? h('table', {}, h('tbody', {}, r.fields.map((f) => h('tr', {},
h('th', {}, f.label), h('td', { class: f.secret ? 'psecret' : '' }, displayValue(f, includeSecrets)))))) : null,
r.notes ? h('p', { class: 'pnotes' }, r.notes) : null);
}
function printItem(it, numbered) {
return h('li', { class: 'pitem ' + it.status },
box(it.status), h('strong', {}, it.text), it.status === 'tbd' ? h('span', { class: 'ptag' }, 'TBD') : null,
it.detail ? h('p', { class: 'pnotes' }, it.detail) : null);
}
function buildPrintDoc(includeSecrets) {
const doc = state.doc;
const kept = includeSecrets ? 'This copy INCLUDES passwords, PINs and other secrets. Keep it locked away (fire safe or sealed envelope).'
: 'Secrets (passwords, PINs, security answers) are WITHHELD from this copy. They are stored in the encrypted file.';
printRoot.replaceChildren(...[
h('div', { class: 'pfooter' }, footerText(doc)),
h('section', { class: 'pcover' },
h('h1', {}, doc.meta.title || 'Final Wishes'),
doc.meta.owner ? h('p', { class: 'powner' }, 'Prepared by ' + doc.meta.owner) : null,
h('p', {}, 'Last update: ' + doc.meta.lastUpdated),
h('p', { class: 'pnote' + (includeSecrets ? ' warn' : '') }, kept),
h('p', {}, 'Items marked TBD are not yet filled in. A box beside an instruction is for ticking it off as it is done.'),
h('h2', {}, 'Contents'),
h('ol', { class: 'ptoc' }, doc.sections.map((s) => h('li', {}, s.title)))),
doc.sections.map((s) => h('section', { class: 'psec' },
h('h2', {}, s.title),
s.intro ? h('p', { class: 'pintro' }, s.intro) : null,
s.kind === 'text' ? h('p', { class: 'pbody' }, s.body) : null,
s.kind === 'records' ? s.records.map((r) => printRecord(r, includeSecrets)) : null,
s.kind === 'steps' ? h('ol', { class: 'pitems' }, s.items.map((i) => printItem(i))) : null,
s.kind === 'checklist' ? h('ul', { class: 'pitems' }, s.items.map((i) => printItem(i))) : null)),
].flat());
}
window.addEventListener('afterprint', () => printRoot.replaceChildren()); // don't leave secrets lingering in the DOM
function openPrintDialog() {
const n = countSecrets(state.doc);
const radio = (value, checked, text) => h('label', { class: 'radio' }, h('input', { type: 'radio', name: 'mode', value, checked }), ' ' + text);
const dlg = h('dialog', { class: 'card', 'aria-label': 'Print options' },
h('h3', {}, 'Print'),
radio('mask', true, 'Withhold secrets (safe overview; ' + n + ' secret value' + (n === 1 ? '' : 's') + ' hidden)'),
radio('include', false, 'Include secrets (full copy: store it securely)'),
h('div', { class: 'row' },
btn('Cancel', () => { dlg.close(); dlg.remove(); }),
btn('Print', () => {
const include = dlg.querySelector('input[name=mode]:checked').value === 'include';
dlg.close(); dlg.remove();
buildPrintDoc(include);
setTimeout(() => window.print(), 50);
}, 'primary')));
document.body.append(dlg);
dlg.showModal();
}
// ---------- shell ----------
function renderNav() {
const nav = h('nav', { 'aria-label': 'Sections' },
@@ -223,6 +285,7 @@
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('Print…', openPrintDialog),
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)]