Rich text editor: the first undo after opening or switching to rich text deletes the whole document
Summary
In the rich text editor the first undo (Cmd+Z, Ctrl+Z) removes the entire document in one step when the editor has just loaded its content: right after opening a field that is already in rich text mode, and after every switch from plain text back to rich text. Redo restores it only while nothing has been typed since; typing after the wipe truncates the redo stack and the content is gone. If the author starts typing within half a second of the load, one undo removes the typing and the document together.
Steps to reproduce
- Open an issue, merge request or comment whose field has content, with the rich text editor active (or switch from plain text to rich text).
- Press Cmd+Z (Ctrl+Z on Windows and Linux) once, without editing first.
- Alternative: type in rich text, switch to plain text, switch back to rich text, press Cmd+Z once.
Example Project
Reproducible on GitLab.com in any issue, merge request or comment description.
What is the current bug behavior?
The whole document disappears in a single undo step.
What is the expected correct behavior?
Undo reverts only the author's own edits; the first undo after a load does nothing.
Root cause
app/assets/javascripts/content_editor/services/content_editor.js loads the document with editor.state.tr.replaceWith(0, doc.content.size, document) and only setMeta('preventUpdate', true); nothing marks the transaction addToHistory: false, so prosemirror-history records the load as an undoable step. app/assets/javascripts/vue_shared/components/markdown/markdown_editor.vue renders the plain text field and the rich text editor with v-if and v-else, so every switch to rich text mounts a fresh editor whose only history entry is that load. Related: Rich text <-> Markdown source toggle collapses undo history to one step (607833) (closed by the reporter as a nice-to-have) and When using undo (Cmd+Z), Content Editor removes the entire content (390831) (closed 2023 as undo grouping). Fix: !254214 (merged).
Fix: !254214 (merged)