forked from nomcopter/react-mosaic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExampleApp.tsx
199 lines (176 loc) · 5.87 KB
/
ExampleApp.tsx
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { Classes, HTMLSelect } from '@blueprintjs/core';
import { IconNames } from '@blueprintjs/icons';
import classNames from 'classnames';
import dropRight from 'lodash/dropRight';
import React from 'react';
import {
Corner,
createBalancedTreeFromLeaves,
getLeaves,
getNodeAtPath,
getOtherDirection,
getPathToCorner,
Mosaic,
MosaicDirection,
MosaicNode,
MosaicParent,
MosaicWindow,
MosaicZeroState,
updateTree,
} from '../src';
import { CloseAdditionalControlsButton } from './CloseAdditionalControlsButton';
import '@blueprintjs/core/lib/css/blueprint.css';
import '@blueprintjs/icons/lib/css/blueprint-icons.css';
import '../styles/index.less';
import './example.less';
// tslint:disable no-console
// tslint:disable-next-line no-var-requires
const gitHubLogo = require('./GitHub-Mark-Light-32px.png');
// tslint:disable-next-line no-var-requires
const { version } = require('../package.json');
let windowCount = 3;
export const THEMES = {
['Blueprint']: 'mosaic-blueprint-theme',
['Blueprint Dark']: classNames('mosaic-blueprint-theme', Classes.DARK),
['None']: '',
};
export type Theme = keyof typeof THEMES;
const additionalControls = React.Children.toArray([<CloseAdditionalControlsButton />]);
const EMPTY_ARRAY: any[] = [];
export interface ExampleAppState {
currentNode: MosaicNode<number> | null;
currentTheme: Theme;
}
export class ExampleApp extends React.PureComponent<{}, ExampleAppState> {
state: ExampleAppState = {
currentNode: {
direction: 'row',
first: 1,
second: {
direction: 'column',
first: 2,
second: 3,
},
splitPercentage: 40,
},
currentTheme: 'Blueprint',
};
render() {
return (
<React.StrictMode>
<div className="react-mosaic-example-app">
{this.renderNavBar()}
<Mosaic<number>
renderTile={(count, path) => (
<MosaicWindow<number>
additionalControls={count === 3 ? additionalControls : EMPTY_ARRAY}
title={`Window ${count}`}
createNode={this.createNode}
path={path}
onDragStart={() => console.log('MosaicWindow.onDragStart')}
onDragEnd={(type) => console.log('MosaicWindow.onDragEnd', type)}
renderToolbar={count === 2 ? () => <div className="toolbar-example">Custom Toolbar</div> : null}
>
<div className="example-window">
<h1>{`Window ${count}`}</h1>
</div>
</MosaicWindow>
)}
zeroStateView={<MosaicZeroState createNode={this.createNode} />}
value={this.state.currentNode}
onChange={this.onChange}
onRelease={this.onRelease}
className={THEMES[this.state.currentTheme]}
/>
</div>
</React.StrictMode>
);
}
private onChange = (currentNode: MosaicNode<number> | null) => {
this.setState({ currentNode });
};
private onRelease = (currentNode: MosaicNode<number> | null) => {
console.log('Mosaic.onRelease():', currentNode);
};
private createNode = () => ++windowCount;
private autoArrange = () => {
const leaves = getLeaves(this.state.currentNode);
this.setState({
currentNode: createBalancedTreeFromLeaves(leaves),
});
};
private addToTopRight = () => {
let { currentNode } = this.state;
if (currentNode) {
const path = getPathToCorner(currentNode, Corner.TOP_RIGHT);
const parent = getNodeAtPath(currentNode, dropRight(path)) as MosaicParent<number>;
const destination = getNodeAtPath(currentNode, path) as MosaicNode<number>;
const direction: MosaicDirection = parent ? getOtherDirection(parent.direction) : 'row';
let first: MosaicNode<number>;
let second: MosaicNode<number>;
if (direction === 'row') {
first = destination;
second = ++windowCount;
} else {
first = ++windowCount;
second = destination;
}
currentNode = updateTree(currentNode, [
{
path,
spec: {
$set: {
direction,
first,
second,
},
},
},
]);
} else {
currentNode = ++windowCount;
}
this.setState({ currentNode });
};
private renderNavBar() {
return (
<div className={classNames(Classes.NAVBAR, Classes.DARK)}>
<div className={Classes.NAVBAR_GROUP}>
<div className={Classes.NAVBAR_HEADING}>
<a href="https://github.com/nomcopter/react-mosaic">
react-mosaic <span className="version">v{version}</span>
</a>
</div>
</div>
<div className={classNames(Classes.NAVBAR_GROUP, Classes.BUTTON_GROUP)}>
<label className={classNames('theme-selection', Classes.LABEL, Classes.INLINE)}>
Theme:
<HTMLSelect
value={this.state.currentTheme}
onChange={(e) => this.setState({ currentTheme: e.currentTarget.value as Theme })}
>
{React.Children.toArray(Object.keys(THEMES).map((label) => <option>{label}</option>))}
</HTMLSelect>
</label>
<div className="navbar-separator" />
<span className="actions-label">Example Actions:</span>
<button
className={classNames(Classes.BUTTON, Classes.iconClass(IconNames.GRID_VIEW))}
onClick={this.autoArrange}
>
Auto Arrange
</button>
<button
className={classNames(Classes.BUTTON, Classes.iconClass(IconNames.ARROW_TOP_RIGHT))}
onClick={this.addToTopRight}
>
Add Window to Top Right
</button>
<a className="github-link" href="https://github.com/nomcopter/react-mosaic">
<img src={gitHubLogo} />
</a>
</div>
</div>
);
}
}