@@ -4,7 +4,6 @@ import { updateStyleDefinition, removeStyleDefinition, coalesce, adjustmentsEqua
44
55const spaceDefaults : Partial < ISpaceDefinition > = {
66 id : "" ,
7- order : 0 ,
87 zIndex : 0 ,
98 scrollable : false ,
109 resizing : false ,
@@ -21,6 +20,8 @@ const spaceDefaults: Partial<ISpaceDefinition> = {
2120 anchoredChildren : ( ) => [ ] ,
2221} ;
2322
23+ const anchorTypes = [ AnchorType . Left , AnchorType . Top , AnchorType . Right , AnchorType . Bottom ] ;
24+
2425function getPosition ( type : Type ) {
2526 if ( type === Type . ViewPort ) {
2627 return "fixed" ;
@@ -74,14 +75,37 @@ export function createStore(): ISpaceStore {
7475 const getSpaces = ( ) => spaces ;
7576
7677 const recalcSpaces = ( parent : ISpaceDefinition ) => {
77- for ( var i = 0 , len = parent . children . length ; i < len ; i ++ ) {
78- const space = parent . children [ i ] ;
78+ const onlyUnique = ( value : number , index : number , self : number [ ] ) => {
79+ return self . indexOf ( value ) === index ;
80+ } ;
81+
82+ const addDefaultOrders = ( spaces : ISpaceDefinition [ ] ) => {
83+ let result : ISpaceDefinition [ ] = [ ] ;
84+
85+ anchorTypes . forEach ( ( t ) => {
86+ const anchoredSpaces = spaces . filter ( ( s ) => s . anchor !== undefined && s . anchor === t ) ;
87+ const zIndices = anchoredSpaces . map ( ( s ) => s . zIndex ) . filter ( onlyUnique ) ;
88+ zIndices . forEach ( ( i ) => {
89+ const anchoredSpacesInLayer = anchoredSpaces . filter ( ( s ) => s . zIndex === i ) ;
90+ const orderedSpaces = anchoredSpacesInLayer . filter ( ( c ) => c . order !== undefined ) ;
91+ const unorderedSpaces = anchoredSpacesInLayer . filter ( ( c ) => c . order === undefined ) ;
92+ var maxOrder = orderedSpaces . length > 0 ? orderedSpaces . map ( ( a ) => a . order ! ) . reduce ( ( a , b ) => Math . max ( a , b ) ) : 0 ;
93+ result = [ ...result , ...[ ...orderedSpaces , ...unorderedSpaces . map ( ( c , idx ) => ( { ...c , ...{ order : maxOrder + idx + 1 } } ) ) ] ] ;
94+ } ) ;
95+ } ) ;
96+
97+ return [ ...result , ...spaces . filter ( ( s ) => s . anchor === undefined ) ] ;
98+ } ;
99+
100+ const orderedSpaces = addDefaultOrders ( parent . children ) ;
101+ for ( var i = 0 , len = orderedSpaces . length ; i < len ; i ++ ) {
102+ const space = orderedSpaces [ i ] ;
79103 let changed = false ;
80104
81105 if ( space . type === Type . Fill ) {
82106 anchorUpdates ( space ) . forEach ( ( info ) => {
83107 const adjusted : SizeUnit [ ] = [ ] ;
84- const anchoredSpaces = parent . anchoredChildren ( info . anchor , space . zIndex ) ;
108+ const anchoredSpaces = parent . anchoredChildren ( orderedSpaces , info . anchor , space . zIndex ) ;
85109
86110 anchoredSpaces . forEach ( ( as ) => {
87111 if ( as . orientation === Orientation . Vertical ) {
@@ -108,8 +132,8 @@ export function createStore(): ISpaceStore {
108132 } else if ( space . type === Type . Anchored ) {
109133 const adjusted : SizeUnit [ ] = [ ] ;
110134 const anchoredSpaces = parent
111- . anchoredChildren ( space . anchor ! , space . zIndex )
112- . filter ( ( s ) => s . id !== space . id && s . order <= space . order ) ;
135+ . anchoredChildren ( orderedSpaces , space . anchor ! , space . zIndex )
136+ . filter ( ( s ) => s . id !== space . id && s . order ! <= space . order ! ) ;
113137
114138 anchoredSpaces . forEach ( ( as ) => {
115139 if ( as . orientation === Orientation . Vertical ) {
@@ -191,7 +215,7 @@ export function createStore(): ISpaceStore {
191215 maximumSize,
192216 handleSize,
193217 touchHandleSize,
194- handlePlacement
218+ handlePlacement,
195219 } = props ;
196220 const canResizeLeft = ( position && position . rightResizable ) || false ;
197221 const canResizeRight = ( position && position . leftResizable ) || false ;
@@ -382,13 +406,9 @@ export function createStore(): ISpaceStore {
382406 } ,
383407 } as ISpaceDefinition ;
384408
385- newSpace . anchoredChildren = ( anchor , zIndex ) => {
386- const children = newSpace . children . filter ( ( s ) => s . type === Type . Anchored && s . anchor === anchor && s . zIndex === zIndex ) ;
387- const orderedChildren = children . filter ( c => c . order !== undefined ) ;
388- const unorderedChildren = children . filter ( c => c . order === undefined ) ;
389- var maxOrder = orderedChildren . length > 0 ? orderedChildren . map ( a => a . order ) . reduce ( ( a , b ) => Math . max ( a , b ) ) : 0 ;
390- return [ ...orderedChildren , ...unorderedChildren . map ( ( c , idx ) => ( { ...c , ...{ order : maxOrder + idx + 1 } } ) ) ] ;
391- }
409+ newSpace . anchoredChildren = ( children , anchor , zIndex ) => {
410+ return children . filter ( ( s ) => s . type === Type . Anchored && s . anchor === anchor && s . zIndex === zIndex ) ;
411+ } ;
392412
393413 newSpace . adjustLeft = ( adjusted ) => {
394414 if ( adjustmentsEqual ( newSpace . left . adjusted , adjusted ) ) {
0 commit comments