We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 128ac0f commit 82147d7Copy full SHA for 82147d7
1 file changed
src/features/MonakoEditor/MonacoEditor.tsx
@@ -273,7 +273,20 @@ export const MonacoEditor = ({
273
if (!editor) return;
274
275
if (editor.getValue() !== value) {
276
+ // Save the cursor position before calling setValue, because it resets the cursor to 0:0
277
+ const position = editor.getPosition();
278
editor.setValue(value);
279
+
280
+ if (position) {
281
+ const model = editor.getModel();
282
+ if (!model) return;
283
284
+ // Keep the cursor within valid bounds in case the new value is shorter than the previous one
285
+ const line = Math.min(position.lineNumber, model.getLineCount());
286
+ const column = Math.min(position.column, model.getLineMaxColumn(line));
287
288
+ editor.setPosition({ lineNumber: line, column });
289
+ }
290
}
291
});
292
0 commit comments