Skip to content

Commit c1c34e2

Browse files
mortalYoungmumiao
authored andcommitted
fix: improve editor tabs change
1 parent a9d2bcf commit c1c34e2

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/components/monaco/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export class MonacoEditor extends PureComponent<IMonacoEditorProps> {
5858

5959
componentDidUpdate(prevProps) {
6060
const { onChangeEditorProps } = this.props;
61+
// TODO: Functions are compared by strict equality
6162
!isEqual(prevProps, this.props) &&
6263
onChangeEditorProps?.(prevProps, this.props);
6364
}

src/controller/editor.tsx

+24-17
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class EditorController extends Controller implements IEditorController {
113113
const { current } = this.editorService.getState();
114114
if (current) {
115115
const model = current?.editorInstance?.getModel();
116-
const newValue = model.getValue();
116+
const newValue = current.tab?.data.value || '';
117117
current?.editorInstance?.executeEdits('update-value', [
118118
{
119119
range: model.getFullModelRange(),
@@ -164,6 +164,9 @@ export class EditorController extends Controller implements IEditorController {
164164
this.emit(EditorEvent.OnSelectTab, tabId, groupId);
165165
};
166166

167+
/**
168+
* Called when open a new group
169+
*/
167170
public onUpdateEditorIns = (
168171
editorInstance: IStandaloneCodeEditor,
169172
groupId: number
@@ -178,7 +181,7 @@ export class EditorController extends Controller implements IEditorController {
178181

179182
const { current } = this.editorService.getState();
180183
const tab = current?.tab;
181-
this.openFile(
184+
this.openTab(
182185
editorInstance,
183186
tab?.id!,
184187
tab?.data?.value!,
@@ -241,6 +244,9 @@ export class EditorController extends Controller implements IEditorController {
241244
});
242245
}
243246

247+
/**
248+
* Called when Editor props changed
249+
*/
244250
public onChangeEditorProps = (
245251
prevProps: IMonacoEditorProps,
246252
props: IMonacoEditorProps
@@ -253,7 +259,7 @@ export class EditorController extends Controller implements IEditorController {
253259
prevProps.path,
254260
editorInstance?.saveViewState()
255261
);
256-
this.openFile(
262+
this.openTab(
257263
editorInstance,
258264
path!,
259265
options?.value!,
@@ -262,32 +268,33 @@ export class EditorController extends Controller implements IEditorController {
262268
}
263269
};
264270

265-
private openFile(
271+
/**
272+
* Open a tab via instance.
273+
* Actually, one tab to one Model, so that
274+
* - the action to open a exist tab equals to switch the model in instance
275+
* - the action to open a new tab equals to create a new model in instance
276+
*/
277+
private openTab(
266278
editorInstance: IStandaloneCodeEditor,
267279
path: string,
268280
value: string,
269281
language: string
270282
) {
271-
this.initializeFile(path, value, language);
272-
const model = monacoEditor.getModel(Uri.parse(path));
273-
editorInstance.setModel(model!);
274-
// Restore the editor state for the file
283+
let model = monacoEditor.getModel(Uri.parse(path));
284+
if (!model) {
285+
model = monacoEditor.createModel(value, language, Uri.parse(path));
286+
}
287+
// 1. switch model
288+
editorInstance.setModel(model);
289+
// 2. Restore view state
275290
const editorState = this.editorStates.get(path);
276291
if (editorState) {
292+
// viewState contains: scroller info, cursor info, contributions info
277293
editorInstance.restoreViewState(editorState);
278294
}
279295
editorInstance?.focus();
280296
}
281297

282-
private initializeFile(path: string, value: string, language: string) {
283-
let model = monacoEditor.getModel(Uri.parse(path));
284-
if (model) {
285-
model.setValue(value);
286-
} else {
287-
model = monacoEditor.createModel(value, language, Uri.parse(path));
288-
}
289-
}
290-
291298
private updateStatusBar(editorInstance: IStandaloneCodeEditor) {
292299
if (editorInstance) {
293300
const model:

0 commit comments

Comments
 (0)