All files / src/lib hotkeys.ts

100% Statements 58/58
100% Branches 71/71
100% Functions 9/9
100% Lines 51/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227                                            5x   5x 5x   5x                               5x       243x     237x   202x       16x     10x     9x       20x       21x   5x   6x   5x     42x     10x     9x     18x   8x   7x   13x   3x                   5x 22x   5x 32x     5x   16x                                                 5x 105x     5x 103x 100x 100x     5x                                 5x 25x   24x     25x 15x 15x   1x         5x                                         5x 58x                       5x           42x 29x 13x    
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 Max Boksem. See NOTICE for additional terms under GPLv3 section 7.
 
import type { SkipIntervals } from "./player-prefs";
 
/** Player keyboard actions. Resolved from key events by `resolvePlayerHotkey`. */
export type PlayerAction =
	| { type: "togglePlay" }
	| { type: "toggleFullscreen" }
	| { type: "toggleMute" }
	| { type: "seekBy"; seconds: number }
	| { type: "seekToPercent"; percent: number }
	| { type: "volumeBy"; delta: number }
	| { type: "nextEpisode" }
	| { type: "prevEpisode" }
	| { type: "toggleSubtitles" }
	| { type: "toggleInfo" }
	| { type: "toggleShortcuts" }
	| { type: "skipSegment" }
	| { type: "escape" };
 
/** How far the plain arrow and J/L keys jump when no skip lengths are passed in. */
export const SEEK_SHORT = 10;
/** How far Shift with an arrow or J/L jumps. */
export const SEEK_LONG = 30;
export const VOLUME_STEP = 5;
 
const DEFAULT_SKIP: SkipIntervals = { back: SEEK_SHORT, forward: SEEK_SHORT };
 
interface KeyLike {
	key: string;
	shiftKey: boolean;
	ctrlKey: boolean;
	metaKey: boolean;
	altKey: boolean;
}
 
/**
 * Map a keydown event to a player action, or null if the key isn't bound.
 * Combinations with Cmd/Ctrl/Alt are never handled so OS and app shortcuts
 * (Cmd+F, Cmd+M, Cmd+Q, …) keep working. `skip` sets how far the plain arrow
 * and J/L keys jump.
 */
export const resolvePlayerHotkey = (
	e: KeyLike,
	skip: SkipIntervals = DEFAULT_SKIP
): PlayerAction | null => {
	if (e.ctrlKey || e.metaKey || e.altKey) return null;
 
	// Digit keys: 0–9 jump to 0%–90% of the runtime (YouTube convention).
	if (/^[0-9]$/.test(e.key)) return { type: "seekToPercent", percent: Number(e.key) * 10 };
 
	switch (e.key) {
		case " ":
		case "k":
		case "K":
			return { type: "togglePlay" };
		case "f":
		case "F":
			return { type: "toggleFullscreen" };
		case "m":
		case "M":
			return { type: "toggleMute" };
		case "ArrowLeft":
		case "j":
		case "J":
			return { type: "seekBy", seconds: e.shiftKey ? -SEEK_LONG : -skip.back };
		case "ArrowRight":
		case "l":
		case "L":
			return { type: "seekBy", seconds: e.shiftKey ? SEEK_LONG : skip.forward };
		case "Home":
			return { type: "seekToPercent", percent: 0 };
		case "ArrowUp":
			return { type: "volumeBy", delta: VOLUME_STEP };
		case "ArrowDown":
			return { type: "volumeBy", delta: -VOLUME_STEP };
		case "n":
		case "N":
			return { type: "nextEpisode" };
		case "p":
		case "P":
			return { type: "prevEpisode" };
		case "s":
		case "S":
			return { type: "toggleSubtitles" };
		case "i":
		case "I":
			return { type: "toggleInfo" };
		case "?":
			return { type: "toggleShortcuts" };
		case "Enter":
			return { type: "skipSegment" };
		case "Escape":
			return { type: "escape" };
		default:
			return null;
	}
};
 
/**
 * Whether the action runs again while its key is held down. Seeking and
 * volume keep going. The rest run once per press, since a held F, Space or M
 * would flip back and forth, a held Escape would leave fullscreen and then
 * the player, and a held N, P or Enter would skip several episodes.
 */
export const repeatsWhenHeld = (action: PlayerAction): boolean =>
	action.type === "seekBy" || action.type === "volumeBy";
 
const skipLabel = ({ back, forward }: SkipIntervals): string =>
	back === forward ? `Seek ${back}s back / forward` : `Seek ${back}s back / ${forward}s forward`;
 
/** Shortcut reference shown in the player's "?" overlay. */
export const playerShortcuts = (
	skip: SkipIntervals = DEFAULT_SKIP
): { keys: string[]; label: string }[] => [
	{ keys: ["Space", "K"], label: "Play / pause" },
	{ keys: ["F"], label: "Toggle fullscreen (or double-click)" },
	{ keys: ["M"], label: "Mute / unmute" },
	{ keys: ["←", "→"], label: skipLabel(skip) },
	{ keys: ["J", "L"], label: skipLabel(skip) },
	{ keys: ["Shift ←", "Shift →"], label: `Seek ${SEEK_LONG}s back / forward` },
	{ keys: ["Shift J", "Shift L"], label: `Seek ${SEEK_LONG}s back / forward` },
	{ keys: ["0–9"], label: "Jump to 0%–90%" },
	{ keys: ["Home"], label: "Jump to start" },
	{ keys: ["↑", "↓"], label: "Volume up / down" },
	{ keys: ["N", "P"], label: "Next / previous episode" },
	{ keys: ["S"], label: "Subtitles" },
	{ keys: ["I"], label: "Info" },
	{ keys: ["Enter"], label: "Skip intro or credits / next episode" },
	{ keys: ["?"], label: "Show / hide this list" },
	{ keys: ["Esc"], label: "Exit fullscreen / close panel / leave player" },
];
 
/**
 * True when `target` sits inside `container` in the page. React passes a key
 * pressed in a portal, such as a confirm opened from the player's controls, to
 * the key handlers of the components that rendered it, even though the portal
 * is elsewhere in the page.
 */
export const isInside = (container: Element | null, target: EventTarget | null): boolean =>
	container !== null && target instanceof Node && container.contains(target);
 
/** True when the event target is a text field, so typing shouldn't trigger hotkeys. */
export const isTypingTarget = (target: EventTarget | null): boolean => {
	if (!(target instanceof HTMLElement)) return false;
	const tag = target.tagName;
	return tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT" || target.isContentEditable;
};
 
const BUTTON_ROLES = new Set([
	"button",
	"link",
	"checkbox",
	"switch",
	"tab",
	"menuitem",
	"menuitemcheckbox",
	"menuitemradio",
]);
 
/**
 * True when the target is a button-like control reached with the keyboard, so
 * Space should activate it rather than play/pause. Clicking a button also
 * focuses it in Chromium and WebKitGTK; those focuses don't match
 * :focus-visible, so Space keeps controlling playback after a mouse click.
 */
export const isKeyboardFocusedControl = (target: EventTarget | null): boolean => {
	if (!(target instanceof HTMLElement)) return false;
	const isControl =
		target.tagName === "BUTTON" ||
		target.tagName === "A" ||
		BUTTON_ROLES.has(target.getAttribute("role") ?? "");
	if (!isControl) return false;
	try {
		return target.matches(":focus-visible");
	} catch {
		return true;
	}
};
 
/** Keys a focused slider moves its value with. */
const SLIDER_KEYS = new Set([
	"ArrowLeft",
	"ArrowRight",
	"ArrowUp",
	"ArrowDown",
	"Home",
	"End",
	"PageUp",
	"PageDown",
]);
 
/**
 * True when a slider reached with the keyboard should move its own value for
 * this key, such as an arrow on the seek or volume bar after Tab. A slider
 * focused by a press leaves these keys to the player, so the arrows keep
 * seeking by the skip length instead of nudging the bar one step.
 *
 * The `Slider` component records how its thumb got focus in
 * `data-focus-source`. `:focus-visible` can't be used here, because WebKit
 * starts matching it on a pressed element as soon as a key goes down.
 */
export const isSliderOwnKey = (target: EventTarget | null, key: string): boolean =>
	target instanceof HTMLElement &&
	target.getAttribute("role") === "slider" &&
	SLIDER_KEYS.has(key) &&
	target.getAttribute("data-focus-source") !== "pointer";
 
/**
 * keydown handler for a slider inside the player. A slider that owns the key
 * (see `isSliderOwnKey`) moves and keeps the key from the player's own
 * handlers, such as the subtitle position arrows. Otherwise the slider stays
 * put and the key goes on to the player. There Left, Right and Home seek, Up
 * and Down change the volume, and End, Page Up and Page Down do nothing.
 */
export const leaveSliderKeysToPlayer = (e: {
	key: string;
	target: EventTarget | null;
	preventDefault: () => void;
	stopPropagation: () => void;
}): void => {
	if (!SLIDER_KEYS.has(e.key)) return;
	if (isSliderOwnKey(e.target, e.key)) e.stopPropagation();
	else e.preventDefault();
};