import { createPortal } from 'react-dom' import { useEffect, useLayoutEffect, useMemo, useRef, useState, type CSSProperties, type ReactNode } from 'react' import { useCaps } from '../lib/capabilities' import { Icon } from '../lib/icons' import { ARROW_GAP, ARROW_KEYS, AREA_COLORS, AREAS, KEY_ROWS, KEYBOARD_UNITS, KEYS_BY_CODE, LAYER_NAMES, LAYERS, MODIFIER_KEY, MOUSE, bindings, modifierLayer, padRange, unusedNote, type Area, type Binding, type KeyDef, type Layer, type MouseGesture, } from '../lib/shortcuts' interface Props { onClose: () => void } /** Key unit bounds, px. The map shrinks to fit the dialog; below LABEL_MIN the * words on the keys go (the caption still explains each key), below SHORT_MIN * the long modifier legends become symbols. */ const UNIT_MAX = 52 const UNIT_MIN = 20 const LABEL_MIN = 44 const SHORT_MIN = 38 const ALL_AREAS = Object.keys(AREAS) as Area[] const matchText = (b: Binding) => `${b.text} ${b.label}`.toLowerCase() const mouseText = (r: MouseGesture) => `${r.gesture} ${r.on ?? ''} ${r.text}`.toLowerCase() /** Every shortcut, drawn on a keyboard: a key lights in the colour of what it * does, pointing at (or pressing) it explains it, and holding a modifier — or * picking one above — relights the map with that layer. Mouse gestures are * listed beneath. The content lives in `lib/shortcuts.ts`. */ export function ShortcutsDialog({ onClose }: Props) { const slots = useCaps().cues.hotcue_slots const binds = useMemo(() => bindings(slots), [slots]) const [sticky, setSticky] = useState('base') const [held, setHeld] = useState>(new Set()) const [hover, setHover] = useState(null) const [pressed, setPressed] = useState(null) const [query, setQuery] = useState('') const [areas, setAreas] = useState>(new Set(ALL_AREAS)) const layer: Layer = held.has('mod') ? 'mod' : held.has('shift') ? 'shift' : sticky const q = query.trim().toLowerCase() // ---- fit: one key unit from the width the map has ---- const fitRef = useRef(null) const [unit, setUnit] = useState(UNIT_MAX) useLayoutEffect(() => { const el = fitRef.current if (!el) return const fit = () => { const u = Math.floor(el.clientWidth / KEYBOARD_UNITS) setUnit(Math.max(UNIT_MIN, Math.min(UNIT_MAX, u))) } fit() const ro = new ResizeObserver(fit) ro.observe(el) return () => ro.disconnect() }, []) // ---- keyboard: pressing a key explains it rather than doing it ---- // Capture phase on window, so the deck's and the table's own listeners (which // are on window too, bubbling) never see a key pressed in here. const queryRef = useRef(query) queryRef.current = query useEffect(() => { let release: number | undefined const onDown = (e: KeyboardEvent) => { const inField = (e.target as HTMLElement | null)?.tagName === 'INPUT' if (e.key === 'Escape') { e.preventDefault() e.stopPropagation() if (inField && queryRef.current) setQuery('') else onClose() return } if (inField) return const mod = modifierLayer(e.code) if (mod) { setHeld((h) => new Set(h).add(mod)) return } if (e.code === 'Tab' || !KEYS_BY_CODE[e.code]) return e.stopPropagation() // Space / Enter still press a focused control (a layer tab, a modifier key). const t = e.target as HTMLElement | null if ((e.code === 'Space' || e.code === 'Enter') && t?.tagName === 'BUTTON' && !t.dataset.mapKey) return e.preventDefault() setHover(e.code) setPressed(e.code) window.clearTimeout(release) release = window.setTimeout(() => setPressed(null), 160) } const onUp = (e: KeyboardEvent) => { const mod = modifierLayer(e.code) if (mod) { setHeld((h) => { const next = new Set(h) next.delete(mod) return next }) } else if (KEYS_BY_CODE[e.code] && (e.target as HTMLElement | null)?.tagName !== 'INPUT') { e.stopPropagation() // the deck's keyup ends a held cue/hotcue preview } } const onBlur = () => setHeld(new Set()) window.addEventListener('keydown', onDown, true) window.addEventListener('keyup', onUp, true) window.addEventListener('blur', onBlur) return () => { window.clearTimeout(release) window.removeEventListener('keydown', onDown, true) window.removeEventListener('keyup', onUp, true) window.removeEventListener('blur', onBlur) } }, [onClose]) const visible = (l: Layer, code: string) => (binds[l][code] ?? []).filter((b) => areas.has(b.area)) const layerCount = (l: Layer) => Object.values(binds[l]).flat().filter((b) => areas.has(b.area) && (!q || matchText(b).includes(q))).length + MOUSE.flatMap((c) => c.rows).filter((r) => (r.mod ?? 'base') === l && (!q || mouseText(r).includes(q))).length const pickLayer = (l: Layer) => setSticky((s) => (s === l && l !== 'base' ? 'base' : l)) const toggleArea = (a: Area) => setAreas((cur) => { const next = new Set(cur) if (next.has(a) && next.size > 1) next.delete(a) else next.add(a) return next }) const gap = Math.max(2, Math.round(unit / 10)) const keyProps = { unit, gap, labels: unit >= LABEL_MIN, short: unit < SHORT_MIN, layer, hover, pressed, q, visible, noted: (code: string) => !!unusedNote(code, slots), bound: (code: string) => LAYERS.some((l) => binds[l][code]), onHover: setHover, onPick: pickLayer, } return createPortal(
e.stopPropagation()} >
Keyboard Shortcuts
setQuery(e.target.value)} placeholder="Search shortcuts" aria-label="Search shortcuts" className="well ml-auto w-64 min-w-0 rounded-[9px] border border-line px-2.5 py-1.5 text-[13px] text-text outline-none" />
{LAYERS.map((l) => ( ))}
{ALL_AREAS.map((a) => ( ))}
setHover(null)} >
{KEY_ROWS.map((row, i) => (
{row.map((k) => ( ))}
))}
{ARROW_KEYS.map((k, i) => k ? : , )}
Mouse
{MOUSE.map((col) => (
{col.title}
{col.rows.map((r, i) => { const m = r.mod ?? 'base' const hit = !q || mouseText(r).includes(q) const lit = layer !== 'base' && m === layer && hit const dim = (layer !== 'base' && m !== layer) || !hit return (
{r.mod && {MODIFIER_KEY[r.mod]}} {r.gesture} {r.on ? ` ${r.on}` : ''} {r.text}
) })}
))}
Press the ? key anywhere to open this list.
, document.body, ) } function Kbd({ children }: { children: ReactNode }) { return ( {children} ) } function Combo({ layer, code }: { layer: Layer; code: string }) { const legend = KEYS_BY_CODE[code]?.legend ?? code return ( {layer !== 'base' && {MODIFIER_KEY[layer]}} {legend} ) } function Line({ layer, code, b, other }: { layer: Layer; code: string; b: Binding; other?: boolean }) { return (
{b.text} {AREAS[b.area]}
) } const Sub = ({ children }: { children: ReactNode }) => (
{children}
) function Caption({ hover, layer, q, query, slots, binds, areas, }: { hover: string | null layer: Layer q: string query: string slots: number binds: ReturnType areas: ReadonlySet }) { if (hover) { const role = KEYS_BY_CODE[hover]?.role if (role) { const keys = Object.keys(binds[role]).length const mouse = MOUSE.flatMap((c) => c.rows).filter((r) => r.mod === role).length return (
{MODIFIER_KEY[role]} Hold, or click, to light up what {LAYER_NAMES[role]} changes: {keys} key{keys === 1 ? '' : 's'} and{' '} {mouse} mouse gesture{mouse === 1 ? '' : 's'}.
) } const here = binds[layer][hover] ?? [] const others = LAYERS.filter((l) => l !== layer && binds[l][hover]) return ( <> {here.length ? ( here.map((b, i) => ) ) : (
{unusedNote(hover, slots) ?? 'Does nothing in Konduktor.'}
)} {others.length > 0 && Also on this key} {others.flatMap((l) => binds[l][hover].map((b, i) => ))} ) } if (q) { const hits = LAYERS.flatMap((l) => Object.entries(binds[l]).flatMap(([code, bs]) => bs .filter((b) => areas.has(b.area) && matchText(b).includes(q)) .map((b, i) => ), ), ) const mouse = MOUSE.flatMap((c) => c.rows).filter((r) => mouseText(r).includes(q)).length if (!hits.length && !mouse) return No shortcut matches “{query.trim()}”. return ( <> {hits} {mouse > 0 && {mouse} mouse gesture{mouse === 1 ? '' : 's'} below} ) } if (layer !== 'base') { return ( <> Showing what {LAYER_NAMES[layer]} changes. The highlighted mouse gestures below use it too. Let go of the key, or pick “No modifier”, to go back. ) } return ( <> Point at a key, or press it, to see what it does. Hold {MODIFIER_KEY.shift} or {MODIFIER_KEY.mod}, or pick one above, to see what each modifier changes. {padRange(slots)} are this library's {slots} hotcue pads; Q W E R are the stems of a stem track. ) } interface KeyProps { def: KeyDef fnRow: boolean unit: number gap: number labels: boolean short: boolean layer: Layer hover: string | null pressed: string | null q: string visible: (layer: Layer, code: string) => Binding[] noted: (code: string) => boolean bound: (code: string) => boolean onHover: (code: string | null) => void onPick: (layer: Layer) => void } function Key({ def, fnRow, unit, gap, labels, short, layer, hover, pressed, q, visible, noted, bound, onHover, onPick }: KeyProps) { const { code, role } = def const legend = short && def.short ? def.short : def.legend const ents = role ? [] : visible(layer, code) const first = ents[0] const kc = first ? (first.color ?? AREA_COLORS[first.area]) : 'var(--color-faint)' const on = role === layer const miss = !!first && !!q && !ents.some((b) => matchText(b).includes(q)) const pad = first?.label === 'Pad' || first?.label === 'Delete' const interactive = !!role || bound(code) || noted(code) const inset = Math.max(2, Math.round(unit * 0.1)) const style: CSSProperties = { width: (def.w ?? 1) * unit - gap, height: fnRow ? Math.round(unit * 0.72) : unit - gap, padding: `${Math.max(2, inset - 1)}px ${inset}px`, borderRadius: pad ? Math.round(unit * 0.18) : Math.round(unit * 0.14), borderColor: 'rgb(255 255 255 / 0.05)', } if (on) { Object.assign(style, { background: 'var(--color-accent)', borderColor: 'transparent', color: 'var(--color-ink-950)', boxShadow: '0 0 22px -4px var(--color-accent)', }) } else if (role) { style.borderColor = 'rgb(255 255 255 / 0.12)' } else if (first) { Object.assign(style, { background: pressed === code ? `color-mix(in oklab, ${kc} 45%, transparent)` : `color-mix(in oklab, ${kc} ${pad ? 24 : 15}%, rgb(255 255 255 / 0.05))`, borderColor: hover === code ? `color-mix(in oklab, ${kc} 85%, white)` : `color-mix(in oklab, ${kc} 50%, transparent)`, boxShadow: `0 0 18px -6px color-mix(in oklab, ${kc} 70%, transparent), inset 0 1px 0 rgb(255 255 255 / 0.1)`, }) } else if (hover === code) { style.borderColor = 'rgb(255 255 255 / 0.3)' } const legendSize = Math.max(8.5, Math.min(11, unit * 0.22)) - (role && !short ? 1 : 0) const body = ( <> {legend} {labels && first && ( {first.label} )} {ents.length > 1 && ( {ents.map((b, i) => ( ))} )} ) const className = `relative flex shrink-0 flex-col items-start justify-between border text-left transition-[background,border-color,opacity,transform,box-shadow] duration-150 ${ first || on ? '' : 'bg-ink-850' } ${role && !on ? 'text-muted' : 'text-faint'} ${miss ? 'opacity-25' : ''} ${pressed === code ? 'scale-[0.96]' : ''}` if (!interactive) { return ( onHover(def.legend ? code : null)}> {body} ) } return ( ) }