diff --git a/README.md b/README.md index 73c5c80..a732e96 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ The passphrase is prompted for (hidden) or read from `FW_PASSPHRASE`. optional print `header`); `src/ops.js` edit helpers - `src/images.js` image-field helpers and the import pipeline (PNG/JPEG only, downscaled and re-encoded in the browser) +- Visual sequences (numbered pictures with optional text, in any section) are + described in `docs/MAINTENANCE.md`. - `src/print.js` print helpers (secret masking, header layout) - `src/app.js` + `src/style.css` the UI, including the Print header panel - `src/template.js` the HTML skeleton shared by `build.js` and in-page Save diff --git a/docs/MAINTENANCE.md b/docs/MAINTENANCE.md index c04a1a6..307ef39 100644 --- a/docs/MAINTENANCE.md +++ b/docs/MAINTENANCE.md @@ -82,6 +82,33 @@ What to expect: fit the page (at most about 4 inches tall). - Dropping a file anywhere on the page never navigates away from unsaved work. +## Visual sequences +A **visual sequence** is a numbered series of pictures, each with optional +text, for things that are easier to show than to describe: how to log in to a +bank site, how to cancel a service that hides the button. It is its own block, +not part of a record or item, so it sits exactly where you put it. + +- **Where:** in any section. In *records*, *steps* and *checklist* sections use + **+ Add visual sequence** next to the add-record / add-item button. It sits + in the same ordered list, so the up/down arrows place it between records or + items. In a *text* section sequences print after the text. A section can + hold as many as you like. A sequence is not a step: it has no number and no + tick box, and numbering of the items around it carries on unbroken. +- **Adding pictures:** **+ Add images…** (choose several files at once), drop + files on the sequence, or paste a screenshot while the cursor is inside it. + Each becomes a step. Dropping or pasting onto an existing step's box replaces + that step's picture. **+ Add step** adds an empty one. Same image rules as + Images above (PNG/JPEG, shrunk, about 3 MB limit). +- **Text:** each step has a text position, chosen per step: *right of image*, + *under image*, or *no text*. Text keeps its line breaks. Switching to *no + text* hides it but keeps what you typed. Empty text never takes room. +- **Secret** (per step) hides both the picture and the text behind Show/Hide + and withholds it from a masked print. Steps are numbered 1, 2, 3… in view + and in print, and a step is kept on one page. +- Files saved with sequences cannot be opened by older builds; opening an + older file in a newer build (see below) still works. **Cancel** and the + other editing rules work the same as for records. + ## Upgrading the app (new version, same data) Your data is stored encrypted inside each `final-wishes.html`; the app code sits beside it in the same file. A file therefore keeps running the version it diff --git a/src/app.js b/src/app.js index db620b4..0981f55 100644 --- a/src/app.js +++ b/src/app.js @@ -129,19 +129,24 @@ } const hasFiles = (e) => !!(e.dataTransfer && [...(e.dataTransfer.types || [])].includes('Files')); const isImageFile = (f) => /^image\//.test(f.type) || /\.(png|jpe?g)$/i.test(f.name || ''); - const allRecords = () => state.doc.sections.flatMap((s) => s.records || []); + const allRecords = () => state.doc.sections.flatMap((s) => (s.records || []).filter((r) => !isSequence(r))); + const addFrameFrom = (seq) => (name, url) => addFrame(seq, url); // Add each file as a new image field on `rec`, or put the first into // `opts.replace` (an existing image field). Per-file problems are reported // together; the files that worked are still kept. + // opts.replace: an existing object whose opts.key (default 'value') takes the first file. + // opts.add(name, url): how a new image is added (default: a new image field on `rec`). async function importFiles(rec, files, opts = {}) { + const key = opts.key || 'value'; + const add = opts.add || ((name, url) => addImageField(rec, opts.pasted ? 'Pasted image' : labelFromFilename(name), url)); let added = 0; const errors = []; for (const file of files) { try { const url = await processImageFile(file); - if (opts.replace && !added) opts.replace.value = url; - else addImageField(rec, opts.pasted ? 'Pasted image' : labelFromFilename(file.name), url); + if (opts.replace && !added) opts.replace[key] = url; + else add(file.name, url); added++; } catch (err) { errors.push((file.name ? file.name + ': ' : '') + err.message); @@ -163,19 +168,27 @@ if (!files.length) return; // ordinary text paste is left alone const t = e.target; if (t && /^(INPUT|TEXTAREA)$/.test(t.tagName) && e.clipboardData.getData('text/plain')) return; - if (!state.editing) return setStatus('Choose Edit, then paste into a record.'); + if (!state.editing) return setStatus('Choose Edit, then paste into a record or visual sequence.'); + const seqEl = t && t.closest ? t.closest('[data-seq]') : null; + const seq = seqEl && allSequences(state.doc).find((q) => q.id === seqEl.dataset.seq); + if (seq) { + e.preventDefault(); + const fz = t.closest('[data-frame]'); + const frame = fz ? seq.frames[Number(fz.dataset.frame)] : null; + return importFiles(seq, files, { key: 'image', replace: frame, add: addFrameFrom(seq) }); + } const recEl = t && t.closest ? t.closest('[data-rec]') : null; const rec = recEl && allRecords().find((r) => r.id === recEl.dataset.rec); - if (!rec) return setStatus('Click inside a record first, then paste.'); + if (!rec) return setStatus('Click inside a record or visual sequence first, then paste.'); e.preventDefault(); const zone = t.closest('[data-field]'); const field = zone ? rec.fields[Number(zone.dataset.field)] : null; importFiles(rec, files, { replace: field && field.kind === 'image' ? field : null, pasted: true }); }); - function openLightbox(f) { - const dlg = h('dialog', { class: 'card lightbox', 'aria-label': f.label }, - h('h3', {}, f.label), h('img', { class: 'bigimg', src: f.value, alt: f.label }), + function openLightbox(label, src) { + const dlg = h('dialog', { class: 'card lightbox', 'aria-label': label }, + h('h3', {}, label), h('img', { class: 'bigimg', src, alt: label }), h('div', { class: 'row' }, btn('Close', () => dlg.close(), 'primary'))); dlg.addEventListener('close', () => dlg.remove()); document.body.append(dlg); @@ -195,7 +208,7 @@ else if (hidden) value = h('span', {}, h('span', { class: 'secret' }, '(hidden image)'), btn('Show', toggle, 'link', { 'aria-label': 'Show ' + f.label })); else value = h('span', {}, h('img', { class: 'fimg', src: f.value, alt: f.label, tabIndex: 0, title: 'Click to enlarge', - onclick: () => openLightbox(f), onkeydown: (e) => { if (e.key === 'Enter') openLightbox(f); } }), + onclick: () => openLightbox(f.label, f.value), onkeydown: (e) => { if (e.key === 'Enter') openLightbox(f.label, f.value); } }), f.secret ? btn('Hide', toggle, 'link', { 'aria-label': 'Hide ' + f.label }) : null); return [h('dt', {}, f.label), h('dd', {}, value)]; } @@ -224,6 +237,30 @@ h('p', { class: 'muted small' }, 'Updated ' + r.updated)); } + // A frame is hidden (picture and text) if marked secret and not revealed. + function viewFrame(q, f, k) { + const hidden = f.secret && !state.revealed.has(f); + const toggle = () => { state.revealed[hidden ? 'add' : 'delete'](f); render(); }; + const label = q.title + ', step ' + (k + 1); + const layout = hidden ? 'none' : frameLayout(f); + return h('div', { class: 'frame ' + layout }, + h('div', { class: 'fnum' }, 'Step ' + (k + 1)), + hidden ? h('div', {}, h('span', { class: 'secret' }, '(hidden step)'), btn('Show', toggle, 'link', { 'aria-label': 'Show ' + label })) : [ + f.image ? h('img', { class: 'fimg', src: f.image, alt: label, tabIndex: 0, title: 'Click to enlarge', + onclick: () => openLightbox(label, f.image), onkeydown: (e) => { if (e.key === 'Enter') openLightbox(label, f.image); } }) + : h('span', { class: 'muted' }, '(no image)'), + layout !== 'none' ? h('p', { class: 'ftext' }, f.text) : null, + f.secret ? btn('Hide', toggle, 'link', { 'aria-label': 'Hide ' + label }) : null]); + } + + function viewSequence(q) { + return h('article', { class: 'card seq' }, + h('h3', {}, q.title), + q.frames.length ? h('div', { class: 'frames' }, q.frames.map((f, k) => viewFrame(q, f, k))) : h('p', { class: 'muted' }, '(no steps yet)'), + h('p', { class: 'muted small' }, 'Updated ' + q.updated)); + } + const seqItem = (q) => h('li', { class: 'seqitem' }, viewSequence(q)); + function viewItem(it, ordered) { return h('li', { class: 'item ' + it.status }, h('div', {}, h('strong', {}, it.text), ' ', statusBadge(it.status)), @@ -233,10 +270,10 @@ function viewSection(s) { return [ s.intro ? h('p', { class: 'intro' }, s.intro) : null, - s.kind === 'text' ? h('p', { class: 'body' }, s.body) : null, - s.kind === 'records' ? s.records.map(viewRecord) : null, - s.kind === 'steps' ? h('ol', { class: 'items' }, s.items.map((i) => viewItem(i))) : null, - s.kind === 'checklist' ? h('ul', { class: 'items' }, s.items.map((i) => viewItem(i))) : null, + s.kind === 'text' ? [h('p', { class: 'body' }, s.body), (s.sequences || []).map(viewSequence)] : null, + s.kind === 'records' ? s.records.map((r) => (isSequence(r) ? viewSequence(r) : viewRecord(r))) : null, + s.kind === 'steps' ? h('ol', { class: 'items' }, s.items.map((i) => (isSequence(i) ? seqItem(i) : viewItem(i)))) : null, + s.kind === 'checklist' ? h('ul', { class: 'items' }, s.items.map((i) => (isSequence(i) ? seqItem(i) : viewItem(i)))) : null, ]; } @@ -262,23 +299,30 @@ btn('✕', () => { if (confirm('Delete this ' + what + '?')) { removeAt(arr, i); note(); render(); } }, 'icon danger', { 'aria-label': 'Delete ' + what })); } - function editImageField(r, f, i) { - const secret = h('input', { type: 'checkbox', checked: !!f.secret, 'aria-label': 'Secret', - onchange: () => { f.secret = secret.checked || undefined; if (!f.secret) delete f.secret; changed(r); } }); + // The drop / paste / choose-a-file box for the picture stored at obj[key]. + // onFiles(files) receives the dropped or chosen files; onChange() runs after Remove. + function imageZone(obj, key, label, attrs, onFiles, onChange) { const picker = h('input', { type: 'file', accept: 'image/png,image/jpeg', hidden: true, 'aria-label': 'Choose an image file', - onchange: () => { importFiles(r, [...picker.files], { replace: f }); picker.value = ''; } }); - const zone = h('div', { class: 'dropzone', tabIndex: 0, 'data-field': String(i), 'aria-label': 'Image for ' + f.label + ': drop, paste or choose a file', + onchange: () => { onFiles([...picker.files]); picker.value = ''; } }); + const zone = h('div', Object.assign({ class: 'dropzone', tabIndex: 0, 'aria-label': 'Image for ' + label + ': drop, paste or choose a file', ondragover: (e) => { if (hasFiles(e)) { e.preventDefault(); zone.classList.add('over'); } }, ondragleave: () => zone.classList.remove('over'), - ondrop: (e) => { e.preventDefault(); e.stopPropagation(); zone.classList.remove('over'); importFiles(r, [...e.dataTransfer.files], { replace: f }); } }, - f.value ? h('img', { class: 'fimg', src: f.value, alt: f.label }) : null, - h('p', { class: 'muted small' }, f.value - ? formatBytes(dataUrlBytes(f.value)) + '. Drop, paste or choose a file to replace it.' + ondrop: (e) => { e.preventDefault(); e.stopPropagation(); zone.classList.remove('over'); onFiles([...e.dataTransfer.files]); } }, attrs), + obj[key] ? h('img', { class: 'fimg', src: obj[key], alt: label }) : null, + h('p', { class: 'muted small' }, obj[key] + ? formatBytes(dataUrlBytes(obj[key])) + '. Drop, paste or choose a file to replace it.' : 'Drop an image here, paste one while this box is selected, or choose a file.'), h('div', { class: 'row' }, btn('Choose file\u2026', () => picker.click(), 'small'), - f.value ? btn('Remove image', () => { f.value = ''; changed(r); render(); }, 'small') : null), + obj[key] ? btn('Remove image', () => { obj[key] = ''; onChange(); render(); }, 'small') : null), picker); + return zone; + } + + function editImageField(r, f, i) { + const secret = h('input', { type: 'checkbox', checked: !!f.secret, 'aria-label': 'Secret', + onchange: () => { f.secret = secret.checked || undefined; if (!f.secret) delete f.secret; changed(r); } }); + const zone = imageZone(f, 'value', f.label, { 'data-field': String(i) }, (files) => importFiles(r, files, { replace: f }), () => changed(r)); return h('div', { class: 'field imagefield' }, h('div', { class: 'row' }, input(f, 'label', { label: 'Field label' }, r), h('label', { class: 'check' }, secret, ' secret'), rowTools(r.fields, i, 'image field', r)), zone); @@ -307,6 +351,34 @@ h('label', {}, 'Notes', input(r, 'notes', { multiline: true, label: 'Notes' }, r))); } + const LAYOUT_LABELS = [['right', 'Text right of image'], ['under', 'Text under image'], ['none', 'No text']]; + + function editFrame(q, f, k) { + const secret = h('input', { type: 'checkbox', checked: !!f.secret, 'aria-label': 'Secret step', + onchange: () => { f.secret = secret.checked || undefined; if (!f.secret) delete f.secret; changed(q); } }); + const layout = h('select', { 'aria-label': 'Text position for step ' + (k + 1), onchange: () => { f.layout = layout.value; changed(q); render(); } }, + LAYOUT_LABELS.map(([v, t]) => h('option', { value: v, selected: f.layout === v }, t))); + return h('div', { class: 'field imagefield frameedit' }, + h('div', { class: 'row' }, h('strong', {}, 'Step ' + (k + 1)), layout, h('label', { class: 'check' }, secret, ' secret'), rowTools(q.frames, k, 'step', q)), + imageZone(f, 'image', q.title + ', step ' + (k + 1), { 'data-frame': String(k) }, + (files) => importFiles(q, files, { key: 'image', replace: f, add: addFrameFrom(q) }), () => changed(q)), + f.layout === 'none' ? null : h('label', {}, 'Text', input(f, 'text', { multiline: true, label: 'Text for step ' + (k + 1) }, q))); + } + + function editSequence(list, q, i) { + const picker = h('input', { type: 'file', multiple: true, accept: 'image/png,image/jpeg', hidden: true, 'aria-label': 'Choose images for new steps', + onchange: () => { importFiles(q, [...picker.files], { add: addFrameFrom(q) }); picker.value = ''; } }); + return h('article', { class: 'card editing seq', 'data-seq': q.id, + ondragover: (e) => { if (hasFiles(e)) e.preventDefault(); }, + ondrop: (e) => { e.preventDefault(); importFiles(q, [...e.dataTransfer.files], { add: addFrameFrom(q) }); } }, + h('div', { class: 'row' }, input(q, 'title', { label: 'Visual sequence title' }, q), rowTools(list, i, 'visual sequence', q)), + q.frames.map((f, k) => editFrame(q, f, k)), + h('div', { class: 'row' }, + btn('+ Add step', () => { addFrame(q); changed(q); render(); }, 'small'), + btn('+ Add images\u2026', () => picker.click(), 'small'), picker)); + } + const addSeqBtn = (s) => btn('+ Add visual sequence', () => { addSequence(state.doc, s); markDirty(); render(); }, 'primary'); + function editItem(s, it, i) { return h('li', { class: 'card editing' }, h('div', { class: 'row' }, input(it, 'text', { label: 'Item text' }), select(it, 'status', ['open', 'done', 'tbd'], 'Item status'), rowTools(s.items, i, 'item')), @@ -319,9 +391,13 @@ h('div', { class: 'card editing' }, h('div', { class: 'row' }, input(s, 'title', { label: 'Section title' }), rowTools(state.doc.sections, secIdx, 'section')), h('label', {}, 'Intro', input(s, 'intro', { multiline: true, label: 'Section intro' }))), - s.kind === 'text' ? h('label', {}, 'Text', input(s, 'body', { multiline: true, label: 'Body' })) : null, - s.kind === 'records' ? [s.records.map((r, i) => editRecord(s, r, i)), btn('+ Add record', () => { addRecord(state.doc, s); markDirty(); render(); }, 'primary')] : null, - s.kind === 'steps' || s.kind === 'checklist' ? [h('ol', { class: 'items' }, s.items.map((it, i) => editItem(s, it, i))), btn('+ Add item', () => { addItem(state.doc, s); markDirty(); render(); }, 'primary')] : null, + s.kind === 'text' ? [h('label', {}, 'Text', input(s, 'body', { multiline: true, label: 'Body' })), + (s.sequences || []).map((q, i) => editSequence(s.sequences, q, i)), h('div', { class: 'row' }, addSeqBtn(s))] : null, + s.kind === 'records' ? [s.records.map((r, i) => (isSequence(r) ? editSequence(s.records, r, i) : editRecord(s, r, i))), + h('div', { class: 'row' }, btn('+ Add record', () => { addRecord(state.doc, s); markDirty(); render(); }, 'primary'), addSeqBtn(s))] : null, + s.kind === 'steps' || s.kind === 'checklist' ? [h('ol', { class: 'items' }, s.items.map((it, i) => (isSequence(it) + ? h('li', { class: 'seqitem' }, editSequence(s.items, it, i)) : editItem(s, it, i)))), + h('div', { class: 'row' }, btn('+ Add item', () => { addItem(state.doc, s); markDirty(); render(); }, 'primary'), addSeqBtn(s))] : null, ]; } @@ -376,6 +452,22 @@ return displayValue(f, includeSecrets); } + function printFrame(f, k, includeSecrets) { + const v = frameView(f, includeSecrets); + return h('div', { class: 'pframe ' + v.layout }, + h('div', { class: 'pfnum' }, 'Step ' + (k + 1)), + v.withheld ? h('p', { class: 'psecret' }, '[withheld]') + : v.image ? h('img', { class: 'pimg', src: v.image, alt: 'Step ' + (k + 1) }) + : h('p', {}, '(no image)'), + v.layout !== 'none' ? h('p', { class: 'pftext' }, v.text) : null); + } + + function printSequence(q, includeSecrets) { + return h('div', { class: 'pseq' }, + h('h3', {}, q.title), + q.frames.map((f, k) => printFrame(f, k, includeSecrets))); + } + 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), @@ -406,10 +498,10 @@ 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)), + s.kind === 'text' ? [h('p', { class: 'pbody' }, s.body), (s.sequences || []).map((q) => printSequence(q, includeSecrets))] : null, + s.kind === 'records' ? s.records.map((r) => (isSequence(r) ? printSequence(r, includeSecrets) : printRecord(r, includeSecrets))) : null, + s.kind === 'steps' ? h('ol', { class: 'pitems' }, s.items.map((i) => (isSequence(i) ? h('li', { class: 'pseqitem' }, printSequence(i, includeSecrets)) : printItem(i)))) : null, + s.kind === 'checklist' ? h('ul', { class: 'pitems' }, s.items.map((i) => (isSequence(i) ? h('li', { class: 'pseqitem' }, printSequence(i, includeSecrets)) : printItem(i)))) : null)), ].flat(); // The header sits in so the browser repeats it, in flow, at the top of every page. printRoot.replaceChildren(h('table', { class: 'ptable' }, diff --git a/src/model.js b/src/model.js index 9cca095..bbe12c0 100644 --- a/src/model.js +++ b/src/model.js @@ -21,6 +21,13 @@ import { isImageDataUrl, MAX_IMAGE_DATA_CHARS } from './images.js'; // Field: { label, value, secret?: boolean, kind?: 'text'|'image' } (kind defaults to text) // 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 } +// 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. @@ -35,6 +42,8 @@ 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']; @@ -111,12 +120,16 @@ export function validate(doc) { 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`); - s.records.forEach((r, j) => checkRecord(r, `${where}.records[${j}]`)); + s.records.forEach((r, j) => (isSequence(r) ? checkSequence : checkRecord)(r, `${where}.records[${j}]`)); } else { if (!Array.isArray(s.items)) return errs.push(`${where}: items must be an array`); - s.items.forEach((it, j) => checkItem(it, `${where}.items[${j}]`)); + s.items.forEach((it, j) => (isSequence(it) ? checkSequence : checkItem)(it, `${where}.items[${j}]`)); } }); @@ -141,6 +154,24 @@ export function validate(doc) { }); } + 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 (!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`); + }); + } + function checkItem(it, where) { if (!isObj(it)) return errs.push(`${where}: must be an object`); checkId(it.id, where); diff --git a/src/ops.js b/src/ops.js index a4da212..d75e22d 100644 --- a/src/ops.js +++ b/src/ops.js @@ -1,4 +1,4 @@ -import { defaultHeaderFields } from './model.js'; +import { defaultHeaderFields, isSequence } from './model.js'; // Pure editing operations on the document model. No DOM; tested in Node. // build.js strips `export` when bundling. @@ -11,6 +11,7 @@ export function allIds(doc) { ids.add(s.id); (s.records || []).forEach((r) => ids.add(r.id)); (s.items || []).forEach((i) => ids.add(i.id)); + (s.sequences || []).forEach((q) => ids.add(q.id)); } return ids; } @@ -164,3 +165,35 @@ export function revertHeader(doc, baseline) { function syncDate(doc, baseline) { if (!docDiffers(doc, baseline)) doc.meta.lastUpdated = baseline.meta.lastUpdated; } + +// ---- visual sequences ---- +// Every sequence in the document, wherever it lives. +export function allSequences(doc) { + const out = []; + for (const s of doc.sections) { + for (const e of s.records || []) if (isSequence(e)) out.push(e); + for (const e of s.items || []) if (isSequence(e)) out.push(e); + for (const e of s.sequences || []) out.push(e); + } + return out; +} + +// The ordered list a new sequence goes into for this kind of section. +export function sequenceList(section) { + if (section.kind === 'records') return section.records; + if (section.kind === 'text') return section.sequences || (section.sequences = []); + return section.items; +} + +export function addSequence(doc, section) { + const q = { id: newId(doc, 'seq'), kind: 'sequence', title: 'New visual sequence', updated: today(), frames: [] }; + sequenceList(section).push(q); + touch(doc, q); + return q; +} + +export function addFrame(seq, image = '', layout = 'under') { + const f = { image, text: '', layout }; + seq.frames.push(f); + return f; +} diff --git a/src/print.js b/src/print.js index ccf2563..78fde8b 100644 --- a/src/print.js +++ b/src/print.js @@ -1,4 +1,4 @@ -import { defaultHeaderFields } from './model.js'; +import { defaultHeaderFields, isSequence } from './model.js'; // Pure helpers for the printed document. No DOM; tested in Node. // build.js strips `export` when bundling. @@ -38,10 +38,34 @@ export function headerLayout(doc) { }; } +// Where a frame's text goes: empty text never takes room, whatever the layout says. +export function frameLayout(frame) { + return frame.layout === 'none' || !String(frame.text || '').trim() ? 'none' : frame.layout; +} + +// What to show for a frame. A secret frame (picture and text) is withheld +// unless secrets are included; a frame with nothing in it is never "withheld". +export function frameView(frame, includeSecrets) { + const hasContent = !!(frame.image || String(frame.text || '').trim()); + const withheld = !!frame.secret && !includeSecrets && hasContent; + return { + withheld, + image: withheld ? '' : frame.image, + text: withheld ? '' : frame.text, + layout: withheld ? 'none' : frameLayout(frame), + }; +} + export function countSecrets(doc) { let n = 0; + const frames = (q) => { for (const f of q.frames) if (f.secret && (f.image || String(f.text || '').trim())) n++; }; for (const s of doc.sections) { - for (const r of s.records || []) for (const f of r.fields) if (f.secret && f.value) n++; + for (const r of s.records || []) { + if (isSequence(r)) frames(r); + else for (const f of r.fields) if (f.secret && f.value) n++; + } + for (const it of s.items || []) if (isSequence(it)) frames(it); + for (const q of s.sequences || []) frames(q); } return n; } diff --git a/src/style.css b/src/style.css index af9e760..f6a205b 100644 --- a/src/style.css +++ b/src/style.css @@ -45,6 +45,14 @@ textarea { width: 100%; display: block; } label { display: block; margin-top: .5 .dropzone { border: 2px dashed var(--line); border-radius: .5rem; padding: .75rem; text-align: center; background: var(--bg); } .dropzone.over, .dropzone:focus-visible { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--bg)); } .dropzone .row { justify-content: center; margin: .25rem 0 0; } .dropzone .fimg { margin: .25rem auto; cursor: default; } .imagefield { margin: 0 0 .75rem; } +/* ---------- visual sequences ---------- */ +.seq { border-left: 3px solid var(--accent); } .frames { display: grid; gap: 1rem; } +.frame { display: grid; grid-template-columns: minmax(0, 1fr); gap: .35rem 1rem; align-items: start; padding-bottom: .75rem; border-bottom: 1px dashed var(--line); } +.frame:last-child { border-bottom: 0; padding-bottom: 0; } .frame .fnum { grid-column: 1 / -1; } +.frame.right { grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); } .frame .fimg { margin: 0; } +.fnum { font-weight: 700; color: var(--accent); } .ftext { white-space: pre-wrap; margin: 0; } +li.seqitem { display: block; list-style: none; margin-left: -1.4rem; } .frameedit { border-top: 1px dashed var(--line); padding-top: .5rem; } +@media (max-width: 700px) { .frame.right { grid-template-columns: minmax(0, 1fr); } } dialog.lightbox { max-width: 92vw; max-height: 92vh; overflow: auto; } .bigimg { display: block; max-width: 100%; max-height: 75vh; margin: 0 auto .75rem; background: #fff; } /* ---------- print header (screen preview and printed running header) ---------- */ @@ -82,6 +90,10 @@ dialog.lightbox { max-width: 92vw; max-height: 92vh; overflow: auto; } .bigimg { .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; } .pimg { display: block; max-width: 100%; max-height: 4.2in; object-fit: contain; border: 0.5pt solid #999; margin: 2pt 0; } + .pseq { margin: 0 0 12pt; } li.pseqitem { display: block; list-style: none; } + .pframe { break-inside: avoid; display: grid; grid-template-columns: minmax(0, 1fr); gap: 3pt 12pt; align-items: start; margin: 0 0 10pt; } + .pframe.right { grid-template-columns: 3.9in minmax(0, 1fr); } .pframe.right .pimg { max-height: 3.4in; } + .pfnum { grid-column: 1 / -1; font: 700 9.5pt system-ui, sans-serif; break-after: avoid; } .pframe p { margin: 0; } .pftext { white-space: pre-wrap; } .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; } diff --git a/test/model.test.js b/test/model.test.js index defe7ea..eae2b14 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -68,3 +68,62 @@ test('data/seed.json, when present, is valid', { skip: !existsSync('data/seed.js const seed = JSON.parse(readFileSync('data/seed.json', 'utf8')); assert.deepEqual(validate(seed), []); }); + +// ---- visual sequences ---- +import { addSequence, addFrame, allSequences, allIds, sequenceList, addSection, addRecord, addItem } from '../src/ops.js'; +import { isSequence } from '../src/model.js'; +import { frameLayout, frameView, countSecrets } from '../src/print.js'; + +const PNG = 'data:image/png;base64,iVBORw0KGgo='; +function docWithSequences() { + const d = emptyDocument('o'); + const rs = addSection(d, 'records', 'Banks'); addRecord(d, rs); + const st = addSection(d, 'steps', 'Wind-down'); addItem(d, st); + const ck = addSection(d, 'checklist', 'Cancel'); addItem(d, ck); + const tx = addSection(d, 'text', 'Start'); + for (const s of [rs, st, ck, tx]) { const q = addSequence(d, s); addFrame(q, PNG, 'right').text = 'hello'; addFrame(q, PNG, 'none'); } + return { d, rs, st, ck, tx }; +} + +test('sequences are valid in every section kind, interleaved with records and items', () => { + const { d, rs, st, ck, tx } = docWithSequences(); + assert.deepEqual(validate(d), []); + assert.equal(isSequence(rs.records[1]) && isSequence(st.items[1]) && isSequence(ck.items[1]) && isSequence(tx.sequences[0]), true); + assert.equal(allSequences(d).length, 4); + assert.equal(new Set(allIds(d)).size, allIds(d).size); + assert.equal(allIds(d).has(tx.sequences[0].id), true); + assert.equal(sequenceList(tx), tx.sequences); +}); + +test('sequence validation: layout, image, text, secret, id, title', () => { + const { d, rs } = docWithSequences(); + const q = rs.records[1]; + const bad = (mut, re) => { const c = structuredClone(d); const cq = c.sections[0].records[1]; mut(cq); assert.match(validate(c).join('|'), re); }; + bad((x) => { x.frames[0].layout = 'diagonal'; }, /bad layout/); + bad((x) => { x.frames[0].image = 'data:image/gif;base64,AAAA'; }, /image must be a PNG or JPEG/); + bad((x) => { x.frames[0].image = 5; }, /image must be a string/); + bad((x) => { x.frames[0].text = null; }, /text must be a string/); + bad((x) => { x.frames[0].secret = 'yes'; }, /secret must be a boolean/); + bad((x) => { x.title = ''; }, /missing title/); + bad((x) => { x.id = d.sections[0].records[0].id; }, /duplicate id/); + bad((x) => { delete x.frames; }, /frames must be an array/); + assert.equal(q.frames[1].image, PNG); + const e = structuredClone(d); e.sections[0].records[1].frames[0].image = ''; assert.deepEqual(validate(e), []); // empty frame is fine +}); + +test('frameLayout hides empty text; frameView withholds secret frames only when masked', () => { + assert.equal(frameLayout({ layout: 'right', text: ' ' }), 'none'); + assert.equal(frameLayout({ layout: 'right', text: 'x' }), 'right'); + assert.equal(frameLayout({ layout: 'none', text: 'x' }), 'none'); + const f = { image: PNG, text: 'pw is hunter2', layout: 'under', secret: true }; + assert.deepEqual(frameView(f, false), { withheld: true, image: '', text: '', layout: 'none' }); + assert.equal(frameView(f, true).text, 'pw is hunter2'); + assert.equal(frameView({ image: '', text: '', layout: 'under', secret: true }, false).withheld, false); +}); + +test('countSecrets counts secret frames in all places and ignores sequences as records', () => { + const { d, rs, st, tx } = docWithSequences(); + assert.equal(countSecrets(d), 0); + rs.records[1].frames[0].secret = true; st.items[1].frames[1].secret = true; tx.sequences[0].frames[0].secret = true; + assert.equal(countSecrets(d), 3); +});