Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(module:code-editor): memorize cursor position and selections #6044

Merged
merged 1 commit into from
Nov 16, 2020

Conversation

Airblader
Copy link
Collaborator

Calling #setValue() on a monaco editor instance causes the mouse cursor to jump to the
beginning of the document, which is undesirable for a ControlValueAccessor implementation
which should only update the content. Also see
microsoft/monaco-editor#1397 for more information.

Unfortunately, using #executeEdits() doesn't really work as a replacement because it will
mess with selections. Therefore we memorize and restore the cursor position before/after
setting the value. Since #setValue() also causes a selection to be lost we do the same for
those. Restoring selections may produce slightly unexpected results if the selection covers
part of the content that has been changed, but is still preferable to losing the selection
altogether.

fixes #6038

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[x] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Application (the showcase website) / infrastructure changes
[ ] Other... Please describe:

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Calling #setValue() on a monaco editor instance causes the mouse cursor to jump to the
beginning of the document, which is undesirable for a ControlValueAccessor implementation
which should only update the content. Also see
microsoft/monaco-editor#1397 for more information.

Unfortunately, using #executeEdits() doesn't really work as a replacement because it will
mess with selections. Therefore we memorize and restore the cursor position before/after
setting the value. Since #setValue() also causes a selection to be lost we do the same for
those. Restoring selections may produce slightly unexpected results if the selection covers
part of the content that has been changed, but is still preferable to losing the selection
altogether.

fixes NG-ZORRO#6038
@Airblader
Copy link
Collaborator Author

CC @sorahn

@codecov
Copy link

codecov bot commented Nov 11, 2020

Codecov Report

Merging #6044 (9a1ec8c) into master (679be27) will decrease coverage by 0.04%.
The diff coverage is 18.75%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #6044      +/-   ##
==========================================
- Coverage   89.95%   89.90%   -0.05%     
==========================================
  Files         465      465              
  Lines       14042    14055      +13     
  Branches     2307     2310       +3     
==========================================
+ Hits        12631    12636       +5     
- Misses        863      872       +9     
+ Partials      548      547       -1     
Impacted Files Coverage Δ
components/code-editor/code-editor.component.ts 14.78% <18.75%> (+1.05%) ⬆️
components/core/util/text-measure.ts 91.30% <0.00%> (+1.73%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 679be27...9a1ec8c. Read the comment docs.

@hsuanxyz hsuanxyz merged commit 84f520d into NG-ZORRO:master Nov 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

code-editor jumps mouse cursor when model changes
2 participants