Skip to content

Commit

Permalink
feat(editor): add the onOpenTab event (#416)
Browse files Browse the repository at this point in the history
* feat(editor): add the onOpenTab event

* test(editor): unit test for the onOpenTab event
  • Loading branch information
wewoor authored Sep 8, 2021
1 parent 5e68b1f commit cdbff6f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/services/workbench/__tests__/editorService.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ describe('Test EditorService', () => {
).not.toBeUndefined();
});

test('Listen to the tab opening event', () => {
const editor = new EditorService();
expectFnCalled((fun) => {
editor.onOpenTab(fun);
// Open in default Group
editor.open(mockTab);

expect(fun.mock.calls[0][0]).toEqual(mockTab);
});
});

test('Get the default editor options', () => {
const editor = new EditorService();
expect(editor.getDefaultEditorOptions()).toEqual(BuiltInEditorOptions);
Expand Down
11 changes: 11 additions & 0 deletions src/services/workbench/editorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ export interface IEditorService extends Component<IEditor> {
* @param callback
*/
onUpdateTab(callback: (tab: IEditorTab) => void): void;
/**
* Listen to the tab opening event
* @param callback
*/
onOpenTab(callback: (tab: IEditorTab) => void): void;
/**
* Listen to the tab move event
* @param callback
Expand Down Expand Up @@ -562,12 +567,18 @@ export class EditorService
groups.push(group);
}

this.emit(EditorEvent.OpenTab, tab);

this.setState({
current: group,
groups: [...groups],
});
}

public onOpenTab(callback: (tab: IEditorTab) => void): void {
this.subscribe(EditorEvent.OpenTab, callback);
}

public closeAll(groupId: number) {
const { current, groups = [] } = this.state;
const groupIndex = this.getGroupIndexById(groupId);
Expand Down

0 comments on commit cdbff6f

Please sign in to comment.