import { type FC, type MutableRefObject, useMemo, useRef, useState } from "react"; import { cn } from "../../../utils/bem"; import { clamp } from "../../../utils/utilities"; import type { TimelineSideControlProps } from "../Types"; import "./FramesControl.prefix.css"; export const FramesControl: FC = ({ position = 0, length = 0, onPositionChange }) => { const [inputMode, setInputMode] = useState(false); const duration = useMemo(() => { return length - 1; }, [length]); return (
setInputMode(true)}> {inputMode ? ( { onPositionChange?.(clamp(value, 0, length)); }} onFinishEditing={() => { setInputMode(false); }} /> ) : ( <> {clamp(Math.round(position + 1), 1, duration + 1)} of {duration + 1} )}
); }; interface FrameInputProps { position: number; length: number; onChange: (value: number) => void; onFinishEditing: () => void; } const allowedKeys = ["ArrowUp", "ArrowDown", "Backspace", "Delete", "Enter", /[0-9]/]; const FrameInput: FC = ({ length, position, onChange, onFinishEditing }) => { const input = useRef() as MutableRefObject; const notifyChange = (value: number) => { onChange?.(clamp(value, 1, length)); }; return ( input.current?.select()} onKeyDown={(e) => { const allowedKey = allowedKeys.find((k) => (k instanceof RegExp ? k.test(e.key) : k === e.key)); if (!allowedKey && !e.metaKey) e.preventDefault(); const value = Number.parseInt(input.current!.value); const step = e.shiftKey ? 10 : 1; if (e.key === "Enter") { notifyChange?.(value); onFinishEditing?.(); } else if (e.key === "Escape") { onFinishEditing?.(); } else if (allowedKey === "ArrowUp") { input.current!.value = clamp(value + step, 1, length).toString(); e.preventDefault(); } else if (allowedKey === "ArrowDown") { input.current!.value = clamp(value - step, 1, length).toString(); e.preventDefault(); } }} onBlur={() => onFinishEditing?.()} /> ); };