Skip to content

Commit 2c5958a

Browse files
committed
refactor: replace setValue
1 parent 82147d7 commit 2c5958a

1 file changed

Lines changed: 9 additions & 14 deletions

File tree

src/features/MonakoEditor/MonacoEditor.tsx

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,20 +273,15 @@ export const MonacoEditor = ({
273273
if (!editor) return;
274274

275275
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-
}
276+
const model = editor.getModel();
277+
if (!model) return;
278+
279+
// Update content without resetting cursor position
280+
model.pushEditOperations(
281+
[],
282+
[{ range: model.getFullModelRange(), text: value }],
283+
() => null,
284+
);
290285
}
291286
});
292287

0 commit comments

Comments
 (0)