Skip to content

Commit 344c1bf

Browse files
authored
fix: add instructions on how to register toolbox (#715)
1 parent 57bca48 commit 344c1bf

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ import {KeyboardNavigation} from '@blockly/keyboard-navigation';
7373
// Must be done before calling Blockly.inject.
7474
KeyboardNavigation.registerKeyboardNavigationStyles();
7575

76+
// Register the default toolbox. Only do this once per page-load.
77+
// Must be done before calling Blockly.inject.
78+
// See instructions below if you don't use the default toolbox.
79+
KeyboardNavigation.registerNavigationDeferringToolbox();
80+
7681
// Inject Blockly.
7782
const workspace = Blockly.inject('blocklyDiv', {
7883
toolbox: toolboxCategories,
@@ -81,7 +86,7 @@ const workspace = Blockly.inject('blocklyDiv', {
8186
const keyboardNav = new KeyboardNavigation(workspace);
8287
```
8388

84-
## Add shortcuts to page
89+
### Add shortcuts to page
8590

8691
In order to see the keyboard help popup when the user presses /, you need to add an empty div element to the hosting page that has the Blockly div element with the id "shortcuts". The plugin will take care of layout and formatting.
8792

@@ -93,7 +98,20 @@ In order to see the keyboard help popup when the user presses /, you need to add
9398
...
9499
```
95100

96-
### Usage with cross-tab-copy-paste plugin
101+
### Use with custom Toolbox implementation
102+
103+
If you supply your own subclass of `Toolbox`, you need to override the `onKeyDown_` method to make it a no-op. The base class has its own keyboard navigation built-in that you need to disable.
104+
105+
```js
106+
class YourCustomToolbox extends Blockly.Toolbox {
107+
protected override onKeyDown_(e: KeyboardEvent) {
108+
// No-op, prevent keyboard handling by superclass in order to defer to
109+
// global keyboard navigation.
110+
}
111+
}
112+
```
113+
114+
### Use with cross-tab-copy-paste plugin
97115

98116
This plugin adds context menu items for copying & pasting. It also adds feedback to copying & pasting as toasts that are shown to the user upon successful copy or cut. It is compatible with the `@blockly/plugin-cross-tab-copy-paste` by following these steps:
99117

src/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ export class KeyboardNavigation {
139139
this.navigationController.shortcutDialog.toggle(this.workspace);
140140
}
141141

142+
/**
143+
* Registers a default toolbox implementation that doesn't handle
144+
* keydown events, since we now handle them in this plugin. If you
145+
* use the default toolbox, call this function before calling
146+
* `Blockly.inject`. If you use a custom toolbox, override the
147+
* `onKeyDown_` method in your toolbox implementation to make it a no-op.
148+
*/
149+
static registerNavigationDeferringToolbox() {
150+
this.registerNavigationDeferringToolbox();
151+
}
152+
142153
/**
143154
* Register CSS used by the plugin.
144155
* This is broken up into sections by purpose, with some notes about

0 commit comments

Comments
 (0)