-
Notifications
You must be signed in to change notification settings - Fork 628
Expand file tree
/
Copy pathsidepanel.tsx
More file actions
294 lines (257 loc) · 13.1 KB
/
sidepanel.tsx
File metadata and controls
294 lines (257 loc) · 13.1 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/// <reference path="../../built/pxtlib.d.ts" />
import * as React from "react";
import * as data from "./data";
import * as filelist from "./filelist";
import * as keymap from "./keymap";
import * as serialindicator from "./serialindicator"
import * as simtoolbar from "./simtoolbar";
import * as simulator from "./simulator";
import { Button } from "./sui";
import { SimulatorPresenceBar } from "./components/SimulatorPresenceBar"
import { TutorialContainer } from "./components/tutorial/TutorialContainer";
import { VerticalResizeContainer } from '../../react-common/components/controls/VerticalResizeContainer'
import ISettingsProps = pxt.editor.ISettingsProps;
import { classList } from "../../react-common/components/util";
interface SidepanelState {
resized?: boolean;
height?: number;
lastResizeHeight?: number;
shouldResize?: boolean;
}
interface SidepanelProps extends ISettingsProps {
inHome: boolean;
showKeymap?: boolean;
showSerialButtons?: boolean;
showFileList?: boolean;
showFullscreenButton?: boolean;
isMultiplayerGame?: boolean;
collapseEditorTools?: boolean;
simSerialActive?: boolean;
deviceSerialActive?: boolean;
tutorialOptions?: pxt.tutorial.TutorialOptions;
/** An optional tutorial layout mode where the sim is on the left and instructions are at the top, regardless of screen size. */
tutorialSimSidebar?: boolean;
onTutorialStepChange?: (step: number) => void;
onTutorialComplete?: () => void;
setEditorOffset?: () => void;
showMiniSim: (visible?: boolean) => void;
openSerial: (isSim: boolean) => void;
handleHardwareDebugClick: () => void;
handleFullscreenButtonClick: () => void;
}
export class Sidepanel extends data.Component<SidepanelProps, SidepanelState> {
protected simRef: HTMLDivElement
protected simPanelRef: HTMLDivElement;
private simResizeObserver?: ResizeObserver;
private simPanelResizeObserver?: ResizeObserver;
constructor(props: SidepanelProps) {
super(props);
if (props.tutorialOptions?.tutorial && !props.tutorialSimSidebar) {
this.props.showMiniSim(true);
}
this.updateShouldResize = this.updateShouldResize.bind(this);
}
UNSAFE_componentWillReceiveProps(props: SidepanelProps) {
// This is necessary because we are not properly mounting and
// unmounting the component as we enter/exit the editor. We
// instead manually reset the state as we transition.
if (!props.tutorialSimSidebar
&& ((!this.props.tutorialOptions && props.tutorialOptions)
|| (this.props.inHome && !props.inHome && props.tutorialOptions)
|| (this.props.tutorialOptions?.tutorial && props.tutorialOptions?.tutorial && this.props.tutorialOptions.tutorial !== props.tutorialOptions.tutorial))) {
this.props.showMiniSim(true);
} else if (
(!this.props.inHome && props.inHome) ||
(this.props.tutorialOptions && !props.tutorialOptions)
) {
this.showSimulator();
}
}
componentDidMount(): void {
this.updateShouldResize();
}
componentDidUpdate(props: SidepanelProps, state: SidepanelState) {
if ((this.state.height || state.height) && this.state.height != state.height) {
this.props.setEditorOffset();
}
this.updateShouldResize();
}
componentWillUnmount(): void {
if (this.simResizeObserver && this.simRef) {
this.simResizeObserver.unobserve(this.simRef);
}
if (this.simPanelResizeObserver && this.simPanelRef) {
this.simPanelResizeObserver.unobserve(this.simPanelRef);
}
}
private updateShouldResize() {
const shouldResize = pxt.BrowserUtils.isTabletSize() || this.props.tutorialSimSidebar;
if (shouldResize != this.state.shouldResize) {
let height = this.state.height;
if (shouldResize && !this.state.height && this.state.lastResizeHeight) {
height = this.state.lastResizeHeight;
}
this.setState({ shouldResize, height });
}
}
protected tryShowSimulator = () => {
const isTabTutorial = this.props.tutorialOptions?.tutorial && !pxt.BrowserUtils.useOldTutorialLayout();
const hasSimulator = !pxt.appTarget.simulator?.headless;
const includeSimulator = (!isTabTutorial || this.props.tutorialSimSidebar) && hasSimulator;
if (includeSimulator) {
this.showSimulator();
}
}
protected showSimulator = () => {
this.props.showMiniSim(false);
simulator.driver.focus();
}
protected handleSimSerialClick = () => {
this.props.openSerial(true);
}
protected handleDeviceSerialClick = () => {
this.props.openSerial(false);
}
protected handleSimOverlayClick = () => {
const { tutorialOptions, handleFullscreenButtonClick } = this.props;
if (!tutorialOptions || pxt.BrowserUtils.useOldTutorialLayout()) {
handleFullscreenButtonClick();
} else {
this.tryShowSimulator();
}
}
protected handleSimPanelRef = (c: HTMLDivElement) => {
this.simPanelRef = c;
if (c && typeof ResizeObserver !== "undefined") {
this.simPanelResizeObserver = new ResizeObserver(() => {
const scrollVisible = c.scrollHeight > c.clientHeight;
if (scrollVisible)
this.simPanelRef?.classList.remove("invisibleScrollbar");
else
this.simPanelRef?.classList.add("invisibleScrollbar");
})
this.simPanelResizeObserver.observe(c);
}
}
protected handleSimRef = (c: HTMLDivElement) => {
this.simRef = c;
if (c && typeof ResizeObserver !== "undefined") {
this.simResizeObserver = new ResizeObserver(() => {
window.requestAnimationFrame(this.updateShouldResize);
});
this.simResizeObserver.observe(c);
}
}
protected setComponentHeight = (height?: number, isResize?: boolean) => {
if (height != this.state.height || isResize != this.state.resized) {
this.setState({
resized: this.state.resized || isResize,
height: height,
lastResizeHeight: isResize ? height : this.state.lastResizeHeight});
}
}
onHostMultiplayerGameClick = (evt: any) => {
evt.preventDefault();
pxt.tickEvent("sidepanel.hostmultiplayergame");
this.props.parent.showShareDialog(undefined, "multiplayer");
}
onOpenInVSCodeClick = (evt: any) => {
evt.preventDefault();
pxt.tickEvent("sidepanel.openinvscode");
this.props.parent.showShareDialog(undefined, "vscode");
}
onResizeDrag = (newSize: number) => {
this.setComponentHeight(newSize, true);
}
onResizeEnd = (newSize: number) => {
pxt.tickEvent("tutorial.resizeInstructions", {newSize });
}
renderCore() {
const { parent, inHome, showKeymap, showSerialButtons, showFileList, showFullscreenButton, isMultiplayerGame,
collapseEditorTools, simSerialActive, deviceSerialActive, tutorialOptions,
handleHardwareDebugClick, onTutorialStepChange, onTutorialComplete } = this.props;
const hasSimulator = !pxt.appTarget.simulator?.headless;
const showOpenInVscodeButton = parent.isJavaScriptActive()
&& pxt.appTarget?.appTheme?.showOpenInVscode
&& !pxt.shell.isTimeMachineEmbed();
const showHostMultiplayerGameButton = isMultiplayerGame
&& !pxt.shell.isTimeMachineEmbed();
const simContainerClassName = classList(
"simulator-container",
!this.props.tutorialSimSidebar && "hidden"
);
const outerTutorialContainerClassName = classList(
"editor-sidebar",
"tutorialWrapper",
"tutorial-container-outer",
this.props.tutorialSimSidebar && "topInstructions"
);
const editorSidebarClassName = classList(
"editor-sidebar",
this.props.tutorialSimSidebar && "tutorial-sim"
);
const editorSidebarHeight = this.state.shouldResize && this.state.height ? `${this.state.height}px` : undefined;
const tutorialContainer = tutorialOptions ? <TutorialContainer
parent={parent}
tutorialId={tutorialOptions.tutorial}
name={tutorialOptions.tutorialName}
steps={tutorialOptions.tutorialStepInfo}
currentStep={tutorialOptions.tutorialStep}
tutorialOptions={tutorialOptions}
hideIteration={tutorialOptions.metadata?.hideIteration}
hasTemplate={!!tutorialOptions.templateCode}
preferredEditor={tutorialOptions.metadata?.preferredEditor}
tutorialSimSidebar={this.props.tutorialSimSidebar}
hasBeenResized={this.state.resized && this.state.shouldResize}
onTutorialStepChange={onTutorialStepChange}
onTutorialComplete={onTutorialComplete}
setParentHeight={newSize => this.setComponentHeight(newSize, false)} /> : undefined;
return <div id="simulator" className="simulator" ref={this.handleSimRef}>
{!hasSimulator && <div id="boardview" className="headless-sim" role="region" aria-label={lf("Simulator")} tabIndex={-1} />}
<div id="editorSidebar" className={editorSidebarClassName} style={!this.props.tutorialSimSidebar ? { height: editorSidebarHeight } : undefined}>
<div className={simContainerClassName}>
<div className={`ui items simPanel ${showHostMultiplayerGameButton ? "multiplayer-preview" : ""}`} ref={this.handleSimPanelRef}>
<div id="boardview" className="ui vertical editorFloat" role="region" aria-label={lf("Simulator")} tabIndex={inHome ? -1 : 0} />
{showHostMultiplayerGameButton && <div className="ui item grid centered portrait multiplayer-presence">
<SimulatorPresenceBar />
</div>}
<simtoolbar.SimulatorToolbar parent={parent} collapsed={collapseEditorTools} simSerialActive={simSerialActive} devSerialActive={deviceSerialActive} showSimulatorSidebar={this.tryShowSimulator} />
{showKeymap && <keymap.Keymap parent={parent} />}
<div className="ui item portrait hide hidefullscreen">
{pxt.options.debug && <Button key="hwdebugbtn" className="tertiary" icon="xicon chip" text={"Dev Debug"} onClick={handleHardwareDebugClick} />}
</div>
<div className="ui item grid centered portrait hide hidefullscreen">
{showOpenInVscodeButton && <Button className={"tertiary hostmultiplayergame-button"} icon={"icon share"} text={lf("Open in VS Code")} ariaLabel={lf("Open in Visual Studio Code for Web")} onClick={this.onOpenInVSCodeClick} />}
</div>
<div className="ui item grid centered portrait hide hidefullscreen">
{showHostMultiplayerGameButton && <Button className={"tertiary hostmultiplayergame-button"} icon={"xicon multiplayer"} text={lf("Host multiplayer game")} ariaLabel={lf("Host multiplayer game")} onClick={this.onHostMultiplayerGameClick} />}
</div>
{showSerialButtons && <div id="serialPreview" className="ui editorFloat portrait hide hidefullscreen">
<serialindicator.SerialIndicator ref="simIndicator" isSim={true} onClick={this.handleSimSerialClick} parent={parent} />
<serialindicator.SerialIndicator ref="devIndicator" isSim={false} onClick={this.handleDeviceSerialClick} parent={parent} />
</div>}
{showFileList && <filelist.FileList parent={parent} />}
{showFullscreenButton && <div id="miniSimOverlay" role="button" title={lf("Open in fullscreen")} onClick={this.handleSimOverlayClick} />}
</div>
</div>
</div>
{tutorialOptions &&
<div className={this.props.tutorialSimSidebar ? "topInstructionsWrapper" : ""}>
{this.state.shouldResize ?
<VerticalResizeContainer
className={outerTutorialContainerClassName}
maxHeight="500px"
minHeight="100px"
initialHeight={editorSidebarHeight}
resizeEnabled={this.state.shouldResize}
onResizeDrag={this.onResizeDrag}
onResizeEnd={this.onResizeEnd}>
{tutorialContainer}
</VerticalResizeContainer> :
<div className={outerTutorialContainerClassName}>
{tutorialContainer}
</div>}
</div>}
</div>
}
}