11import uniqueId from 'lodash/uniqueId'
22import { extendObservable , observable , computed , action , autorun , autorunAsync } from 'mobx'
33import EditorTabState , { TabGroup } from 'components/Editor/state'
4+ import PaneScope from 'commons/Pane/state'
45
5- const state = observable ( {
6- panes : observable . map ( { } ) ,
6+ const { state, BasePane } = PaneScope ( )
7+
8+ extendObservable ( state , {
9+ get panes ( ) { return this . entities } ,
710 activePaneId : null ,
8- rootPaneId : null ,
911 autoCloseEmptyPane : true ,
1012 get rootPane ( ) {
11- const rootPane = this . panes . get ( this . rootPaneId )
13+ const rootPane = this . panes . values ( ) . find ( pane => pane . isRoot )
1214 return rootPane || this . panes . values ( ) [ 0 ]
1315 } ,
1416 get activePane ( ) {
@@ -17,51 +19,9 @@ const state = observable({
1719 } ,
1820} )
1921
20- class BasePane {
21- constructor ( paneConfig ) {
22- const defaults = {
23- id : uniqueId ( 'pane_view_' ) ,
24- flexDirection : 'row' ,
25- size : 100 ,
26- parentId : '' ,
27- index : 0 ,
28- }
29-
30- paneConfig = Object . assign ( { } , defaults , paneConfig )
31- extendObservable ( this , paneConfig )
32- state . panes . set ( this . id , this )
33- }
34-
35- @computed
36- get parent ( ) {
37- return state . panes . get ( this . parentId )
38- }
39-
40- @computed
41- get views ( ) {
42- return state . panes . values ( )
43- . filter ( pane => pane . parentId === this . id )
44- . sort ( ( a , b ) => a . index - b . index )
45- }
46-
47- @computed
48- get siblings ( ) {
49- return this . parent . views
50- }
51-
52- @computed
53- get prev ( ) {
54- return this . siblings [ this . index - 1 ]
55- }
56-
57- @computed
58- get next ( ) {
59- return this . siblings [ this . index + 1 ]
60- }
61- }
62-
6322class Pane extends BasePane {
6423 constructor ( paneConfig ) {
24+ if ( ! paneConfig . id ) paneConfig . id = uniqueId ( 'pane_view_' )
6525 super ( paneConfig )
6626 this . contentType = 'tabGroup'
6727 const tabGroup = this . tabGroup || new TabGroup ( )
@@ -76,14 +36,6 @@ class Pane extends BasePane {
7636 return EditorTabState . tabGroups . get ( this . contentId )
7737 }
7838
79- @computed
80- get leafChildren ( ) {
81- if ( ! this . views . length ) return [ this ]
82- return this . views . reduce ( ( acc , pane ) => {
83- return acc . concat ( pane . leafChildren )
84- } , [ ] )
85- }
86-
8739 @action
8840 destroy ( ) {
8941 if ( this . isRoot ) return
@@ -110,29 +62,30 @@ class Pane extends BasePane {
11062}
11163
11264const rootPane = new Pane ( {
113- id : 'pane_view_1' ,
11465 flexDirection : 'row' ,
11566 size : 100 ,
116- isRoot : true ,
11767} )
11868
11969state . panes . set ( rootPane . id , rootPane )
120- state . rootPaneId = rootPane . id
12170
122- autorun ( ( ) => {
71+ autorun ( 'normalize pane indexes' , ( ) => {
12372 state . panes . forEach ( parentPane =>
12473 parentPane . views . forEach ( ( pane , index ) => {
12574 if ( pane . index !== index ) pane . index = index
12675 } )
12776 )
12877} )
12978
130- autorunAsync ( ( ) => {
79+ autorunAsync ( 'short-circuit unnecessary internal pane node' , ( ) => {
80+ // pane.parent -> pane -> lonelyChild
81+ // => pane.panret -> lonelyChild
82+ // delete pane
13183 state . panes . forEach ( pane => {
132- if ( ! pane || pane . isRoot ) return
84+ if ( ! pane ) return
13385 if ( pane . views . length === 1 ) {
134- pane . contentId = pane . views [ 0 ] . contentId
135- state . panes . delete ( pane . views [ 0 ] . id )
86+ const lonelyChild = pane . views [ 0 ]
87+ lonelyChild . parentId = pane . parentId
88+ state . panes . delete ( pane . id )
13689 }
13790 } )
13891} )
0 commit comments