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:
63
src/app.js
63
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)]
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
<style>/*__CSS__*/</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="printdoc" aria-hidden="true"></div>
|
||||
<main id="app"><noscript>This document needs JavaScript enabled.</noscript></main>
|
||||
<!-- Encrypted data blob is injected by build.js (issue #6). -->
|
||||
<script id="payload" type="application/octet-stream">/*__PAYLOAD__*/</script>
|
||||
|
||||
24
src/print.js
Normal file
24
src/print.js
Normal file
@@ -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;
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user