mirror of
https://github.com/docmost/docmost.git
synced 2026-05-20 00:14:10 +08:00
fix: cursor jumps to end of text when editing a comment (#1924)
* fix: cursor jumps to end of text when editing a comment When editing a comment mid-text, the cursor would jump to the end after every keystroke, making it impossible to insert text at any position other than the end. Root cause: on each keystroke, the comment editor's onUpdate callback updated parent state (setContent), which changed the defaultContent prop passed back to CommentEditor. A useEffect watching defaultContent then called commentEditor.commands.setContent(), which reset the entire editor content and moved the cursor to the end. Fix: - Store in-progress edits in a ref instead of state to avoid triggering React re-renders and the prop->effect->setContent cascade - Read from the ref when saving the comment - Sync the ref back into state after a successful save so the read-only view updates immediately - Guard the setContent useEffect to only run for read-only editors, so websocket-driven updates from other browsers still work Fixes #1791 Functionally tested on Firefox and Chrome: mid-text editing, saving, cross-browser live updates via websocket. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix stale content on edit cancel --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Philipinho <16838612+Philipinho@users.noreply.github.com>
This commit is contained in:
@@ -84,9 +84,14 @@ const CommentEditor = forwardRef(
|
||||
autofocus: (autofocus && "end") || false,
|
||||
});
|
||||
|
||||
// Sync content from props for read-only editors (e.g. when updated via
|
||||
// websocket on another browser). Skip for editable editors to avoid
|
||||
// resetting the cursor position on every keystroke.
|
||||
useEffect(() => {
|
||||
commentEditor.commands.setContent(defaultContent);
|
||||
}, [defaultContent]);
|
||||
if (!editable && commentEditor && defaultContent) {
|
||||
commentEditor.commands.setContent(defaultContent);
|
||||
}
|
||||
}, [defaultContent, editable, commentEditor]);
|
||||
|
||||
useEffect(() => {
|
||||
setTimeout(() => {
|
||||
|
||||
Reference in New Issue
Block a user