@@ -17,6 +17,7 @@ import * as dialogs from "./dialogs";
17
17
import * as blocklyFieldView from "./blocklyFieldView" ;
18
18
import { CreateFunctionDialog } from "./createFunction" ;
19
19
import { initializeSnippetExtensions } from './snippetBuilder' ;
20
+ import * as cmds from "./cmds"
20
21
21
22
import * as pxtblockly from "../../pxtblocks" ;
22
23
import { KeyboardNavigation } from '@blockly/keyboard-experiment' ;
@@ -40,6 +41,7 @@ import { Measurements } from "./constants";
40
41
import { flow } from "../../pxtblocks" ;
41
42
import { HIDDEN_CLASS_NAME } from "../../pxtblocks/plugins/flyout/blockInflater" ;
42
43
import { FlyoutButton } from "../../pxtblocks/plugins/flyout/flyoutButton" ;
44
+ import { userPrefersDownloadFlagSet } from "./webusb" ;
43
45
44
46
interface CopyDataEntry {
45
47
version : 1 ;
@@ -585,6 +587,53 @@ export class Editor extends toolboxeditor.ToolboxEditor {
585
587
return true
586
588
}
587
589
} ) ;
590
+
591
+ const triggerEditorAction = ( action : pxsim . SimulatorActionMessage [ "type" ] ) => {
592
+ switch ( action ) {
593
+ case "focusWorkspace" : {
594
+ this . parent . editor . focusWorkspace ( ) ;
595
+ return
596
+ }
597
+ case "focusSimulator" : {
598
+ // Note that pxtsim.driver.focus() isn't the same as tabbing to the sim.
599
+ ( document . querySelector ( "#boardview" ) as HTMLElement ) . focus ( ) ;
600
+ return
601
+ }
602
+ case "webUSBDownload" : {
603
+ ( async ( ) => {
604
+ // TODO: refactor and share with editortoolbar.tsx
605
+ const shouldShowPairingDialogOnDownload = pxt . appTarget . appTheme . preferWebUSBDownload
606
+ && pxt . appTarget ?. compile ?. webUSB
607
+ && pxt . usb . isEnabled
608
+ && ! userPrefersDownloadFlagSet ( ) ;
609
+ if ( shouldShowPairingDialogOnDownload
610
+ && ! pxt . packetio . isConnected ( )
611
+ && ! pxt . packetio . isConnecting ( )
612
+ ) {
613
+ await cmds . pairAsync ( true ) ;
614
+ }
615
+ this . parent . compile ( ) ;
616
+ } ) ( ) ;
617
+ return
618
+ }
619
+ }
620
+ }
621
+
622
+ const simulatorOrigins = [
623
+ window . location . origin ,
624
+ // Simulator deployed origin.
625
+ "https://trg-microbit.userpxt.io"
626
+ ]
627
+ window . addEventListener ( "message" , ( e : MessageEvent < pxsim . SimulatorActionMessage > ) => {
628
+ // Listen to simulator iframe keydown post messages.
629
+ if ( simulatorOrigins . includes ( e . origin ) ) {
630
+ triggerEditorAction ( e . data . type )
631
+ }
632
+ } , false )
633
+ document . addEventListener ( "keydown" , ( e : KeyboardEvent ) => {
634
+ const action = pxsim . accessibility . getKeyboardShortcutEditorAction ( e )
635
+ triggerEditorAction ( action )
636
+ } ) ;
588
637
}
589
638
}
590
639
0 commit comments