Skip to content

Commit 44c2a7b

Browse files
authored
feat: support to get groupId via tabId (#431)
1 parent b3f6be6 commit 44c2a7b

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/services/workbench/__tests__/editorService.test.tsx

+23
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,29 @@ describe('Test EditorService', () => {
365365
expect(updated.name).toBe('updateGroup');
366366
});
367367

368+
test('Should support to get the groupId via tab', () => {
369+
const editor = new EditorService();
370+
editor.open(mockTab);
371+
372+
let groupId = editor.getGroupIdByTab(mockTab.id!);
373+
expect(groupId).toBe(1);
374+
375+
groupId = editor.getGroupIdByTab('non-exist');
376+
expect(groupId).toBeNull();
377+
378+
editor.open(mockTab, 2);
379+
const groups = editor.getState().groups;
380+
expect(groups).toHaveLength(2);
381+
// To be sure mockTab is opened both in two groups
382+
expect(
383+
groups!.every((group) => editor.getTabById(mockTab.id!, group))
384+
).toBeTruthy();
385+
386+
// Only get the first one
387+
groupId = editor.getGroupIdByTab(mockTab.id!);
388+
expect(groupId).toBe(1);
389+
});
390+
368391
test('Listen to the Tab update event', () => {
369392
const editor = new EditorService();
370393
expectFnCalled((testFn) => {

src/services/workbench/editorService.ts

+18
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ export interface IEditorService extends Component<IEditor> {
184184
* The instance of MonacoEditor
185185
*/
186186
readonly editorInstance: MonacoEditor.IStandaloneCodeEditor;
187+
/**
188+
* Get the group's id which contains the tab
189+
* @param tabId
190+
*/
191+
getGroupIdByTab(tabId: string): number | null;
187192
}
188193
@singleton()
189194
export class EditorService
@@ -480,6 +485,19 @@ export class EditorService
480485
return groups!.findIndex(searchById(id));
481486
}
482487

488+
public getGroupIdByTab(tabId: string) {
489+
const { groups = [] } = this.state;
490+
const isOpened = this.isOpened(tabId, groups);
491+
if (isOpened) {
492+
const targetGroup = groups.find((group) =>
493+
this.getTabById(tabId, group)
494+
)!;
495+
return targetGroup.id!;
496+
} else {
497+
return null;
498+
}
499+
}
500+
483501
public setActive(groupId: number, tabId: string) {
484502
const { groups = [] } = this.state;
485503
const groupIndex = this.getGroupIndexById(groupId);

0 commit comments

Comments
 (0)