Skip to content

Fix Cmd+Z clearing content for unsaved documents in the editor #247

Description

@petertzy

Problem Description

There is an issue with the Cmd+Z (Undo) behavior in the editor.

After further investigation, the issue appears to affect unsaved documents / newly created documents that have not been saved yet.

Current behavior:

  • Cmd+Z works correctly for files that have already been saved.
  • For an unsaved document, pressing Cmd+Z does not restore the previous editing state.
  • Instead, the editor content may be cleared completely.

This suggests that the issue is likely related to the initialization or state management of the Undo history for unsaved documents, rather than a general problem with the editor's Undo functionality.

Steps to Reproduce

  1. Open the editor.
  2. Create a new document that has not been saved yet.
  3. Enter some Markdown content.
  4. Make additional edits, such as adding, deleting, or modifying text.
  5. Press Cmd+Z.
  6. Observe the editor content.

Current Behavior

For an unsaved document:

  • Cmd+Z does not correctly undo the most recent edit.
  • The editor content may be cleared completely.
  • Undo behavior differs from that of an already-saved file.

For already-saved files, Cmd+Z currently works as expected.

Expected Behavior

Cmd+Z should work consistently regardless of whether the document has been saved.

For example:

  1. Create a new unsaved document.
  2. Type ABC.
  3. Type DEF.
  4. Press Cmd+ZDEF should be undone.
  5. Press Cmd+Z again → the previous edit should be undone.
  6. The editor should never clear the entire document simply because the document has not been saved yet.

Areas to Investigate

Because Undo works correctly for saved files but fails for unsaved documents, please focus on the following areas:

1. Initial State of Unsaved Documents

Check how a newly created document is initialized:

  • How is the editor value / document state initialized?
  • Are null, undefined, and empty string handled differently?
  • Is the Undo history initialized correctly?
  • Does the lack of a file path cause the new document to follow a different code path?

2. Undo History Initialization

Please verify the editor's history behavior when a new document is created:

  • Is the Undo stack initialized correctly?
  • Is the initial empty state incorrectly recorded as the only history entry?
  • Does the first user edit create a valid history entry?
  • Does Cmd+Z fall back to an incorrect reset/initialization state when the history is empty?

In particular, please check for state transitions such as:

undefined/null → ""

or:

currentContent → initialContent

that could cause Undo to resolve to an empty document.

3. Synchronization Between Document State and Editor State

Check whether the unsaved document has additional state synchronization logic, such as:

  • editor state → application state;
  • application state → editor;
  • draft state;
  • autosave state;
  • current document state.

Please verify whether the editor correctly restores the previous content on Undo, but that content is then immediately overwritten by another state synchronization process.

4. Saved vs. Unsaved Lifecycle

Compare the lifecycle of:

  • a newly created unsaved document;
  • the same document after it has been saved.

Identify the differences in initialization, state management, editor lifecycle, and history management that could explain why Undo works after saving but not before saving.

5. Editor Reinitialization

Check whether creating, switching, or saving a document causes the editor instance or its content to be reinitialized.

Please verify that these operations do not unexpectedly:

  • recreate the editor instance;
  • reset the editor content;
  • clear the Undo history;
  • initialize the editor with an empty value.

Verification Requirements

After the fix, please verify at least the following:

  1. Cmd+Z works correctly in a newly created unsaved document.
  2. Multiple edits can be undone step by step.
  3. Cmd+Z never clears the entire unsaved document unexpectedly.
  4. Deleting a large amount of content can be correctly undone.
  5. The flow Create → Edit → Undo → Save works correctly.
  6. Undo continues to work correctly for already-saved files.
  7. Switching between documents does not corrupt the Undo history.
  8. If Redo is supported, Cmd+Shift+Z continues to work correctly.
  9. Add or improve regression tests covering Undo behavior for unsaved documents.

Acceptance Criteria

  • Identify the root cause of Cmd+Z clearing content in unsaved documents.
  • Fix Undo behavior for unsaved documents.
  • Cmd+Z correctly restores the previous editing state.
  • Existing Undo behavior for saved files is not affected.
  • Saving, document switching, and editor state synchronization continue to work correctly.
  • Add appropriate regression tests.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions