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
23 changes: 23 additions & 0 deletions src/services/workbench/__tests__/editorService.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
18 changes: 18 additions & 0 deletions src/services/workbench/editorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ export interface IEditorService extends Component<IEditor> {
* 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
Expand Down Expand Up @@ -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);
Expand Down