@tanstack/hotkeys
Version:
Type-safe, framework-agnostic keyboard hotkey management for the browser
93 lines (91 loc) • 3.46 kB
JavaScript
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();
const listening = /* @__PURE__ */ new Set();
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, or a recording window closing,
* 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 (sessions.size === 0 && pending.size === 0 && stopped.size === 0) {
for (const view of listening) {
view.removeEventListener("keyup", release, true);
view.removeEventListener("keydown", repeat, true);
view.removeEventListener("blur", blur);
view.removeEventListener("pagehide", blur);
}
listening.clear();
}
}
/**
* Pauses application shortcut dispatch and starts tracking releases for this
* recorder in the window it records in. Starting one cancels any other, so
* one key press can't be recorded for two shortcuts.
*/
function beginRecording(owner, view) {
for (const other of [...sessions]) other.cancel();
for (const other of [...sessions]) other.stop();
sessions.add(owner);
recordingStarted?.();
if (!listening.has(view)) {
view.addEventListener("keyup", release, true);
view.addEventListener("keydown", repeat, true);
view.addEventListener("blur", blur);
view.addEventListener("pagehide", blur);
listening.add(view);
}
}
/** 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 };