Skip to content

Commit 695cb94

Browse files
committed
feat(codemirror): migrate openFile thing
1 parent 530df85 commit 695cb94

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/lib/openFile.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ export default async function openFile(file, options = {}) {
3939

4040
if (existingFile) {
4141
// If file is already opened and new text is provided
42-
const existingText = existingFile.session.getValue();
43-
const existingCursorPos = existingFile.session.selection.getCursor();
42+
const existingText = existingFile.session.doc.toString() ?? "";
4443

4544
// If file is already opened
4645
existingFile.makeActive();
4746

47+
const { editor } = editorManager;
48+
4849
if (onsave) {
4950
existingFile.onsave = onsave;
5051
}
@@ -57,20 +58,24 @@ export default async function openFile(file, options = {}) {
5758
// }
5859
// if (confirmation) {
5960
// }
60-
existingFile.session.setValue(text);
61+
editor.dispatch({
62+
changes: {
63+
from: 0,
64+
to: editor.state.doc.length,
65+
insert: String(text),
66+
},
67+
});
6168
}
6269

63-
if (
64-
cursorPos &&
65-
existingCursorPos &&
66-
existingCursorPos.row !== cursorPos.row &&
67-
existingCursorPos.column !== cursorPos.column
68-
) {
69-
existingFile.session.selection.moveCursorTo(
70-
cursorPos.row,
71-
cursorPos.column,
72-
);
73-
}
70+
// Move cursor if requested and different
71+
try {
72+
if (cursorPos) {
73+
const cur = editor.getCursorPosition();
74+
if (cur.row !== cursorPos.row || cur.column !== cursorPos.column) {
75+
editor.gotoLine(cursorPos.row, cursorPos.column);
76+
}
77+
}
78+
} catch (_) {}
7479

7580
if (encoding && existingFile.encoding !== encoding) {
7681
reopenWithNewEncoding(encoding);

0 commit comments

Comments
 (0)