diff --git a/build.js b/build.js index da6dd36..52528d7 100644 --- a/build.js +++ b/build.js @@ -10,7 +10,7 @@ const read = (p) => readFileSync(join(root, 'src', p), 'utf8'); const out = read('index.html') .replace('/*__CSS__*/', () => read('style.css')) // Plain-script bundle: drop ES `export` so modules concatenate into one scope. - .replace('/*__JS__*/', () => ['crypto.js', 'model.js', 'ops.js'].map((f) => read(f).replace(/^export /gm, '')).concat(read('app.js')).join('\n')) + .replace('/*__JS__*/', () => ['crypto.js', 'model.js', 'ops.js', 'print.js'].map((f) => read(f).replace(/^export /gm, '')).concat(read('app.js')).join('\n')) .replace('/*__PAYLOAD__*/', () => ''); mkdirSync(join(root, 'dist'), { recursive: true }); diff --git a/src/app.js b/src/app.js index 7fef43b..3d1ff86 100644 --- a/src/app.js +++ b/src/app.js @@ -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)] diff --git a/src/index.html b/src/index.html index 06fd489..d4ec584 100644 --- a/src/index.html +++ b/src/index.html @@ -8,6 +8,7 @@ +
diff --git a/src/print.js b/src/print.js new file mode 100644 index 0000000..08f6176 --- /dev/null +++ b/src/print.js @@ -0,0 +1,24 @@ +// Pure helpers for the printed document. No DOM; tested in Node. +// build.js strips `export` when bundling. + +// What to print for a field. Secrets are either shown or withheld; an empty +// secret is always "(not set)" so a blank is never mistaken for a hidden value. +export function displayValue(field, includeSecrets) { + if (field.secret) { + if (!field.value) return '(not set)'; + return includeSecrets ? field.value : '[withheld]'; + } + return field.value || '(blank)'; +} + +export function footerText(doc) { + return `${doc.meta.title || 'Final Wishes'} — Last update: ${doc.meta.lastUpdated}`; +} + +export function countSecrets(doc) { + let n = 0; + for (const s of doc.sections) { + for (const r of s.records || []) for (const f of r.fields) if (f.secret && f.value) n++; + } + return n; +} diff --git a/src/style.css b/src/style.css index 267f37c..a609957 100644 --- a/src/style.css +++ b/src/style.css @@ -39,3 +39,37 @@ textarea { width: 100%; display: block; } label { display: block; margin-top: .5 .btn.icon { padding: .2rem .5rem; } .btn.danger { color: var(--danger); } .btn.small { font-size: .85rem; } .btn:disabled { opacity: .35; cursor: default; } .tools { display: inline-flex; gap: .2rem; } .addsec { display: grid; gap: .4rem; margin-top: 1rem; } :focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; } + +/* ---------- print ---------- */ +@media screen { #printdoc { display: none; } .radio { display: block; margin: .5rem 0; } dialog.card { max-width: 30rem; } dialog::backdrop { background: rgba(0,0,0,.45); } } +@page { size: letter; margin: 0.8in 0.8in 1in; } +@media print { + :root { color-scheme: light; } + html, body { background: #fff !important; color: #000 !important; font-size: 11pt; } + #app, dialog { display: none !important; } + #printdoc { display: block; font-family: Georgia, "Times New Roman", serif; line-height: 1.35; } + .pfooter { position: fixed; bottom: -0.6in; left: 0; right: 0; text-align: center; font: 9pt system-ui, sans-serif; color: #444; border-top: 0.5pt solid #999; padding-top: 3pt; } + .pcover { break-after: page; } + .pcover h1 { font-size: 26pt; margin: 1.2in 0 .2in; } + .powner { font-size: 14pt; } + .pnote { border: 1.5pt solid #000; padding: 8pt 10pt; font: 600 10.5pt system-ui, sans-serif; } + .pnote.warn { border-width: 3pt; } + .ptoc { columns: 2; padding-left: 1.2em; } + .psec { break-before: page; } + .psec:first-of-type { break-before: auto; } + h2 { font-size: 17pt; border-bottom: 1pt solid #000; padding-bottom: 3pt; margin: 0 0 10pt; break-after: avoid; } + h3 { font-size: 12.5pt; margin: 0 0 4pt; break-after: avoid; } + .prec { break-inside: avoid; margin: 0 0 12pt; padding: 6pt 8pt; border: 0.75pt solid #888; border-left-width: 3pt; } + .prec.tbd { border-left-color: #000; background: #eee; -webkit-print-color-adjust: exact; print-color-adjust: exact; } + .prec table { border-collapse: collapse; width: 100%; font-size: 10.5pt; } + .prec th { text-align: left; font-weight: 600; width: 30%; vertical-align: top; padding: 1.5pt 8pt 1.5pt 0; font-family: system-ui, sans-serif; font-size: 9.5pt; color: #333; } + .prec td { padding: 1.5pt 0; overflow-wrap: anywhere; } + .psecret { font-family: ui-monospace, Consolas, monospace; } + .ptag { font: 700 8pt system-ui, sans-serif; border: 1pt solid #000; padding: 0 4pt; margin-left: 8pt; border-radius: 2pt; vertical-align: middle; } + .pnotes, .pbody, .pintro { white-space: pre-wrap; margin: 4pt 0 0; } + .pintro { font-style: italic; margin-bottom: 10pt; } + .pitems { padding-left: 0; list-style-position: inside; } + .pitem { break-inside: avoid; margin: 0 0 9pt; } + .pitem .pnotes { margin-left: 2.2em; } + .cb { display: inline-block; width: 11pt; height: 11pt; border: 1.2pt solid #000; margin: 0 7pt 0 4pt; vertical-align: -1.5pt; text-align: center; line-height: 10pt; font: 700 9pt system-ui, sans-serif; } +} diff --git a/test/print.test.js b/test/print.test.js new file mode 100644 index 0000000..1ca9199 --- /dev/null +++ b/test/print.test.js @@ -0,0 +1,25 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { displayValue, footerText, countSecrets } from '../src/print.js'; + +test('displayValue shows or withholds secrets, never confuses empty with hidden', () => { + assert.equal(displayValue({ label: 'a', value: 'x' }, false), 'x'); + assert.equal(displayValue({ label: 'a', value: '' }, false), '(blank)'); + assert.equal(displayValue({ label: 'p', value: 'pw', secret: true }, true), 'pw'); + assert.equal(displayValue({ label: 'p', value: 'pw', secret: true }, false), '[withheld]'); + assert.equal(displayValue({ label: 'p', value: '', secret: true }, true), '(not set)'); + assert.equal(displayValue({ label: 'p', value: '', secret: true }, false), '(not set)'); +}); + +test('masked output never contains the secret text', () => { + const out = displayValue({ label: 'p', value: 'hunter2', secret: true }, false); + assert.ok(!out.includes('hunter2')); +}); + +test('footerText and countSecrets', () => { + const doc = { meta: { title: 'T', lastUpdated: '2022-04-14' }, sections: [ + { records: [{ fields: [{ label: 'a', value: 'x', secret: true }, { label: 'b', value: '', secret: true }, { label: 'c', value: 'y' }] }] }, + { items: [] } ] }; + assert.equal(footerText(doc), 'T — Last update: 2022-04-14'); + assert.equal(countSecrets(doc), 1); +});