From 1736548ef9a5e66d95ab9798b8cc7f09077046a8 Mon Sep 17 00:00:00 2001 From: Eric Bell Date: Mon, 9 Nov 2026 18:37:58 -0500 Subject: [PATCH] Say what is in a section when confirming its deletion Closes #13 Co-Authored-By: Claude Sonnet 5.5 --- docs/MAINTENANCE.md | 2 ++ src/app.js | 7 ++++--- src/ops.js | 26 ++++++++++++++++++++++++++ test/ops.test.js | 29 +++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/docs/MAINTENANCE.md b/docs/MAINTENANCE.md index 307ef39..2c9203d 100644 --- a/docs/MAINTENANCE.md +++ b/docs/MAINTENANCE.md @@ -46,6 +46,8 @@ After sealing, `data/seed.json` is still plaintext. Delete it (and see works on the Print header page too. Other sections keep their unsaved edits. A section you added this session is removed. Deleting or moving sections is not undone by Cancel, and neither is a passphrase change. + Deleting a section asks first and says what is in it (records, items, + visual sequences, images). 3. Saving updates "Last update" and the per-record dates. "Last update" is what the printed header shows. Editing the header settings themselves does not change it. diff --git a/src/app.js b/src/app.js index 0981f55..542984a 100644 --- a/src/app.js +++ b/src/app.js @@ -290,13 +290,14 @@ return el; } // Up / down / delete controls for element i of arr. - function rowTools(arr, i, what, record, quiet = false) { + // `message` (string or function) replaces the default delete confirmation. + function rowTools(arr, i, what, record, quiet = false, message = null) { 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); note(); render(); } }, 'icon danger', { 'aria-label': 'Delete ' + what })); + btn('✕', () => { if (confirm(typeof message === 'function' ? message() : message || 'Delete this ' + what + '?')) { removeAt(arr, i); note(); render(); } }, 'icon danger', { 'aria-label': 'Delete ' + what })); } // The drop / paste / choose-a-file box for the picture stored at obj[key]. @@ -389,7 +390,7 @@ const secIdx = state.doc.sections.indexOf(s); return [ h('div', { class: 'card editing' }, - h('div', { class: 'row' }, input(s, 'title', { label: 'Section title' }), rowTools(state.doc.sections, secIdx, 'section')), + h('div', { class: 'row' }, input(s, 'title', { label: 'Section title' }), rowTools(state.doc.sections, secIdx, 'section', undefined, false, () => deleteSectionMessage(s))), h('label', {}, 'Intro', input(s, 'intro', { multiline: true, label: 'Section intro' }))), 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, diff --git a/src/ops.js b/src/ops.js index d75e22d..97580a0 100644 --- a/src/ops.js +++ b/src/ops.js @@ -197,3 +197,29 @@ export function addFrame(seq, image = '', layout = 'under') { seq.frames.push(f); return f; } + +// ---- what a section holds (for the delete confirmation) ---- +export function sectionContents(section) { + const c = { records: 0, items: 0, sequences: 0, images: 0, text: false }; + const countFrames = (q) => { for (const f of q.frames) if (f.image) c.images++; }; + for (const r of section.records || []) { + if (isSequence(r)) { c.sequences++; countFrames(r); } else { + c.records++; + for (const f of r.fields) if (f.kind === 'image' && f.value) c.images++; + } + } + for (const it of section.items || []) { if (isSequence(it)) { c.sequences++; countFrames(it); } else c.items++; } + for (const q of section.sequences || []) { c.sequences++; countFrames(q); } + c.text = section.kind === 'text' && !!String(section.body || '').trim(); + return c; +} + +export function deleteSectionMessage(section) { + const c = sectionContents(section); + const n = (k, one, many) => (k ? [k + ' ' + (k === 1 ? one : many)] : []); + const parts = [...n(c.records, 'record', 'records'), ...n(c.items, 'item', 'items'), + ...n(c.sequences, 'visual sequence', 'visual sequences'), ...n(c.images, 'image', 'images'), ...(c.text ? ['its text'] : [])]; + const name = '"' + section.title + '"'; + if (!parts.length) return 'Delete the empty section ' + name + '?'; + return 'Delete the section ' + name + ' and everything in it (' + parts.join(', ') + ')? This cannot be undone with Cancel.'; +} diff --git a/test/ops.test.js b/test/ops.test.js index 853357b..7ddbdfe 100644 --- a/test/ops.test.js +++ b/test/ops.test.js @@ -162,3 +162,32 @@ test('header revert restores the default (absent) and a custom header', () => { assert.notEqual(d.header.fields[0].text, 'changed'); assert.notEqual(d.header, base.header); // a copy, not shared }); + +// ---- delete-section confirmation ---- +import { sectionContents, deleteSectionMessage, addSequence, addFrame } from '../src/ops.js'; +const PNGX = 'data:image/png;base64,iVBORw0KGgo='; + +test('deleteSectionMessage names the section and counts what is in it', () => { + const d = emptyDocument('o'); + const s = addSection(d, 'records', 'Bank Accounts'); + assert.equal(deleteSectionMessage(s), 'Delete the empty section "Bank Accounts"?'); + const r1 = addRecord(d, s); addRecord(d, s); + r1.fields.push({ kind: 'image', label: 'ID', value: PNGX }, { kind: 'image', label: 'blank', value: '' }, { label: 'a', value: 'b' }); + const q = addSequence(d, s); addFrame(q, PNGX); addFrame(q, ''); + assert.deepEqual(sectionContents(s), { records: 2, items: 0, sequences: 1, images: 2, text: false }); + assert.equal(deleteSectionMessage(s), + 'Delete the section "Bank Accounts" and everything in it (2 records, 1 visual sequence, 2 images)? This cannot be undone with Cancel.'); +}); + +test('deleteSectionMessage: items, singular forms, and text sections', () => { + const d = emptyDocument('o'); + const c = addSection(d, 'checklist', 'To do'); addItem(d, c); + const q = addSequence(d, c); addFrame(q, PNGX); addFrame(q, PNGX); + assert.match(deleteSectionMessage(c), /\(1 item, 1 visual sequence, 2 images\)/); + const t = addSection(d, 'text', 'Start'); + assert.match(deleteSectionMessage(t), /^Delete the empty section "Start"\?$/); + t.body = 'hello'; + assert.match(deleteSectionMessage(t), /\(its text\)/); + addSequence(d, t); + assert.match(deleteSectionMessage(t), /\(1 visual sequence, its text\)/); +});