Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): reset isPressed value when switching from boolean to other keyfilter #1086

Merged
merged 2 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/yellow-tables-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vue-flow/core": patch
---

Reset isPressed value when switching from a boolean keyFilter to another type of keyFilter
42 changes: 29 additions & 13 deletions packages/core/src/composables/useKeyPress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,37 @@ export function useKeyPress(keyFilter: MaybeRefOrGetter<KeyFilter | null>, onCha

watch(
() => toValue(keyFilter),
(unrefKeyFilter) => {
(nextKeyFilter, previousKeyFilter) => {
if (window && typeof window.addEventListener !== 'undefined') {
useEventListener(window, 'blur', () => {
isPressed.value = false
})
}

if (isBoolean(unrefKeyFilter)) {
isPressed.value = unrefKeyFilter
// if the previous keyFilter was a boolean but is now something else, we need to reset the isPressed value
if (isBoolean(previousKeyFilter) && !isBoolean(nextKeyFilter)) {
reset()
}

// if the keyFilter is null, we just set the isPressed value to false
if (nextKeyFilter === null) {
reset()
return
}

// if the keyFilter is a boolean, we just set the isPressed value to that boolean
if (isBoolean(nextKeyFilter)) {
isPressed.value = nextKeyFilter
return
}

if (Array.isArray(unrefKeyFilter) || (isString(unrefKeyFilter) && unrefKeyFilter.includes('+'))) {
unrefKeyFilter = createKeyPredicate(unrefKeyFilter, pressedKeys)
if (Array.isArray(nextKeyFilter) || (isString(nextKeyFilter) && nextKeyFilter.includes('+'))) {
nextKeyFilter = createKeyPredicate(nextKeyFilter, pressedKeys)
}

if (unrefKeyFilter) {
if (nextKeyFilter) {
onKeyStroke(
unrefKeyFilter,
nextKeyFilter,
(e) => {
modifierPressed = wasModifierPressed(e)

Expand All @@ -105,18 +117,14 @@ export function useKeyPress(keyFilter: MaybeRefOrGetter<KeyFilter | null>, onCha
)

onKeyStroke(
unrefKeyFilter,
nextKeyFilter,
(e) => {
if (isPressed.value) {
if (!modifierPressed && isInputDOMNode(e)) {
return
}

modifierPressed = false

pressedKeys.clear()

isPressed.value = false
reset()
}
},
{ eventName: 'keyup' },
Expand All @@ -129,4 +137,12 @@ export function useKeyPress(keyFilter: MaybeRefOrGetter<KeyFilter | null>, onCha
)

return isPressed

function reset() {
modifierPressed = false

pressedKeys.clear()

isPressed.value = false
}
}
Loading