Add Cancel to revert unsaved changes in a section

Reverts the section (or print header) on screen to the last open/save
and leaves edit mode.

Closes #11

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 18:24:26 -05:00
parent f5f05a3e26
commit d588e30b9b
5 changed files with 175 additions and 1 deletions

View File

@@ -1,7 +1,7 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { emptyDocument, validate } from '../src/model.js';
import { move, removeAt, newId, addSection, addRecord, addField, addItem, touch, ensureHeader, addHeaderField, setAlign, takenAligns } from '../src/ops.js';
import { move, removeAt, newId, addSection, addRecord, addField, addItem, touch, ensureHeader, addHeaderField, setAlign, takenAligns, snapshotDoc, docDiffers, sectionDiffers, headerDiffers, revertSection, revertHeader } from '../src/ops.js';
test('move swaps neighbors and refuses out of range', () => {
const a = [1, 2, 3];
@@ -81,3 +81,84 @@ test('setAlign enforces one left and one right', () => {
assert.equal('align' in fields[0], false);
assert.equal(setAlign(fields, fields[2], 'left'), true); // freed
});
// ---- revert (Cancel) ----
function sample() {
const d = emptyDocument('Owner');
d.meta.lastUpdated = '2020-01-01';
const a = addSection(d, 'records', 'Banks');
const r = addRecord(d, a); r.title = 'Bank one'; addField(r).value = 'v';
const b = addSection(d, 'records', 'Cards');
d.meta.lastUpdated = '2020-01-01';
return { d, a, b, r };
}
test('revertSection restores an edited section and leaves others alone', () => {
const { d, a, b } = sample();
const base = snapshotDoc(d);
a.records[0].title = 'Changed'; a.records[0].fields.push({ label: 'x', value: 'y' });
b.title = 'Cards edited';
touch(d, a.records[0]);
assert.equal(sectionDiffers(d, base, a.id), true);
assert.equal(revertSection(d, base, a.id), a.id);
assert.deepEqual(d.sections[0], base.sections[0]);
assert.equal(d.sections[1].title, 'Cards edited');
assert.equal(docDiffers(d, base), true); // Cards still differs
assert.notEqual(d.meta.lastUpdated, '2020-01-01'); // date stays while something differs
});
test('last revert clears the difference and restores the date', () => {
const { d, a } = sample();
const base = snapshotDoc(d);
a.title = 'Edited'; touch(d, a);
revertSection(d, base, a.id);
assert.equal(docDiffers(d, base), false);
assert.equal(d.meta.lastUpdated, '2020-01-01');
});
test('the date stamp alone is not a difference', () => {
const { d } = sample();
const base = snapshotDoc(d);
d.meta.lastUpdated = '2030-05-05';
assert.equal(docDiffers(d, base), false);
});
test('a section added since the baseline is removed, selecting a neighbour', () => {
const { d, b } = sample();
const base = snapshotDoc(d);
const c = addSection(d, 'text', 'New');
assert.equal(sectionDiffers(d, base, c.id), true);
assert.equal(revertSection(d, base, c.id), b.id);
assert.equal(d.sections.length, 2);
const only = emptyDocument('');
const base2 = snapshotDoc(only);
const x = addSection(only, 'text', 'X');
assert.equal(revertSection(only, base2, x.id), undefined);
assert.equal(only.sections.length, 0);
});
test('an unchanged section does not differ; a moved one still reverts in place', () => {
const { d, a, b } = sample();
const base = snapshotDoc(d);
assert.equal(sectionDiffers(d, base, a.id), false);
move(d.sections, 0, 1);
a.title = 'Edited';
revertSection(d, base, a.id);
assert.equal(d.sections[1].title, 'Banks');
assert.equal(d.sections[0].id, b.id);
});
test('header revert restores the default (absent) and a custom header', () => {
const { d } = sample();
let base = snapshotDoc(d);
ensureHeader(d); addHeaderField(d);
assert.equal(headerDiffers(d, base), true);
revertHeader(d, base);
assert.equal(d.header, undefined);
assert.equal(headerDiffers(d, base), false);
ensureHeader(d); base = snapshotDoc(d);
d.header.fields[0].text = 'changed';
revertHeader(d, base);
assert.notEqual(d.header.fields[0].text, 'changed');
assert.notEqual(d.header, base.header); // a copy, not shared
});