The cursor moves to the document header when setValue
Describe the bug
I am listening the onDidChangeModelContent event. When the content has been changed, I did some updates on the editor value and set the value e.g. editor.getModel().setValue(trimmedValue);. However, when the setValue is called, the cursor moves to the document header.
To Reproduce
editor.onDidChangeModelContent(() => {
const curValue = editor.getModel().getValue() || "";
if (curValue.length > maxLength) {
const trimmedValue = curValue.substring(0, maxLength);
editor.getModel().setValue(trimmedValue);
}
});
Expected behavior When setValue is called, the cursor should stay at the original position
Screenshots If applicable, add screenshots to help to explain your problem.
Desktop (please complete the following information):
- OS: Mac
- Browser: Chrome
- Version: 4.6.0 (with
monaco-editor: 0.47.0)
Additional context Add any other context about the problem here.
Try use editor.getPosition()
const position = editor.getPosition()
editor.setValue(text)
editor.setPosition(position)
This issue has been marked as stale due to inactivity. It will be closed in 7 days unless further activity occurs.
Closing due to inactivity. Feel free to reopen if needed.