Fields and steps are Normal, Lawyer or Secret. Lawyer is stored as secret: true plus lawyer: true, so older builds treat it as a plain secret. Print offers masked, lawyer and complete views. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
256 lines
14 KiB
JavaScript
256 lines
14 KiB
JavaScript
import { isImageDataUrl, MAX_IMAGE_DATA_CHARS, isMark } from './images.js';
|
|
|
|
// Data model for the document (plaintext JSON before encryption).
|
|
// Pure functions, no DOM; runs in browsers and Node. build.js strips `export`.
|
|
//
|
|
// Document:
|
|
// { schemaVersion: 1,
|
|
// meta: { owner, title, lastUpdated: 'YYYY-MM-DD' },
|
|
// header?: { fields: [HeaderField] }, // running print header; absent = defaults
|
|
// sections: [ Section ] }
|
|
//
|
|
// Section (ordered). `kind` decides which body it has:
|
|
// 'text' -> { body: string } free narrative ("Start here")
|
|
// 'records' -> { records: [Record] } accounts, people, places, items
|
|
// 'steps' -> { items: [Item] } ordered instructions (wind-down)
|
|
// 'checklist' -> { items: [Item] } things still to add/do
|
|
// Every section has { id, title, kind } and may have { intro: string }.
|
|
// A 'records' section may also have sub-tabs:
|
|
// subsections?: [{ id, title }] user-defined tabs, in display order
|
|
// generalTitle?: string name of the catch-all first tab (default 'General')
|
|
// Each record or sequence in its list may carry `subsection: <a subsections id>`.
|
|
// No `subsection` (or one that matches no tab) means the General tab. With no
|
|
// subsections the section shows no tabs at all.
|
|
//
|
|
// Record: { id, title, status: 'ok'|'tbd', updated: 'YYYY-MM-DD',
|
|
// fields: [Field], notes?: string }
|
|
// Field: { label, value, secret?: boolean, lawyer?: boolean, kind?: 'text'|'image' } (kind defaults to text)
|
|
// Three levels: normal (no flags), lawyer (`secret: true, lawyer: true`: masked on screen and
|
|
// in the masked print, shown in the lawyer's view) and secret (`secret: true`: shown only in
|
|
// the complete print). `lawyer` is only valid together with `secret: true`, so an older
|
|
// build that ignores it still treats the item as a secret.
|
|
// An image field's value is a PNG/JPEG data URL, or '' when no image is set yet.
|
|
// Item: { id, text, status: 'open'|'done'|'tbd', detail?: string }
|
|
// Sequence (visual sequence): { id, kind: 'sequence', title, updated: 'YYYY-MM-DD',
|
|
// frames: [Frame] } -- a numbered series of pictures with optional text.
|
|
// Frame: { image: PNG/JPEG data URL or '', text, layout: 'right'|'under'|'none', secret?: boolean, lawyer?: boolean,
|
|
// ifStuck?: string, what to do if the screen looks different or the step fails
|
|
// stop?: boolean, "stop here and call someone" marker; ifStuck says who/why
|
|
// marks?: [Mark] overlay on the picture: { type:'marker', x, y } or
|
|
// { type:'box', x, y, w, h }, all fractions 0..1 of the picture
|
|
// uses?: [{ record: <record id>, field: <field id> }] credentials this step needs,
|
|
// shown beside the step with Show/Hide and Copy. A reference
|
|
// that no longer resolves is shown as missing, never an error.
|
|
// A record Field may carry `id` (unique within its record): assigned the first time a step
|
|
// references it, so the reference survives reordering and renaming the field.
|
|
// A sequence may also carry `captured: 'YYYY-MM-DD'`, set when a step is captured from the screen.
|
|
// A sequence lives in the same ordered list as the records (records sections) or
|
|
// items (steps / checklist sections), so it sits wherever the user puts it, but it
|
|
// is not a record or an item (no number, no tick box). A 'text' section keeps its
|
|
// sequences in an optional `sequences` array, printed after the body.
|
|
//
|
|
// HeaderField: { kind: 'text'|'lastUpdated', text, align?: 'left'|'right' }
|
|
// kind 'text' prints `text`; 'lastUpdated' prints `text` (a label) + meta.lastUpdated.
|
|
// At most one field is 'left' and one 'right'. Printed order: the left field,
|
|
// then unaligned fields in list order, then the right field.
|
|
//
|
|
// Ids are unique across the whole document. `secret` fields are masked
|
|
// in the UI and optionally in print output.
|
|
|
|
export const SCHEMA_VERSION = 1;
|
|
export const GENERAL_TITLE = 'General';
|
|
export const SECTION_KINDS = ['text', 'records', 'steps', 'checklist'];
|
|
export const RECORD_STATUSES = ['ok', 'tbd'];
|
|
export const ITEM_STATUSES = ['open', 'done', 'tbd'];
|
|
export const FIELD_KINDS = ['text', 'image'];
|
|
export const FRAME_LAYOUTS = ['right', 'under', 'none'];
|
|
export const isSequence = (e) => !!e && typeof e === 'object' && e.kind === 'sequence';
|
|
export const HEADER_KINDS = ['text', 'lastUpdated'];
|
|
export const HEADER_ALIGNS = ['left', 'right'];
|
|
|
|
// What the header is when the document doesn't define one.
|
|
export function defaultHeaderFields(doc) {
|
|
return [
|
|
{ kind: 'text', text: (doc.meta && doc.meta.title) || 'Final Wishes', align: 'left' },
|
|
{ kind: 'lastUpdated', text: 'Last update: ', align: 'right' },
|
|
];
|
|
}
|
|
|
|
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
|
|
const isStr = (v) => typeof v === 'string';
|
|
const isObj = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
|
|
export function emptyDocument(owner = '', title = 'In the event of my death or incapacity') {
|
|
return {
|
|
schemaVersion: SCHEMA_VERSION,
|
|
meta: { owner, title, lastUpdated: new Date().toISOString().slice(0, 10) },
|
|
sections: [],
|
|
};
|
|
}
|
|
|
|
// Returns a list of human-readable problems; empty means valid.
|
|
export function validate(doc) {
|
|
const errs = [];
|
|
const seen = new Set();
|
|
const checkId = (id, where) => {
|
|
if (!isStr(id) || !id) errs.push(`${where}: missing id`);
|
|
else if (seen.has(id)) errs.push(`${where}: duplicate id "${id}"`);
|
|
else seen.add(id);
|
|
};
|
|
|
|
if (!isObj(doc)) return ['document must be an object'];
|
|
if (doc.schemaVersion !== SCHEMA_VERSION) errs.push(`schemaVersion must be ${SCHEMA_VERSION}`);
|
|
|
|
if (!isObj(doc.meta)) errs.push('meta must be an object');
|
|
else {
|
|
if (!isStr(doc.meta.owner)) errs.push('meta.owner must be a string');
|
|
if (!isStr(doc.meta.title)) errs.push('meta.title must be a string');
|
|
if (!DATE_RE.test(doc.meta.lastUpdated ?? '')) errs.push('meta.lastUpdated must be YYYY-MM-DD');
|
|
}
|
|
|
|
if (doc.header !== undefined) {
|
|
if (!isObj(doc.header) || !Array.isArray(doc.header.fields)) errs.push('header.fields must be an array');
|
|
else {
|
|
let left = 0;
|
|
let right = 0;
|
|
doc.header.fields.forEach((f, i) => {
|
|
const w = `header.fields[${i}]`;
|
|
if (!isObj(f)) return errs.push(`${w}: must be an object`);
|
|
if (!HEADER_KINDS.includes(f.kind)) errs.push(`${w}: unknown kind "${f.kind}"`);
|
|
if (!isStr(f.text)) errs.push(`${w}: text must be a string`);
|
|
if (f.align !== undefined) {
|
|
if (!HEADER_ALIGNS.includes(f.align)) errs.push(`${w}: bad align "${f.align}"`);
|
|
else if (f.align === 'left') left++;
|
|
else right++;
|
|
}
|
|
});
|
|
if (left > 1) errs.push('header: only one field can be left aligned');
|
|
if (right > 1) errs.push('header: only one field can be right aligned');
|
|
}
|
|
}
|
|
|
|
if (!Array.isArray(doc.sections)) return [...errs, 'sections must be an array'];
|
|
|
|
doc.sections.forEach((s, i) => {
|
|
const where = `sections[${i}]`;
|
|
if (!isObj(s)) return errs.push(`${where}: must be an object`);
|
|
checkId(s.id, where);
|
|
if (!isStr(s.title) || !s.title) errs.push(`${where}: missing title`);
|
|
if (s.intro !== undefined && !isStr(s.intro)) errs.push(`${where}: intro must be a string`);
|
|
if (!SECTION_KINDS.includes(s.kind)) return errs.push(`${where}: unknown kind "${s.kind}"`);
|
|
if (s.kind !== 'records' && (s.subsections !== undefined || s.generalTitle !== undefined)) errs.push(`${where}: only records sections can have sub-tabs`);
|
|
|
|
if (s.kind === 'text') {
|
|
if (!isStr(s.body)) errs.push(`${where}: text section needs a body string`);
|
|
if (s.sequences !== undefined) {
|
|
if (!Array.isArray(s.sequences)) errs.push(`${where}: sequences must be an array`);
|
|
else s.sequences.forEach((q, j) => checkSequence(q, `${where}.sequences[${j}]`));
|
|
}
|
|
} else if (s.kind === 'records') {
|
|
if (!Array.isArray(s.records)) return errs.push(`${where}: records must be an array`);
|
|
const tabIds = new Set();
|
|
if (s.generalTitle !== undefined && (!isStr(s.generalTitle) || !s.generalTitle.trim())) errs.push(`${where}: generalTitle must be a non-empty string`);
|
|
if (s.subsections !== undefined) {
|
|
if (!Array.isArray(s.subsections)) errs.push(`${where}: subsections must be an array`);
|
|
else s.subsections.forEach((t, j) => {
|
|
const tw = `${where}.subsections[${j}]`;
|
|
if (!isObj(t)) return errs.push(`${tw}: must be an object`);
|
|
checkId(t.id, tw);
|
|
if (isStr(t.id)) tabIds.add(t.id);
|
|
if (!isStr(t.title) || !t.title.trim()) errs.push(`${tw}: missing title`);
|
|
});
|
|
}
|
|
s.records.forEach((r, j) => {
|
|
const rw = `${where}.records[${j}]`;
|
|
(isSequence(r) ? checkSequence : checkRecord)(r, rw);
|
|
if (isObj(r) && r.subsection !== undefined && !tabIds.has(r.subsection)) errs.push(`${rw}: subsection "${r.subsection}" is not one of this section's sub-tabs`);
|
|
});
|
|
} else {
|
|
if (!Array.isArray(s.items)) return errs.push(`${where}: items must be an array`);
|
|
s.items.forEach((it, j) => (isSequence(it) ? checkSequence : checkItem)(it, `${where}.items[${j}]`));
|
|
}
|
|
});
|
|
|
|
function checkRecord(r, where) {
|
|
if (!isObj(r)) return errs.push(`${where}: must be an object`);
|
|
checkId(r.id, where);
|
|
if (!isStr(r.title) || !r.title) errs.push(`${where}: missing title`);
|
|
if (!RECORD_STATUSES.includes(r.status)) errs.push(`${where}: bad status "${r.status}"`);
|
|
if (!DATE_RE.test(r.updated ?? '')) errs.push(`${where}: updated must be YYYY-MM-DD`);
|
|
if (r.notes !== undefined && !isStr(r.notes)) errs.push(`${where}: notes must be a string`);
|
|
if (!Array.isArray(r.fields)) return errs.push(`${where}: fields must be an array`);
|
|
const fieldIds = new Set();
|
|
r.fields.forEach((f, k) => {
|
|
const fw = `${where}.fields[${k}]`;
|
|
if (!isObj(f)) return errs.push(`${fw}: must be an object`);
|
|
if (!isStr(f.label) || !f.label) errs.push(`${fw}: missing label`);
|
|
if (!isStr(f.value)) errs.push(`${fw}: value must be a string`);
|
|
if (f.secret !== undefined && typeof f.secret !== 'boolean') errs.push(`${fw}: secret must be a boolean`);
|
|
checkLawyer(f, fw);
|
|
if (f.id !== undefined) {
|
|
if (!isStr(f.id) || !f.id) errs.push(`${fw}: id must be a non-empty string`);
|
|
else if (fieldIds.has(f.id)) errs.push(`${fw}: duplicate field id "${f.id}"`);
|
|
else fieldIds.add(f.id);
|
|
}
|
|
if (f.kind !== undefined && !FIELD_KINDS.includes(f.kind)) errs.push(`${fw}: unknown kind "${f.kind}"`);
|
|
if (f.kind === 'image' && isStr(f.value) && f.value !== '' && !isImageDataUrl(f.value)) {
|
|
errs.push(`${fw}: image must be a PNG or JPEG under ${Math.round(MAX_IMAGE_DATA_CHARS / 1e6)} MB`);
|
|
}
|
|
});
|
|
}
|
|
|
|
function checkLawyer(f, fw) {
|
|
if (f.lawyer === undefined) return;
|
|
if (typeof f.lawyer !== 'boolean') errs.push(`${fw}: lawyer must be a boolean`);
|
|
else if (f.lawyer && f.secret !== true) errs.push(`${fw}: lawyer needs secret: true`);
|
|
}
|
|
|
|
function checkSequence(q, where) {
|
|
if (!isObj(q)) return errs.push(`${where}: must be an object`);
|
|
checkId(q.id, where);
|
|
if (q.kind !== 'sequence') errs.push(`${where}: kind must be "sequence"`);
|
|
if (!isStr(q.title) || !q.title) errs.push(`${where}: missing title`);
|
|
if (!DATE_RE.test(q.updated ?? '')) errs.push(`${where}: updated must be YYYY-MM-DD`);
|
|
if (q.captured !== undefined && !DATE_RE.test(q.captured)) errs.push(`${where}: captured must be YYYY-MM-DD`);
|
|
if (!Array.isArray(q.frames)) return errs.push(`${where}: frames must be an array`);
|
|
q.frames.forEach((f, k) => {
|
|
const fw = `${where}.frames[${k}]`;
|
|
if (!isObj(f)) return errs.push(`${fw}: must be an object`);
|
|
if (!isStr(f.image)) errs.push(`${fw}: image must be a string`);
|
|
else if (f.image !== '' && !isImageDataUrl(f.image)) errs.push(`${fw}: image must be a PNG or JPEG under ${Math.round(MAX_IMAGE_DATA_CHARS / 1e6)} MB`);
|
|
if (!isStr(f.text)) errs.push(`${fw}: text must be a string`);
|
|
if (!FRAME_LAYOUTS.includes(f.layout)) errs.push(`${fw}: bad layout "${f.layout}"`);
|
|
if (f.secret !== undefined && typeof f.secret !== 'boolean') errs.push(`${fw}: secret must be a boolean`);
|
|
checkLawyer(f, fw);
|
|
if (f.ifStuck !== undefined && !isStr(f.ifStuck)) errs.push(`${fw}: ifStuck must be a string`);
|
|
if (f.stop !== undefined && typeof f.stop !== 'boolean') errs.push(`${fw}: stop must be a boolean`);
|
|
if (f.marks !== undefined) {
|
|
if (!Array.isArray(f.marks)) errs.push(`${fw}: marks must be an array`);
|
|
else f.marks.forEach((m, n) => { if (!isMark(m)) errs.push(`${fw}.marks[${n}]: not a valid marker or box`); });
|
|
}
|
|
if (f.uses !== undefined) {
|
|
if (!Array.isArray(f.uses)) errs.push(`${fw}: uses must be an array`);
|
|
else f.uses.forEach((u, m) => {
|
|
if (!isObj(u) || !isStr(u.record) || !u.record || !isStr(u.field) || !u.field) errs.push(`${fw}.uses[${m}]: needs record and field ids`);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
function checkItem(it, where) {
|
|
if (!isObj(it)) return errs.push(`${where}: must be an object`);
|
|
checkId(it.id, where);
|
|
if (!isStr(it.text) || !it.text) errs.push(`${where}: missing text`);
|
|
if (!ITEM_STATUSES.includes(it.status)) errs.push(`${where}: bad status "${it.status}"`);
|
|
if (it.detail !== undefined && !isStr(it.detail)) errs.push(`${where}: detail must be a string`);
|
|
}
|
|
|
|
return errs;
|
|
}
|
|
|
|
export function assertValid(doc) {
|
|
const errs = validate(doc);
|
|
if (errs.length) throw new Error('Invalid document:\n- ' + errs.join('\n- '));
|
|
return doc;
|
|
}
|