fix: now you can delete stuff with backspace as well
This commit is contained in:
parent
5667b1652a
commit
8c8502d15d
|
@ -19,7 +19,6 @@ import { cleanSimulation } from '../../simulation-actions/helpers/clean'
|
||||||
import { getSimulationState } from '../../saving/helpers/getState'
|
import { getSimulationState } from '../../saving/helpers/getState'
|
||||||
import { categories } from '../../saving/data/categories'
|
import { categories } from '../../saving/data/categories'
|
||||||
import { getTemplateSafely } from '../../logic-gates/helpers/getTemplateSafely'
|
import { getTemplateSafely } from '../../logic-gates/helpers/getTemplateSafely'
|
||||||
import { reservedPropNames } from '../../simulation/constants'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compiles a simulation into a logicGate
|
* Compiles a simulation into a logicGate
|
||||||
|
|
|
@ -30,15 +30,36 @@ export class KeyboardInput {
|
||||||
/**
|
/**
|
||||||
* Check if a key is pressed
|
* Check if a key is pressed
|
||||||
*/
|
*/
|
||||||
private isPressed(key: string, event: KeyboardEvent) {
|
private isPressed(
|
||||||
return (key === 'ctrl' && event.metaKey) || keycode(event) === key
|
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
|
* use for keyboard events
|
||||||
|
* @param aliases Specifies a mapping of aliases to bind.
|
||||||
* @param params the keys to listen to
|
* @param params the keys to listen to
|
||||||
*/
|
*/
|
||||||
public constructor(...params: string[]) {
|
public constructor(
|
||||||
|
public aliases: Record<string, string[]>,
|
||||||
|
...params: string[]
|
||||||
|
) {
|
||||||
//save the keys
|
//save the keys
|
||||||
this.keys = params
|
this.keys = params
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
import { KeyboardInput } from '../classes/KeyboardInput'
|
import { KeyboardInput } from '../classes/KeyboardInput'
|
||||||
import { KeyBindingMap, KeyBinding } from '../types/KeyBindingMap'
|
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'
|
import { modalIsOpen } from '../../modals/helpers/modalIsOpen'
|
||||||
|
|
||||||
export const listeners: Record<string, KeyboardInput> = {}
|
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>()
|
const allKeys = new Set<string>()
|
||||||
|
|
||||||
for (const binding of bindings) {
|
for (const binding of bindings) {
|
||||||
|
@ -26,7 +32,7 @@ export const initKeyBindings = (bindings: KeyBindingMap = keyBindings) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const key of allKeys.values()) {
|
for (const key of allKeys.values()) {
|
||||||
listeners[key] = new KeyboardInput(key)
|
listeners[key] = new KeyboardInput(aliases, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener('keydown', (e) => {
|
window.addEventListener('keydown', (e) => {
|
||||||
|
|
|
@ -30,39 +30,43 @@ export const actionIcons: Record<possibleAction, string> = {
|
||||||
* Array with all the actions for the SimulationAction component to render
|
* Array with all the actions for the SimulationAction component to render
|
||||||
*/
|
*/
|
||||||
export const SidebarActions: Record<possibleAction, SidebarAction> = {
|
export const SidebarActions: Record<possibleAction, SidebarAction> = {
|
||||||
...createActionConfig('save', save, ['ctrl', 's']),
|
...createActionConfig('save', save, ['control', 's']),
|
||||||
...createActionConfig(
|
...createActionConfig(
|
||||||
'refresh',
|
'refresh',
|
||||||
{
|
{
|
||||||
run: refresh
|
run: refresh
|
||||||
},
|
},
|
||||||
['ctrl', 'r']
|
['control', 'r']
|
||||||
),
|
),
|
||||||
...createActionConfig(
|
...createActionConfig(
|
||||||
'undo',
|
'undo',
|
||||||
{
|
{
|
||||||
run: undo
|
run: undo
|
||||||
},
|
},
|
||||||
['ctrl', 'z']
|
['control', 'z']
|
||||||
),
|
),
|
||||||
...createActionConfig(
|
...createActionConfig(
|
||||||
'clean',
|
'clean',
|
||||||
{
|
{
|
||||||
run: cleanRenderer
|
run: cleanRenderer
|
||||||
},
|
},
|
||||||
['ctrl', 'delete']
|
['control', 'delete']
|
||||||
),
|
),
|
||||||
...createActionConfig(
|
...createActionConfig(
|
||||||
'delete simulation',
|
'delete simulation',
|
||||||
{
|
{
|
||||||
run: deleteSimulation
|
run: deleteSimulation
|
||||||
},
|
},
|
||||||
['ctrl', 'shift', 'delete']
|
['control', 'shift', 'delete']
|
||||||
),
|
),
|
||||||
...createActionConfig('cut', cut, ['ctrl', 'x']),
|
...createActionConfig('cut', cut, ['control', 'x']),
|
||||||
...createActionConfig('paste', paste, ['ctrl', 'v']),
|
...createActionConfig('paste', paste, ['control', 'v']),
|
||||||
...createActionConfig('duplicate', duplicate, ['ctrl', 'd']),
|
...createActionConfig('duplicate', duplicate, ['control', 'd']),
|
||||||
...createActionConfig('copy', copy, ['ctrl', 'c']),
|
...createActionConfig('copy', copy, ['control', 'c']),
|
||||||
...createActionConfig('select all', selectAll, ['ctrl', 'a']),
|
...createActionConfig('select all', selectAll, ['control', 'a']),
|
||||||
...createActionConfig('delete selection', deleteSelection, ['delete'])
|
...createActionConfig('delete selection', deleteSelection, ['delete'])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const keyboardAliases = {
|
||||||
|
delete: ['backspace']
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue