-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathApp.tsx
More file actions
276 lines (247 loc) · 11.3 KB
/
App.tsx
File metadata and controls
276 lines (247 loc) · 11.3 KB
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
// SPDX-License-Identifier: MIT
// Copyright (c) 2020-2024 The Pybricks Authors
import 'react-splitter-layout/lib/index.css';
import './app.scss';
import { Classes, Spinner } from '@blueprintjs/core';
import React, { useEffect, useState } from 'react';
import SplitterLayout from 'react-splitter-layout';
import { useLocalStorage, useTernaryDarkMode } from 'usehooks-ts';
import Activities from '../activities/Activities';
import DfuWindowsDriverInstallDialog from '../firmware/dfuWindowsDriverInstallDialog/DfuWindowsDriverInstallDialog';
import { InstallPybricksDialog } from '../firmware/installPybricksDialog/InstallPybricksDialog';
import RestoreOfficialDialog from '../firmware/restoreOfficialDialog/RestoreOfficialDialog';
import { useSettingIsShowDocsEnabled } from '../settings/hooks';
import SponsorDialog from '../sponsor/SponsorDialog';
import StatusBar from '../status-bar/StatusBar';
import Toolbar from '../toolbar/Toolbar';
import Tour from '../tour/Tour';
import { isMacOS } from '../utils/os';
import { useAppLastDocsPageSetting } from './hooks';
import { useI18n } from './i18n';
const Editor = React.lazy(async () => {
const [sagaModule, componentModule] = await Promise.all([
import('../editor/sagas'),
import('../editor/Editor'),
]);
window.dispatchEvent(
new CustomEvent('pb-lazy-saga', { detail: { saga: sagaModule.default } }),
);
return componentModule;
});
const Terminal = React.lazy(async () => {
const [sagaModule, componentModule] = await Promise.all([
import('../terminal/sagas'),
import('../terminal/Terminal'),
]);
window.dispatchEvent(
new CustomEvent('pb-lazy-saga', { detail: { saga: sagaModule.default } }),
);
return componentModule;
});
const HubCenter = React.lazy(async () => {
const [sagaModule, componentModule] = await Promise.all([
import('../hubcenter/sagas'),
import('../hubcenter/HubCenterDialog'),
]);
window.dispatchEvent(
new CustomEvent('pb-lazy-saga', { detail: { saga: sagaModule.default } }),
);
return componentModule;
});
const Docs: React.FunctionComponent = () => {
const { setIsSettingShowDocsEnabled } = useSettingIsShowDocsEnabled();
const { initialDocsPage, setLastDocsPage } = useAppLastDocsPageSetting();
return (
<iframe
// REVISIT: some of this could be moved to the docs repo
// so that it runs earlier to prevent flashing in the UI.
// The load event doesn't run until after the page is fully
// loaded and there doesn't seem to be a reasonable way to
// hook into the iframe to know when it has a new document.
onLoad={(e) => {
// HACK: this mess restores the scroll position when
// the documentation iframe visibility is toggled.
// The iframe will be automatically scrolled to 0 when
// CSS `display: none` is set.
const target = e.target as HTMLIFrameElement;
const contentWindow = target.contentWindow;
if (!contentWindow) {
console.error('could not get iframe content window');
return;
}
// the last "good" scrollY value of the iframe
let iframeScroll = 0;
// This bit monitors the visibility.
// https://stackoverflow.com/a/44670818/1976323
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
// Restore the scroll position when the iframe is
// shown. Toggling the visibility prevents flashing
// the contents from the top of the page before the
// scroll is done.
if (entry.intersectionRatio > 0) {
contentWindow.scrollTo(0, iframeScroll);
contentWindow.document.documentElement.style.visibility =
'visible';
} else {
contentWindow.document.documentElement.style.visibility =
'hidden';
}
});
},
{
root: target.parentElement,
},
);
observer.observe(target);
// Have to remove he observer, otherwise we end up with
// conflicting values when a new page is loaded in the iframe.
contentWindow.addEventListener('unload', () => {
observer.unobserve(target);
});
// And this keeps track of the scroll position.
contentWindow.addEventListener('scroll', () => {
if (contentWindow.scrollY !== 0) {
// Record the current scroll position. If it is 0, it
// could be that the iframe has been hidden or the user
// scrolled there. So we have to ignore 0. But we don't
// want to be one pixel off if the user really did
// scroll there, so we assume that if the last scroll
// is 1, then the user probably went all the way to 0.
if (contentWindow.scrollY === 1) {
iframeScroll = 0;
} else {
iframeScroll = contentWindow.scrollY;
}
}
});
// Override browser default key bindings in iframe.
contentWindow.document.addEventListener('keydown', (e) => {
// use Ctrl-D/Cmd-D to toggle docs
if (
(isMacOS()
? e.metaKey && !e.ctrlKey
: e.ctrlKey && !e.metaKey) &&
!e.altKey &&
e.key === 'd'
) {
e.preventDefault();
// since the iframe is only visible when docs are shown
// the only action is to hide the docs
setIsSettingShowDocsEnabled(false);
}
});
if (document.body.classList.contains(Classes.DARK)) {
contentWindow.document.documentElement.classList.add(Classes.DARK);
}
setLastDocsPage(contentWindow.location.href);
}}
src={initialDocsPage}
allowFullScreen={true}
width="100%"
height="100%"
/>
);
};
const App: React.FunctionComponent = () => {
const i18n = useI18n();
const { isDarkMode } = useTernaryDarkMode();
const { isSettingShowDocsEnabled } = useSettingIsShowDocsEnabled();
const [isDragging, setIsDragging] = useState(false);
const [docsSplit, setDocsSplit] = useLocalStorage('app-docs-split', 30);
const [terminalSplit, setTerminalSplit] = useLocalStorage('app-terminal-split', 30);
// Classes.DARK has to be applied to body element, otherwise it won't
// affect portals
useEffect(() => {
if (!isDarkMode) {
// no class for light mode, so nothing to do
return;
}
document.body.classList.add(Classes.DARK);
return () => document.body.classList.remove(Classes.DARK);
}, [isDarkMode]);
useEffect(() => {
const listener = (e: KeyboardEvent) => {
// prevent default browser keyboard shortcuts that we use
// NB: some of these like 'n' and 'w' cannot be prevented when
// running "in the browser"
if (e.ctrlKey && ['d', 'n', 's', 'w'].includes(e.key)) {
e.preventDefault();
}
};
addEventListener('keydown', listener);
return () => removeEventListener('keydown', listener);
}, []);
return (
<div className="pb-app" onContextMenu={(e) => e.preventDefault()}>
<div className="pb-app-body">
<aside
className="pb-app-activities"
aria-label={i18n.translate('landmark.activities')}
>
<Activities />
</aside>
{/* need a container with position: relative; for SplitterLayout since it uses position: absolute; */}
<div className="pb-app-main" style={{ position: 'relative' }}>
<SplitterLayout
customClassName={
isSettingShowDocsEnabled ? 'pb-show-docs' : 'pb-hide-docs'
}
onDragStart={(): void => setIsDragging(true)}
onDragEnd={(): void => setIsDragging(false)}
percentage={true}
secondaryInitialSize={docsSplit}
onSecondaryPaneSizeChange={setDocsSplit}
>
<SplitterLayout
vertical={true}
percentage={true}
secondaryInitialSize={terminalSplit}
onSecondaryPaneSizeChange={setTerminalSplit}
>
<main
className="pb-app-editor"
aria-label={i18n.translate('landmark.editor')}
>
<Toolbar />
<React.Suspense
fallback={<Spinner className="pb-editor" />}
>
<Editor />
</React.Suspense>
</main>
<aside
className="pb-app-terminal"
aria-label={i18n.translate('landmark.terminal')}
>
<React.Suspense
fallback={<Spinner className="h-100" />}
>
<Terminal />
</React.Suspense>
</aside>
</SplitterLayout>
<aside
className="pb-app-docs"
aria-label={i18n.translate('landmark.documentation')}
>
{isDragging && <div className="pb-app-docs-drag-helper" />}
<Docs />
</aside>
</SplitterLayout>
</div>
</div>
<StatusBar />
<Tour />
<DfuWindowsDriverInstallDialog />
<InstallPybricksDialog />
<RestoreOfficialDialog />
<SponsorDialog />
<React.Suspense fallback={<Spinner className="h-100" />}>
<HubCenter />
</React.Suspense>
</div>
);
};
export default App;