diff --git a/src/controller/editor.tsx b/src/controller/editor.tsx index edf375436..c7154af5b 100644 --- a/src/controller/editor.tsx +++ b/src/controller/editor.tsx @@ -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, diff --git a/src/services/workbench/__tests__/editorService.test.tsx b/src/services/workbench/__tests__/editorService.test.tsx index 67f9a1717..73ef5c71c 100644 --- a/src/services/workbench/__tests__/editorService.test.tsx +++ b/src/services/workbench/__tests__/editorService.test.tsx @@ -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', () => { @@ -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(); diff --git a/src/services/workbench/editorService.ts b/src/services/workbench/editorService.ts index 097c8e24d..5b1bc273b 100644 --- a/src/services/workbench/editorService.ts +++ b/src/services/workbench/editorService.ts @@ -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, @@ -41,6 +41,12 @@ export interface IEditorService extends Component { * @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 */ @@ -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); @@ -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); @@ -336,6 +344,8 @@ export class EditorService } if (group.activeTab === tab.id) { + isString(editorValue) && + this.setGroupEditorValue(group, editorValue); updatedTab = Object.assign(group.tab, tab); } }); @@ -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;