monaco-react icon indicating copy to clipboard operation
monaco-react copied to clipboard

The cursor moves to the document header when setValue

Open susan88323 opened this issue 1 year ago • 1 comments

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.

susan88323 avatar Jun 03 '24 18:06 susan88323

Try use editor.getPosition()

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

feri-irawan avatar Jun 09 '24 05:06 feri-irawan

This issue has been marked as stale due to inactivity. It will be closed in 7 days unless further activity occurs.

github-actions[bot] avatar Feb 18 '25 00:02 github-actions[bot]

Closing due to inactivity. Feel free to reopen if needed.

github-actions[bot] avatar Feb 26 '25 00:02 github-actions[bot]