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:
@@ -40,6 +40,12 @@ After sealing, `data/seed.json` is still plaintext. Delete it (and see
|
||||
- Chrome/Edge: pick the drive file once; later saves overwrite silently.
|
||||
- Firefox/Safari: a new `final-wishes.html` downloads. Copy it over the
|
||||
old one on the drive.
|
||||
- **Cancel** (next to Save, edit mode only) throws away the unsaved
|
||||
changes to the section on screen, back to how it was at the last open
|
||||
or save, and leaves edit mode. It asks first if anything changed. It
|
||||
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.
|
||||
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.
|
||||
|
||||
@@ -29,6 +29,8 @@ TO PRINT IT
|
||||
|
||||
IF YOU NEED TO CHANGE SOMETHING
|
||||
Click "Edit", make the change, click "Done editing", then "Save".
|
||||
Changed your mind? "Cancel" undoes the unsaved changes to the section
|
||||
you are looking at.
|
||||
Chrome and Edge ask where to save the first time: choose this drive and
|
||||
the same file name, final-wishes.html, and agree to replace it.
|
||||
Firefox downloads a new final-wishes.html to your Downloads folder
|
||||
|
||||
30
src/app.js
30
src/app.js
@@ -9,6 +9,8 @@
|
||||
current: null, // current section id
|
||||
editing: false,
|
||||
dirty: false,
|
||||
baseline: null, // deep copy of the document at open/save; Cancel reverts to it
|
||||
pwChanged: false, // passphrase changed since the last save
|
||||
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
|
||||
@@ -46,6 +48,10 @@
|
||||
touch(state.doc, record);
|
||||
markDirty();
|
||||
}
|
||||
function snapshot() {
|
||||
state.baseline = snapshotDoc(state.doc);
|
||||
state.pwChanged = false;
|
||||
}
|
||||
window.addEventListener('beforeunload', (e) => {
|
||||
if (state.dirty) { e.preventDefault(); e.returnValue = ''; }
|
||||
});
|
||||
@@ -68,6 +74,7 @@
|
||||
const doc = JSON.parse(await decrypt(blob, pw.value));
|
||||
assertValid(doc);
|
||||
state.doc = doc;
|
||||
snapshot();
|
||||
state.passphrase = pw.value;
|
||||
state.blob = null;
|
||||
state.current = doc.sections[0] && doc.sections[0].id;
|
||||
@@ -88,6 +95,7 @@
|
||||
const doc = JSON.parse(await file.files[0].text());
|
||||
assertValid(doc);
|
||||
state.doc = doc;
|
||||
snapshot();
|
||||
state.current = doc.sections[0] && doc.sections[0].id;
|
||||
state.error = '';
|
||||
} catch (err) { state.error = 'Could not open that file: ' + err.message; }
|
||||
@@ -107,6 +115,7 @@
|
||||
h('p', {}, 'or'),
|
||||
btn('Start a new document', () => {
|
||||
state.doc = emptyDocument('');
|
||||
snapshot();
|
||||
state.editing = true;
|
||||
render();
|
||||
}, 'primary'));
|
||||
@@ -493,10 +502,12 @@
|
||||
await w.close();
|
||||
state.status = 'Saved to ' + state.handle.name + '.';
|
||||
state.dirty = false;
|
||||
snapshot();
|
||||
} else {
|
||||
downloadFile(html, 'final-wishes.html');
|
||||
state.status = 'Downloaded final-wishes.html. Replace the old copy on the thumbdrive with it.';
|
||||
state.dirty = false;
|
||||
snapshot();
|
||||
}
|
||||
} catch (e) {
|
||||
if (e && e.name === 'AbortError') return; // user closed the file picker
|
||||
@@ -505,10 +516,28 @@
|
||||
render();
|
||||
}
|
||||
|
||||
// Cancel: throw away unsaved edits to the section (or print header) on
|
||||
// screen, back to how it was at the last open/save, then leave edit mode.
|
||||
function cancelEdit() {
|
||||
const onHeader = state.current === HEADER_ID;
|
||||
const id = state.current;
|
||||
const differs = onHeader ? headerDiffers(state.doc, state.baseline) : sectionDiffers(state.doc, state.baseline, id);
|
||||
if (differs) {
|
||||
if (!confirm('Discard your unsaved changes to this ' + (onHeader ? 'print header' : 'section') + '?')) return;
|
||||
if (onHeader) revertHeader(state.doc, state.baseline);
|
||||
else state.current = revertSection(state.doc, state.baseline, id);
|
||||
state.dirty = state.pwChanged || docDiffers(state.doc, state.baseline);
|
||||
state.status = 'Changes discarded.';
|
||||
}
|
||||
state.editing = false;
|
||||
render();
|
||||
}
|
||||
|
||||
async function changePassphrase() {
|
||||
const p = await askPassphrase('Change passphrase', 'The new passphrase takes effect when you next save. Until then the saved file still opens with the old one.');
|
||||
if (!p) return;
|
||||
state.passphrase = p;
|
||||
state.pwChanged = true;
|
||||
markDirty();
|
||||
state.status = 'Passphrase changed. Save to apply it.';
|
||||
render();
|
||||
@@ -550,6 +579,7 @@
|
||||
h('span', { id: 'dirty', class: 'badge tbd', hidden: !state.dirty }, 'Unsaved changes'),
|
||||
h('span', { id: 'status', class: 'muted', role: 'status' }, state.status),
|
||||
btn('Save', () => save(), state.dirty ? 'primary' : ''),
|
||||
state.editing ? btn('Cancel', cancelEdit, '', { title: 'Discard unsaved changes to this section and stop editing' }) : null,
|
||||
btn('Backup copy', () => save({ backup: true })),
|
||||
btn('Change passphrase', changePassphrase),
|
||||
btn('Print…', openPrintDialog),
|
||||
|
||||
55
src/ops.js
55
src/ops.js
@@ -109,3 +109,58 @@ export function addImageField(record, label, value) {
|
||||
record.fields.push(f);
|
||||
return f;
|
||||
}
|
||||
|
||||
// ---- revert to the last saved state (Cancel) ----
|
||||
// A baseline is a deep copy of the document taken at open / save.
|
||||
export function snapshotDoc(doc) {
|
||||
return JSON.parse(JSON.stringify(doc));
|
||||
}
|
||||
|
||||
// The date stamp changes on every edit, so it never counts as a difference.
|
||||
const stable = (doc) => JSON.stringify({ ...doc, meta: { ...doc.meta, lastUpdated: '' } });
|
||||
const sameJson = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
||||
|
||||
export function docDiffers(doc, baseline) {
|
||||
return stable(doc) !== stable(baseline);
|
||||
}
|
||||
|
||||
// A section the baseline doesn't have was added since: that counts as changed.
|
||||
export function sectionDiffers(doc, baseline, id) {
|
||||
const cur = doc.sections.find((s) => s.id === id);
|
||||
const old = baseline.sections.find((s) => s.id === id);
|
||||
if (!cur) return false;
|
||||
return !old || !sameJson(cur, old);
|
||||
}
|
||||
|
||||
export function headerDiffers(doc, baseline) {
|
||||
return !sameJson(doc.header, baseline.header);
|
||||
}
|
||||
|
||||
// Put the section back as it was in the baseline (in its current position),
|
||||
// or remove it if it was added since. Returns the id of the section to show
|
||||
// next (the same one, or a neighbour after a removal; undefined if none left).
|
||||
export function revertSection(doc, baseline, id) {
|
||||
const i = doc.sections.findIndex((s) => s.id === id);
|
||||
if (i < 0) return undefined;
|
||||
const old = baseline.sections.find((s) => s.id === id);
|
||||
if (old) {
|
||||
doc.sections[i] = snapshotDoc(old);
|
||||
syncDate(doc, baseline);
|
||||
return id;
|
||||
}
|
||||
doc.sections.splice(i, 1);
|
||||
syncDate(doc, baseline);
|
||||
const next = doc.sections[i] || doc.sections[i - 1];
|
||||
return next && next.id;
|
||||
}
|
||||
|
||||
export function revertHeader(doc, baseline) {
|
||||
if (baseline.header === undefined) delete doc.header;
|
||||
else doc.header = snapshotDoc(baseline.header);
|
||||
syncDate(doc, baseline);
|
||||
}
|
||||
|
||||
// If nothing differs any more, the "last updated" date goes back too.
|
||||
function syncDate(doc, baseline) {
|
||||
if (!docDiffers(doc, baseline)) doc.meta.lastUpdated = baseline.meta.lastUpdated;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user