(
sequence: HotkeySequence,
options: { timeout?: number; platform?: 'mac' | 'windows' | 'linux' } = {},
)
| 675 | * ``` |
| 676 | */ |
| 677 | export function createSequenceMatcher( |
| 678 | sequence: HotkeySequence, |
| 679 | options: { timeout?: number; platform?: 'mac' | 'windows' | 'linux' } = {}, |
| 680 | ): { |
| 681 | match: (event: KeyboardEvent) => boolean |
| 682 | reset: () => void |
| 683 | getProgress: () => number |
| 684 | } { |
| 685 | const platform = options.platform ?? detectPlatform() |
| 686 | const timeout = options.timeout ?? DEFAULT_SEQUENCE_TIMEOUT |
| 687 | const parsedSequence = sequence.map((hotkey) => parseHotkey(hotkey, platform)) |
| 688 | |
| 689 | let currentIndex = 0 |
| 690 | let lastKeyTime = 0 |
| 691 | |
| 692 | return { |
| 693 | match(event: KeyboardEvent): boolean { |
| 694 | const now = Date.now() |
| 695 | |
| 696 | if (currentIndex > 0 && now - lastKeyTime > timeout) { |
| 697 | currentIndex = 0 |
| 698 | } |
| 699 | |
| 700 | const expected = parsedSequence[currentIndex] |
| 701 | if (!expected) { |
| 702 | return false |
| 703 | } |
| 704 | |
| 705 | if (matchesKeyboardEvent(event, expected, platform)) { |
| 706 | lastKeyTime = now |
| 707 | currentIndex++ |
| 708 | |
| 709 | if (currentIndex >= parsedSequence.length) { |
| 710 | currentIndex = 0 |
| 711 | return true |
| 712 | } |
| 713 | } else if (currentIndex > 0) { |
| 714 | if (matchesKeyboardEvent(event, parsedSequence[0]!, platform)) { |
| 715 | currentIndex = 1 |
| 716 | lastKeyTime = now |
| 717 | } else { |
| 718 | currentIndex = 0 |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | return false |
| 723 | }, |
| 724 | |
| 725 | reset(): void { |
| 726 | currentIndex = 0 |
| 727 | lastKeyTime = 0 |
| 728 | }, |
| 729 | |
| 730 | getProgress(): number { |
| 731 | return currentIndex |
| 732 | }, |
| 733 | } |
| 734 | } |
no test coverage detected
searching dependent graphs…