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),
|
||||
|
||||
Reference in New Issue
Block a user