-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathIDEView.jsx
298 lines (269 loc) · 9.42 KB
/
IDEView.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
import React, { useEffect, useRef, useState } from 'react';
import { useLocation, Prompt, useParams, useHistory } from 'react-router-dom';
import { useDispatch, useSelector } from 'react-redux';
import { useTranslation } from 'react-i18next';
import { Helmet } from 'react-helmet';
import SplitPane from 'react-split-pane';
import MediaQuery from 'react-responsive';
import IDEKeyHandlers from '../components/IDEKeyHandlers';
import Sidebar from '../components/Sidebar';
import PreviewFrame from '../components/PreviewFrame';
import Console from '../components/Console';
import Toast from '../components/Toast';
import { updateFileContent } from '../actions/files';
import {
autosaveProject,
clearPersistedState,
getProject
} from '../actions/project';
import { getIsUserOwner } from '../selectors/users';
import RootPage from '../../../components/RootPage';
import Header from '../components/Header';
import FloatingActionButton from '../components/FloatingActionButton';
import Editor from '../components/Editor';
import {
EditorSidebarWrapper,
PreviewWrapper
} from '../components/Editor/MobileEditor';
import IDEOverlays from '../components/IDEOverlays';
function getTitle(project) {
const { id } = project;
return id ? `p5.js Web Editor | ${project.name}` : 'p5.js Web Editor';
}
function isAuth(pathname) {
return pathname === '/login' || pathname === '/signup';
}
function isOverlay(pathname) {
return pathname === '/about' || pathname === '/feedback';
}
function WarnIfUnsavedChanges() {
const hasUnsavedChanges = useSelector((state) => state.ide.unsavedChanges);
const { t } = useTranslation();
const currentLocation = useLocation();
// beforeunload handles closing or refreshing the window.
useEffect(() => {
const handleUnload = (e) => {
// See: https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event#browser_compatibility
e.preventDefault();
e.returnValue = t('Nav.WarningUnsavedChanges');
};
if (hasUnsavedChanges) {
window.addEventListener('beforeunload', handleUnload);
} else {
window.removeEventListener('beforeunload', handleUnload);
}
return () => {
window.removeEventListener('beforeunload', handleUnload);
};
}, [t, hasUnsavedChanges]);
// Prompt handles internal navigation between pages.
return (
<Prompt
when={hasUnsavedChanges}
message={(nextLocation) => {
if (
isAuth(nextLocation.pathname) ||
isAuth(currentLocation.pathname) ||
isOverlay(nextLocation.pathname) ||
isOverlay(currentLocation.pathname) ||
nextLocation.state?.confirmed
) {
return true; // allow navigation
}
return t('Nav.WarningUnsavedChanges');
}}
/>
);
}
export const CmControllerContext = React.createContext({});
const IDEView = () => {
const ide = useSelector((state) => state.ide);
const preferences = useSelector((state) => state.preferences);
const project = useSelector((state) => state.project);
const isUserOwner = useSelector(getIsUserOwner);
const dispatch = useDispatch();
const { t } = useTranslation();
const params = useParams();
const [consoleSize, setConsoleSize] = useState(150);
const [sidebarSize, setSidebarSize] = useState(160);
const [isOverlayVisible, setIsOverlayVisible] = useState(false);
const [MaxSize, setMaxSize] = useState(window.innerWidth);
const history = useHistory();
const cmRef = useRef({});
const autosaveIntervalRef = useRef(null);
const syncFileContent = () => {
const file = cmRef.current.getContent();
dispatch(updateFileContent(file.id, file.content));
};
useEffect(() => {
dispatch(clearPersistedState());
}, [dispatch]);
useEffect(() => {
const { project_id: id, username } = params;
if (id && project.id !== id) {
dispatch(getProject(id, username));
}
}, [dispatch, params, project.id]);
useEffect(() => {
if (!isUserOwner && project.visibility === 'Private') {
// TODO: we might want to have a 'Sorry, this sketch is private' page for this
history.push('/');
}
}, [isUserOwner, project.visibility, history]);
const autosaveAllowed = isUserOwner && project.id && preferences.autosave;
const shouldAutosave = autosaveAllowed && ide.unsavedChanges;
// For autosave - send to API after 5 seconds without changes
useEffect(() => {
const handleAutosave = () => {
dispatch(autosaveProject());
};
if (autosaveIntervalRef.current) {
clearTimeout(autosaveIntervalRef.current);
}
if (shouldAutosave) {
autosaveIntervalRef.current = setTimeout(handleAutosave, 5000);
}
return () => {
if (autosaveIntervalRef.current) {
clearTimeout(autosaveIntervalRef.current);
}
};
}, [shouldAutosave, dispatch]);
useEffect(() => {
const updateInnerWidth = (e) => {
setMaxSize(e.target.innerWidth);
};
window.addEventListener('resize', updateInnerWidth);
return () => {
window.removeEventListener('resize', updateInnerWidth);
};
}, [setMaxSize]);
const consoleCollapsedSize = 29;
const currentConsoleSize = ide.consoleIsExpanded
? consoleSize
: consoleCollapsedSize;
return (
<RootPage>
<Helmet>
<title>{getTitle(project)}</title>
</Helmet>
<IDEKeyHandlers getContent={() => cmRef.current?.getContent()} />
<WarnIfUnsavedChanges />
<Toast />
<CmControllerContext.Provider value={cmRef}>
<Header syncFileContent={syncFileContent} />
</CmControllerContext.Provider>
<MediaQuery minWidth={770}>
{(matches) =>
matches ? (
<main className="editor-preview-container">
<SplitPane
split="vertical"
size={ide.sidebarIsExpanded ? sidebarSize : 20}
onChange={(size) => {
setSidebarSize(size);
}}
allowResize={ide.sidebarIsExpanded}
minSize={150}
>
<Sidebar />
<SplitPane
split="vertical"
maxSize={MaxSize * 0.965}
defaultSize="50%"
onChange={() => {
setIsOverlayVisible(true);
}}
onDragFinished={() => {
setIsOverlayVisible(false);
}}
resizerStyle={{
borderLeftWidth: '2px',
borderRightWidth: '2px',
width: '2px',
margin: '0px 0px'
}}
>
<SplitPane
split="horizontal"
primary="second"
size={currentConsoleSize}
minSize={consoleCollapsedSize}
onChange={(size) => {
setConsoleSize(size);
}}
allowResize={ide.consoleIsExpanded}
className="editor-preview-subpanel"
>
<Editor
provideController={(ctl) => {
cmRef.current = ctl;
}}
/>
<Console />
</SplitPane>
<section className="preview-frame-holder">
<header className="preview-frame__header">
<h2 className="preview-frame__title">
{t('Toolbar.Preview')}
</h2>
</header>
<div className="preview-frame__content">
<PreviewFrame
cmController={cmRef.current}
isOverlayVisible={isOverlayVisible}
/>
</div>
</section>
</SplitPane>
</SplitPane>
</main>
) : (
<>
<FloatingActionButton
syncFileContent={syncFileContent}
offsetBottom={ide.isPlaying ? currentConsoleSize : 0}
/>
<PreviewWrapper show={ide.isPlaying}>
<SplitPane
style={{ position: 'static' }}
split="horizontal"
primary="second"
size={currentConsoleSize}
minSize={consoleCollapsedSize}
onChange={(size) => {
setConsoleSize(size);
setIsOverlayVisible(true);
}}
onDragFinished={() => {
setIsOverlayVisible(false);
}}
allowResize={ide.consoleIsExpanded}
className="editor-preview-subpanel"
>
<PreviewFrame
fullView
hide={!ide.isPlaying}
cmController={cmRef.current}
isOverlayVisible={isOverlayVisible}
/>
<Console />
</SplitPane>
</PreviewWrapper>
<EditorSidebarWrapper show={!ide.isPlaying}>
<Sidebar />
<Editor
provideController={(ctl) => {
cmRef.current = ctl;
}}
/>
</EditorSidebarWrapper>
</>
)
}
</MediaQuery>
<IDEOverlays />
</RootPage>
);
};
export default IDEView;