forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranch.js
More file actions
104 lines (97 loc) · 2.8 KB
/
branch.js
File metadata and controls
104 lines (97 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* External dependencies
*/
import { map, compact } from 'lodash';
/**
* WordPress dependencies
*/
import { Fragment } from '@wordpress/element';
/**
* Internal dependencies
*/
import BlockNavigationBlock from './block';
import BlockNavigationAppender from './appender';
export default function BlockNavigationBranch( props ) {
const {
blocks,
selectBlock,
selectedBlockClientId,
showAppender,
showBlockMovers,
showNestedBlocks,
parentBlockClientId,
level = 1,
terminatedLevels = [],
path = [],
} = props;
const isTreeRoot = ! parentBlockClientId;
const filteredBlocks = compact( blocks );
const itemHasAppender = ( parentClientId ) =>
showAppender &&
! isTreeRoot &&
selectedBlockClientId === parentClientId;
const hasAppender = itemHasAppender( parentBlockClientId );
// Add +1 to the rowCount to take the block appender into account.
const blockCount = filteredBlocks.length;
const rowCount = hasAppender ? blockCount + 1 : blockCount;
const appenderPosition = rowCount;
return (
<>
{ map( filteredBlocks, ( block, index ) => {
const { clientId, innerBlocks } = block;
const position = index + 1;
const isLastRowAtLevel = rowCount === position;
const updatedTerminatedLevels = isLastRowAtLevel
? [ ...terminatedLevels, level ]
: terminatedLevels;
const updatedPath = [ ...path, position ];
const hasNestedBlocks =
showNestedBlocks && !! innerBlocks && !! innerBlocks.length;
const hasNestedAppender = itemHasAppender( clientId );
return (
<Fragment key={ clientId }>
<BlockNavigationBlock
block={ block }
onClick={ selectBlock }
isSelected={ selectedBlockClientId === clientId }
level={ level }
position={ position }
rowCount={ rowCount }
siblingBlockCount={ blockCount }
showBlockMovers={ showBlockMovers }
terminatedLevels={ terminatedLevels }
path={ updatedPath }
/>
{ ( hasNestedBlocks || hasNestedAppender ) && (
<BlockNavigationBranch
blocks={ innerBlocks }
selectedBlockClientId={ selectedBlockClientId }
selectBlock={ selectBlock }
showAppender={ showAppender }
showBlockMovers={ showBlockMovers }
showNestedBlocks={ showNestedBlocks }
parentBlockClientId={ clientId }
level={ level + 1 }
terminatedLevels={ updatedTerminatedLevels }
path={ updatedPath }
/>
) }
</Fragment>
);
} ) }
{ hasAppender && (
<BlockNavigationAppender
parentBlockClientId={ parentBlockClientId }
position={ rowCount }
rowCount={ appenderPosition }
level={ level }
terminatedLevels={ terminatedLevels }
path={ [ ...path, appenderPosition ] }
/>
) }
</>
);
}
BlockNavigationBranch.defaultProps = {
selectBlock: () => {},
};