This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
executable file
·206 lines (178 loc) · 4.52 KB
/
index.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
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
200
201
202
203
204
205
206
import React from 'react';
import VSplit from './vsplit';
import HSplit from './hsplit';
import Dimensions from './dimensions';
import SplitPane from '@kadira/react-split-pane';
const rootStyle = {
height: '100vh',
backgroundColor: '#F7F7F7',
};
const leftPanelStyle = {
position: 'absolute',
width: '100%',
height: '100%',
};
const downPanelStyle = {
display: 'flex',
position: 'absolute',
width: '100%',
height: '100%',
padding: '5px 10px 10px 0',
boxSizing: 'border-box',
};
const contentPanelStyle = {
position: 'absolute',
boxSizing: 'border-box',
width: '100%',
height: '100%',
padding: '10px 10px 10px 0',
};
const normalPreviewStyle = {
width: '100%',
height: '100%',
backgroundColor: '#FFF',
border: '1px solid #ECECEC',
borderRadius: 4,
};
const fullScreenPreviewStyle = {
position: 'fixed',
left: '0px',
right: '0px',
top: '0px',
zIndex: 1,
backgroundColor: '#FFF',
height: '100%',
width: '100%',
border: 0,
margin: 0,
padding: 0,
overflow: 'hidden',
};
const vsplit = <VSplit />;
const hsplit = <HSplit />;
const onDragStart = function () {
document.body.classList.add('dragging');
};
const onDragEnd = function () {
document.body.classList.remove('dragging');
};
const saveHeightPanel = h => {
try {
localStorage.setItem('splitPos', h);
return true;
} catch (e) {
return false;
}
};
const getSavedHeight = h => {
try {
return localStorage.getItem('splitPos');
} catch (e) {
return h;
}
};
class Layout extends React.Component {
constructor(props) {
super(props);
this.state = {
previewPanelDimensions: {
height: 0,
width: 0,
},
};
this.onResize = this.onResize.bind(this);
}
componentDidMount() {
window.addEventListener('resize', this.onResize);
}
componentWillUnmount() {
window.removeEventListener('resize', this.onResize);
}
onResize() {
const {
clientWidth,
clientHeight,
} = this.previewPanelRef;
this.setState({
previewPanelDimensions: {
width: clientWidth,
height: clientHeight,
},
});
}
render() {
const {
goFullScreen, showLeftPanel, showDownPanel, downPanelInRight,
downPanel, leftPanel, preview,
} = this.props;
const {
previewPanelDimensions,
} = this.state;
let previewStyle = normalPreviewStyle;
if (goFullScreen) {
previewStyle = fullScreenPreviewStyle;
}
const leftPanelDefaultSize = showLeftPanel ? 250 : 1;
let downPanelDefaultSize = 1;
if (showDownPanel) {
downPanelDefaultSize = downPanelInRight ? 400 : 200;
}
// Get the value from localStorage or user downPanelDefaultSize
downPanelDefaultSize = getSavedHeight(downPanelDefaultSize);
return (
<div style={rootStyle}>
<SplitPane
split="vertical"
minSize={leftPanelDefaultSize}
defaultSize={leftPanelDefaultSize}
resizerChildren={vsplit}
onDragStarted={onDragStart}
onDragFinished={onDragEnd}
onChange={this.onResize}
>
<div style={leftPanelStyle}>
{showLeftPanel ? leftPanel() : null}
</div>
<SplitPane
split={downPanelInRight ? 'vertical' : 'horizontal'}
primary="second"
minSize={downPanelInRight ? 200 : 100}
defaultSize={downPanelDefaultSize}
resizerChildren={downPanelInRight ? vsplit : hsplit}
onDragStarted={onDragStart}
onDragFinished={onDragEnd}
onChange={
size => {
saveHeightPanel(size);
this.onResize();
}
}
>
<div style={contentPanelStyle}>
<div
style={previewStyle}
ref={(ref) => { this.previewPanelRef = ref; }}
>
{preview()}
</div>
<Dimensions {...previewPanelDimensions} />
</div>
<div style={downPanelStyle}>
{showDownPanel ? downPanel() : null}
</div>
</SplitPane>
</SplitPane>
</div>
);
}
}
Layout.propTypes = {
showLeftPanel: React.PropTypes.bool.isRequired,
showDownPanel: React.PropTypes.bool.isRequired,
goFullScreen: React.PropTypes.bool.isRequired,
leftPanel: React.PropTypes.func.isRequired,
preview: React.PropTypes.func.isRequired,
downPanel: React.PropTypes.func.isRequired,
downPanelInRight: React.PropTypes.bool.isRequired,
};
export default Layout;