Files
chatpapers/src/modules/ui/lecturePrefs.ts
T

95 lines
3.1 KiB
TypeScript

import { getPref, setPref } from "../../utils/prefs";
import { getString } from "../../utils/locale";
import { getHostPlatform } from "../lecture/infrastructure/platform/os";
import {
TTS_PROVIDER_ORDER,
TTS_PROVIDER_PRESETS,
getDefaultTtsProviderId,
isTtsProviderAvailable,
} from "../lecture/infrastructure/tts/providers";
export function registerLecturePrefsScripts(win: Window) {
const doc = win.document;
const providerSelect = doc.getElementById(
"chatpapers-pref-tts-provider",
) as HTMLSelectElement | null;
const voiceInput = doc.getElementById(
"chatpapers-pref-tts-voice",
) as HTMLInputElement | null;
const piperPathInput = doc.getElementById(
"chatpapers-pref-piper-path",
) as HTMLInputElement | null;
const piperModelInput = doc.getElementById(
"chatpapers-pref-piper-model",
) as HTMLInputElement | null;
const mineruPathInput = doc.getElementById(
"chatpapers-pref-mineru-path",
) as HTMLInputElement | null;
const lectureDataDirInput = doc.getElementById(
"chatpapers-pref-lecture-data-dir",
) as HTMLInputElement | null;
const hint = doc.getElementById("chatpapers-pref-tts-hint");
const piperBox = doc.getElementById("chatpapers-pref-piper-box");
if (providerSelect && providerSelect.options.length === 0) {
for (const id of TTS_PROVIDER_ORDER) {
const preset = TTS_PROVIDER_PRESETS[id];
if (!isTtsProviderAvailable(preset)) continue;
const opt = doc.createElement("option");
opt.value = id;
opt.textContent = preset.label;
providerSelect.appendChild(opt);
}
const current = getPref("ttsProvider") || getDefaultTtsProviderId();
providerSelect.value = current;
if (!getPref("ttsProvider")) setPref("ttsProvider", current);
}
const updateHint = () => {
const id = providerSelect?.value || getPref("ttsProvider") || "";
const preset = TTS_PROVIDER_PRESETS[id as keyof typeof TTS_PROVIDER_PRESETS];
if (hint) {
hint.textContent = preset?.description || getString("prefs-tts-hint-empty");
}
if (piperBox) {
(piperBox as HTMLElement).hidden = id !== "piper";
}
if (voiceInput) {
const platform = getHostPlatform();
voiceInput.placeholder =
platform === "mac"
? "Ting-Ting(中文)/ Samantha(英文)"
: platform === "win"
? "Microsoft Huihui Desktop"
: "";
}
};
providerSelect?.addEventListener("change", () => {
setPref("ttsProvider", providerSelect.value);
updateHint();
});
voiceInput?.addEventListener("change", () => {
setPref("ttsVoice", voiceInput.value);
});
piperPathInput?.addEventListener("change", () => {
setPref("piperPath", piperPathInput.value);
});
piperModelInput?.addEventListener("change", () => {
setPref("piperModelPath", piperModelInput.value);
});
mineruPathInput?.addEventListener("change", () => {
setPref("mineruPath", mineruPathInput.value);
});
lectureDataDirInput?.addEventListener("change", () => {
setPref("lectureDataDir", lectureDataDirInput.value.trim());
});
updateHint();
}