fix: now you can delete stuff with backspace as well

This commit is contained in:
Matei Adriel 2020-04-15 17:58:04 +03:00
parent 5667b1652a
commit 8c8502d15d
4 changed files with 47 additions and 17 deletions

View file

@ -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

View file

@ -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<string> = 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<string, string[]>,
...params: string[]
) {
//save the keys
this.keys = params

View file

@ -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<string, KeyboardInput> = {}
@ -16,7 +19,10 @@ const keyBindings = Object.values(SidebarActions)
}
)
export const initKeyBindings = (bindings: KeyBindingMap = keyBindings) => {
export const initKeyBindings = (
aliases: Record<string, string[]> = keyboardAliases,
bindings: KeyBindingMap = keyBindings
) => {
const allKeys = new Set<string>()
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) => {

View file

@ -30,39 +30,43 @@ export const actionIcons: Record<possibleAction, string> = {
* Array with all the actions for the SimulationAction component to render
*/
export const SidebarActions: Record<possibleAction, SidebarAction> = {
...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']
}