feat(ui): H5风格与世界书使用底部选择抽屉

- H5入口:设定页只显示当前风格和已选世界书,单选与多选均从底部抽屉完成

- 交互:独立草稿、确认回填、取消还原、恢复默认、清空选择和焦点返回

- 响应式:桌面保留展开式选择,主设定页和抽屉的提交按钮固定可见

- 验证:类型检查与构建通过,90项浏览器断言覆盖小屏、双主题、键盘和真实保存
This commit is contained in:
湛兮
2026-10-27 12:11:32 +08:00
parent 5adb6eaec3
commit 374ab1c606
2 changed files with 540 additions and 127 deletions
+335 -125
View File
@@ -35,6 +35,7 @@ import {
RefreshCw,
Download,
ChevronDown,
ChevronRight,
} from "lucide-react";
import {
digital,
@@ -130,6 +131,14 @@ export function DigitalHome({
const [profileDraft, setProfileDraft] = useState<Profile | null>(
initial.profile,
);
const [selectionSheet, setSelectionSheet] = useState<
"style" | "books" | null
>(null);
const [choiceDraft, setChoiceDraft] = useState<{
presetId?: string;
bookIds: string[];
}>({ bookIds: [] });
const selectionTrigger = useRef<HTMLElement | null>(null);
const favoritePending = useRef(false);
const [activeMessage, setActiveMessage] = useState<string | null>(null),
[showJump, setShowJump] = useState(false),
@@ -258,13 +267,20 @@ export function DigitalHome({
);
const controls = () =>
Array.from(
dialog?.querySelectorAll<HTMLElement>(
(
document.querySelector<HTMLElement>(".dh-selection-sheet") ?? dialog
)?.querySelectorAll<HTMLElement>(
'button:not([disabled]),input,textarea,select,[tabindex="0"]',
) ?? [],
);
const frame = requestAnimationFrame(() => controls()[0]?.focus());
const onKey = (event: globalThis.KeyboardEvent) => {
if (event.key === "Escape") {
event.preventDefault();
if (document.querySelector(".dh-selection-sheet")) {
setSelectionSheet(null);
return;
}
setDetail(null);
setShowSettings(false);
}
@@ -288,6 +304,35 @@ export function DigitalHome({
previous?.focus();
};
}, [detail, showSettings]);
/** 抽屉聚焦当前选项并限制滚动;取消、确认及切回桌面后恢复入口焦点。 */
useEffect(() => {
if (!selectionSheet) return;
const previous = selectionTrigger.current;
const media = window.matchMedia("(max-width: 600px)");
const resize = () => {
if (!media.matches) setSelectionSheet(null);
};
resize();
media.addEventListener("change", resize);
const frame = requestAnimationFrame(() => {
const panel = document.querySelector<HTMLElement>(".dh-selection-sheet");
(
panel?.querySelector<HTMLElement>("input:checked") ??
panel?.querySelector<HTMLElement>("input") ??
panel?.querySelector<HTMLElement>("button")
)?.focus();
});
return () => {
cancelAnimationFrame(frame);
media.removeEventListener("change", resize);
if (previous?.isConnected && previous.getClientRects().length)
previous.focus();
else
document
.querySelector<HTMLElement>(".dh-desktop-choices input:checked")
?.focus();
};
}, [selectionSheet]);
useEffect(() => {
const id = boot?.decision.decisionId;
if (!id || document.hidden) return;
@@ -586,6 +631,26 @@ export function DigitalHome({
reportError(e);
}
}
/** 抽屉仅编辑临时值;确认后回填设定,取消不改动已有选择。 */
function openSelection(kind: "style" | "books") {
if (!settings) return;
selectionTrigger.current = document.activeElement as HTMLElement;
setChoiceDraft({
presetId: settings.presetId,
bookIds: [...settings.bookIds],
});
setSelectionSheet(kind);
}
function confirmSelection() {
if (settings)
setSettings({
...settings,
...(selectionSheet === "style"
? { presetId: choiceDraft.presetId }
: { bookIds: choiceDraft.bookIds }),
});
setSelectionSheet(null);
}
async function saveSettings(event: FormEvent) {
event.preventDefault();
if (!conversation || !settings) return;
@@ -662,6 +727,97 @@ export function DigitalHome({
(tag === "全部" || c.tags.includes(tag)) &&
(!onlyFavorites || profile?.favorites.includes(c.id)),
);
const styleChoices = (
value: string | undefined,
onChange: (value: string | undefined) => void,
inSheet = false,
) => (
<fieldset className="dh-style-picker">
<legend className={inSheet ? "dh-visually-hidden" : undefined}>
叙事风格
</legend>
<p className="dh-style-help">为这段故事选一种氛围</p>
<div className="dh-style-options">
{[
{
id: "",
name: "角色默认风格",
description: "沿用角色原有的语气与叙事节奏。",
},
...presets,
...(value && !presets.some((p) => p.id === value)
? [
{
id: value,
name: "当前会话风格",
description:
"此风格暂不在可选列表中,可以保留或选择其他风格。",
},
]
: []),
].map((p) => (
<label className="dh-style-option" key={p.id}>
<input
type="radio"
name={
inSheet
? "conversation-preset-sheet"
: "conversation-preset-inline"
}
value={p.id}
checked={(value ?? "") === p.id}
onChange={() => onChange(p.id || undefined)}
/>
<span className="dh-style-card">
<span className="dh-style-symbol" aria-hidden="true">
{p.id ? <WandSparkles /> : <Sparkles />}
</span>
<span className="dh-style-copy">
<strong>{p.name}</strong>
<small>{p.description || "以这套风格继续你们的故事。"}</small>
</span>
<span className="dh-style-check" aria-hidden="true">
<Check />
</span>
</span>
</label>
))}
</div>
</fieldset>
);
const bookChoices = (
value: string[],
onChange: (value: string[]) => void,
inSheet = false,
) => (
<fieldset className="dh-book-picker">
<legend className={inSheet ? "dh-visually-hidden" : undefined}>
加入世界书
</legend>
{books.map((b) => (
<label
className={`dh-checkbox ${value.includes(b.id) ? "is-selected" : ""}`}
key={b.id}
>
<input
type="checkbox"
checked={value.includes(b.id)}
onChange={(e) =>
onChange(
e.target.checked
? [...value, b.id]
: value.filter((v) => v !== b.id),
)
}
/>
<span>
{b.name}
<small>{b.description}</small>
</span>
</label>
))}
</fieldset>
);
const modern = boot?.decision.variant === "B";
return (
<div
@@ -1384,12 +1540,14 @@ export function DigitalHome({
</div>
)}
{showSettings && settings && (
<div className="dh-overlay">
<div className={`dh-overlay ${selectionSheet ? "dh-sheet-open" : ""}`}>
<form
className="dh-settings"
role="dialog"
aria-modal="true"
aria-label="对话设定"
aria-hidden={selectionSheet ? true : undefined}
inert={selectionSheet ? true : undefined}
onSubmit={saveSettings}
>
<header>
@@ -1406,136 +1564,188 @@ export function DigitalHome({
<X />
</button>
</header>
<label>
你的称呼
<input
maxLength={60}
value={settings.userName}
onChange={(e) =>
setSettings({ ...settings, userName: e.target.value })
<div className="dh-settings-body">
<label>
你的称呼
<input
maxLength={60}
value={settings.userName}
onChange={(e) =>
setSettings({ ...settings, userName: e.target.value })
}
/>
</label>
<label>
本次身份
<textarea
rows={3}
maxLength={2000}
value={settings.persona}
onChange={(e) =>
setSettings({ ...settings, persona: e.target.value })
}
/>
</label>
<button
className="dh-text-button"
type="button"
onClick={() =>
setSettings({
...settings,
userName: profile?.userName ?? "旅人",
persona: profile?.persona ?? "",
})
}
/>
</label>
<label>
本次身份
<textarea
rows={3}
maxLength={2000}
value={settings.persona}
onChange={(e) =>
setSettings({ ...settings, persona: e.target.value })
}
/>
</label>
<button
className="dh-text-button"
type="button"
onClick={() =>
setSettings({
...settings,
userName: profile?.userName ?? "旅人",
persona: profile?.persona ?? "",
})
}
>
使用我的身份资料
</button>
<label>
固定记忆
<textarea
rows={3}
maxLength={3000}
value={settings.memory}
placeholder="记录你们共同经历的重要事实…"
onChange={(e) =>
setSettings({ ...settings, memory: e.target.value })
}
/>
</label>
<fieldset className="dh-style-picker">
<legend>叙事风格</legend>
<p className="dh-style-help">为这段故事选一种氛围</p>
<div className="dh-style-options">
{[
{
id: "",
name: "角色默认风格",
description: "沿用角色原有的语气与叙事节奏。",
},
...presets,
...(settings.presetId &&
!presets.some((p) => p.id === settings.presetId)
? [
{
id: settings.presetId,
name: "当前会话风格",
description:
"此风格暂不在可选列表中,可以保留或选择其他风格。",
},
]
: []),
].map((p) => (
<label className="dh-style-option" key={p.id}>
<input
type="radio"
name="conversation-preset"
value={p.id}
checked={(settings.presetId ?? "") === p.id}
onChange={() =>
setSettings({
...settings,
presetId: p.id || undefined,
})
}
/>
<span className="dh-style-card">
<span className="dh-style-symbol" aria-hidden="true">
{p.id ? <WandSparkles /> : <Sparkles />}
</span>
<span className="dh-style-copy">
<strong>{p.name}</strong>
<small>
{p.description || "以这套风格继续你们的故事。"}
</small>
</span>
<span className="dh-style-check" aria-hidden="true">
<Check />
</span>
</span>
</label>
))}
</div>
</fieldset>
<fieldset>
<legend>加入世界书</legend>
{books.map((b) => (
<label className="dh-checkbox" key={b.id}>
<input
type="checkbox"
checked={settings.bookIds.includes(b.id)}
onChange={(e) =>
setSettings({
...settings,
bookIds: e.target.checked
? [...settings.bookIds, b.id]
: settings.bookIds.filter((v) => v !== b.id),
})
}
/>
>
使用我的身份资料
</button>
<label>
固定记忆
<textarea
rows={3}
maxLength={3000}
value={settings.memory}
placeholder="记录你们共同经历的重要事实…"
onChange={(e) =>
setSettings({ ...settings, memory: e.target.value })
}
/>
</label>
<div className="dh-mobile-choices">
<button
type="button"
className="dh-choice-trigger"
aria-label="选择叙事风格"
onClick={() => openSelection("style")}
>
<WandSparkles aria-hidden="true" />
<span>
{b.name}
<small>{b.description}</small>
<strong>叙事风格</strong>
<small>
{presets.find((p) => p.id === settings.presetId)?.name ??
(settings.presetId ? "当前会话风格" : "角色默认风格")}
</small>
</span>
</label>
))}
</fieldset>
<p className="dh-form-note">
保存后从下一次回复生效。可以固定单条消息,将它加入对话记忆。
</p>
<ChevronRight aria-hidden="true" />
</button>
<button
type="button"
className="dh-choice-trigger"
aria-label="选择世界书"
onClick={() => openSelection("books")}
>
<BookOpen aria-hidden="true" />
<span>
<strong>加入世界书</strong>
<small>
{settings.bookIds.length
? `已选 ${settings.bookIds.length} 本 · ${books
.filter((b) => settings.bookIds.includes(b.id))
.map((b) => b.name)
.join("、")}`
: "未加入世界书"}
</small>
</span>
<ChevronRight aria-hidden="true" />
</button>
</div>
<div className="dh-desktop-choices">
{styleChoices(settings.presetId, (value) =>
setSettings({ ...settings, presetId: value }),
)}
{bookChoices(settings.bookIds, (value) =>
setSettings({ ...settings, bookIds: value }),
)}
</div>
<p className="dh-form-note">
保存后从下一次回复生效。可以固定单条消息,将它加入对话记忆。
</p>
</div>
<button className="dh-primary" type="submit">
保存设定
</button>
</form>
{selectionSheet && (
<div className="dh-selection-layer">
<button
type="button"
className="dh-selection-backdrop"
aria-label="取消选择"
tabIndex={-1}
onClick={() => setSelectionSheet(null)}
/>
<section
className="dh-selection-sheet"
role="dialog"
aria-modal="true"
aria-label={
selectionSheet === "style" ? "选择叙事风格" : "选择世界书"
}
>
<div className="dh-sheet-handle" aria-hidden="true" />
<header>
<div>
<h2>
{selectionSheet === "style" ? "叙事风格" : "加入世界书"}
</h2>
<p>
{selectionSheet === "style"
? "选择你喜欢的故事氛围"
: "可选择多本,丰富角色的故事背景"}
</p>
</div>
<button
type="button"
className="dh-icon"
aria-label="关闭选择抽屉"
onClick={() => setSelectionSheet(null)}
>
<X />
</button>
</header>
<div className="dh-selection-body">
{selectionSheet === "style"
? styleChoices(
choiceDraft.presetId,
(value) =>
setChoiceDraft({ ...choiceDraft, presetId: value }),
true,
)
: bookChoices(
choiceDraft.bookIds,
(value) =>
setChoiceDraft({ ...choiceDraft, bookIds: value }),
true,
)}
</div>
<footer>
<button
type="button"
className="dh-sheet-reset"
onClick={() =>
setChoiceDraft(
selectionSheet === "style"
? { ...choiceDraft, presetId: undefined }
: { ...choiceDraft, bookIds: [] },
)
}
>
{selectionSheet === "style" ? "恢复默认" : "清空选择"}
</button>
<button
type="button"
className="dh-primary"
onClick={confirmSelection}
>
确认选择
{selectionSheet === "books"
? `(${choiceDraft.bookIds.length})`
: ""}
</button>
</footer>
</section>
</div>
)}
</div>
)}
</div>