fix: 完善模板可视化编辑闭环与通用验收

This commit is contained in:
que01 committed 2026-10-10 16:20:08 +08:00
1 parent fa6b492bce
commit 22a82dbfba
39 files changed
+7773 -443

No files matched your search

@@ -21,7 +21,7 @@ export function TemplateListField({
},
]}
>
{(rows, { add, remove }, { errors }) => (
{(rows, { add, remove, move }, { errors }) => (
<Space direction="vertical" style={{ width: "100%" }}>
{rows.map((row, index) => (
<Space key={row.key} align="start" wrap>
@@ -40,6 +40,20 @@ export function TemplateListField({
</Form.Item>
))}
<Button onClick={() => remove(row.name)}>删除</Button>
<Button
aria-label={`上移第 ${index + 1} 项`}
disabled={index === 0}
onClick={() => move(index, index - 1)}
>
上移
</Button>
<Button
aria-label={`下移第 ${index + 1} 项`}
disabled={index === rows.length - 1}
onClick={() => move(index, index + 1)}
>
下移
</Button>
</Space>
))}
<Form.ErrorList errors={errors} />
@@ -44,7 +44,7 @@ export function ComponentTemplatePreview({
if (target?.composite) composite = candidate;
candidate = candidate.parentElement?.closest<HTMLElement>("[data-editor-target]") ?? null;
}
return composite ?? deepest;
return deepest ?? composite;
},
[targets],
);
@@ -36,6 +36,7 @@ import { TemplatePreview } from "./TemplatePreview.js";
import { InquiryPreviewEditor } from "./InquiryPreviewEditor.js";
import {
resolvePreviewSelection,
previewTrustRecordId,
type PreviewSelection,
type PreviewTarget,
} from "./preview-selection.js";
@@ -72,7 +73,12 @@ function trustSection(
description: declaration.help ?? "编辑当前展示内容。",
page,
target: "trust",
previewTargets: [{ key, label: declaration.label }],
previewTargets: [
{ key, label: declaration.label },
...(declaration.previewTargets ?? []).filter(
(target) => !target.page || target.page === page,
),
],
fields: [
{
key: "title",
@@ -94,11 +100,21 @@ function trustSection(
: {}),
...(field === "content" && declaration.required ? { required: true } : {}),
})),
...(declaration.configFields ?? []).map((field) => ({
...field,
previewTarget: `trust:${declaration.type}`,
})),
].map((field) => ({ ...field, previewTarget: `trust:${declaration.type}` })),
...(declaration.configFields ?? [])
.filter((field) => {
const target = declaration.previewTargets?.find(
(target) => target.key === field.previewTarget,
);
return !target?.page || target.page === page;
})
.map((field) => ({
...field,
previewTarget: field.previewTarget ?? `trust:${declaration.type}`,
})),
].map((field) => ({
...field,
previewTarget: (field as TemplateAdminField).previewTarget ?? `trust:${declaration.type}`,
})),
};
}
@@ -185,18 +201,14 @@ export function PresentationEditor({
binding: field.binding ?? `brand.config.templateData.${field.key}`,
}));
return [
...(declaredTemplateFields.length
? [
{
key: "template-data",
title: "模板专属内容",
description: "这些字段由当前小程序模板定义,保存后发布才会对外展示。",
page: templateContent?.templateFields?.[0]?.page ?? "home",
target: "brand" as const,
fields: declaredTemplateFields,
},
]
: []),
...[...new Set(declaredTemplateFields.map((field) => field.page ?? "home"))].map((page) => ({
key: `template-data:${page}`,
title: "模板专属内容",
description: "这些字段由当前小程序模板定义,保存后发布才会对外展示。",
page,
target: "brand" as const,
fields: declaredTemplateFields.filter((field) => (field.page ?? "home") === page),
})),
...(templateContent?.admin.sections ?? []),
].map((section) => ({
...section,
@@ -204,6 +216,8 @@ export function PresentationEditor({
(field) =>
field.binding.startsWith("brand.config.") ||
[
"brand.name",
"brand.tagline",
"brand.avatarUrl",
"brand.description",
"brand.contactPhone",
@@ -226,12 +240,7 @@ export function PresentationEditor({
const selectedPage = pageOptions.some((item) => item.value === activePage)
? activePage
: pageOptions[0]?.value;
const visibleSections = sections.filter(
(section) =>
section.page === selectedPage &&
// 详情页内容来自所选业务记录;不再展示一套额外的品牌级详情编辑器。
!(selectedPage === "detail" && section.target === "brand"),
);
const visibleSections = sections.filter((section) => section.page === selectedPage);
const recordSections = useMemo(
() =>
(templateContent?.trustContentTypes ?? [])
@@ -242,12 +251,17 @@ export function PresentationEditor({
const visibleTargets = useMemo<PreviewTarget[]>(
() =>
[...(templateContent?.admin.sections ?? []), ...recordSections]
.filter((section) => section.page === selectedPage && selectedPage !== "detail")
.filter(
(section) =>
section.page === selectedPage &&
(selectedPage !== "detail" || section.target === "brand"),
)
.flatMap((section) =>
(section.previewTargets ?? []).map((target) => ({
...target,
page: section.page,
source: previewTargetSource(section, target.key),
...(section.key.startsWith("trust:") ? { trustType: section.key.slice(6) } : {}),
})),
)
.concat(
@@ -384,9 +398,22 @@ export function PresentationEditor({
const save = useMutation({
mutationFn: (values: PresentationValues) => {
const nextConfig = applyPresentationValues(config, sections, values);
// 页面内容编辑不提交模板选择,避免触发切换模板的权限校验。
const { visualTemplateKey: _visualTemplateKey, ...contentConfig } = nextConfig;
const standard = applyStandardBrandValues(
brand as unknown as Record<string, unknown>,
sections,
values,
);
const changed = Object.fromEntries(
Object.entries(standard).filter(
([key, value]) =>
key !== "config" && value !== (brand as unknown as Record<string, unknown>)[key],
),
);
return createApiClient().updateAdminBrand({
config: nextConfig,
...applyStandardBrandValues(brand as unknown as Record<string, unknown>, sections, values),
...changed,
config: contentConfig,
});
},
onSuccess: () => {
@@ -436,23 +463,48 @@ export function PresentationEditor({
[brand, previewBrandValues, previewConfig],
);
const handleValuesChange = useCallback(
(_changed: PresentationValues, allValues: PresentationValues) => {
(changed: PresentationValues, allValues: PresentationValues) => {
setBrandDirty(true);
const synchronized: PresentationValues = {
...(form.getFieldsValue(true) as PresentationValues),
...allValues,
};
for (const section of sections) {
for (const field of section.fields) {
const name = presentationFieldName(section.key, field.key);
if (!Object.prototype.hasOwnProperty.call(changed, name)) continue;
for (const peer of sections) {
for (const item of peer.fields) {
if (item.binding === field.binding)
synchronized[presentationFieldName(peer.key, item.key)] = changed[name];
}
}
}
}
form.setFieldsValue(synchronized);
const next = applyBrandPreviewValues(
brand as unknown as Record<string, unknown>,
config,
sections,
allValues,
synchronized,
);
setPreviewConfig((next.config as Record<string, unknown>) ?? {});
setPreviewBrandValues(next);
},
[brand, config, sections],
[brand, config, sections, form],
);
const draftWorkspace = useMemo(() => {
if (!previewWorkspace) return undefined;
const next: RuntimeWorkspace = {
...previewWorkspace,
templateData: {
...previewWorkspace.templateData,
...(previewConfig.templateData &&
typeof previewConfig.templateData === "object" &&
!Array.isArray(previewConfig.templateData)
? (previewConfig.templateData as Record<string, unknown>)
: {}),
},
inquiryForms: previewWorkspace.inquiryForms.map((item) =>
item.key === inquiryRuntime?.key && inquiryDraft ? { ...item, fields: inquiryDraft } : item,
),
@@ -657,9 +709,10 @@ export function PresentationEditor({
? selectedDetailOfferingId
: draftWorkspace?.offerings[0]?.id
: target.source === "trust"
? draftWorkspace?.trustItems.find(
(item) => target.key === `trust:${item.type}`,
)?.id
? previewTrustRecordId(
draftWorkspace?.trustItems ?? [],
target.trustType,
)
: undefined;
handleTargetSelected({
targetKey: target.key,
@@ -689,12 +742,21 @@ export function PresentationEditor({
? "有未保存修改"
: "草稿已同步"}
</Tag>
{selectedPage === "detail" ? (
{selectedPage === "detail" && !selectedTarget ? (
<Alert
type="info"
showIcon
message="详情页由所选业务内容动态生成"
description="选择不同的业务内容即可预览对应详情;内容请在业务提供项中维护。"
description="选择业务内容预览详情,记录内容在业务提供项中维护;公共标题和按钮文案可从左侧区域列表配置,对全部详情生效。"
action={
<Button
onClick={() => {
void navigate("/admin/services");
}}
>
管理业务内容
</Button>
}
/>
) : activeTarget?.startsWith("inquiry-field:") && inquiryRuntime ? (
<InquiryPreviewEditor
@@ -726,9 +788,10 @@ export function PresentationEditor({
? selectedDetailOfferingId
: draftWorkspace?.offerings[0]?.id
: child.source === "trust"
? draftWorkspace?.trustItems.find(
(item) => child.key === `trust:${item.type}`,
)?.id
? previewTrustRecordId(
draftWorkspace?.trustItems ?? [],
child.trustType,
)
: undefined;
handleTargetSelected({
targetKey: child.key,
@@ -807,6 +870,28 @@ export function PresentationEditor({
: {})}
/>
)
) : selectedRecordSource ? (
<Alert
type="info"
showIcon
message="此区域尚未配置内容"
description="创建并启用对应内容后,即可在预览中点选编辑。"
action={
<Button
onClick={() =>
guardDraft(() => {
void navigate(
selectedRecordSource === "trust"
? `/admin/trust?type=${encodeURIComponent(selectedTarget?.trustType ?? "")}`
: "/admin/services",
);
})
}
>
配置此区域内容
</Button>
}
/>
) : !selectedTarget ? (
<div className="presentation-editor__empty">
<Typography.Title level={5}>选择要编辑的区域</Typography.Title>
@@ -820,7 +905,7 @@ export function PresentationEditor({
layout="vertical"
initialValues={initialValues}
onValuesChange={handleValuesChange}
onFinish={(values) => save.mutate(values)}
onFinish={() => save.mutate(form.getFieldsValue(true) as PresentationValues)}
style={{ minHeight: 0, display: "flex", flexDirection: "column" }}
>
<div style={{ flex: 1 }}>
@@ -34,7 +34,7 @@ function safePath(binding: string): string[] | null {
const prefixes = ["brand.config.", "offering.config.", "trust.config."];
const prefix = prefixes.find((candidate) => binding.startsWith(candidate));
const standardField =
/^(brand\.(avatarUrl|description|contactPhone|contactWechat|address|hours|deliveryNote)|offering\.(name|summary|description|coverUrl|priceText)|trust\.(title|summary|content|coverUrl))$/.test(
/^(brand\.(name|tagline|avatarUrl|description|contactPhone|contactWechat|address|hours|deliveryNote)|offering\.(name|summary|description|coverUrl|priceText)|trust\.(title|summary|content|coverUrl))$/.test(
binding,
);
if (!prefix && !standardField) return null;
@@ -100,6 +100,7 @@ export function applyPresentationValues(
const path = safePath(field.binding);
if (!path) continue;
const raw = values[presentationFieldName(section.key, field.key)];
if (raw === undefined) continue;
const value =
field.input === "list" && Array.isArray(raw)
? field.itemValueKey
@@ -1,11 +1,21 @@
export type PreviewSelection = { targetKey: string; recordId?: string };
// 与模板 slot 一致,优先选择承载结构化内容的记录。
export function previewTrustRecordId(
items: { id?: string; type: string; config: Record<string, unknown> }[],
type: string | undefined,
): string | undefined {
const matches = items.filter((item) => item.type === type);
return (matches.find((item) => Object.keys(item.config).length > 0) ?? matches[0])?.id;
}
export type PreviewTarget = {
key: string;
label: string;
source: "brand" | "offering" | "trust" | "inquiry";
page: string;
composite?: boolean;
trustType?: string;
children?: string[];
};
@@ -18,6 +28,6 @@ export function resolvePreviewSelection(
const target = targets.find((item) => item.key === targetKey && item.page === page);
if (!target) return undefined;
if (recordId && target.source !== "offering" && target.source !== "trust") return undefined;
if (!recordId && (target.source === "offering" || target.source === "trust")) return undefined;
// 空区域仍可选中,编辑面板提供创建内容的入口。
return recordId ? { targetKey, recordId } : { targetKey };
}