-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathWorkspace.jsx
341 lines (318 loc) · 10.6 KB
/
Workspace.jsx
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import {faInfoCircle, faPenSquare} from '@fortawesome/free-solid-svg-icons';
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
import classnames from 'classnames';
import i18next from 'i18next';
import bindAll from 'lodash-es/bindAll';
import clone from 'lodash-es/clone';
import get from 'lodash-es/get';
import includes from 'lodash-es/includes';
import isNull from 'lodash-es/isNull';
import partial from 'lodash-es/partial';
import some from 'lodash-es/some';
import PropTypes from 'prop-types';
import React, {Suspense} from 'react';
import {DraggableCore} from 'react-draggable';
import ImmutablePropTypes from 'react-immutable-proptypes';
import {dehydrateProject} from '../clients/localStorage';
import AccountMigration from '../containers/AccountMigration';
import EditorsColumn from '../containers/EditorsColumn';
import Instructions from '../containers/Instructions';
import KeyboardHandler from '../containers/KeyboardHandler';
import LoginPrompt from '../containers/LoginPrompt';
import NotificationList from '../containers/NotificationList';
import ProjectPickerModal from '../containers/ProjectPickerModal';
import TopBar from '../containers/TopBar';
import prefix from '../services/inlineStylePrefixer';
import {LANGUAGES} from '../util/editor';
import {isPristineProject} from '../util/projectUtils';
import {RIGHT_COLUMN_COMPONENTS} from '../util/ui';
import CollapsedComponent from './CollapsedComponent';
import Output from './Output';
import PopThrobber from './PopThrobber';
export default class Workspace extends React.Component {
constructor() {
super();
bindAll(
this,
'_handleUnload',
'_handleClickInstructionsBar',
'_handleClickInstructionsEditButton',
);
this.columnRefs = [null, null];
}
componentDidMount() {
addEventListener('beforeunload', this._handleUnload);
}
componentWillUnmount() {
removeEventListener('beforeunload', this._handleUnload);
}
_handleUnload() {
const {currentProject} = this.props;
if (!isNull(currentProject) && !isPristineProject(currentProject)) {
dehydrateProject(currentProject);
}
}
_handleClickInstructionsBar() {
const {isEditingInstructions, onComponentToggle} = this.props;
if (!isEditingInstructions) {
onComponentToggle(
get(this.props, ['currentProject', 'projectKey']),
'instructions',
);
}
}
_handleClickInstructionsEditButton() {
const {isEditingInstructions, onClickInstructionsEditButton} = this.props;
if (!isEditingInstructions) {
onClickInstructionsEditButton(
get(this.props, ['currentProject', 'projectKey']),
);
}
}
_renderInstructionsBar() {
const currentInstructions = get(this.props, [
'currentProject',
'instructions',
]);
if (!this.props.isEditingInstructions && !currentInstructions) {
return null;
}
const isInstructionsHidden = get(this.props, [
'currentProject',
'hiddenUIComponents',
]).includes('instructions');
return (
<div
className={classnames('layout__instructions-bar', {
'layout__instructions-bar_disabled': this.props.isEditingInstructions,
})}
onClick={this._handleClickInstructionsBar}
>
<FontAwesomeIcon
fixedWidth
className={classnames({
u__pointer: !this.props.isEditingInstructions,
})}
icon={faInfoCircle}
/>
{!isInstructionsHidden && !this.props.isEditingInstructions && (
<FontAwesomeIcon
fixedWidth
className="layout__instructions-bar-edit-button"
icon={faPenSquare}
onClick={e => {
e.stopPropagation();
this._handleClickInstructionsEditButton();
}}
/>
)}
</div>
);
}
_renderHiddenLanguages() {
const {currentProject, hiddenLanguages, onComponentToggle} = this.props;
const isRightJustified = !this._isSingleColumn();
return (
<>
{hiddenLanguages.map(({language}) => (
<CollapsedComponent
component={`editor.${language}`}
isRightJustified={isRightJustified}
key={language}
projectKey={currentProject.projectKey}
text={i18next.t(`languages.${language}`)}
onComponentUnhide={onComponentToggle}
/>
))}
</>
);
}
_renderHiddenRightColumnComponents() {
const {currentProject, hasErrors, onComponentToggle, title} = this.props;
// Errors take over the whole right side of the screen
if (hasErrors) {
return null;
}
return RIGHT_COLUMN_COMPONENTS.filter(component =>
includes(currentProject.hiddenUIComponents, component),
).map(component => {
switch (component) {
case 'console':
return (
<CollapsedComponent
component="console"
isRightJustified={false}
key="console"
projectKey={currentProject.projectKey}
text={i18next.t('workspace.components.console')}
onComponentUnhide={onComponentToggle}
/>
);
case 'preview':
return (
<CollapsedComponent
component="preview"
isRightJustified={false}
key="preview"
projectKey={currentProject.projectKey}
text={title}
onComponentUnhide={onComponentToggle}
/>
);
default:
return null;
}
});
}
_shouldRenderLeftColumn() {
return this.props.hiddenLanguages.length !== LANGUAGES.length;
}
_shouldRenderRightColumn() {
const {currentProject, shouldRenderOutput} = this.props;
return (
shouldRenderOutput ||
some(
RIGHT_COLUMN_COMPONENTS,
component => !includes(currentProject.hiddenUIComponents, component),
)
);
}
_isSingleColumn() {
const shouldRenderLeft = this._shouldRenderLeftColumn();
const shouldRenderRight = this._shouldRenderRightColumn();
return (
this._isEverythingHidden() ||
(shouldRenderLeft && !shouldRenderRight) ||
(!shouldRenderLeft && shouldRenderRight)
);
}
_isEverythingHidden() {
return !this._shouldRenderLeftColumn() && !this._shouldRenderRightColumn();
}
_renderEverythingHidden() {
return (
<div className="environment__column">
{this._renderHiddenRightColumnComponents()}
{this._renderHiddenLanguages()}
</div>
);
}
_renderEnvironment() {
const {
currentProject,
isAnyTopBarMenuOpen,
isDraggingColumnDivider,
isFlexResizingSupported,
resizableFlexGrow,
resizableFlexRefs,
onResizableFlexDividerDrag,
onStartDragColumnDivider,
onStopDragColumnDivider,
shouldRenderOutput,
} = this.props;
if (isNull(currentProject)) {
return <PopThrobber message={i18next.t('workspace.loading')} />;
}
const [_handleEditorsRef, _handleOutputRef] = resizableFlexRefs;
const ignorePointerEvents = isDraggingColumnDivider || isAnyTopBarMenuOpen;
return (
<Suspense
fallback={<PopThrobber message={i18next.t('workspace.loading')} />}
>
<div className="environment">
{this._shouldRenderLeftColumn() && (
<>
<div
className="environment__column"
ref={_handleEditorsRef}
style={prefix(clone({flexGrow: resizableFlexGrow.get(0)}))}
>
<div className="environment__column-contents">
<div className="environment__column-contents-inner">
<EditorsColumn />
{!this._shouldRenderRightColumn() &&
this._renderHiddenRightColumnComponents()}
{this._renderHiddenLanguages()}
</div>
</div>
</div>
<DraggableCore
onDrag={partial(onResizableFlexDividerDrag, 0)}
onStart={onStartDragColumnDivider}
onStop={onStopDragColumnDivider}
>
<div
className={classnames('editors__column-divider', {
'editors__column-divider_draggable': isFlexResizingSupported,
})}
/>
</DraggableCore>
</>
)}
{this._shouldRenderRightColumn() && (
<div
className="environment__column"
ref={_handleOutputRef}
style={prefix({
flexGrow: resizableFlexGrow.get(1),
pointerEvents: ignorePointerEvents ? 'none' : 'all',
})}
>
<div className="environment__column-contents">
<div className="environment__column-contents-inner">
{shouldRenderOutput && <Output />}
{this._renderHiddenRightColumnComponents()}
{!this._shouldRenderLeftColumn() &&
this._renderHiddenLanguages()}
</div>
</div>
</div>
)}
{this._isEverythingHidden() && this._renderEverythingHidden()}
</div>
</Suspense>
);
}
render() {
return (
<>
<div className="layout">
<TopBar />
<NotificationList />
<ProjectPickerModal />
<div className="layout__columns">
<Instructions />
{this._renderInstructionsBar()}
<div className="workspace layout__main">
{this._renderEnvironment()}
</div>
</div>
<AccountMigration />
<LoginPrompt />
</div>
<KeyboardHandler />
</>
);
}
}
Workspace.propTypes = {
currentProject: PropTypes.object,
hasErrors: PropTypes.bool.isRequired,
hiddenLanguages: PropTypes.array.isRequired,
isAnyTopBarMenuOpen: PropTypes.bool.isRequired,
isDraggingColumnDivider: PropTypes.bool.isRequired,
isEditingInstructions: PropTypes.bool.isRequired,
isFlexResizingSupported: PropTypes.bool.isRequired,
resizableFlexGrow: ImmutablePropTypes.list.isRequired,
resizableFlexRefs: PropTypes.array.isRequired,
shouldRenderOutput: PropTypes.bool.isRequired,
title: PropTypes.string.isRequired,
onClickInstructionsEditButton: PropTypes.func.isRequired,
onComponentToggle: PropTypes.func.isRequired,
onResizableFlexDividerDrag: PropTypes.func.isRequired,
onStartDragColumnDivider: PropTypes.func.isRequired,
onStopDragColumnDivider: PropTypes.func.isRequired,
};
Workspace.defaultProps = {
currentProject: null,
};