@@ -498,16 +498,19 @@ export class Editor extends toolboxeditor.ToolboxEditor {
498498
499499 /**
500500 * Override blockly methods to support our custom toolbox.
501+ *
502+ * We don't generally use this selection but keyboard nav will trigger
503+ * it to select the first category and clear the selection.
501504 */
502505 const that = this ;
503- ( Blockly as any ) . Toolbox . prototype . updateFlyout_ = function ( oldItem : Blockly . ISelectableToolboxItem | null , newItem : Blockly . ISelectableToolboxItem | null ) {
504- // Keyboard nav triggers selection on the Blockly toolbox
505- // (first item + clear as we intercept all other events).
506- // Hook in just to hide the flyout when selection is cleared.
506+ ( Blockly as any ) . Toolbox . prototype . setSelectedItem = function ( newItem : Blockly . ISelectableToolboxItem | null ) {
507507 if ( newItem === null ) {
508508 that . hideFlyout ( ) ;
509509 }
510510 } ;
511+ ( Blockly as any ) . Toolbox . prototype . clearSelection = function ( ) {
512+ that . hideFlyout ( ) ;
513+ } ;
511514 ( Blockly . WorkspaceSvg as any ) . prototype . refreshToolboxSelection = function ( ) {
512515 let ws = this . isFlyout ? this . targetWorkspace : this ;
513516 if ( ws && ! ws . currentGesture_ && ws . toolbox_ && ws . toolbox_ . flyout_ ) {
@@ -547,6 +550,17 @@ export class Editor extends toolboxeditor.ToolboxEditor {
547550 const enabled = pxt . appTarget . appTheme ?. accessibleBlocks ;
548551 if ( enabled && ! this . keyboardNavigation ) {
549552 this . keyboardNavigation = new KeyboardNavigation ( this . editor ) ;
553+
554+ const injectionDiv = document . getElementById ( 'blocksEditor' ) ;
555+ injectionDiv . classList . add ( "accessibleBlocks" ) ;
556+ const focusRingDiv = injectionDiv . appendChild ( document . createElement ( "div" ) )
557+ focusRingDiv . className = "blocklyWorkspaceFocusRingLayer" ;
558+ this . editor . getSvgGroup ( ) . addEventListener ( "focus" , ( ) => {
559+ focusRingDiv . dataset . focused = "true" ;
560+ } )
561+ this . editor . getSvgGroup ( ) . addEventListener ( "blur" , ( ) => {
562+ delete focusRingDiv . dataset . focused ;
563+ } )
550564 }
551565 }
552566
@@ -937,21 +951,23 @@ export class Editor extends toolboxeditor.ToolboxEditor {
937951 if ( pxt . shell . isReadOnly ( ) ) return ;
938952 const blocklyToolboxDiv = this . getBlocklyToolboxDiv ( ) ;
939953 const blocklyToolbox = < div className = "blocklyToolbox" >
940- < div className = "blocklyToolboxContents" tabIndex = { - 1 } >
954+ < div className = "blocklyToolboxContents" tabIndex = { - 1 } onFocus = { this . handleToolboxContentsFocusCapture } >
941955 < toolbox . Toolbox ref = { this . handleToolboxRef } editorname = "blocks" parent = { this } />
942956 { < div id = "debuggerToolbox" > </ div > }
943957 </ div >
944958 </ div > ;
945959 Util . assert ( ! ! blocklyToolboxDiv ) ;
946960 ReactDOM . render ( blocklyToolbox , blocklyToolboxDiv ) ;
947- blocklyToolboxDiv . querySelector ( ".blocklyToolboxContents" ) . addEventListener ( "focus" , ( ) => {
948- console . log ( "Did it happen?" ) ;
949- ( blocklyToolboxDiv . querySelector ( "[role=tree]" ) as HTMLElement ) . focus ( )
950- } )
951961
952962 if ( ! immediate ) this . toolbox . showLoading ( ) ;
953963 }
954964
965+ private handleToolboxContentsFocusCapture = ( e : React . FocusEvent ) => {
966+ if ( e . target === e . currentTarget ) {
967+ ( this . getBlocklyToolboxDiv ( ) . querySelector ( "[role=tree]" ) as HTMLElement ) . focus ( )
968+ }
969+ }
970+
955971 updateToolbox ( ) {
956972 const container = document . getElementById ( 'debuggerToolbox' ) ;
957973 if ( ! container ) return ;
0 commit comments