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:
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user