Add configurable running print header
The line printed on every page is now a header stored in the document as header.fields: any number of Text / Last updated fields, each optionally aligned left or right (at most one of each). Printed order is left, unaligned, right. Edited from a Print header panel with live preview; documents without a header use the previous title/last-update default. The header is a repeating table header so it sits in flow and cannot overlap body text, replacing the fixed-position footer. Also add "Open your existing Final Wishes file" to the start screen so data in an older encrypted file can be carried into a newer build. Closes #9 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
81
src/app.js
81
src/app.js
@@ -10,6 +10,7 @@
|
||||
editing: false,
|
||||
dirty: false,
|
||||
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: '',
|
||||
@@ -56,7 +57,7 @@
|
||||
}
|
||||
|
||||
function renderStart() {
|
||||
const blob = payloadText();
|
||||
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' });
|
||||
@@ -68,6 +69,7 @@
|
||||
assertValid(doc);
|
||||
state.doc = doc;
|
||||
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;
|
||||
@@ -76,7 +78,8 @@
|
||||
} },
|
||||
h('h1', {}, 'Final Wishes'),
|
||||
h('p', {}, 'Enter the passphrase to open this document.'),
|
||||
pw, msg, btn('Open', null, 'primary', { type: 'submit' }));
|
||||
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).
|
||||
@@ -90,9 +93,16 @@
|
||||
} 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(/<script id="payload"[^>]*>([^<]*)<\/script>/);
|
||||
if (m && m[1].trim()) { state.blob = m[1].trim(); state.error = ''; }
|
||||
else state.error = 'That file has no encrypted Final Wishes data in it.';
|
||||
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 your existing Final Wishes file (your data is carried into this newer version; the old file is not changed until you save over it):'), existing,
|
||||
h('p', {}, 'Import a plain JSON file (you will choose a passphrase when you save):'), file, msg,
|
||||
h('p', {}, 'or'),
|
||||
btn('Start a new document', () => {
|
||||
@@ -159,12 +169,13 @@
|
||||
return el;
|
||||
}
|
||||
// Up / down / delete controls for element i of arr.
|
||||
function rowTools(arr, i, what, record) {
|
||||
const act = (fn) => () => { fn(); changed(record); render(); };
|
||||
function rowTools(arr, i, what, record, quiet = false) {
|
||||
const note = () => (quiet ? markDirty() : changed(record));
|
||||
const act = (fn) => () => { fn(); note(); 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 }));
|
||||
btn('✕', () => { if (confirm('Delete this ' + what + '?')) { removeAt(arr, i); note(); render(); } }, 'icon danger', { 'aria-label': 'Delete ' + what }));
|
||||
}
|
||||
|
||||
function editField(r, f, i) {
|
||||
@@ -201,6 +212,47 @@
|
||||
];
|
||||
}
|
||||
|
||||
// ---------- print header ----------
|
||||
const HEADER_ID = '__header';
|
||||
|
||||
// The running header line, laid out left | unaligned... | right.
|
||||
function headerEl(doc) {
|
||||
const L = headerLayout(doc);
|
||||
return h('div', { class: 'phead' },
|
||||
L.left ? h('span', { class: 'ph-left' }, L.left) : null,
|
||||
h('span', { class: 'ph-mid' }, L.middle.map((t) => h('span', {}, t))),
|
||||
L.right ? h('span', { class: 'ph-right' }, L.right) : null);
|
||||
}
|
||||
|
||||
function headerPanel() {
|
||||
const doc = state.doc;
|
||||
const preview = h('div', { class: 'card hpreview', 'aria-label': 'Header preview' });
|
||||
const refresh = () => preview.replaceChildren(headerEl(doc));
|
||||
refresh();
|
||||
const intro = h('p', { class: 'intro' }, 'This line is printed at the top of every page. Left-aligned text comes first, unaligned fields sit in the middle, and the right-aligned field comes last. Only one field can be left aligned and one right aligned.');
|
||||
if (!state.editing) return [intro, preview, h('p', { class: 'muted' }, 'Choose Edit to change it.')];
|
||||
|
||||
const fields = ensureHeader(doc).fields;
|
||||
return [intro, preview,
|
||||
fields.map((f, i) => {
|
||||
const taken = takenAligns(fields, f);
|
||||
const kind = h('select', { 'aria-label': 'Field type', onchange: () => { f.kind = kind.value; markDirty(); render(); } },
|
||||
[['text', 'Text'], ['lastUpdated', 'Last updated']].map(([v, l]) => h('option', { value: v, selected: f.kind === v }, l)));
|
||||
const text = h('input', { value: f.text, 'aria-label': f.kind === 'lastUpdated' ? 'Label before the date' : 'Header text',
|
||||
placeholder: f.kind === 'lastUpdated' ? 'Label before the date (optional)' : 'Text',
|
||||
oninput: () => { f.text = text.value; markDirty(); refresh(); } });
|
||||
const align = h('select', { 'aria-label': 'Alignment', onchange: () => {
|
||||
if (!setAlign(fields, f, align.value)) return;
|
||||
markDirty(); render();
|
||||
} }, [['', 'Not aligned'], ['left', 'Left'], ['right', 'Right']].map(([v, l]) =>
|
||||
h('option', { value: v, selected: (f.align || '') === v, disabled: !!v && taken.has(v) }, l + (v && taken.has(v) ? ' (in use)' : ''))));
|
||||
return h('div', { class: 'card editing' }, h('div', { class: 'row' }, kind, text, align, rowTools(fields, i, 'header field', null, true)));
|
||||
}),
|
||||
h('div', { class: 'row' },
|
||||
btn('+ Add field', () => { addHeaderField(doc); markDirty(); render(); }, 'primary'),
|
||||
btn('Reset to default', () => { delete doc.header; markDirty(); render(); }, 'small'))];
|
||||
}
|
||||
|
||||
// ---------- print ----------
|
||||
const printRoot = document.getElementById('printdoc');
|
||||
const box = (status) => h('span', { class: 'cb ' + status, 'aria-hidden': 'true' }, status === 'done' ? '✓' : '');
|
||||
@@ -223,8 +275,7 @@
|
||||
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)),
|
||||
const content = [
|
||||
h('section', { class: 'pcover' },
|
||||
h('h1', {}, doc.meta.title || 'Final Wishes'),
|
||||
doc.meta.owner ? h('p', { class: 'powner' }, 'Prepared by ' + doc.meta.owner) : null,
|
||||
@@ -240,7 +291,11 @@
|
||||
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());
|
||||
].flat();
|
||||
// The header sits in <thead> so the browser repeats it, in flow, at the top of every page.
|
||||
printRoot.replaceChildren(h('table', { class: 'ptable' },
|
||||
h('thead', {}, h('tr', {}, h('td', {}, headerEl(doc)))),
|
||||
h('tbody', {}, h('tr', {}, h('td', {}, content)))));
|
||||
}
|
||||
window.addEventListener('afterprint', () => printRoot.replaceChildren()); // don't leave secrets lingering in the DOM
|
||||
|
||||
@@ -355,7 +410,9 @@
|
||||
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)))));
|
||||
h('a', { href: '#' + s.id, class: s.id === state.current ? 'current' : '', onclick: (e) => { e.preventDefault(); state.current = s.id; render(); } }, s.title)))),
|
||||
h('ul', { class: 'navextra' }, h('li', {},
|
||||
h('a', { href: '#header', class: state.current === HEADER_ID ? 'current' : '', onclick: (e) => { e.preventDefault(); state.current = HEADER_ID; render(); } }, 'Print header'))));
|
||||
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' });
|
||||
@@ -371,7 +428,8 @@
|
||||
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];
|
||||
const onHeader = state.current === HEADER_ID;
|
||||
const sec = onHeader ? null : 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' },
|
||||
@@ -385,7 +443,8 @@
|
||||
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)]
|
||||
h('main', {}, onHeader ? [h('h2', {}, 'Print header'), headerPanel()]
|
||||
: sec ? [h('h2', {}, sec.title), state.editing ? editSection(sec) : viewSection(sec)]
|
||||
: h('p', { class: 'muted' }, 'No sections yet. Choose Edit to add one.'))));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user