@tanstack/hotkeys
Version:
Type-safe, framework-agnostic keyboard hotkey management for the browser
25 lines (23 loc) • 1.1 kB
JavaScript
import { parsedChordsEqual } from "./_chord.js";
import { matchesKeyboardEvent } from "./match.js";
import { parseHotkey } from "./parse.js";
// src/_steps.ts
/**
* Whether two hotkeys' steps collide: at every step they share, the chords are
* equal or the key press that recorded that step fires both. One sequence
* that starts with the other collides too. Each side resolves `Mod` on its own
* platform. A step is parsed only when the steps before it collide, so pass
* raw steps: parsing an already-parsed chord can throw for keys like `[1]`.
*/
function stepsOverlap(left, right, platforms, events) {
const [leftPlatform, rightPlatform] = platforms;
const length = Math.min(left.length, right.length);
for (let index = 0; index < length; index++) {
const a = parseHotkey(left[index], leftPlatform);
const b = parseHotkey(right[index], rightPlatform);
const event = events?.[index];
if (!parsedChordsEqual(a, b) && !(event && matchesKeyboardEvent(event, a, leftPlatform) && matchesKeyboardEvent(event, b, rightPlatform))) return false;
}
return true;
}
export { stepsOverlap };