From 8c8502d15db445e124bba6e3c9820fe72ee67368 Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Wed, 15 Apr 2020 17:58:04 +0300 Subject: [PATCH] fix: now you can delete stuff with backspace as well --- .../integrated-circuits/helpers/compileIc.ts | 1 - .../keybindings/classes/KeyboardInput.ts | 27 ++++++++++++++++--- .../helpers/initialiseKeyBindings.ts | 12 ++++++--- src/modules/simulation-actions/constants.ts | 24 ++++++++++------- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/modules/integrated-circuits/helpers/compileIc.ts b/src/modules/integrated-circuits/helpers/compileIc.ts index 69c0a0c..89d8fa8 100644 --- a/src/modules/integrated-circuits/helpers/compileIc.ts +++ b/src/modules/integrated-circuits/helpers/compileIc.ts @@ -19,7 +19,6 @@ import { cleanSimulation } from '../../simulation-actions/helpers/clean' import { getSimulationState } from '../../saving/helpers/getState' import { categories } from '../../saving/data/categories' import { getTemplateSafely } from '../../logic-gates/helpers/getTemplateSafely' -import { reservedPropNames } from '../../simulation/constants' /** * Compiles a simulation into a logicGate diff --git a/src/modules/keybindings/classes/KeyboardInput.ts b/src/modules/keybindings/classes/KeyboardInput.ts index 7c32b7c..24dea96 100644 --- a/src/modules/keybindings/classes/KeyboardInput.ts +++ b/src/modules/keybindings/classes/KeyboardInput.ts @@ -30,15 +30,36 @@ export class KeyboardInput { /** * Check if a key is pressed */ - private isPressed(key: string, event: KeyboardEvent) { - return (key === 'ctrl' && event.metaKey) || keycode(event) === key + private isPressed( + key: string, + event: KeyboardEvent, + visited: Set = new Set() + ) { + // Prevent inifnite recursion + if (visited.has(key)) { + return false + } + + visited.add(key) + + return ( + (key === 'control' && event.metaKey) || + event.key.toLowerCase() === key || + this.aliases[key]?.some((alias) => + this.isPressed(alias, event, visited) + ) + ) } /** * use for keyboard events + * @param aliases Specifies a mapping of aliases to bind. * @param params the keys to listen to */ - public constructor(...params: string[]) { + public constructor( + public aliases: Record, + ...params: string[] + ) { //save the keys this.keys = params diff --git a/src/modules/keybindings/helpers/initialiseKeyBindings.ts b/src/modules/keybindings/helpers/initialiseKeyBindings.ts index 85287e4..6bcecba 100644 --- a/src/modules/keybindings/helpers/initialiseKeyBindings.ts +++ b/src/modules/keybindings/helpers/initialiseKeyBindings.ts @@ -1,6 +1,9 @@ import { KeyboardInput } from '../classes/KeyboardInput' import { KeyBindingMap, KeyBinding } from '../types/KeyBindingMap' -import { SidebarActions } from '../../simulation-actions/constants' +import { + SidebarActions, + keyboardAliases +} from '../../simulation-actions/constants' import { modalIsOpen } from '../../modals/helpers/modalIsOpen' export const listeners: Record = {} @@ -16,7 +19,10 @@ const keyBindings = Object.values(SidebarActions) } ) -export const initKeyBindings = (bindings: KeyBindingMap = keyBindings) => { +export const initKeyBindings = ( + aliases: Record = keyboardAliases, + bindings: KeyBindingMap = keyBindings +) => { const allKeys = new Set() for (const binding of bindings) { @@ -26,7 +32,7 @@ export const initKeyBindings = (bindings: KeyBindingMap = keyBindings) => { } for (const key of allKeys.values()) { - listeners[key] = new KeyboardInput(key) + listeners[key] = new KeyboardInput(aliases, key) } window.addEventListener('keydown', (e) => { diff --git a/src/modules/simulation-actions/constants.ts b/src/modules/simulation-actions/constants.ts index 5f12ad9..6c92592 100644 --- a/src/modules/simulation-actions/constants.ts +++ b/src/modules/simulation-actions/constants.ts @@ -30,39 +30,43 @@ export const actionIcons: Record = { * Array with all the actions for the SimulationAction component to render */ export const SidebarActions: Record = { - ...createActionConfig('save', save, ['ctrl', 's']), + ...createActionConfig('save', save, ['control', 's']), ...createActionConfig( 'refresh', { run: refresh }, - ['ctrl', 'r'] + ['control', 'r'] ), ...createActionConfig( 'undo', { run: undo }, - ['ctrl', 'z'] + ['control', 'z'] ), ...createActionConfig( 'clean', { run: cleanRenderer }, - ['ctrl', 'delete'] + ['control', 'delete'] ), ...createActionConfig( 'delete simulation', { run: deleteSimulation }, - ['ctrl', 'shift', 'delete'] + ['control', 'shift', 'delete'] ), - ...createActionConfig('cut', cut, ['ctrl', 'x']), - ...createActionConfig('paste', paste, ['ctrl', 'v']), - ...createActionConfig('duplicate', duplicate, ['ctrl', 'd']), - ...createActionConfig('copy', copy, ['ctrl', 'c']), - ...createActionConfig('select all', selectAll, ['ctrl', 'a']), + ...createActionConfig('cut', cut, ['control', 'x']), + ...createActionConfig('paste', paste, ['control', 'v']), + ...createActionConfig('duplicate', duplicate, ['control', 'd']), + ...createActionConfig('copy', copy, ['control', 'c']), + ...createActionConfig('select all', selectAll, ['control', 'a']), ...createActionConfig('delete selection', deleteSelection, ['delete']) } + +export const keyboardAliases = { + delete: ['backspace'] +}