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

The cursor moves to the document header when setValue #624

Open
susan88323 opened this issue Jun 3, 2024 · 1 comment
Open

The cursor moves to the document header when setValue #624

susan88323 opened this issue Jun 3, 2024 · 1 comment

Comments

@susan88323
Copy link

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.

@feri-irawan
Copy link

Try use editor.getPosition()

const position = editor.getPosition()
editor.setValue(text)
editor.setPosition(position)

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

No branches or pull requests

2 participants