This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 513
/
EditorMenuBreadcrumbs.js
140 lines (127 loc) · 3.86 KB
/
EditorMenuBreadcrumbs.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// @flow
import * as React from 'react';
import Box from '../core/Box';
import type { SectionName } from './EditorMenu';
import EditorMenuSeparator from './EditorMenuSeparator';
import EditorMenuButton from './EditorMenuButton';
import type { Web, Path } from './Editor';
import { getElementKey } from './EditorElement';
import arrayEqual from 'array-equal';
type ChildrenProps = {
pathChildren: *,
activePath: Path,
};
class Children extends React.PureComponent<ChildrenProps> {
getChildPath(child) {
const index = this.props.pathChildren.findIndex(item => item === child);
return this.props.activePath.concat(index);
}
renderChildren(elementChildren) {
return elementChildren.map((child, index) => (
<React.Fragment key={getElementKey(child)}>
{index !== 0 && <EditorMenuSeparator type="sibling" />}
<EditorMenuButton path={this.getChildPath(child)}>
{child.type}
</EditorMenuButton>
</React.Fragment>
));
}
render() {
// Only elements, not strings.
const elementChildren = this.props.pathChildren.filter(
item => typeof item !== 'string',
);
if (elementChildren.length === 0) return null;
return (
<React.Fragment>
<EditorMenuSeparator />
{this.renderChildren(elementChildren)}
</React.Fragment>
);
}
}
type PathButtonsProps = {
activeSection: SectionName,
activePath: Path,
elements: *,
};
class PathButtons extends React.PureComponent<PathButtonsProps> {
render() {
const { activeSection, activePath, elements } = this.props;
let pathChildren = elements;
let stringFound = false;
let buttonPath = [];
const buttons = activePath.reduce((buttons, pathIndex, index) => {
const child = pathChildren[pathIndex];
// Written like this because of Flow type refinements.
if (stringFound || typeof child === 'string') {
stringFound = true;
return buttons;
}
pathChildren = child.props.children;
const key = getElementKey(child);
// Enforce autoFocus via activePath. Remember autoFocus uses identity.
const autoFocus = index === activePath.length - 1 && activePath;
buttonPath = buttonPath.concat(pathIndex);
const active =
activeSection === 'element' && arrayEqual(activePath, buttonPath);
return [
...buttons,
<React.Fragment key={key}>
<EditorMenuSeparator />
<EditorMenuButton
active={active}
autoFocus={autoFocus}
path={buttonPath}
>
{child.type}
</EditorMenuButton>
</React.Fragment>,
];
}, []);
return [
...buttons,
<Children
key="children"
pathChildren={pathChildren}
activePath={activePath}
/>,
];
}
}
type EditorMenuBreadcrumbsProps = {|
activeSection: SectionName,
activePath: Path,
pageName: string,
web: Web,
webName: string,
|};
class EditorMenuBreadcrumbs extends React.PureComponent<
EditorMenuBreadcrumbsProps,
> {
render() {
const { activeSection, activePath, pageName, web, webName } = this.props;
return (
<Box flexDirection="row" justifyContent="space-between">
<Box flexDirection="row" flexWrap="wrap">
<EditorMenuButton autoFocus={activeSection === 'web'} section="web">
{webName}
</EditorMenuButton>
<EditorMenuSeparator />
<EditorMenuButton autoFocus={activeSection === 'page'} section="page">
{pageName}
</EditorMenuButton>
<PathButtons
activeSection={activeSection}
activePath={activePath}
elements={web.pages[pageName].elements}
/>
</Box>
<EditorMenuButton flexDirection="row" section="hamburger">
☰
</EditorMenuButton>
</Box>
);
}
}
export default EditorMenuBreadcrumbs;