js-draw
Version:
Draw pictures using a pen, touchscreen, or mouse! JS-draw is a drawing library for JavaScript and TypeScript.
35 lines (34 loc) • 1.07 kB
JavaScript
import BaseTool from './BaseTool.mjs';
/**
* Handles keyboard events used, by default, to select tools. By default,
* 1 maps to the first primary tool, 2 to the second primary tool, ... .
*
* This is in the default set of {@link ToolController} tools.
*
*/
export default class ToolSwitcherShortcut extends BaseTool {
constructor(editor) {
super(editor.notifier, editor.localization.changeTool);
this.editor = editor;
}
canReceiveInputInReadOnlyEditor() {
return true;
}
// @internal
onKeyPress({ key }) {
const toolController = this.editor.toolController;
const primaryTools = toolController.getPrimaryTools();
// Map keys 0-9 to primary tools.
const keyMatch = /^[0-9]$/.exec(key);
let targetTool;
if (keyMatch) {
const targetIdx = parseInt(keyMatch[0], 10) - 1;
targetTool = primaryTools[targetIdx];
}
if (targetTool) {
targetTool.setEnabled(true);
return true;
}
return false;
}
}