Skip to content

Commit 2b9002b

Browse files
authored
fix: the value still in when file reopen (#228)
* fix: the value still in when file reopen * fix: extract model dispose to a function
1 parent 3210191 commit 2b9002b

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/controller/editor.tsx

-20
Original file line numberDiff line numberDiff line change
@@ -109,46 +109,26 @@ export class EditorController extends Controller implements IEditorController {
109109
this.emit(EditorEvent.OnCloseAll, groupId);
110110
};
111111

112-
public updateCurrentValue = () => {
113-
const { current } = this.editorService.getState();
114-
if (current) {
115-
const model = current?.editorInstance?.getModel();
116-
const newValue = current.tab?.data.value || '';
117-
current?.editorInstance?.executeEdits('update-value', [
118-
{
119-
range: model.getFullModelRange(),
120-
text: newValue,
121-
forceMoveMarkers: true,
122-
},
123-
]);
124-
current?.editorInstance?.focus();
125-
}
126-
};
127-
128112
public onCloseTab = (tabId?: string, groupId?: number) => {
129113
if (tabId && groupId) {
130114
this.editorService.closeTab(tabId, groupId);
131115
this.explorerService.forceUpdate();
132-
this.updateCurrentValue();
133116
this.emit(EditorEvent.OnCloseTab, tabId, groupId);
134117
}
135118
};
136119

137120
public onCloseToRight = (tabItem: IEditorTab, groupId: number) => {
138121
this.editorService.closeToRight(tabItem, groupId);
139-
this.updateCurrentValue();
140122
this.emit(EditorEvent.OnCloseToRight, tabItem, groupId);
141123
};
142124

143125
public onCloseToLeft = (tabItem: IEditorTab, groupId: number) => {
144126
this.editorService.closeToLeft(tabItem, groupId);
145-
this.updateCurrentValue();
146127
this.emit(EditorEvent.OnCloseToLeft, tabItem, groupId);
147128
};
148129

149130
public onCloseOthers = (tabItem: IEditorTab, groupId: number) => {
150131
this.editorService.closeOthers(tabItem, groupId);
151-
this.updateCurrentValue();
152132
this.emit(EditorEvent.OnCloseOthers, tabItem, groupId);
153133
};
154134

src/services/workbench/editorService.ts

+26
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
EditorEvent,
1212
} from 'mo/model';
1313
import { searchById } from '../helper';
14+
import { editor as monacoEditor, Uri } from 'mo/monaco';
1415

1516
export interface IEditorService extends Component<IEditor> {
1617
/**
@@ -72,6 +73,13 @@ export class EditorService
7273
this.state = container.resolve(EditorModel);
7374
}
7475

76+
private disposeModel(tabs: IEditorTab | IEditorTab[]) {
77+
const arr = Array.isArray(tabs) ? tabs : [tabs];
78+
arr.forEach((tab) => {
79+
monacoEditor.getModel(Uri.parse(tab.id!))?.dispose();
80+
});
81+
}
82+
7583
public setEntry(component: React.ReactNode) {
7684
this.setState({
7785
entry: component,
@@ -126,6 +134,9 @@ export class EditorService
126134
// so delete group and choose last or former group as current one
127135
const activeGroup =
128136
nextGroups[groupIndex + 1] || nextGroups[groupIndex - 1];
137+
138+
// the model of closed tab should be disposed after closing
139+
this.disposeModel(nextGroup.data![tabIndex]);
129140
nextGroups.splice(groupIndex, 1);
130141

131142
this.setState({
@@ -144,6 +155,8 @@ export class EditorService
144155
nextGroup.activeTab = nextTab?.id;
145156
}
146157

158+
this.disposeModel(nextGroup.data![tabIndex]);
159+
147160
nextGroup.data!.splice(tabIndex, 1);
148161
nextGroups[groupIndex] = nextGroup;
149162

@@ -164,6 +177,10 @@ export class EditorService
164177
const nextTabData = nextGroup.data!;
165178

166179
const updateTabs = nextTabData!.filter(searchById(tabId));
180+
// tab data is unlikely to be large enough to affect exec time, so we filter twice for maintainability
181+
const removedTabs = nextTabData!.filter((item) => item.id !== tabId);
182+
183+
this.disposeModel(removedTabs);
167184

168185
this.updateGroup(groupId, {
169186
data: updateTabs,
@@ -185,6 +202,9 @@ export class EditorService
185202
if (tabIndex <= -1) return;
186203

187204
const updateTabs = nextTabData?.slice(0, tabIndex + 1);
205+
const removedTabs = nextTabData?.slice(tabIndex + 1);
206+
207+
removedTabs && this.disposeModel(removedTabs);
188208

189209
this.updateGroup(groupId, {
190210
data: updateTabs,
@@ -206,6 +226,9 @@ export class EditorService
206226
if (tabIndex <= -1) return;
207227

208228
const updateTabs = nextTabData?.slice(tabIndex, nextTabData.length);
229+
const removedTabs = nextTabData?.slice(0, tabIndex);
230+
231+
this.disposeModel(removedTabs || []);
209232

210233
this.updateGroup(groupId, {
211234
data: updateTabs,
@@ -318,6 +341,9 @@ export class EditorService
318341
const nextGroups = [...groups];
319342
let nextCurrentGroup = current;
320343

344+
// dispose all models in specific group
345+
this.disposeModel(nextGroups[groupIndex].data || []);
346+
321347
nextGroups.splice(groupIndex, 1);
322348

323349
if (current && current.id === groupId) {

0 commit comments

Comments
 (0)