feat(web): add player name, freeform input, and unified settings modal

- Player name: stored in localStorage, injected into Architect/Writer/InsertBeat
  prompts so NPCs address the player by name, displayed in dialogue UI
- Freeform input: compact button at choice nodes expands to text input, LLM
  classifier routes to insert-beat (interactive NPC response) or change-scene
- SettingsModal: unified panel merging player name, voice toggle (with
  collapsible TTS key section), replacing the old TtsKeyModal
- Insert-beat upgrade: prompt now requires NPC reaction when characters are
  present, shared by both freeform and Vision paths
- IME guard: isComposing check on freeform input to prevent CJK mid-composition
  submission

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
yuanzonghao
2026-06-07 12:03:13 +08:00
parent b0b5630a25
commit ae3dd17e6b
11 changed files with 897 additions and 77 deletions
+5 -1
View File
@@ -16,11 +16,15 @@ export function DialogueHistoryModal({
items,
portrait,
onClose,
playerName,
}: {
items: DialogueHistoryItem[];
portrait: boolean;
onClose: () => void;
playerName?: string;
}) {
const displaySpeaker = (s: string | undefined) =>
s === "你" && playerName ? playerName : s;
const listRef = useRef<HTMLDivElement>(null);
useEffect(() => {
@@ -97,7 +101,7 @@ export function DialogueHistoryModal({
</span>
{item.speaker && (
<span className="font-serif text-[12px] text-[rgba(205,165,90,0.92)]">
{item.speaker}
{displaySpeaker(item.speaker)}
</span>
)}
</div>
+132 -11
View File
@@ -174,8 +174,10 @@ export function PlayCanvas({
onBackgroundClick,
onAdvance,
onSelectChoice,
onFreeformInput,
fullViewport = false,
orientation = "landscape",
playerName,
aboveCanvas,
aboveCanvasLeft,
belowCanvas,
@@ -190,9 +192,11 @@ export function PlayCanvas({
onBackgroundClick: (click: { x: number; y: number }) => void;
onAdvance: () => void;
onSelectChoice: (choice: BeatChoice) => void;
onFreeformInput?: (text: string) => void;
fullViewport?: boolean;
// 会话锁定的图片朝向。"portrait" 时整图铺满视口(object-fit:cover)、选项竖排、字号放大。
orientation?: Orientation;
playerName?: string;
// 渲染在图片正上方、右对齐的 slot(画面外、紧贴右上角)。
aboveCanvas?: ReactNode;
// 渲染在图片正上方、左对齐的 slot(画面外、紧贴左上角),与 aboveCanvas 水平镜像。
@@ -204,6 +208,11 @@ export function PlayCanvas({
const imgRef = useRef<HTMLImageElement>(null);
const audioRef = useRef<HTMLAudioElement>(null);
const [historyOpen, setHistoryOpen] = useState(false);
const [freeformOpen, setFreeformOpen] = useState(false);
const [freeformText, setFreeformText] = useState("");
const freeformInputRef = useRef<HTMLInputElement>(null);
const displaySpeaker = (s: string | undefined) =>
s === "你" && playerName ? playerName : s;
const [audioDurationMs, setAudioDurationMs] = useState<number | undefined>(
undefined,
);
@@ -424,6 +433,7 @@ export function PlayCanvas({
items={dialogueHistory}
portrait={portrait}
onClose={() => setHistoryOpen(false)}
playerName={playerName}
/>
)}
@@ -435,16 +445,127 @@ export function PlayCanvas({
: "gap-[1.5%]"
}`}
>
{choices.map((choice, i) => (
<ChoiceButton
key={choice.id}
index={i}
label={choice.label}
disabled={phase !== "ready"}
vertical={portrait}
onClick={() => onSelectChoice(choice)}
/>
))}
{freeformOpen && onFreeformInput ? (
/* ── Expanded: full-width input replaces all choices ── */
<div
className="flex-1 flex items-center gap-2"
style={{
background: "rgba(20, 14, 8, 0.68)",
border: "1.5px solid rgba(180, 140, 80, 0.65)",
borderRadius: "6px",
backdropFilter: "blur(8px)",
WebkitBackdropFilter: "blur(8px)",
boxShadow: "0 2px 12px rgba(0,0,0,0.4), inset 0 1px 0 rgba(200,165,90,0.12)",
padding: "8px 12px",
}}
>
<input
ref={freeformInputRef}
value={freeformText}
onChange={(e) => setFreeformText(e.target.value.slice(0, 50))}
onKeyDown={(e) => {
if (e.key === "Enter" && !e.nativeEvent.isComposing && freeformText.trim() && phase === "ready") {
onFreeformInput(freeformText.trim());
setFreeformOpen(false);
setFreeformText("");
} else if (e.key === "Escape") {
setFreeformOpen(false);
setFreeformText("");
}
}}
placeholder="输入你想说的或想做的..."
maxLength={50}
autoFocus
className="flex-1 min-w-0 bg-transparent border-none outline-none font-serif text-[14px] placeholder:text-[rgba(200,185,155,0.50)]"
style={{ color: "rgba(245,235,210,0.95)" }}
/>
<button
type="button"
disabled={!freeformText.trim() || phase !== "ready"}
onClick={() => {
if (freeformText.trim()) {
onFreeformInput(freeformText.trim());
setFreeformOpen(false);
setFreeformText("");
}
}}
className="shrink-0 flex items-center justify-center w-8 h-8 rounded-sm transition-colors disabled:opacity-30"
style={{ color: "rgba(195,155,75,0.9)" }}
>
<i className="fa-solid fa-paper-plane text-[12px]" />
</button>
<button
type="button"
onClick={() => { setFreeformOpen(false); setFreeformText(""); }}
className="shrink-0 flex items-center justify-center w-8 h-8 rounded-sm transition-colors"
style={{ color: "rgba(200,185,155,0.55)" }}
>
<i className="fa-solid fa-xmark text-[13px]" />
</button>
</div>
) : (
/* ── Collapsed: normal choices + small freeform trigger ── */
<>
{choices.map((choice, i) => (
<ChoiceButton
key={choice.id}
index={i}
label={choice.label}
disabled={phase !== "ready"}
vertical={portrait}
onClick={() => onSelectChoice(choice)}
/>
))}
{onFreeformInput && (
<button
type="button"
disabled={phase !== "ready"}
onClick={() => {
setFreeformOpen(true);
requestAnimationFrame(() => freeformInputRef.current?.focus());
}}
className="group shrink-0 flex items-center justify-center transition-all duration-200 disabled:opacity-50 disabled:cursor-wait"
style={{
background: "rgba(20, 14, 8, 0.45)",
border: "1.5px dashed rgba(180, 140, 80, 0.40)",
borderRadius: "6px",
backdropFilter: "blur(8px)",
WebkitBackdropFilter: "blur(8px)",
width: portrait ? "100%" : "42px",
padding: portrait ? "10px 16px" : "0",
}}
title="自由输入"
>
<span
className="opacity-0 group-hover:opacity-100 absolute inset-0 rounded-[5px] transition-opacity duration-200 pointer-events-none"
style={{
background: "rgba(180,140,60,0.08)",
border: "1.5px dashed rgba(200,165,90,0.70)",
}}
/>
{portrait ? (
<span className="relative flex items-center gap-2">
<i
className="fa-solid fa-pen-to-square text-[11px]"
style={{ color: "rgba(195,155,75,0.60)" }}
/>
<span
className="font-serif text-[13px]"
style={{ color: "rgba(200,185,155,0.70)" }}
>
</span>
</span>
) : (
<i
className="fa-solid fa-pen-to-square text-[12px] relative"
style={{ color: "rgba(195,155,75,0.55)" }}
/>
)}
</button>
)}
</>
)}
</div>
)}
@@ -484,7 +605,7 @@ export function PlayCanvas({
}`}
style={{ color: "rgba(205,165,90,0.92)" }}
>
{beat.speaker}
{displaySpeaker(beat.speaker)}
</p>
)}
+395
View File
@@ -0,0 +1,395 @@
"use client";
import { type ReactNode, useEffect, useState } from "react";
import {
clearStoredTtsConfig,
readStoredTtsConfig,
writeStoredTtsConfig,
} from "@/lib/clientTtsConfig";
import {
findTtsPreset,
PAYG_PRESET_ID,
TTS_KEY_DOC_URL,
TTS_REGION_PRESETS,
} from "@/lib/ttsPresets";
const PLAYER_NAME_STORAGE_KEY = "infiplot:playerName";
export function readStoredPlayerName(): string {
try {
return localStorage.getItem(PLAYER_NAME_STORAGE_KEY) ?? "";
} catch {
return "";
}
}
export function writeStoredPlayerName(name: string): void {
try {
if (name) {
localStorage.setItem(PLAYER_NAME_STORAGE_KEY, name);
} else {
localStorage.removeItem(PLAYER_NAME_STORAGE_KEY);
}
} catch {
/* ignore */
}
}
export function SettingsModal({
initialAudioEnabled = true,
onClose,
onSaved,
footerNote,
}: {
initialAudioEnabled?: boolean;
onClose: () => void;
onSaved: (settings: { ttsConfigured: boolean; playerName: string; audioEnabled: boolean }) => void;
footerNote?: ReactNode;
}) {
const [initialTts] = useState(() => readStoredTtsConfig());
const initialKind = findTtsPreset(initialTts?.presetId)?.kind ?? "payg";
const [keyType, setKeyType] = useState<"token-plan" | "payg">(initialKind);
const [regionId, setRegionId] = useState<string>(
initialKind === "token-plan"
? (initialTts?.presetId ?? TTS_REGION_PRESETS[0]!.id)
: TTS_REGION_PRESETS[0]!.id,
);
const [apiKey, setApiKey] = useState<string>(initialTts?.apiKey ?? "");
const [showKey, setShowKey] = useState(false);
const ttsAlreadyConfigured = initialTts != null;
const [playerName, setPlayerName] = useState(() => readStoredPlayerName());
const [voiceOn, setVoiceOn] = useState(initialAudioEnabled);
const [shown, setShown] = useState(false);
const expectedPrefix = keyType === "payg" ? "sk-" : "tp-";
const prefixMismatch =
apiKey.trim().length > 0 && !apiKey.trim().startsWith(expectedPrefix);
useEffect(() => {
const id = requestAnimationFrame(() => setShown(true));
return () => cancelAnimationFrame(id);
}, []);
const close = () => {
setShown(false);
setTimeout(onClose, 280);
};
const save = () => {
const name = playerName.trim();
writeStoredPlayerName(name);
try {
localStorage.setItem("infiplot:muted", voiceOn ? "0" : "1");
} catch { /* ignore */ }
const key = apiKey.trim();
let ttsConfigured = false;
if (key) {
const presetId = keyType === "payg" ? PAYG_PRESET_ID : regionId;
writeStoredTtsConfig({ presetId, apiKey: key });
ttsConfigured = true;
} else if (!ttsAlreadyConfigured) {
ttsConfigured = false;
} else {
ttsConfigured = true;
}
if (ttsConfigured && !voiceOn) setVoiceOn(true);
const finalVoiceOn = ttsConfigured ? true : voiceOn;
onSaved({ ttsConfigured, playerName: name, audioEnabled: finalVoiceOn });
close();
};
const clearAll = () => {
clearStoredTtsConfig();
writeStoredPlayerName("");
try { localStorage.removeItem("infiplot:muted"); } catch { /* ignore */ }
onSaved({ ttsConfigured: false, playerName: "", audioEnabled: true });
close();
};
const hasAnySetting = ttsAlreadyConfigured || readStoredPlayerName().length > 0;
return (
<div
onMouseDown={close}
className={
"fixed inset-0 z-[60] flex items-center justify-center p-6 md:p-10 transition-all duration-300 " +
(shown
? "bg-clay-900/30 backdrop-blur-md"
: "bg-clay-900/0 backdrop-blur-0")
}
>
<div
onMouseDown={(e) => e.stopPropagation()}
className={
"flex w-[560px] max-w-[94vw] max-h-[88vh] flex-col overflow-hidden rounded-sm border border-clay-900/15 bg-cream-50 shadow-2xl shadow-clay-900/25 transition-all duration-300 " +
(shown ? "opacity-100 scale-100" : "opacity-0 scale-95")
}
>
{/* Header */}
<div className="flex items-center gap-5 px-6 md:px-8 py-5 border-b border-clay-900/10">
<div className="flex flex-col">
<span className="font-serif text-xl md:text-2xl text-clay-900">
</span>
<span className="text-[11px] text-clay-500 mt-1 tracking-wide">
·
</span>
</div>
<button
type="button"
onClick={close}
aria-label="关闭"
className="ml-auto text-xl leading-none text-clay-500 hover:text-clay-900 transition-colors"
>
<i className="fa-solid fa-xmark" />
</button>
</div>
<div className="flex flex-col gap-0 overflow-y-auto">
{/* ── Player Name Section ── */}
<div className="flex flex-col gap-3 px-6 md:px-8 py-5">
<div className="flex items-center gap-2.5">
<span className="flex h-7 w-7 items-center justify-center rounded-sm border border-clay-900/10 bg-cream-100 text-clay-400">
<i className="fa-solid fa-user-pen text-[11px]" />
</span>
<span className="font-serif text-base text-clay-900">
</span>
</div>
<input
value={playerName}
onChange={(e) => setPlayerName(e.target.value)}
type="text"
maxLength={20}
autoComplete="off"
spellCheck={false}
placeholder="不填则使用「你」"
className="h-11 w-full rounded-sm border border-clay-900/15 bg-cream-100 px-4 font-sans text-sm text-clay-900 outline-none transition-colors focus:border-ember-500 placeholder:text-clay-400"
/>
<span className="text-[11px] text-clay-400">
NPC
</span>
</div>
<div className="border-t border-clay-900/8 mx-6 md:mx-8" />
{/* ── Voice Section (toggle + key as child) ── */}
<div className="flex flex-col gap-3 px-6 md:px-8 pt-5 pb-5">
<div className="flex items-center gap-2.5">
<span className="flex h-7 w-7 items-center justify-center rounded-sm border border-clay-900/10 bg-cream-100 text-clay-400">
<i className="fa-solid fa-volume-high text-[11px]" />
</span>
<span className="font-serif text-base text-clay-900">
</span>
</div>
<div className="grid grid-cols-2 gap-2">
{(
[
{ on: true, label: "开启", icon: "fa-solid fa-volume-high" },
{ on: false, label: "关闭", icon: "fa-solid fa-volume-xmark" },
] as const
).map((t) => {
const active = voiceOn === t.on;
return (
<button
key={String(t.on)}
type="button"
onClick={() => setVoiceOn(t.on)}
className={
"flex items-center justify-center gap-2 rounded-sm border px-3 py-2.5 text-[13px] transition-all " +
(active
? "border-ember-500 bg-ember-500/5 text-clay-900"
: "border-clay-900/12 text-clay-600 hover:border-clay-900/35 hover:bg-cream-100")
}
>
<i className={t.icon + " text-[11px]"} />
{t.label}
</button>
);
})}
</div>
{/* ── TTS Key (sub-section, only when voice is on) ── */}
{voiceOn && (
<div className="mt-3 flex flex-col gap-4 rounded-sm border border-clay-900/8 bg-cream-100/40 p-4">
<div className="flex items-center gap-2">
<i className="fa-solid fa-key text-[10px] text-clay-400" />
<span className="text-[13px] text-clay-800">
Key
</span>
<span className="text-[10px] text-clay-400"></span>
</div>
<p className="text-[12px] leading-relaxed text-clay-500">
<span className="text-clay-800"> MiMo API Key</span>
Key MiMo
TTS
<span className="text-clay-800"></span>
使
</p>
<div className="flex flex-col gap-2">
<span className="text-[10px] smallcaps text-clay-500">
K e y ·
</span>
<div className="grid grid-cols-2 gap-2">
{(
[
{
kind: "payg",
label: "按量付费 Pay-as-you-go",
sub: "sk- 开头",
},
{
kind: "token-plan",
label: "套餐 Token Plan",
sub: "tp- 开头",
},
] as const
).map((t) => {
const active = keyType === t.kind;
return (
<button
key={t.kind}
type="button"
onClick={() => setKeyType(t.kind)}
className={
"flex flex-col gap-0.5 rounded-sm border px-3 py-2.5 text-left transition-all " +
(active
? "border-ember-500 bg-ember-500/5 text-clay-900"
: "border-clay-900/12 text-clay-600 hover:border-clay-900/35 hover:bg-cream-100")
}
>
<span className="text-[13px]">{t.label}</span>
<span className="text-[10px] text-clay-400">
{t.sub}
</span>
</button>
);
})}
</div>
</div>
{keyType === "token-plan" && (
<div className="flex flex-col gap-2">
<span className="text-[10px] smallcaps text-clay-500">
</span>
<div className="grid grid-cols-1 gap-2 sm:grid-cols-3">
{TTS_REGION_PRESETS.map((p) => {
const active = p.id === regionId;
return (
<button
key={p.id}
type="button"
onClick={() => setRegionId(p.id)}
className={
"rounded-sm border px-3 py-2.5 text-left text-[13px] transition-all " +
(active
? "border-ember-500 bg-ember-500/5 text-clay-900"
: "border-clay-900/12 text-clay-600 hover:border-clay-900/35 hover:bg-cream-100")
}
>
{p.label}
</button>
);
})}
</div>
<span className="text-[11px] text-clay-400">
</span>
</div>
)}
<div className="flex flex-col gap-2">
<span className="text-[10px] smallcaps text-clay-500">
A P I · K e y
</span>
<div className="relative">
<input
value={apiKey}
onChange={(e) => setApiKey(e.target.value)}
type={showKey ? "text" : "password"}
autoComplete="off"
spellCheck={false}
placeholder={
keyType === "payg"
? "粘贴 sk- 开头的按量 Key"
: "粘贴 tp- 开头的套餐 Key"
}
className="h-11 w-full rounded-sm border border-clay-900/15 bg-cream-50 pl-4 pr-11 font-sans text-sm text-clay-900 outline-none transition-colors focus:border-ember-500 placeholder:text-clay-400"
/>
<button
type="button"
onClick={() => setShowKey((v) => !v)}
aria-label={showKey ? "隐藏" : "显示"}
className="absolute right-3 top-1/2 -translate-y-1/2 text-clay-400 hover:text-clay-700 transition-colors"
>
<i
className={`fa-solid ${showKey ? "fa-eye-slash" : "fa-eye"} text-sm`}
/>
</button>
</div>
{prefixMismatch && (
<span className="flex items-start gap-1.5 text-[11px] leading-relaxed text-ember-500">
<i className="fa-solid fa-triangle-exclamation mt-0.5 text-[10px]" />
Key {expectedPrefix}
{keyType === "payg"
? "按量付费 Pay-as-you-go"
: "套餐 Token Plan"}
</span>
)}
<a
href={TTS_KEY_DOC_URL}
target="_blank"
rel="noopener noreferrer"
className="inline-flex items-center gap-1.5 text-[11px] text-ember-500 hover:text-ember-400 transition-colors"
>
<i className="fa-brands fa-github text-[11px]" />
Key
</a>
</div>
{footerNote && (
<p className="text-[11px] leading-relaxed text-clay-400">
{footerNote}
</p>
)}
</div>
)}
</div>
</div>
{/* Footer */}
<div className="flex items-center gap-3 border-t border-clay-900/10 px-6 md:px-8 py-4">
{hasAnySetting && (
<button
type="button"
onClick={clearAll}
className="inline-flex items-center gap-2 rounded-sm border border-clay-900/15 px-4 py-2 font-sans text-sm text-clay-600 transition-colors hover:border-clay-900/35 hover:text-clay-900"
>
<i className="fa-solid fa-rotate-left text-xs" />
</button>
)}
<button
type="button"
onClick={save}
className="ml-auto inline-flex items-center gap-2 rounded-sm bg-clay-900 px-5 py-2.5 font-sans text-sm text-cream-50 transition-colors hover:bg-ember-500"
>
<i className="fa-solid fa-check text-xs" />
</button>
</div>
</div>
</div>
);
}