Skip to content

Commit

Permalink
fix: the value still in when file reopen (#228)
Browse files Browse the repository at this point in the history
* fix: the value still in when file reopen

* fix: extract model dispose to a function
  • Loading branch information
mortalYoung authored Jul 8, 2021
1 parent 3210191 commit 2b9002b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
20 changes: 0 additions & 20 deletions src/controller/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,46 +109,26 @@ export class EditorController extends Controller implements IEditorController {
this.emit(EditorEvent.OnCloseAll, groupId);
};

public updateCurrentValue = () => {
const { current } = this.editorService.getState();
if (current) {
const model = current?.editorInstance?.getModel();
const newValue = current.tab?.data.value || '';
current?.editorInstance?.executeEdits('update-value', [
{
range: model.getFullModelRange(),
text: newValue,
forceMoveMarkers: true,
},
]);
current?.editorInstance?.focus();
}
};

public onCloseTab = (tabId?: string, groupId?: number) => {
if (tabId && groupId) {
this.editorService.closeTab(tabId, groupId);
this.explorerService.forceUpdate();
this.updateCurrentValue();
this.emit(EditorEvent.OnCloseTab, tabId, groupId);
}
};

public onCloseToRight = (tabItem: IEditorTab, groupId: number) => {
this.editorService.closeToRight(tabItem, groupId);
this.updateCurrentValue();
this.emit(EditorEvent.OnCloseToRight, tabItem, groupId);
};

public onCloseToLeft = (tabItem: IEditorTab, groupId: number) => {
this.editorService.closeToLeft(tabItem, groupId);
this.updateCurrentValue();
this.emit(EditorEvent.OnCloseToLeft, tabItem, groupId);
};

public onCloseOthers = (tabItem: IEditorTab, groupId: number) => {
this.editorService.closeOthers(tabItem, groupId);
this.updateCurrentValue();
this.emit(EditorEvent.OnCloseOthers, tabItem, groupId);
};

Expand Down
26 changes: 26 additions & 0 deletions src/services/workbench/editorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
EditorEvent,
} from 'mo/model';
import { searchById } from '../helper';
import { editor as monacoEditor, Uri } from 'mo/monaco';

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

private disposeModel(tabs: IEditorTab | IEditorTab[]) {
const arr = Array.isArray(tabs) ? tabs : [tabs];
arr.forEach((tab) => {
monacoEditor.getModel(Uri.parse(tab.id!))?.dispose();
});
}

public setEntry(component: React.ReactNode) {
this.setState({
entry: component,
Expand Down Expand Up @@ -126,6 +134,9 @@ export class EditorService
// so delete group and choose last or former group as current one
const activeGroup =
nextGroups[groupIndex + 1] || nextGroups[groupIndex - 1];

// the model of closed tab should be disposed after closing
this.disposeModel(nextGroup.data![tabIndex]);
nextGroups.splice(groupIndex, 1);

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

this.disposeModel(nextGroup.data![tabIndex]);

nextGroup.data!.splice(tabIndex, 1);
nextGroups[groupIndex] = nextGroup;

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

const updateTabs = nextTabData!.filter(searchById(tabId));
// tab data is unlikely to be large enough to affect exec time, so we filter twice for maintainability
const removedTabs = nextTabData!.filter((item) => item.id !== tabId);

this.disposeModel(removedTabs);

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

const updateTabs = nextTabData?.slice(0, tabIndex + 1);
const removedTabs = nextTabData?.slice(tabIndex + 1);

removedTabs && this.disposeModel(removedTabs);

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

const updateTabs = nextTabData?.slice(tabIndex, nextTabData.length);
const removedTabs = nextTabData?.slice(0, tabIndex);

this.disposeModel(removedTabs || []);

this.updateGroup(groupId, {
data: updateTabs,
Expand Down Expand Up @@ -318,6 +341,9 @@ export class EditorService
const nextGroups = [...groups];
let nextCurrentGroup = current;

// dispose all models in specific group
this.disposeModel(nextGroups[groupIndex].data || []);

nextGroups.splice(groupIndex, 1);

if (current && current.id === groupId) {
Expand Down

0 comments on commit 2b9002b

Please sign in to comment.