@@ -2,6 +2,8 @@ import { TabGroup } from "@/browser/tabs/objects/tab-group";
22import { TabFolder } from "@/browser/tabs/objects/tab-containers/tab-folder" ;
33import { TypedEventEmitter } from "@/modules/typed-event-emitter" ;
44import { NormalTabGroup } from "@/browser/tabs/objects/tab-group/types/normal" ;
5+ import { TabbedBrowserWindow } from "@/browser/window" ;
6+ import { Browser } from "@/browser/browser" ;
57
68// Container Child //
79type TabGroupChild = {
@@ -35,6 +37,13 @@ export type ExportedTabsFolder = {
3537// ExportedTabContainer would not be a child: it should only be the root container.
3638type ExportedTabData = ExportedTabGroup | ExportedTabsFolder ;
3739
40+ // Tab Container Shared Data //
41+ export interface TabContainerSharedData {
42+ browser : Browser ;
43+ window : TabbedBrowserWindow ;
44+ space : string ;
45+ }
46+
3847// Base Tab Container //
3948export type BaseTabContainerEvents = {
4049 "child-added" : [ child : ContainerChild ] ;
@@ -47,34 +56,32 @@ export class BaseTabContainer<
4756> extends TypedEventEmitter < TEvents > {
4857 public children : ContainerChild [ ] ;
4958
50- constructor ( ) {
59+ public sharedData : TabContainerSharedData ;
60+
61+ constructor ( sharedData : TabContainerSharedData ) {
5162 super ( ) ;
5263
5364 this . children = [ ] ;
65+ this . sharedData = sharedData ;
5466 }
5567
56- public getAllTabGroups ( ) : TabGroup [ ] {
57- const scanTabContainer = ( container : BaseTabContainer ) : TabGroup [ ] => {
58- return container . children . flatMap ( ( child ) => {
59- if ( child . type === "tab-group" ) {
60- return [ child . item ] ;
61- }
62- if ( child . type === "tab-folder" ) {
63- return scanTabContainer ( child . item ) ;
64- }
65- return [ ] ;
66- } ) ;
67- } ;
68-
69- return scanTabContainer ( this ) ;
70- }
68+ // Modify Children //
7169
72- public addChild ( child : ContainerChild ) : void {
70+ /**
71+ * Add a child to the container.
72+ * @param child - The child to add.
73+ */
74+ private addChild ( child : ContainerChild ) : void {
7375 this . children . push ( child ) ;
7476 this . emit ( "child-added" , child ) ;
7577 }
7678
77- public removeChild ( child : ContainerChild ) : boolean {
79+ /**
80+ * Remove a child from the container.
81+ * @param child - The child to remove.
82+ * @returns Whether the child was removed.
83+ */
84+ private removeChild ( child : ContainerChild ) : boolean {
7885 const index = this . children . indexOf ( child ) ;
7986 if ( index === - 1 ) return false ;
8087
@@ -83,6 +90,12 @@ export class BaseTabContainer<
8390 return true ;
8491 }
8592
93+ /**
94+ * Move a child to a new index.
95+ * @param from - The index to move from.
96+ * @param to - The index to move to.
97+ * @returns Whether the child was moved.
98+ */
8699 public moveChild ( from : number , to : number ) : boolean {
87100 if ( from < 0 || from >= this . children . length || to < 0 || to >= this . children . length ) {
88101 return false ;
@@ -94,6 +107,8 @@ export class BaseTabContainer<
94107 return true ;
95108 }
96109
110+ // Get Children //
111+
97112 public findChildrenByType ( type : "tab-group" | "tab-container" ) : ContainerChild [ ] {
98113 return this . children . filter ( ( child ) => child . type === type ) ;
99114 }
@@ -106,22 +121,48 @@ export class BaseTabContainer<
106121 return this . children . length ;
107122 }
108123
124+ // New Children //
125+
126+ public newTabFolder ( name : string ) : TabFolder {
127+ const folder = new TabFolder ( name , this ) ;
128+ this . addChild ( { type : "tab-folder" , item : folder } ) ;
129+ return folder ;
130+ }
131+
109132 public newNormalTabGroup ( ) : NormalTabGroup {
110133 const tabGroup = new NormalTabGroup ( {
111- browser : this . browser ,
112- window : this . window ,
113- space : this . space
134+ browser : this . sharedData . browser ,
135+ window : this . sharedData . window ,
136+ space : this . sharedData . space
114137 } ) ;
115138 this . addChild ( { type : "tab-group" , item : tabGroup } ) ;
116139 return tabGroup ;
117140 }
118141
119- public createTabFolder ( name : string ) : TabFolder {
120- const folder = new TabFolder ( name ) ;
121- this . addChild ( { type : "tab-folder" , item : folder } ) ;
122- return folder ;
142+ // Others //
143+
144+ /**
145+ * Get all tab groups in the container.
146+ * @returns All tab groups in the container.
147+ */
148+ public getAllTabGroups ( ) : TabGroup [ ] {
149+ const scanTabContainer = ( container : BaseTabContainer ) : TabGroup [ ] => {
150+ return container . children . flatMap ( ( child ) => {
151+ if ( child . type === "tab-group" ) {
152+ return [ child . item ] ;
153+ }
154+ if ( child . type === "tab-folder" ) {
155+ return scanTabContainer ( child . item ) ;
156+ }
157+ return [ ] ;
158+ } ) ;
159+ } ;
160+
161+ return scanTabContainer ( this ) ;
123162 }
124163
164+ // Export //
165+
125166 protected baseExport ( ) : ExportedBaseTabContainer {
126167 return {
127168 type : "tab-container" ,
0 commit comments