Fix cursor jump when typing in markdown list items (#658)

The debounced onUpdate serialization feeds back through the content prop,
and if the user types between the debounce firing and the useEffect
running, the stale prop triggers setContent — resetting the cursor.
Skip the destructive setContent when the content prop matches our own
last serialization (lastCommittedMarkdownRef).
This commit is contained in:
Jinjing 2026-04-14 16:09:49 -07:00 committed by GitHub
parent e7f973339a
commit 92353e7233
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -443,6 +443,17 @@ export default function RichMarkdownEditor({
return
}
// Why: the debounced onUpdate serializes the editor and feeds it back
// through onContentChange → editorDrafts → the content prop. If the
// user typed between the debounce firing and this effect running, the
// editor already contains newer content than the prop. Comparing
// against lastCommittedMarkdownRef (which is set in the same tick as
// onContentChange) lets us recognise our own serialization and skip the
// destructive setContent that would reset the cursor mid-typing.
if (content === lastCommittedMarkdownRef.current) {
return
}
const currentMarkdown = editor.getMarkdown()
if (currentMarkdown === content) {
return