Skip to content

Commit cdbff6f

Browse files
authored
feat(editor): add the onOpenTab event (#416)
* feat(editor): add the onOpenTab event * test(editor): unit test for the onOpenTab event
1 parent 5e68b1f commit cdbff6f

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ describe('Test EditorService', () => {
5555
).not.toBeUndefined();
5656
});
5757

58+
test('Listen to the tab opening event', () => {
59+
const editor = new EditorService();
60+
expectFnCalled((fun) => {
61+
editor.onOpenTab(fun);
62+
// Open in default Group
63+
editor.open(mockTab);
64+
65+
expect(fun.mock.calls[0][0]).toEqual(mockTab);
66+
});
67+
});
68+
5869
test('Get the default editor options', () => {
5970
const editor = new EditorService();
6071
expect(editor.getDefaultEditorOptions()).toEqual(BuiltInEditorOptions);

src/services/workbench/editorService.ts

+11
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ export interface IEditorService extends Component<IEditor> {
9494
* @param callback
9595
*/
9696
onUpdateTab(callback: (tab: IEditorTab) => void): void;
97+
/**
98+
* Listen to the tab opening event
99+
* @param callback
100+
*/
101+
onOpenTab(callback: (tab: IEditorTab) => void): void;
97102
/**
98103
* Listen to the tab move event
99104
* @param callback
@@ -562,12 +567,18 @@ export class EditorService
562567
groups.push(group);
563568
}
564569

570+
this.emit(EditorEvent.OpenTab, tab);
571+
565572
this.setState({
566573
current: group,
567574
groups: [...groups],
568575
});
569576
}
570577

578+
public onOpenTab(callback: (tab: IEditorTab) => void): void {
579+
this.subscribe(EditorEvent.OpenTab, callback);
580+
}
581+
571582
public closeAll(groupId: number) {
572583
const { current, groups = [] } = this.state;
573584
const groupIndex = this.getGroupIndexById(groupId);

0 commit comments

Comments
 (0)