From 03feb965b340d228b18d81ca0740487ef75dd9a6 Mon Sep 17 00:00:00 2001 From: braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Fri, 18 Aug 2023 23:09:54 +0200 Subject: [PATCH 1/2] fix(core): reset `isPressed` value when switching from boolean to other keyfilter --- packages/core/src/composables/useKeyPress.ts | 42 ++++++++++++++------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/packages/core/src/composables/useKeyPress.ts b/packages/core/src/composables/useKeyPress.ts index 50b0b8b2d..c2907a19a 100644 --- a/packages/core/src/composables/useKeyPress.ts +++ b/packages/core/src/composables/useKeyPress.ts @@ -71,25 +71,37 @@ export function useKeyPress(keyFilter: MaybeRefOrGetter, 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) @@ -105,18 +117,14 @@ export function useKeyPress(keyFilter: MaybeRefOrGetter, onCha ) onKeyStroke( - unrefKeyFilter, + nextKeyFilter, (e) => { if (isPressed.value) { if (!modifierPressed && isInputDOMNode(e)) { return } - modifierPressed = false - - pressedKeys.clear() - - isPressed.value = false + reset() } }, { eventName: 'keyup' }, @@ -129,4 +137,12 @@ export function useKeyPress(keyFilter: MaybeRefOrGetter, onCha ) return isPressed + + function reset() { + modifierPressed = false + + pressedKeys.clear() + + isPressed.value = false + } } From 6c518d26e08f9f9ad32da823ca5d01cca53bb4d7 Mon Sep 17 00:00:00 2001 From: braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Fri, 18 Aug 2023 23:10:24 +0200 Subject: [PATCH 2/2] chore(changeset): add --- .changeset/yellow-tables-serve.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/yellow-tables-serve.md diff --git a/.changeset/yellow-tables-serve.md b/.changeset/yellow-tables-serve.md new file mode 100644 index 000000000..b39f4c9f8 --- /dev/null +++ b/.changeset/yellow-tables-serve.md @@ -0,0 +1,5 @@ +--- +"@vue-flow/core": patch +--- + +Reset isPressed value when switching from a boolean keyFilter to another type of keyFilter