diff --git a/src/services/workbench/__tests__/editorService.test.tsx b/src/services/workbench/__tests__/editorService.test.tsx index 53f824b78..3ebb307fc 100644 --- a/src/services/workbench/__tests__/editorService.test.tsx +++ b/src/services/workbench/__tests__/editorService.test.tsx @@ -365,6 +365,29 @@ describe('Test EditorService', () => { expect(updated.name).toBe('updateGroup'); }); + test('Should support to get the groupId via tab', () => { + const editor = new EditorService(); + editor.open(mockTab); + + let groupId = editor.getGroupIdByTab(mockTab.id!); + expect(groupId).toBe(1); + + groupId = editor.getGroupIdByTab('non-exist'); + expect(groupId).toBeNull(); + + editor.open(mockTab, 2); + const groups = editor.getState().groups; + expect(groups).toHaveLength(2); + // To be sure mockTab is opened both in two groups + expect( + groups!.every((group) => editor.getTabById(mockTab.id!, group)) + ).toBeTruthy(); + + // Only get the first one + groupId = editor.getGroupIdByTab(mockTab.id!); + expect(groupId).toBe(1); + }); + test('Listen to the Tab update event', () => { const editor = new EditorService(); expectFnCalled((testFn) => { diff --git a/src/services/workbench/editorService.ts b/src/services/workbench/editorService.ts index 8d5761cbd..23a91209b 100644 --- a/src/services/workbench/editorService.ts +++ b/src/services/workbench/editorService.ts @@ -184,6 +184,11 @@ export interface IEditorService extends Component { * The instance of MonacoEditor */ readonly editorInstance: MonacoEditor.IStandaloneCodeEditor; + /** + * Get the group's id which contains the tab + * @param tabId + */ + getGroupIdByTab(tabId: string): number | null; } @singleton() export class EditorService @@ -480,6 +485,19 @@ export class EditorService return groups!.findIndex(searchById(id)); } + public getGroupIdByTab(tabId: string) { + const { groups = [] } = this.state; + const isOpened = this.isOpened(tabId, groups); + if (isOpened) { + const targetGroup = groups.find((group) => + this.getTabById(tabId, group) + )!; + return targetGroup.id!; + } else { + return null; + } + } + public setActive(groupId: number, tabId: string) { const { groups = [] } = this.state; const groupIndex = this.getGroupIndexById(groupId);