-
-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#39): adds
useMagicSequence
composable + shortcuts for nav
- Loading branch information
1 parent
432a79e
commit becb1cb
Showing
3 changed files
with
80 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { ComputedRef } from 'vue' | ||
|
||
// TODO: consider to allow combinations similar to useMagicKeys using proxy? | ||
// e.g. `const magicSequence = useMagicSequence()` | ||
// `magicSequence['Shift+Ctrl+A']` | ||
// `const { Ctrl_A_B } = useMagicSequence()` | ||
|
||
export function useMagicSequence(keys: string[]): ComputedRef<boolean> { | ||
const magicKeys = useMagicKeys() | ||
|
||
const success = ref(false) | ||
const i = ref(0) | ||
let down = false | ||
|
||
watch( | ||
() => magicKeys.current, | ||
() => { | ||
if (magicKeys[keys[i.value]].value && !down) { | ||
down = true | ||
i.value += 1 | ||
} | ||
else if (i.value > 0 && !magicKeys[keys[i.value - 1]].value && down) { | ||
down = false | ||
} | ||
else { | ||
i.value = 0 | ||
down = false | ||
success.value = false | ||
} | ||
if (i.value >= keys.length && !down) { | ||
i.value = 0 | ||
down = false | ||
success.value = true | ||
} | ||
}, { | ||
deep: true, | ||
}) | ||
|
||
return computed(() => success.value) | ||
} |