11import { ShallowRef , shallowRef } from 'vue'
22import { Tab } from './Tab'
3- import { TabSystem } from './TabSystem'
4- import { TextTab } from '@/components/Tabs/Text/TextTab'
5- import { TreeEditorTab } from '@/components/Tabs/TreeEditor/TreeEditorTab'
6- import { ImageTab } from '@/components/Tabs/Image/ImageTab'
3+ import { TabSystem , TabSystemRecoveryState } from './TabSystem'
74import { FileTab } from './FileTab'
85import { Settings } from '@/libs/settings/Settings'
6+ import { TabTypes } from './TabTypes'
7+ import { ProjectManager } from '@/libs/project/ProjectManager'
8+ import { Disposable } from '@/libs/disposeable/Disposeable'
9+
10+ type TabManagerRecoveryState = { tabSystems : TabSystemRecoveryState [ ] ; focusedTabSystem : string }
911
1012export class TabManager {
11- public static tabSystems : TabSystem [ ] = [ new TabSystem ( ) ]
13+ public static tabSystems : ShallowRef < TabSystem [ ] > = shallowRef ( [ ] )
1214 public static focusedTabSystem : ShallowRef < TabSystem | null > = shallowRef ( null )
1315
14- private static tabTypes : ( typeof FileTab ) [ ] = [ ImageTab , TextTab , TreeEditorTab ]
16+ private static tabSystemSaveListenters : Record < string , Disposable > = { }
1517
1618 public static setup ( ) {
17- Settings . addSetting ( 'jsonEditor' , {
18- default : 'text' ,
19- } )
20-
2119 Settings . addSetting ( 'compactTabDesign' , {
2220 default : true ,
2321 } )
2422
25- Settings . updated . on ( ( event ) => {
26- const { id, value } = event as { id : string ; value : any }
27-
28- if ( id !== 'jsonEditor' ) return
29-
30- if ( value === 'text' ) {
31- this . tabTypes = [ ImageTab , TextTab , TreeEditorTab ]
23+ ProjectManager . updatedCurrentProject . on ( ( ) => {
24+ if ( ProjectManager . currentProject === null ) {
25+ TabManager . clear ( )
3226 } else {
33- this . tabTypes = [ ImageTab , TreeEditorTab , TextTab ]
27+ TabManager . loadedProject ( )
3428 }
3529 } )
3630 }
3731
32+ public static async addTabSystem ( recoveryState ?: TabSystemRecoveryState ) : Promise < TabSystem > {
33+ const tabSystem = new TabSystem ( )
34+
35+ if ( recoveryState ) await tabSystem . applyRecoverState ( recoveryState )
36+
37+ TabManager . tabSystemSaveListenters [ tabSystem . id ] = tabSystem . savedState . on ( ( ) => {
38+ TabManager . save ( )
39+ } )
40+
41+ TabManager . tabSystems . value . push ( tabSystem )
42+
43+ return tabSystem
44+ }
45+
46+ public static async removeTabSystem ( tabSystem : TabSystem ) {
47+ if ( ! TabManager . tabSystems . value . includes ( tabSystem ) ) return
48+
49+ if ( TabManager . focusedTabSystem . value ?. id === tabSystem . id ) TabManager . focusedTabSystem . value = null
50+
51+ TabManager . tabSystems . value . splice ( TabManager . tabSystems . value . indexOf ( tabSystem ) , 1 )
52+
53+ TabManager . tabSystemSaveListenters [ tabSystem . id ] . dispose ( )
54+ delete TabManager . tabSystemSaveListenters [ tabSystem . id ]
55+
56+ await tabSystem . clear ( )
57+ }
58+
59+ public static async clear ( ) {
60+ const tabSystems = TabManager . tabSystems . value
61+
62+ for ( const tabSystem of tabSystems ) {
63+ await TabManager . removeTabSystem ( tabSystem )
64+ }
65+ }
66+
67+ public static async loadedProject ( ) {
68+ if ( ! ProjectManager . currentProject ) return
69+
70+ TabManager . tabSystems . value = [ ]
71+ TabManager . focusedTabSystem . value = null
72+ TabManager . tabSystemSaveListenters = { }
73+
74+ await TabManager . addTabSystem ( )
75+
76+ await TabManager . recover ( )
77+ }
78+
3879 public static async openTab ( tab : Tab ) {
39- for ( const tabSystem of TabManager . tabSystems ) {
80+ for ( const tabSystem of TabManager . tabSystems . value ) {
4081 for ( const otherTab of tabSystem . tabs . value ) {
4182 if ( otherTab === tab ) {
4283 await tabSystem . selectTab ( tab )
@@ -54,7 +95,7 @@ export class TabManager {
5495 }
5596
5697 public static async openFile ( path : string ) {
57- for ( const tabSystem of TabManager . tabSystems ) {
98+ for ( const tabSystem of TabManager . tabSystems . value ) {
5899 for ( const tab of tabSystem . tabs . value ) {
59100 if ( tab instanceof FileTab && tab . is ( path ) ) {
60101 await tabSystem . selectTab ( tab )
@@ -66,7 +107,7 @@ export class TabManager {
66107 }
67108 }
68109
69- for ( const TabType of TabManager . tabTypes ) {
110+ for ( const TabType of TabTypes . fileTabTypes ) {
70111 if ( TabType . canEdit ( path ) ) {
71112 await TabManager . openTab ( new TabType ( path ) )
72113
@@ -76,7 +117,7 @@ export class TabManager {
76117 }
77118
78119 public static getTabByType < T extends Tab > ( tabType : { new ( ...args : any [ ] ) : T } ) : T | null {
79- for ( const tabSystem of TabManager . tabSystems ) {
120+ for ( const tabSystem of TabManager . tabSystems . value ) {
80121 for ( const tab of tabSystem . tabs . value ) {
81122 if ( tab instanceof tabType ) {
82123 return tab
@@ -88,12 +129,41 @@ export class TabManager {
88129 }
89130
90131 public static getDefaultTabSystem ( ) : TabSystem {
91- return TabManager . tabSystems [ 0 ]
132+ return TabManager . tabSystems . value [ 0 ]
92133 }
93134
94135 public static getFocusedTab ( ) : Tab | null {
95136 if ( TabManager . focusedTabSystem . value === null ) return null
96137
97138 return TabManager . focusedTabSystem . value . selectedTab . value
98139 }
140+
141+ public static async save ( ) {
142+ if ( ! ProjectManager . currentProject ) return
143+
144+ const state = {
145+ tabSystems : await Promise . all ( TabManager . tabSystems . value . map ( ( tabSystem ) => tabSystem . getRecoveryState ( ) ) ) ,
146+ focusedTabSystem : TabManager . focusedTabSystem . value ? TabManager . focusedTabSystem . value . id : null ,
147+ }
148+
149+ await ProjectManager . currentProject . saveTabManagerState ( state )
150+ }
151+
152+ public static async recover ( ) {
153+ if ( ! ProjectManager . currentProject ) return
154+
155+ const state = ( await ProjectManager . currentProject . getTabManagerState ( ) ) as TabManagerRecoveryState | null
156+
157+ if ( state === null ) return
158+
159+ await TabManager . clear ( )
160+
161+ for ( const tabSystemState of state . tabSystems ) {
162+ await TabManager . addTabSystem ( tabSystemState )
163+ }
164+
165+ TabManager . tabSystems . value = [ ...TabManager . tabSystems . value ]
166+
167+ TabManager . focusedTabSystem . value = TabManager . tabSystems . value . find ( ( tabSystem ) => tabSystem . id === state . focusedTabSystem ) ?? null
168+ }
99169}
0 commit comments