Skip to content

Commit 82147d7

Browse files
committed
fix: restore cursor position
1 parent 128ac0f commit 82147d7

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

src/features/MonakoEditor/MonacoEditor.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,20 @@ 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();
276278
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+
}
277290
}
278291
});
279292

0 commit comments

Comments
 (0)