Skip to content

fix: In MonacoEditor the caret jumps to the start#264

Open
katsyuta wants to merge 4 commits into
DeepinkApp:masterfrom
katsyuta:228-bug-monaco-editor-caret-jump
Open

fix: In MonacoEditor the caret jumps to the start#264
katsyuta wants to merge 4 commits into
DeepinkApp:masterfrom
katsyuta:228-bug-monaco-editor-caret-jump

Conversation

@katsyuta

@katsyuta katsyuta commented Mar 17, 2026

Copy link
Copy Markdown
Collaborator

Closes #228

When the user types text, a is triggered a code to update the note text. After the note updates, the Monaco editor compares its own current editor content with the updated note text — if they differ, setValue is called to update editor content.

On Ubuntu, the note text was sometimes one character behind the editor content. This caused editor content updates to be called unnecessarily, which reset the cursor position. It is unclear why this happens on Ubuntu but not on other systems.

The fix was to replace useEffect with useLayoutEffect. It runs after React has applied the state change, but before the browser has painted the frame. At that point the browser is blocked and no new input events can arrive — the note text is to be up to date.

Fix:

monaco.mp4

Bug:

monaco_bug_2.mp4

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced the Monaco editor's value synchronization mechanism to execute at the proper timing phase during the rendering cycle and only respond when the editor's value actually changes. This resolves timing inconsistencies, eliminates unnecessary operations, and ensures the editor maintains consistent and correct state throughout all value updates and user interactions.

@coderabbitai

coderabbitai Bot commented Mar 17, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The Monaco editor's value synchronization was refactored from a useEffect without an explicit dependency array to a useLayoutEffect with a value dependency. This ensures the editor's setValue logic executes during the layout phase and only when the value prop changes, preventing unintended caret position shifts during editing.

Changes

Cohort / File(s) Summary
Monaco Editor Value Sync
packages/app/src/features/MonakoEditor/MonacoEditor.tsx
Moved value synchronization from useEffect to useLayoutEffect with explicit value dependency to fix caret jumping to start position during editing.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Suggested reviewers

  • vitonsky

Poem

🐰✨ The caret dances, no more a leap,
Layout timing runs deep,
When value changes, we synchronize right,
No more jumping to start—the editor shines bright! 🎯

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main bug fix: preventing the caret from jumping to the start of the line in MonacoEditor, which directly matches the changeset's purpose.
Linked Issues check ✅ Passed The code change directly addresses issue #228 by replacing useEffect with useLayoutEffect to prevent caret position loss during editor sync, ensuring the note text is up-to-date before setValue is called.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the caret jump issue in MonacoEditor; no unrelated modifications or out-of-scope changes are present.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@katsyuta

Copy link
Copy Markdown
Collaborator Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Mar 22, 2026

Copy link
Copy Markdown
✅ Actions performed

Full review triggered.

@katsyuta katsyuta marked this pull request as ready for review March 22, 2026 14:53
@katsyuta katsyuta closed this Mar 25, 2026
@katsyuta katsyuta force-pushed the 228-bug-monaco-editor-caret-jump branch from cb35b38 to bd30e7a Compare March 25, 2026 22:45
@katsyuta katsyuta reopened this Mar 25, 2026
@katsyuta katsyuta force-pushed the 228-bug-monaco-editor-caret-jump branch from bd30e7a to 64d3ea9 Compare April 13, 2026 12:18
@vitonsky vitonsky marked this pull request as draft April 16, 2026 10:52
@katsyuta katsyuta force-pushed the 228-bug-monaco-editor-caret-jump branch from 64d3ea9 to 323894c Compare April 24, 2026 14:25
@katsyuta katsyuta marked this pull request as ready for review April 25, 2026 20:50
@katsyuta

Copy link
Copy Markdown
Collaborator Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Apr 25, 2026

Copy link
Copy Markdown
✅ Actions performed

Full review triggered.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: In MonacoEditor the caret jumps to the start of the line while editing a note

1 participant