Skip to content

AI-native design editor. Open-source Figma alternative.

fix(document): mark a document saved when undo returns to its last save (#1157)

Danila Poyarkovbd0a9ae6f276
diff --git a/changes/1157/fixed.md b/changes/1157/fixed.md
new file mode 100644
index 000000000..88b2c5fc4
--- /dev/null
+++ b/changes/1157/fixed.md
@@ -0,0 +1 @@
+- Undoing or redoing back to the last save shows the document as saved again, unless a collaborator or another change outside undo changed it since.
diff --git a/src/app/document/io/changes.ts b/src/app/document/io/changes.ts
index 31a9f958c..f1ea3616a 100644
--- a/src/app/document/io/changes.ts
+++ b/src/app/document/io/changes.ts
@@ -1,28 +1,66 @@
import { computed, ref } from 'vue'
import type { Editor } from '@open-pencil/core/editor'
+import type { UndoEntry } from '@open-pencil/scene-graph'
+
+/** Stands for the history of a save whose state was not captured, which history never returns to. */
+const UNKNOWN_HISTORY: UndoEntry = { label: '', forward: () => undefined, inverse: () => undefined }
/**
* Content-only revisions: viewport repainting, layout, loading a page's layers, and recovery
* never mark a document changed or saved. Autosave and recovery follow the same revision.
+ *
+ * A document whose history goes back to where it was saved, by undo or redo, is saved again,
+ * unless something changed it outside the history since, such as a collaborator. An editor edit
+ * records its step in the same task as the change, or, while text is being edited or an edit is
+ * being coalesced, such as a picker drag, when that edit ends. A change no step follows within
+ * its task, with neither of those open, is one made outside the history; one an open edit makes
+ * waits for that edit's step, and keeps the document unsaved if the edit records none.
*/
export function createDocumentChanges(editor: Editor) {
const revision = ref(0)
const savedRevision = ref(0)
- const dirty = computed(() => revision.value !== savedRevision.value)
+ /** The revision of the last change to the history, which accounts for the changes before it. */
+ let historyRevision = 0
+ /** The last content change, and the last one no history change accounted for. */
+ let contentRevision = 0
+ let outsideRevision = 0
+ let savedHistory: UndoEntry | undefined = editor.undo.peekUndo()
+ /** The history at each captured revision, for a save that finishes after later edits. */
+ const capturedHistory = new Map<number, UndoEntry | undefined>()
+
+ const dirty = computed(() => {
+ if (revision.value === savedRevision.value) return false
+ const saved = savedRevision.value
+ if (outsideRevision > saved) return true
+ if (contentRevision > Math.max(historyRevision, saved)) return true
+ return editor.undo.peekUndo() !== savedHistory
+ })
+
const changed = () => {
revision.value++
+ outsideRevision = revision.value
}
- // A page's layers load from the opened file when it is first shown; they are the document
- // as saved, not an edit.
const contentChanged = () => {
- if (!editor.graph.isApplyingImportedState) changed()
+ if (editor.graph.isApplyingImportedState) return
+ revision.value++
+ contentRevision = revision.value
+ const at = contentRevision
+ // Text editing and coalesced edits record their step when they end, not with each change.
+ if (editor.state.editingTextId !== null || editor.undo.isBatching) return
+ queueMicrotask(() => {
+ if (historyRevision < at) outsideRevision = Math.max(outsideRevision, at)
+ })
}
// Layout derives sizes and positions from the document; opening a page or loading a font
// relays it out without an edit, and an edit that relays it out has already counted.
const updated = () => {
if (!editor.graph.isApplyingLayout) contentChanged()
}
+ const historyChanged = () => {
+ revision.value++
+ historyRevision = revision.value
+ }
const unsubscribers = [
editor.onEditorEvent('node:created', contentChanged),
editor.onEditorEvent('node:updated', updated),
@@ -30,12 +68,23 @@ export function createDocumentChanges(editor: Editor) {
editor.onEditorEvent('node:reparented', contentChanged),
editor.onEditorEvent('node:reordered', contentChanged),
editor.onEditorEvent('graph:replaced', changed),
- editor.onEditorEvent('history:changed', changed)
+ editor.onEditorEvent('history:changed', historyChanged)
]
+
+ function capture() {
+ capturedHistory.set(revision.value, editor.undo.peekUndo())
+ return revision.value
+ }
+
return {
hasUnsavedChanges: () => dirty.value,
- capture: () => revision.value,
- markSaved: (version = revision.value) => {
+ capture,
+ markSaved: (version = capture()) => {
+ savedHistory = capturedHistory.has(version) ? capturedHistory.get(version) : UNKNOWN_HISTORY
+ // A later save still in progress, such as a manual save overlapping an autosave, keeps its own.
+ for (const captured of capturedHistory.keys()) {
+ if (captured <= version) capturedHistory.delete(captured)
+ }
savedRevision.value = version
},
markChanged: changed,
diff --git a/tests/engine/app/document/io/changes.test.ts b/tests/engine/app/document/io/changes.test.ts
index 3b4e233fd..c5851c95f 100644
--- a/tests/engine/app/document/io/changes.test.ts
+++ b/tests/engine/app/document/io/changes.test.ts
@@ -103,3 +103,116 @@ test("loading a page's layers from the opened file does not dirty a document", a
editor.dispose()
}
})
+
+/** Lets the tracker tell a change outside the history from one a step follows in its task. */
+const settle = () =>
+ new Promise<void>((resolve) => {
+ queueMicrotask(resolve)
+ })
+
+type Changes = ReturnType<typeof createDocumentChanges>
+
+/** A saved document holding one rectangle, resized once with undo before it was saved. */
+async function withSavedRectangle(
+ run: (editor: ReturnType<typeof createEditor>, id: string, changes: Changes) => Promise<void>
+) {
+ const editor = createEditor()
+ const page = editor.state.currentPageId
+ const id = editor.graph.createNode('RECTANGLE', page, { width: 10 }).id
+ const changes = createDocumentChanges(editor)
+ try {
+ editor.updateNodeWithUndo(id, { width: 20 }, 'Resize')
+ await settle()
+ changes.markSaved()
+ await run(editor, id, changes)
+ } finally {
+ changes.dispose()
+ editor.dispose()
+ }
+}
+
+test('undoing or redoing back to the saved state saves the document again', async () => {
+ await withSavedRectangle(async (editor, id, changes) => {
+ const states: boolean[] = []
+ const record = async () => {
+ await settle()
+ states.push(changes.hasUnsavedChanges())
+ }
+ editor.updateNodeWithUndo(id, { width: 30 }, 'Resize')
+ await record()
+ editor.undoAction()
+ await record()
+ editor.undoAction()
+ await record()
+ editor.redoAction()
+ await record()
+ expect(states).toEqual([true, false, true, false])
+ })
+})
+
+test('a change outside the history keeps the document unsaved after undoing back', async () => {
+ await withSavedRectangle(async (editor, id, changes) => {
+ // A collaborator's change reaches the graph without a step of this tab's history.
+ editor.graph.updateNode(id, { name: 'Renamed by a peer' })
+ await settle()
+ editor.updateNodeWithUndo(id, { width: 30 }, 'Resize')
+ editor.undoAction()
+ await settle()
+ expect(changes.hasUnsavedChanges()).toBe(true)
+ })
+})
+
+test('changes typed while editing text belong to the step the edit records when it ends', async () => {
+ await withSavedRectangle(async (editor, id, changes) => {
+ // Typing resizes the layer as it goes; the text edit records one step when it is committed.
+ editor.state.editingTextId = id
+ editor.graph.updateNode(id, { name: 'Typing', width: 31 })
+ await settle()
+ editor.graph.updateNode(id, { name: 'Typing more', width: 47 })
+ await settle()
+ expect(changes.hasUnsavedChanges()).toBe(true)
+
+ editor.state.editingTextId = null
+ editor.undo.push({
+ label: 'Edit text',
+ forward: () => editor.graph.updateNode(id, { name: 'Typing more', width: 47 }),
+ inverse: () => editor.graph.updateNode(id, { name: 'Rectangle', width: 20 })
+ })
+ await settle()
+ expect(changes.hasUnsavedChanges()).toBe(true)
+
+ editor.undoAction()
+ await settle()
+ expect(changes.hasUnsavedChanges()).toBe(false)
+ })
+})
+
+test('a save that finishes after an earlier one still returns to saved by undo', async () => {
+ await withSavedRectangle(async (editor, id, changes) => {
+ // An autosave starts, then a manual save starts after another edit; the autosave ends first.
+ editor.updateNodeWithUndo(id, { width: 30 }, 'Resize')
+ await settle()
+ const autosave = changes.capture()
+ editor.updateNodeWithUndo(id, { width: 40 }, 'Resize')
+ await settle()
+ const manualSave = changes.capture()
+ changes.markSaved(autosave)
+ changes.markSaved(manualSave)
+ expect(changes.hasUnsavedChanges()).toBe(false)
+
+ editor.updateNodeWithUndo(id, { width: 50 }, 'Resize')
+ editor.undoAction()
+ await settle()
+ expect(changes.hasUnsavedChanges()).toBe(false)
+ })
+})
+
+test('an edit made after undoing past the save leaves the saved state out of reach', async () => {
+ await withSavedRectangle(async (editor, id, changes) => {
+ editor.undoAction()
+ editor.updateNodeWithUndo(id, { width: 40 }, 'Resize')
+ editor.undoAction()
+ await settle()
+ expect(changes.hasUnsavedChanges()).toBe(true)
+ })
+})