UNPKG

@tanstack/hotkeys

Version:

Type-safe, framework-agnostic keyboard hotkey management for the browser

86 lines (84 loc) • 3.28 kB
import { MODIFIER_KEYS, normalizeKeyName } from "./constants.js"; // src/_recording-guard.ts const sessions = /* @__PURE__ */ new Set(); const captured = /* @__PURE__ */ new WeakSet(); const pending = /* @__PURE__ */ new Map(); const stopped = /* @__PURE__ */ new Set(); let listening = false; let recordingStarted; /** Sets what runs when a recording starts, such as resetting sequences in progress. */ function onRecordingStart(handler) { recordingStarted = handler; } const identity = (event) => event.code || `key:${event.key}`; /** Consumes recorded key releases and removes guards for completed chords. */ function release(event) { const id = identity(event); if (pending.delete(id)) captured.add(event); if (stopped.delete(id)) event.stopPropagation(); if (MODIFIER_KEYS.has(normalizeKeyName(event.key))) { for (const [id, key] of pending) if (!MODIFIER_KEYS.has(key)) pending.delete(id); } cleanup(); } /** Hides the repeats of a stopped press, which go on after a recording commits. */ function repeat(event) { const id = identity(event); if (!event.repeat) stopped.delete(id); else if (stopped.has(id)) { event.preventDefault(); event.stopPropagation(); } } /** Clears pending releases when focus loss prevents the browser from delivering them. */ function blur() { pending.clear(); stopped.clear(); cleanup(); } /** Detaches shared listeners once neither active sessions nor pending releases need them. */ function cleanup() { if (listening && sessions.size === 0 && pending.size === 0 && stopped.size === 0) { window.removeEventListener("keyup", release, true); window.removeEventListener("keydown", repeat, true); window.removeEventListener("blur", blur); listening = false; } } /** * Pauses application shortcut dispatch and starts tracking releases for this * recorder. Starting one cancels any other, so one key press can't be * recorded for two shortcuts. */ function beginRecording(owner) { for (const other of [...sessions]) other.cancel(); for (const other of [...sessions]) other.stop(); sessions.add(owner); recordingStarted?.(); if (!listening && typeof document !== "undefined") { window.addEventListener("keyup", release, true); window.addEventListener("keydown", repeat, true); window.addEventListener("blur", blur); listening = true; } } /** Ends a session while retaining guards for keys still held after commit or cancellation. */ function endRecording(owner) { sessions.delete(owner); cleanup(); } /** Marks a consumed keydown and guards subsequent repeats and its release. */ function captureRecordingEvent(event) { captured.add(event); if (event.key || event.code) pending.set(identity(event), normalizeKeyName(event.key)); } /** Marks a captured keydown the recorder stopped, so its keyup is stopped too. */ function stopRecordingRelease(event) { if (event.key || event.code) stopped.add(identity(event)); } /** Whether managers should suppress callbacks for this recording session or captured chord. */ function isRecordingEvent(event) { const id = identity(event); return sessions.size > 0 || captured.has(event) || pending.has(id) || !!event.repeat && stopped.has(id); } export { beginRecording, captureRecordingEvent, endRecording, isRecordingEvent, onRecordingStart, stopRecordingRelease };