Skip to content

Commit

Permalink
fix(core): allow using + as a key in key combinations (#1693)
Browse files Browse the repository at this point in the history
* fix(core): allow using `+` as a key in key combinations

Signed-off-by: braks <[email protected]>

* chore(changeset): add

Signed-off-by: braks <[email protected]>

* chore(core): cleanup

Signed-off-by: braks <[email protected]>

---------

Signed-off-by: braks <[email protected]>
  • Loading branch information
bcakmakoglu authored Nov 13, 2024
1 parent f08c15d commit f28ffba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-falcons-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vue-flow/core": patch
---

Allow using the `+` key in key combinations
17 changes: 13 additions & 4 deletions packages/core/src/composables/useKeyPress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ function wasModifierPressed(event: KeyboardEvent) {
}

function isKeyMatch(pressedKey: string, keyToMatch: string, pressedKeys: Set<string>, isKeyUp: boolean) {
const keyCombination = keyToMatch.split('+').map((k) => k.trim().toLowerCase())
const keyCombination = keyToMatch
.replace('+', '\n')
.replace('\n\n', '\n+')
.split('\n')
.map((k) => k.trim().toLowerCase())

if (keyCombination.length === 1) {
return pressedKey.toLowerCase() === keyToMatch.toLowerCase()
Expand All @@ -50,13 +54,16 @@ function createKeyPredicate(keyFilter: string | string[], pressedKeys: Set<strin

const keyOrCode = useKeyOrCode(event.code, keyFilter)

const isKeyUp = event.type === 'keyup'
const pressedKey = event[keyOrCode]

// if the keyFilter is an array of multiple keys, we need to check each possible key combination
if (Array.isArray(keyFilter)) {
return keyFilter.some((key) => isKeyMatch(event[keyOrCode], key, pressedKeys, event.type === 'keyup'))
return keyFilter.some((key) => isKeyMatch(pressedKey, key, pressedKeys, isKeyUp))
}

// if the keyFilter is a string, we need to check if the key matches the string
return isKeyMatch(event[keyOrCode], keyFilter, pressedKeys, event.type === 'keyup')
return isKeyMatch(pressedKey, keyFilter, pressedKeys, isKeyUp)
}
}

Expand Down Expand Up @@ -126,7 +133,9 @@ export function useKeyPress(keyFilter: MaybeRefOrGetter<KeyFilter | boolean | nu
)

onKeyStroke(
(...args) => currentFilter(...args),
(...args) => {
return currentFilter(...args)
},
(e) => {
if (isPressed.value) {
const preventAction = (!modifierPressed || (modifierPressed && !actInsideInputWithModifier.value)) && isInputDOMNode(e)
Expand Down

0 comments on commit f28ffba

Please sign in to comment.