1
1
import uniqueId from 'lodash/uniqueId'
2
2
import { extendObservable , observable , computed , action , autorun , autorunAsync } from 'mobx'
3
3
import EditorTabState , { TabGroup } from 'components/Editor/state'
4
+ import PaneScope from 'commons/Pane/state'
4
5
5
- const state = observable ( {
6
- panes : observable . map ( { } ) ,
6
+ const { state, BasePane } = PaneScope ( )
7
+
8
+ extendObservable ( state , {
9
+ get panes ( ) { return this . entities } ,
7
10
activePaneId : null ,
8
- rootPaneId : null ,
9
11
autoCloseEmptyPane : true ,
10
12
get rootPane ( ) {
11
- const rootPane = this . panes . get ( this . rootPaneId )
13
+ const rootPane = this . panes . values ( ) . find ( pane => pane . isRoot )
12
14
return rootPane || this . panes . values ( ) [ 0 ]
13
15
} ,
14
16
get activePane ( ) {
@@ -17,51 +19,9 @@ const state = observable({
17
19
} ,
18
20
} )
19
21
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
-
63
22
class Pane extends BasePane {
64
23
constructor ( paneConfig ) {
24
+ if ( ! paneConfig . id ) paneConfig . id = uniqueId ( 'pane_view_' )
65
25
super ( paneConfig )
66
26
this . contentType = 'tabGroup'
67
27
const tabGroup = this . tabGroup || new TabGroup ( )
@@ -76,14 +36,6 @@ class Pane extends BasePane {
76
36
return EditorTabState . tabGroups . get ( this . contentId )
77
37
}
78
38
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
-
87
39
@action
88
40
destroy ( ) {
89
41
if ( this . isRoot ) return
@@ -110,29 +62,30 @@ class Pane extends BasePane {
110
62
}
111
63
112
64
const rootPane = new Pane ( {
113
- id : 'pane_view_1' ,
114
65
flexDirection : 'row' ,
115
66
size : 100 ,
116
- isRoot : true ,
117
67
} )
118
68
119
69
state . panes . set ( rootPane . id , rootPane )
120
- state . rootPaneId = rootPane . id
121
70
122
- autorun ( ( ) => {
71
+ autorun ( 'normalize pane indexes' , ( ) => {
123
72
state . panes . forEach ( parentPane =>
124
73
parentPane . views . forEach ( ( pane , index ) => {
125
74
if ( pane . index !== index ) pane . index = index
126
75
} )
127
76
)
128
77
} )
129
78
130
- autorunAsync ( ( ) => {
79
+ autorunAsync ( 'short-circuit unnecessary internal pane node' , ( ) => {
80
+ // pane.parent -> pane -> lonelyChild
81
+ // => pane.panret -> lonelyChild
82
+ // delete pane
131
83
state . panes . forEach ( pane => {
132
- if ( ! pane || pane . isRoot ) return
84
+ if ( ! pane ) return
133
85
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 )
136
89
}
137
90
} )
138
91
} )
0 commit comments