File tree 2 files changed +41
-0
lines changed
2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -365,6 +365,29 @@ describe('Test EditorService', () => {
365
365
expect ( updated . name ) . toBe ( 'updateGroup' ) ;
366
366
} ) ;
367
367
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
+
368
391
test ( 'Listen to the Tab update event' , ( ) => {
369
392
const editor = new EditorService ( ) ;
370
393
expectFnCalled ( ( testFn ) => {
Original file line number Diff line number Diff line change @@ -184,6 +184,11 @@ export interface IEditorService extends Component<IEditor> {
184
184
* The instance of MonacoEditor
185
185
*/
186
186
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 ;
187
192
}
188
193
@singleton ( )
189
194
export class EditorService
@@ -480,6 +485,19 @@ export class EditorService
480
485
return groups ! . findIndex ( searchById ( id ) ) ;
481
486
}
482
487
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
+
483
501
public setActive ( groupId : number , tabId : string ) {
484
502
const { groups = [ ] } = this . state ;
485
503
const groupIndex = this . getGroupIndexById ( groupId ) ;
You can’t perform that action at this time.
0 commit comments