forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer.tsx
More file actions
790 lines (666 loc) · 34.2 KB
/
container.tsx
File metadata and controls
790 lines (666 loc) · 34.2 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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
/// <reference path="../../built/pxtlib.d.ts" />
import * as React from "react";
import * as data from "./data";
import * as sui from "./sui";
import * as core from "./core";
import * as auth from "./auth";
import * as pkg from "./package";
import { fireClickOnEnter } from "./util";
import IProjectView = pxt.editor.IProjectView;
import ISettingsProps = pxt.editor.ISettingsProps;
import UserInfo = pxt.editor.UserInfo;
import SimState = pxt.editor.SimState;
import { sendUpdateFeedbackTheme } from "../../react-common/components/controls/Feedback/FeedbackEventListener";
import { TabPane } from "./components/core/TabPane";
import KeyboardControlsHelp from "./components/KeyboardControlsHelp";
// common menu items -- do not remove
// lf("About")
// lf("Getting started")
// lf("Buy")
// lf("Blocks")
// lf("Examples")
// lf("Tutorials")
// lf("Projects")
// lf("Reference")
// lf("Support")
// lf("Hardware")
// lf("Tour")
function openTutorial(parent: IProjectView, path: string) {
pxt.tickEvent(`docs`, { path }, { interactiveConsent: true });
parent.startActivity({
activity: "tutorial",
path
});
}
function openDocs(parent: IProjectView, path: string) {
pxt.tickEvent(`docs`, { path }, { interactiveConsent: true });
parent.setSideDoc(path);
}
function startTour(parent: IProjectView) {
pxt.tickEvent(`tour.start`, { origin: "help-menu" });
parent.showOnboarding();
}
function openKeyboardNavHelp(parent: IProjectView) {
parent.toggleBuiltInSideDoc("keyboardControls", true);
}
function renderDocItems(parent: IProjectView, elements: pxt.DocMenuEntry[], cls: string = "") {
return elements.map(m =>
m.tutorial ? <DocsMenuItem key={"docsmenututorial" + m.path} role="menuitem" ariaLabel={pxt.Util.rlf(m.name)} text={pxt.Util.rlf(m.name)} className={"ui " + cls} parent={parent} path={m.path} onItemClick={openTutorial} />
: !/^\//.test(m.path) ? <a key={"docsmenulink" + m.path} role="menuitem" aria-label={m.name} title={m.name} className={`ui item link ${cls}`} href={m.path} target="docs">{pxt.Util.rlf(m.name)}</a>
: <DocsMenuItem key={"docsmenu" + m.path} role="menuitem" ariaLabel={pxt.Util.rlf(m.name)} text={pxt.Util.rlf(m.name)} className={"ui " + cls} parent={parent} path={m.path} onItemClick={openDocs} />
);
}
// Always append a link to the appropriate language (Blocks, JS, Python) to the help menu
function getDocsLanguageItem(editor: DocsMenuEditorName, parent: IProjectView, cls: string = ""): JSX.Element {
const path = "/" + editor.toLowerCase();
// Use rlf as "Blocks" is localized above & "JavaScript" and "Python" should not be localized
return <DocsMenuItem key={"docsmenu" + path} role="menuitem" ariaLabel={pxt.Util.rlf(editor)} text={pxt.Util.rlf(editor)} className={`ui ${cls}`} parent={parent} path={path} onItemClick={openDocs} />
}
function getTourItem(parent: IProjectView, cls: string = ""): JSX.Element {
const path = "/tour";
return <DocsMenuItem key={"docsmenu" + path} role="menuitem" ariaLabel={lf("Tour")} text={lf("Tour")} className={`ui ${cls}`} parent={parent} path={path} onItemClick={startTour} />
}
function getKeyboardNavHelpItem(parent: IProjectView, cls: string = ""): JSX.Element {
const path = "/keyboardControls";
return <DocsMenuItem key={"docsmenu" + path} role="menuitem" ariaLabel={lf("Keyboard Controls")} text={lf("Keyboard Controls")} className={`ui ${cls}`} parent={parent} path={path} onItemClick={openKeyboardNavHelp} />
}
type DocsMenuEditorName = "Blocks" | "JavaScript" | "Python";
interface DocsMenuProps extends ISettingsProps {
editor: DocsMenuEditorName;
}
function showKeyboardControls() {
const languageRestriction = pkg.mainPkg?.config?.languageRestriction;
const pyOnly = languageRestriction === pxt.editor.LanguageRestriction.PythonOnly;
const noBlocks = languageRestriction === pxt.editor.LanguageRestriction.NoBlocks;
const tsOnly = languageRestriction === pxt.editor.LanguageRestriction.JavaScriptOnly;
return !pyOnly && !tsOnly && !noBlocks && !!pkg.mainEditorPkg().files[pxt.MAIN_BLOCKS];
}
export class DocsMenu extends data.PureComponent<DocsMenuProps & { hasMainBlocksFile: boolean }, {}> {
renderCore() {
const parent = this.props.parent;
const targetTheme = pxt.appTarget.appTheme;
const accessibleBlocksEnabled = data.getData<boolean>(auth.ACCESSIBLE_BLOCKS);
return <sui.DropdownMenu role="menuitem" icon="help circle large"
className="item mobile hide help-dropdown-menuitem" textClass={"landscape only"} title={lf("Help")} >
{this.props.hasMainBlocksFile && showKeyboardControls() && accessibleBlocksEnabled && getKeyboardNavHelpItem(parent)}
{targetTheme.tours?.editor && getTourItem(parent)}
{renderDocItems(parent, targetTheme.docMenu)}
{getDocsLanguageItem(this.props.editor, parent)}
</sui.DropdownMenu>
}
}
interface DocsMenuItemProps extends sui.ItemProps {
parent: IProjectView;
path: string;
onItemClick: (parent: IProjectView, path: string) => void;
}
class DocsMenuItem extends sui.StatelessUIElement<DocsMenuItemProps> {
constructor(props: DocsMenuItemProps) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
const { onItemClick, parent, path } = this.props;
onItemClick(parent, path);
}
renderCore() {
const { onClick, onItemClick, parent, path, ...rest } = this.props;
return <sui.Item {...rest} onClick={this.handleClick} />
}
}
export interface SettingsMenuProps extends ISettingsProps {
greenScreen: boolean;
accessibleBlocks: boolean;
showShare?: boolean;
inBlocks: boolean;
}
// This Component overrides shouldComponentUpdate, be sure to update that if the state is updated
export interface SettingsMenuState {
greenScreen?: boolean;
accessibleBlocks?: boolean;
showShare?: boolean;
}
export class SettingsMenu extends data.Component<SettingsMenuProps, SettingsMenuState> {
dropdown: sui.DropdownMenu;
constructor(props: SettingsMenuProps) {
super(props);
this.state = {
}
this.openSettings = this.openSettings.bind(this);
this.showPackageDialog = this.showPackageDialog.bind(this);
this.showBoardDialog = this.showBoardDialog.bind(this);
this.removeProject = this.removeProject.bind(this);
this.saveProject = this.saveProject.bind(this);
this.toggleCollapse = this.toggleCollapse.bind(this);
this.showReportAbuse = this.showReportAbuse.bind(this);
this.showLanguagePicker = this.showLanguagePicker.bind(this);
this.showThemePicker = this.showThemePicker.bind(this);
this.toggleHighContrast = this.toggleHighContrast.bind(this);
this.toggleGreenScreen = this.toggleGreenScreen.bind(this);
this.toggleAccessibleBlocks = this.toggleAccessibleBlocks.bind(this);
this.showResetDialog = this.showResetDialog.bind(this);
this.showShareDialog = this.showShareDialog.bind(this);
this.showFeedbackDialog = this.showFeedbackDialog.bind(this);
this.showExitAndSaveDialog = this.showExitAndSaveDialog.bind(this);
this.pair = this.pair.bind(this);
this.pairBluetooth = this.pairBluetooth.bind(this);
this.showAboutDialog = this.showAboutDialog.bind(this);
this.showTurnBackTimeDialog = this.showTurnBackTimeDialog.bind(this);
this.print = this.print.bind(this);
this.signOutGithub = this.signOutGithub.bind(this);
this.hide = this.hide.bind(this);
}
showExitAndSaveDialog() {
pxt.tickEvent("menu.home", undefined, { interactiveConsent: true });
this.props.parent.showExitAndSaveDialog();
}
showShareDialog() {
pxt.tickEvent("menu.share", undefined, { interactiveConsent: true });
this.props.parent.showShareDialog();
}
showFeedbackDialog() {
pxt.tickEvent("menu.feedback");
this.props.parent.showFeedbackDialog("generic");
}
openSettings() {
pxt.tickEvent("menu.settings", undefined, { interactiveConsent: true });
this.props.parent.openSettings();
}
showPackageDialog() {
pxt.tickEvent("menu.addpackage", undefined, { interactiveConsent: true });
this.props.parent.showPackageDialog();
}
showBoardDialog() {
pxt.tickEvent("menu.changeboard", undefined, { interactiveConsent: true });
if (pxt.hasHwVariants())
this.props.parent.showChooseHwDialog();
else
this.props.parent.showBoardDialogAsync(undefined, true);
}
saveProject() {
pxt.tickEvent("menu.saveproject", undefined, { interactiveConsent: true });
this.props.parent.saveAndCompile();
}
removeProject() {
pxt.tickEvent("menu.removeproject", undefined, { interactiveConsent: true });
this.props.parent.removeProject();
}
toggleCollapse() {
pxt.tickEvent("menu.toggleSim", undefined, { interactiveConsent: true });
this.props.parent.toggleSimulatorCollapse();
}
showReportAbuse() {
pxt.tickEvent("menu.reportabuse", undefined, { interactiveConsent: true });
this.props.parent.showReportAbuse();
}
showLanguagePicker() {
pxt.tickEvent("menu.langpicker", undefined, { interactiveConsent: true });
this.props.parent.showLanguagePicker();
}
showThemePicker() {
pxt.tickEvent("menu.themepicker", undefined, { interactiveConsent: true });
this.props.parent.showThemePicker();
}
toggleHighContrast() {
pxt.tickEvent("menu.togglecontrast", undefined, { interactiveConsent: true });
this.props.parent.toggleHighContrast();
}
toggleGreenScreen() {
pxt.tickEvent("menu.togglegreenscreen", undefined, { interactiveConsent: true });
this.props.parent.toggleGreenScreen();
}
toggleAccessibleBlocks() {
pxt.tickEvent("menu.toggleaccessibleblocks", undefined, { interactiveConsent: true });
this.props.parent.toggleAccessibleBlocks();
}
showResetDialog() {
pxt.tickEvent("menu.reset", undefined, { interactiveConsent: true });
pxt.tickEvent("reset"); // Deprecated, will Feb 2018.
this.props.parent.showResetDialog();
}
pair() {
pxt.tickEvent("menu.pair");
this.props.parent.pairAsync();
}
pairBluetooth() {
pxt.tickEvent("menu.pair.bluetooth")
core.showLoading("webblepair", lf("Pairing Bluetooth device..."))
pxt.webBluetooth.pairAsync()
.then(() => core.hideLoading("webblepair"));
}
showAboutDialog() {
pxt.tickEvent("menu.about");
this.props.parent.showAboutDialog();
}
showTurnBackTimeDialog() {
pxt.tickEvent("menu.turnBackTime");
this.props.parent.showTurnBackTimeDialogAsync();
}
print() {
pxt.tickEvent("menu.print");
this.props.parent.printCode();
}
hide() {
this.dropdown?.hide();
}
signOutGithub() {
pxt.tickEvent("menu.github.signout");
this.hide();
this.props.parent.signOutGithub();
}
UNSAFE_componentWillReceiveProps(nextProps: SettingsMenuProps) {
const newState: SettingsMenuState = {};
if (nextProps.greenScreen !== undefined) {
newState.greenScreen = nextProps.greenScreen;
}
if (nextProps.accessibleBlocks !== undefined) {
newState.accessibleBlocks = nextProps.accessibleBlocks;
}
if (nextProps.showShare !== undefined) {
newState.showShare = nextProps.showShare;
}
if (Object.keys(newState).length > 0) this.setState(newState)
}
shouldComponentUpdate(nextProps: SettingsMenuProps, nextState: SettingsMenuState, nextContext: any): boolean {
return this.state.greenScreen != nextState.greenScreen
|| this.state.accessibleBlocks != nextState.accessibleBlocks
|| this.state.showShare != nextState.showShare
|| nextProps.inBlocks !== this.props.inBlocks
}
renderCore() {
const hasIdentity = pxt.auth.hasIdentity();
const highContrast = this.getData<boolean>(auth.HIGHCONTRAST)
const { greenScreen } = this.state;
const accessibleBlocks = this.getData<boolean>(auth.ACCESSIBLE_BLOCKS);
const targetTheme = pxt.appTarget.appTheme;
const packages = pxt.appTarget.cloud && !!pxt.appTarget.cloud.packages;
const reportAbuse = pxt.appTarget.cloud && pxt.appTarget.cloud.sharing && pxt.appTarget.cloud.importing;
const readOnly = pxt.shell.isReadOnly();
const isController = pxt.shell.isControllerMode();
const disableFileAccessinMaciOs = targetTheme.disableFileAccessinMaciOs && (pxt.BrowserUtils.isIOS() || pxt.BrowserUtils.isMac())
const disableFileAccessinAndroid = pxt.appTarget.appTheme.disableFileAccessinAndroid && pxt.BrowserUtils.isAndroid();
sendUpdateFeedbackTheme(highContrast);
const headless = pxt.appTarget.simulator?.headless;
const showHome = !targetTheme.lockedEditor && !isController && auth.hasIdentity();
const showShare = this.props.showShare && pxt.appTarget.cloud?.sharing && !isController;
const showSave = !readOnly && !isController && !!targetTheme.saveInMenu && !disableFileAccessinMaciOs && !disableFileAccessinAndroid;
const showSimCollapse = !readOnly && !isController && !!targetTheme.simCollapseInMenu && !(headless && this.props.inBlocks);
const showGreenScreen = targetTheme.greenScreen || /greenscreen=1/i.test(window.location.href);
const showPrint = targetTheme.print && !pxt.BrowserUtils.isIE();
const showProjectSettings = targetTheme.showProjectSettings;
const docItems = targetTheme.docMenu && targetTheme.docMenu.filter(d => !d.tutorial);
const usbIcon = pxt.appTarget.appTheme.downloadDialogTheme?.deviceIcon || "usb";
// Electron does not currently support webusb
// Targets with identity show github user on the profile screen.
const githubUser = !hasIdentity && !readOnly && !isController && this.getData("github:user") as UserInfo;
const showPairDevice = pxt.usb.isEnabled;
const showCenterDivider = targetTheme.selectLanguage || targetTheme.highContrast || showGreenScreen || githubUser;
const showFeedbackOption = pxt.U.ocvEnabled();
const simCollapseText = headless ? lf("Toggle the File Explorer") : lf("Toggle the simulator");
const extDownloadMenuItems = pxt.commands.getDownloadMenuItems?.() || [];
return <sui.DropdownMenu role="menuitem" icon={'setting large'} title={lf("Settings")} className="item icon more-dropdown-menuitem" ref={ref => this.dropdown = ref} closeOnItemClick={true} onShow={
() => this.forceUpdate() // force update to refresh extDownloadMenuItems
}>
{showHome && <sui.Item className="mobile only inherit" role="menuitem" icon="home" title={lf("Home")} text={lf("Home")} ariaLabel={lf("Home screen")} onClick={this.showExitAndSaveDialog} />}
{showShare && <sui.Item className="mobile only inherit" role="menuitem" icon="share alternate" title={lf("Publish your game to create a shareable link")} text={lf("Share")} ariaLabel={lf("Share Project")} onClick={this.showShareDialog} />}
{(showHome || showShare) && <div className="ui divider mobile only inherit" />}
{showProjectSettings ? <sui.Item role="menuitem" icon="options" text={lf("Project Settings")} onClick={this.openSettings} /> : undefined}
{packages ? <sui.Item role="menuitem" icon="disk outline" text={lf("Extensions")} onClick={this.showPackageDialog} /> : undefined}
{showPairDevice ? <sui.Item role="menuitem" icon={usbIcon} text={lf("Connect Device")} onClick={this.pair} /> : undefined}
{pxt.webBluetooth.isAvailable() ? <sui.Item role="menuitem" icon='bluetooth' text={lf("Pair Bluetooth")} onClick={this.pairBluetooth} /> : undefined}
{showPrint ? <sui.Item role="menuitem" icon="print" text={lf("Print...")} onClick={this.print} /> : undefined}
{showSave ? <sui.Item role="menuitem" icon="save" text={lf("Save Project")} onClick={this.saveProject} /> : undefined}
{!isController ? <sui.Item role="menuitem" icon="trash" text={lf("Delete Project")} onClick={this.removeProject} /> : undefined}
{targetTheme.timeMachine ? <sui.Item role="menuitem" icon="history" text={lf("Version History")} onClick={this.showTurnBackTimeDialog} /> : undefined}
{showSimCollapse ? <sui.Item role="menuitem" icon='toggle right' text={simCollapseText} onClick={this.toggleCollapse} /> : undefined}
{!!extDownloadMenuItems.length && <>
<div className="ui divider" />
{extDownloadMenuItems.map((props, index) => <sui.Item key={index} role="menuitem" tabIndex={-1} {...props} />)}
</>}
<div className="ui divider"></div>
{targetTheme.selectLanguage ? <sui.Item icon='xicon globe' role="menuitem" text={lf("Language")} onClick={this.showLanguagePicker} /> : undefined}
<sui.Item role="menuitem" icon="paint brush" text={lf("Theme")} onClick={this.showThemePicker} />
{showKeyboardControls() && (<sui.Item role="menuitem" text={accessibleBlocks ? lf("Keyboard Controls Off") : lf("Keyboard Controls On")} onClick={this.toggleAccessibleBlocks} />)}
{showGreenScreen ? <sui.Item role="menuitem" text={greenScreen ? lf("Green Screen Off") : lf("Green Screen On")} onClick={this.toggleGreenScreen} /> : undefined}
{docItems && renderDocItems(this.props.parent, docItems, "setting-docs-item mobile only inherit")}
{githubUser ? <div className="ui divider"></div> : undefined}
{githubUser ? <div className="ui item" title={lf("Unlink {0} from GitHub", githubUser.name)} role="menuitem" onClick={this.signOutGithub}>
<div className="avatar" role="presentation">
<img className="ui circular image" src={githubUser.photo} alt={lf("User picture")} />
</div>
{lf("Disconnect GitHub")}
</div> : undefined}
{showCenterDivider && <div className="ui divider"></div>}
{reportAbuse ? <sui.Item role="menuitem" icon="warning circle" text={lf("Report Abuse...")} onClick={this.showReportAbuse} /> : undefined}
{!isController ? <sui.Item role="menuitem" icon='sign out' text={lf("Reset")} onClick={this.showResetDialog} /> : undefined}
<sui.Item role="menuitem" text={lf("About...")} onClick={this.showAboutDialog} />
{
// we always need a way to clear local storage, regardless if signed in or not
}
{showFeedbackOption ? <sui.Item role="menuitem" icon="comment" text={lf("Feedback")} onClick={this.showFeedbackDialog} /> : undefined}
</sui.DropdownMenu>;
}
}
interface IBaseMenuItemProps extends ISettingsProps {
onClick: (e: React.MouseEvent<HTMLElement>) => void;
isActive: () => boolean;
icon?: string;
text?: string;
title?: string;
className?: string;
ariaLabel?: string;
}
class BaseMenuItemProps extends data.Component<IBaseMenuItemProps, {}> {
constructor(props: IBaseMenuItemProps) {
super(props);
}
renderCore() {
const active = this.props.isActive();
return <sui.Item className={`base-menuitem ${this.props.className} ${active ? "selected" : ""}`} role="menuitem" textClass="landscape only"
text={this.props.text} icon={this.props.icon} active={active} onClick={this.props.onClick} title={this.props.title} ariaLabel={this.props.ariaLabel} />
}
}
class JavascriptMenuItem extends data.Component<ISettingsProps, {}> {
constructor(props: ISettingsProps) {
super(props);
}
protected onClick = (): void => {
pxt.tickEvent("menu.javascript", undefined, { interactiveConsent: true });
this.props.parent.openJavaScript();
}
protected isActive = (): boolean => {
return this.props.parent.isJavaScriptActive();
}
renderCore() {
return <BaseMenuItemProps className="javascript-menuitem" icon="xicon js" text="JavaScript" title={lf("Convert code to JavaScript")} onClick={this.onClick} isActive={this.isActive} parent={this.props.parent} ariaLabel={lf("Convert code to JavaScript")}/>
}
}
class PythonMenuItem extends data.Component<ISettingsProps, {}> {
constructor(props: ISettingsProps) {
super(props);
}
protected onClick = (): void => {
pxt.tickEvent("menu.python", undefined, { interactiveConsent: true });
this.props.parent.openPython();
}
protected isActive = (): boolean => {
return this.props.parent.isPythonActive();
}
renderCore() {
return <BaseMenuItemProps className="python-menuitem" icon="xicon python" text="Python" title={lf("Convert code to Python")} onClick={this.onClick} isActive={this.isActive} parent={this.props.parent} ariaLabel={lf("Convert code to Python")} />
}
}
class BlocksMenuItem extends data.Component<ISettingsProps, {}> {
constructor(props: ISettingsProps) {
super(props);
}
protected onClick = (e: React.MouseEvent<HTMLElement>): void => {
pxt.tickEvent("menu.blocks", undefined, { interactiveConsent: true });
this.props.parent.openBlocks();
}
protected isActive = (): boolean => {
return this.props.parent.isBlocksActive();
}
renderCore() {
return <BaseMenuItemProps className="blocks-menuitem" icon="xicon blocks" text={lf("Blocks")} title={lf("Convert code to Blocks")} onClick={this.onClick} isActive={this.isActive} parent={this.props.parent} ariaLabel={lf("Convert code to Blocks")}/>
}
}
class SandboxMenuItem extends data.Component<ISettingsProps, {}> {
constructor(props: ISettingsProps) {
super(props);
}
protected onClick = (): void => {
pxt.tickEvent("menu.simView", undefined, { interactiveConsent: true });
this.props.parent.openSimView();
}
protected isActive = (): boolean => {
return this.props.parent.isEmbedSimActive();
}
renderCore() {
const active = this.isActive();
const isRunning = this.props.parent.state.simState == SimState.Running;
const runTooltip = isRunning ? lf("Stop the simulator") : lf("Start the simulator");
return <BaseMenuItemProps className="sim-menuitem" icon={active && isRunning ? "stop" : "play"} text={lf("Simulator")} title={!active ? lf("Show Simulator") : runTooltip} onClick={this.onClick} isActive={this.isActive} parent={this.props.parent} />
}
}
class AssetMenuItem extends data.Component<ISettingsProps, {}> {
constructor(props: ISettingsProps) {
super(props);
}
protected onClick = (): void => {
pxt.tickEvent("menu.assets", undefined, { interactiveConsent: true });
this.props.parent.openAssets();
}
protected isActive = (): boolean => {
return this.props.parent.isAssetsActive();
}
renderCore() {
return <BaseMenuItemProps className="assets-menuitem" icon="picture" text={lf("Assets")} title={lf("View project assets")} onClick={this.onClick} isActive={this.isActive} parent={this.props.parent} />
}
}
interface IEditorSelectorProps extends ISettingsProps {
python?: boolean;
sandbox?: boolean;
headless?: boolean;
languageRestriction?: pxt.editor.LanguageRestriction;
}
export class EditorSelector extends data.Component<IEditorSelectorProps, {}> {
constructor(props: ISettingsProps) {
super(props);
}
renderCore() {
const { python, sandbox, headless, languageRestriction, parent } = this.props;
const dropdownActive = python && (parent.isJavaScriptActive() || parent.isPythonActive());
const tsOnly = languageRestriction === pxt.editor.LanguageRestriction.JavaScriptOnly;
const pyOnly = languageRestriction === pxt.editor.LanguageRestriction.PythonOnly;
const blocksOnly = languageRestriction === pxt.editor.LanguageRestriction.BlocksOnly;
const noJavaScript = languageRestriction === pxt.editor.LanguageRestriction.NoJavaScript;
const noPython = languageRestriction === pxt.editor.LanguageRestriction.NoPython;
const noBlocks = languageRestriction === pxt.editor.LanguageRestriction.NoBlocks;
// show python in toggle if: python editor currently active, or blocks editor active & saved language pref is python
const pythonIsActive = (parent.isPythonActive() || pxt.shell.isPyLangPref());
const showPython = python && !tsOnly && !blocksOnly && !noPython;
const showBlocks = !pyOnly && !tsOnly && !noBlocks && !!pkg.mainEditorPkg().files[pxt.MAIN_BLOCKS];
const showJavaScript = !noJavaScript && !pyOnly && !blocksOnly;
const showSandbox = sandbox && !headless;
const showDropdown = showPython && showJavaScript && showBlocks;
const showAssets = pxt.appTarget.appTheme.assetEditor && !sandbox;
let textLanguage: JSX.Element = undefined;
let secondTextLanguage: JSX.Element = undefined;
if (showDropdown) {
if (pythonIsActive) textLanguage = <PythonMenuItem parent={parent}/>
else textLanguage = <JavascriptMenuItem parent={parent}/>
}
else if (showPython && showJavaScript) {
textLanguage = <JavascriptMenuItem parent={parent}/>;
secondTextLanguage = <PythonMenuItem parent={parent}/>;
}
else if (showPython) {
textLanguage = <PythonMenuItem parent={parent}/>;
}
else if (showJavaScript) {
textLanguage = <JavascriptMenuItem parent={parent}/>
}
return (
<div id="editortoggle" className={`ui grid padded ${(pyOnly || tsOnly) ? "one-language" : ""}`} role="menubar" aria-orientation="horizontal" aria-label={lf("Editor toggle")}>
{showSandbox && <SandboxMenuItem parent={parent} />}
{showBlocks && <BlocksMenuItem parent={parent} />}
{textLanguage}
{secondTextLanguage}
{showDropdown && <sui.DropdownMenu id="editordropdown" role="menuitem" icon="chevron down" rightIcon title={lf("Select code editor language")} className={`item button attached right ${dropdownActive ? "active" : ""}`}>
<JavascriptMenuItem parent={parent} />
<PythonMenuItem parent={parent} />
</sui.DropdownMenu>}
{showAssets && <AssetMenuItem parent={parent} />}
<div className={`ui item toggle ${dropdownActive ? 'dropdown-attached' : ''}`}></div>
</div>
)
}
}
export interface SideDocsProps extends ISettingsProps {
docsUrl: string;
sideDocsCollapsed: boolean;
}
// This Component overrides shouldComponentUpdate, be sure to update that if the state is updated
export interface SideDocsState {
docsUrl?: string;
sideDocsCollapsed?: boolean;
}
export const builtInPrefix = "/builtin/";
export class SideDocs extends data.Component<SideDocsProps, SideDocsState> {
private openingSideDoc = false;
constructor(props: SideDocsProps) {
super(props);
this.state = {
}
this.toggleVisibility = this.toggleVisibility.bind(this);
this.popOut = this.popOut.bind(this);
}
private rootDocsUrl(): string {
return (pxt.webConfig.docsUrl || '/--docs') + "#";
}
public static notify(message: pxsim.SimulatorMessage) {
let sd = document.getElementById("sidedocsframe") as HTMLIFrameElement;
if (sd && sd.contentWindow) sd.contentWindow.postMessage(message, "*");
}
setPath(path: string, blocksEditor: boolean) {
this.openingSideDoc = true;
let docsUrl = this.rootDocsUrl();
const mode = blocksEditor ? "blocks" : "js";
const query = pxt.BrowserUtils.isPxtElectron() ? "?pxtElectron=true" : "";
if (query && docsUrl.endsWith("#")) docsUrl = docsUrl.substr(0, docsUrl.length - 1) + query + "#";
const url = `${docsUrl}doc:${path}:${mode}:${pxt.Util.localeInfo()}`;
this.setUrl(url);
}
setMarkdown(md: string) {
const docsUrl = this.rootDocsUrl();
// always render blocks by default when sending custom markdown
// to side bar
const mode = "blocks" // this.props.parent.isBlocksEditor() ? "blocks" : "js";
const url = `${docsUrl}md:${encodeURIComponent(md)}:${mode}:${pxt.Util.localeInfo()}`;
this.props.parent.setState({ sideDocsLoadUrl: url });
}
toggleBuiltInHelp(help: pxt.editor.BuiltInHelp, focusIfVisible: boolean) {
const url = `${builtInPrefix}${help}`;
if (this.state.docsUrl === url && !this.state.sideDocsCollapsed && !focusIfVisible) {
this.toggleVisibility();
this.props.parent.editor.focusWorkspace();
} else {
this.openingSideDoc = true;
this.setUrl(url);
}
}
private setUrl(url: string) {
this.props.parent.setState({ sideDocsLoadUrl: url, sideDocsCollapsed: false });
}
expand() {
this.props.parent.setState({ sideDocsCollapsed: false });
}
collapse() {
this.props.parent.setState({ sideDocsCollapsed: true });
}
isCollapsed() {
return !!this.state.sideDocsCollapsed;
}
popOut() {
SideDocs.notify({
type: "popout"
})
}
toggleVisibility() {
const state = this.props.parent.state;
this.props.parent.setState({ sideDocsCollapsed: !state.sideDocsCollapsed });
document.getElementById("sidedocstoggle").focus();
}
componentDidUpdate() {
this.props.parent.editor.resize();
let sidedocstoggle = document.getElementById("sidedocstoggle");
if (this.openingSideDoc && sidedocstoggle) {
sidedocstoggle.focus();
this.openingSideDoc = false;
}
}
UNSAFE_componentWillReceiveProps(nextProps: SideDocsProps) {
const newState: SideDocsState = {};
if (nextProps.sideDocsCollapsed != undefined) {
newState.sideDocsCollapsed = nextProps.sideDocsCollapsed;
}
if (nextProps.docsUrl != undefined) {
newState.docsUrl = nextProps.docsUrl;
}
if (Object.keys(newState).length > 0) this.setState(newState)
}
shouldComponentUpdate(nextProps: SideDocsProps, nextState: SideDocsState, nextContext: any): boolean {
return this.state.sideDocsCollapsed != nextState.sideDocsCollapsed
|| this.state.docsUrl != nextState.docsUrl;
}
private handleKeyDown = (ev: React.KeyboardEvent<HTMLElement>) => {
if (ev.key == "Escape") {
this.collapse();
}
}
renderCore() {
const { sideDocsCollapsed, docsUrl } = this.state;
const isRTL = pxt.Util.isUserLanguageRtl();
const showLeftChevron = (sideDocsCollapsed || isRTL) && !(sideDocsCollapsed && isRTL); // Collapsed XOR RTL
const lockedEditor = !!pxt.appTarget.appTheme.lockedEditor;
if (!docsUrl) return null;
const url = sideDocsCollapsed ? this.rootDocsUrl() : docsUrl;
const isBuiltIn = url.startsWith(`${builtInPrefix}`);
/* eslint-disable @microsoft/sdl/react-iframe-missing-sandbox */
return <div>
<button id="sidedocstoggle" role="button" aria-label={sideDocsCollapsed ? lf("Expand the side documentation") : lf("Collapse the side documentation")} className="ui icon button large" onClick={this.toggleVisibility}>
<sui.Icon icon={`icon inverted chevron ${showLeftChevron ? 'left' : 'right'}`} />
</button>
<div id="sidedocs" onKeyDown={this.handleKeyDown}>
<div id="sidedocsframe-wrapper">
{this.renderContent(url, isBuiltIn, lockedEditor)}
</div>
{!lockedEditor && !isBuiltIn && <div className="ui app hide" id="sidedocsbar">
<a className="ui icon link" role="button" tabIndex={0} data-content={lf("Open documentation in new tab")} aria-label={lf("Open documentation in new tab")} onClick={this.popOut} onKeyDown={fireClickOnEnter} >
<sui.Icon icon="external" />
</a>
</div>}
</div>
</div>
/* eslint-enable @microsoft/sdl/react-iframe-missing-sandbox */
}
renderContent(url: string, isBuiltin: boolean, lockedEditor: boolean) {
if (isBuiltin) {
const component = url.slice(builtInPrefix.length) as pxt.editor.BuiltInHelp;
switch (component) {
case "keyboardControls": {
return <KeyboardControlsHelp />
}
}
}
return (
<iframe id="sidedocsframe" src={url} title={lf("Documentation")} aria-atomic="true" aria-live="assertive"
sandbox={`allow-scripts allow-same-origin allow-forms ${lockedEditor ? "" : "allow-popups"}`} />
)
}
}
export interface SandboxFooterProps extends ISettingsProps {
}
export class SandboxFooter extends data.PureComponent<SandboxFooterProps, {}> {
constructor(props: SandboxFooterProps) {
super(props);
this.state = {
}
this.compile = this.compile.bind(this);
}
compile() {
pxt.tickEvent("sandboxfooter.compile", undefined, { interactiveConsent: true });
this.props.parent.compile();
}
renderCore() {
const targetTheme = pxt.appTarget.appTheme;
const compileTooltip = lf("Download your code to the {0}", targetTheme.boardName);
return <div className="ui horizontal small divided link list sandboxfooter">
{targetTheme.organizationUrl && targetTheme.organization ? <a className="item" target="_blank" rel="noopener noreferrer" href={targetTheme.organizationUrl}>{targetTheme.organization}</a> : undefined}
<a target="_blank" className="item" href={targetTheme.termsOfUseUrl} rel="noopener noreferrer">{lf("Terms of Use")}</a>
<a target="_blank" className="item" href={targetTheme.privacyUrl} rel="noopener noreferrer">{lf("Privacy")}</a>
<span className="item"><a role="button" className="ui thin portrait only" title={compileTooltip} onClick={this.compile}><sui.Icon icon={`icon ${pxt.appTarget.appTheme.downloadIcon || 'download'}`} />{pxt.appTarget.appTheme.useUploadMessage ? lf("Upload") : lf("Download")}</a></span>
</div>;
}
}