-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
IDEView.jsx
244 lines (220 loc) · 7.7 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
import React, { useEffect, useRef, useState } from 'react';
import { useLocation, Prompt, useParams } 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();
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 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]);
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]);
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={125}
>
<Sidebar />
<SplitPane
split="vertical"
defaultSize="50%"
onChange={() => {
setIsOverlayVisible(true);
}}
onDragFinished={() => {
setIsOverlayVisible(false);
}}
resizerStyle={{
borderLeftWidth: '2px',
borderRightWidth: '2px',
width: '2px',
margin: '0px 0px'
}}
>
<SplitPane
split="horizontal"
primary="second"
size={ide.consoleIsExpanded ? consoleSize : 29}
minSize={29}
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">
<div
className="preview-frame-overlay"
style={{ display: isOverlayVisible ? 'block' : 'none' }}
/>
<div>
{((preferences.textOutput || preferences.gridOutput) &&
ide.isPlaying) ||
ide.isAccessibleOutputPlaying}
</div>
<PreviewFrame cmController={cmRef.current} />
</div>
</section>
</SplitPane>
</SplitPane>
</main>
) : (
<>
<FloatingActionButton syncFileContent={syncFileContent} />
<PreviewWrapper show={ide.isPlaying}>
<SplitPane
style={{ position: 'static' }}
split="horizontal"
primary="second"
minSize={200}
>
<PreviewFrame
fullView
hide={!ide.isPlaying}
cmController={cmRef.current}
/>
<Console />
</SplitPane>
</PreviewWrapper>
<EditorSidebarWrapper show={!ide.isPlaying}>
<Sidebar />
<Editor
provideController={(ctl) => {
cmRef.current = ctl;
}}
/>
</EditorSidebarWrapper>
</>
)
}
</MediaQuery>
<IDEOverlays />
</RootPage>
);
};
export default IDEView;