import classNames from "classnames";
import { useCallback, useEffect, useRef, useState } from "react";
import { Toggle } from "@/components/buttons/Toggle";
import { Icon, Icons } from "@/components/Icon";
import { Menu } from "@/components/player/internals/ContextMenu";
import { useOverlayRouter } from "@/hooks/useOverlayRouter";
import { useProgressBar } from "@/hooks/useProgressBar";
import { useSubtitleStore } from "@/stores/subtitles";
export function ColorOption(props: {
color: string;
active?: boolean;
onClick: () => void;
}) {
return (
{props.active ? (
) : null}
);
}
function CaptionSetting(props: {
textTransformer?: (s: string) => string;
value: number;
onChange?: (val: number) => void;
max: number;
label: string;
min: number;
}) {
const inputRef = useRef(null);
const ref = useRef(null);
const currentPercentage = (props.value - props.min) / (props.max - props.min);
const commit = useCallback(
(percentage) => {
const range = props.max - props.min;
const newPercentage = Math.min(Math.max(percentage, 0), 1);
props.onChange?.(props.min + range * newPercentage);
},
[props]
);
const { dragging, dragPercentage, dragMouseDown } = useProgressBar(
ref,
commit,
true
);
const [isFocused, setIsFocused] = useState(false);
const [inputValue, setInputValue] = useState("");
useEffect(() => {
function listener(e: KeyboardEvent) {
if (e.key === "Enter" && isFocused) {
inputRef.current?.blur();
}
}
window.addEventListener("keydown", listener);
return () => {
window.removeEventListener("keydown", listener);
};
}, [isFocused]);
const inputClasses =
"px-3 py-1 bg-video-context-inputBg rounded w-20 text-left text-white cursor-text";
const textTransformer = props.textTransformer ?? ((s) => s);
return (
);
}
const colors = ["#ffffff", "#80b1fa", "#e2e535"];
export function CaptionSettingsView({ id }: { id: string }) {
const router = useOverlayRouter(id);
const styling = useSubtitleStore((s) => s.styling);
const overrideCasing = useSubtitleStore((s) => s.overrideCasing);
const setOverrideCasing = useSubtitleStore((s) => s.setOverrideCasing);
const updateStyling = useSubtitleStore((s) => s.updateStyling);
return (
<>
router.navigate("/captions")}>
Custom captions
`${s}%`}
onChange={(v) => updateStyling({ size: v / 100 })}
value={styling.size * 100}
/>
updateStyling({ backgroundOpacity: v / 100 })}
value={styling.backgroundOpacity * 100}
textTransformer={(s) => `${s}%`}
/>
Color
{colors.map((v) => (
updateStyling({ color: v })}
color={v}
active={styling.color === v}
/>
))}
Fix capitalization
setOverrideCasing(!overrideCasing)}
/>
>
);
}