Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/controller/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ export class EditorController extends Controller implements IEditorController {
const tab = current?.tab;
if (!tab) return;

const currentEditorUri = current.editorInstance?.getModel()?.uri;
const updateEditorUri = editorInstance?.getModel()?.uri;
if (currentEditorUri?.path !== updateEditorUri?.path) return;

const newValue = editorInstance.getModel()?.getValue();
const updatedTab = {
...tab,
Expand Down
34 changes: 34 additions & 0 deletions src/services/workbench/__tests__/editorService.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { container } from 'tsyringe';
import { EditorEvent, IEditorTab } from 'mo/model';
import { expectFnCalled } from '@test/utils';
import { modules } from 'mo/services/builtinService/const';
import { editor as MonacoEditor } from 'mo/monaco';
import { cloneDeep } from 'lodash';

describe('Test EditorService', () => {
Expand Down Expand Up @@ -155,6 +156,39 @@ describe('Test EditorService', () => {
).toBe('updated1');
});

test('Set the editor text for a specific group', () => {
const editor = new EditorService();
const tabData = {
...mockTab,
data: {
value: 'tabData',
},
};
editor.open(tabData);
const { groups } = editor.getState();
if (groups) {
const modifyText = 'newValue';
const editorData = {
value: '',
};
const editorInstance = {} as MonacoEditor.IStandaloneCodeEditor;
editorInstance.getModel = jest.fn(() => {
return {
getValue: () => editorData.value,
setValue: (newValue: string) => {
editorData.value = newValue;
},
} as MonacoEditor.ITextModel;
});

groups[0].editorInstance = editorInstance;
editor.setGroupEditorValue(groups[0], modifyText);
expect(groups[0].editorInstance?.getModel()?.getValue()).toBe(
modifyText
);
}
});

test('Close a tab', () => {
const editor: any = new EditorService();
editor.disposeModel = jest.fn();
Expand Down
25 changes: 23 additions & 2 deletions src/services/workbench/editorService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'reflect-metadata';
import { singleton, container } from 'tsyringe';
import cloneDeep from 'lodash/cloneDeep';
import { cloneDeep, isString } from 'lodash';
import { Component } from 'mo/react';
import {
EditorModel,
Expand Down Expand Up @@ -41,6 +41,12 @@ export interface IEditorService extends Component<IEditor> {
* @param groupId
*/
updateTab(tab: IEditorTab, groupId?: UniqueId): IEditorTab;
/**
* Updates the editor content for a specific group
* @param group The editorInstance is required
* @param value
*/
setGroupEditorValue(group: IEditorGroup, value: string): void;
/**
* Specify the Entry page of Workbench
*/
Expand Down Expand Up @@ -308,6 +314,7 @@ export class EditorService

public updateTab(tab: IEditorTab, groupId?: UniqueId): IEditorTab {
let updatedTab;
const editorValue = tab?.data?.value;
if (groupId) {
const group = this.getGroupById(groupId);

Expand All @@ -317,8 +324,9 @@ export class EditorService
if (tabData) {
updatedTab = Object.assign(tabData, tab);
}

if (group.activeTab === tab.id) {
isString(editorValue) &&
this.setGroupEditorValue(group, editorValue);
updatedTab = Object.assign(group.tab, tab);
}
this.updateGroup(groupId, group);
Expand All @@ -336,6 +344,8 @@ export class EditorService
}

if (group.activeTab === tab.id) {
isString(editorValue) &&
this.setGroupEditorValue(group, editorValue);
updatedTab = Object.assign(group.tab, tab);
}
});
Expand All @@ -349,9 +359,20 @@ export class EditorService
groups,
});
}

return updatedTab;
}

public setGroupEditorValue(group: IEditorGroup, value: string) {
const model = group.editorInstance?.getModel();
if (!model) return;

const currentValue = model?.getValue();
if (currentValue !== value) {
model?.setValue(value);
}
}

public closeTab(tabId: UniqueId, groupId: UniqueId) {
const groupIndex = this.getGroupIndexById(groupId);
if (groupIndex === -1) return;
Expand Down