feat!: Add support for keyboard navigation by gonfunko · Pull Request #9634 · RaspberryPiFoundation/blockly
The basics
The details
Proposed Changes
This PR backports the core support for keyboard navigation of the workspace, toolbox and flyout from the blockly-keyboard-experimentation repo. It also refactors things to provide a more cohesive API for interacting with navigation. The general aspirational approach is:
- The various
*_navigation_policy.tsfiles specify, for each kind of focusable/navigable entity in Blockly, what its parent, first child, and previous/next siblings are, from a "just the facts"/AST perspective. Navigatorprovides methods to query the "just the facts" relationships between elements, but also provides methods that incorporate business logic to make navigation order in response to the arrow keys make sense/feel right/pass the vibe test.FlyoutNavigatorandToolboxNavigatorcustomize the business logic for navigation in a flyout and toolbox context, respectively, and operate on a set of navigation policies relevant to focusable/navigable entities that exist in those contexts- Every
IFocusableTreeprovides aNavigatorinstance which should be used to handle navigation within that tree - The actual shortcuts for the arrow keys are effectively one-liners that delegate to the
Navigatorfor the active focus tree
This means that keyboard navigation does not have to keep track of the current state/context as in the keyboard-experimentation repo, because that maps precisely to the currently focused tree, which the FocusManager already keeps track of.
I backported the navigation business logic from the add-screen-reader-support-experimental branch, where up/down move between "rows" and left/right navigate within the current "row".
Future Work
- Navigation within the toolbox needs to be adjusted to accomodate horizontal layouts and the various start/end toolbox positions
- Some CSS rules need to be backported to make it clearer where focus is
- The navigation policies should be cleaned up a bit to remove some vestiges of business logic
- Additional tests for navigation behavior should be added; the suite from the keyboard-experimentation repo was backported with LLM assistance and manual review, but additional cases should be exercised, particularly with looping enabled
- Enter/space to act on the focused element still needs to be backported
Breaking Changes
MarkerManager,Marker, andLineCursorhave all been removed, along with their accessors. If you were interacting with one of these classes,Navigatorshould be close to a drop-in replacement.IFocusableTreemust implementgetNavigator(), which should return aNavigatorinstance or subclass appropriate for navigating the treeFlyoutno longer implementsIFocusableNodeorIFocusableTree. The flyout's workspace continues to implement those interfaces, and should be used in place of the flyout itself.- Changes have been made to several toolbox-related interfaces. If you have custom toolbox-related classes that do not inherit from those provided in Blockly, you may need to adjust your implementation to ensure that:
IToolboxprovides agetToolboxItems()method that returns an array of all itemsIToolboxItemprovides agetParentToolbox()method that returns a reference to its containing toolbox