1087 lines
42 KiB
TypeScript
1087 lines
42 KiB
TypeScript
import {
|
|
Alert,
|
|
Button,
|
|
Divider,
|
|
Col,
|
|
Form,
|
|
Grid,
|
|
Input,
|
|
InputNumber,
|
|
Row,
|
|
Select,
|
|
Segmented,
|
|
Space,
|
|
Tag,
|
|
Typography,
|
|
message,
|
|
} from "antd";
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
import { useNavigate } from "react-router-dom";
|
|
import type {
|
|
AdminTemplateContent,
|
|
RuntimeWorkspace,
|
|
TemplateAdminField,
|
|
TemplateAdminSection,
|
|
} from "@personal-brand/api-client";
|
|
import type { BrandProfile } from "@personal-brand/api-client";
|
|
import { projectBrandAliases, projectTrustPresentation } from "@personal-brand/api-client";
|
|
import { createApiClient } from "../../lib/api.js";
|
|
import { ImageUrlField } from "../../components/ImageUrlField.js";
|
|
import { TemplateListField } from "../../components/TemplateListField.js";
|
|
import { MutationError } from "../../components/QueryState.js";
|
|
import { RecordContentEditor } from "../../components/RecordContentEditor.js";
|
|
import { TemplatePreview } from "./TemplatePreview.js";
|
|
import { InquiryPreviewEditor } from "./InquiryPreviewEditor.js";
|
|
import {
|
|
resolvePreviewSelection,
|
|
previewTrustRecordId,
|
|
type PreviewSelection,
|
|
type PreviewTarget,
|
|
} from "./preview-selection.js";
|
|
import {
|
|
applyBrandPreviewValues,
|
|
applyStandardBrandValues,
|
|
applyStandardEntityValues,
|
|
applyPresentationValues,
|
|
filterSectionsForPreviewTarget,
|
|
presentationFieldName,
|
|
readPresentationValue,
|
|
type PresentationValues,
|
|
} from "./presentation-bindings.js";
|
|
import "./PresentationEditor.css";
|
|
import { useEditorDraft } from "./use-editor-draft.js";
|
|
|
|
interface PresentationEditorProps {
|
|
templateContent: AdminTemplateContent | null | undefined;
|
|
previewWorkspace?: RuntimeWorkspace | undefined;
|
|
config: Record<string, unknown> | null | undefined;
|
|
brand: BrandProfile;
|
|
loading?: boolean;
|
|
error?: unknown;
|
|
initialSelection?: PreviewSelection;
|
|
}
|
|
|
|
function trustSection(
|
|
declaration: NonNullable<AdminTemplateContent["trustContentTypes"]>[number],
|
|
page: string,
|
|
): TemplateAdminSection {
|
|
const key = `trust:${declaration.type}`;
|
|
return {
|
|
key,
|
|
title: declaration.label,
|
|
description: declaration.help ?? "编辑当前展示内容。",
|
|
page,
|
|
target: "trust",
|
|
previewTargets: [
|
|
{ key, label: declaration.label },
|
|
...(declaration.previewTargets ?? []).filter(
|
|
(target) => !target.page || target.page === page,
|
|
),
|
|
],
|
|
fields: [
|
|
{
|
|
key: "title",
|
|
binding: "trust.title",
|
|
label: declaration.adminFields.title,
|
|
input: "text" as const,
|
|
required: true,
|
|
},
|
|
...(["summary", "content"] as const)
|
|
.filter((field) => !declaration.hideStandardFields?.includes(field))
|
|
.map((field) => ({
|
|
key: field,
|
|
binding: `trust.${field}`,
|
|
label: declaration.adminFields[field],
|
|
input: "textarea" as const,
|
|
previewTarget: `trust:${declaration.type}`,
|
|
...(field === "summary" && declaration.adminFields.summaryHelp
|
|
? { help: declaration.adminFields.summaryHelp }
|
|
: {}),
|
|
...(field === "content" && declaration.required ? { required: true } : {}),
|
|
})),
|
|
...(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}`,
|
|
})),
|
|
};
|
|
}
|
|
|
|
function renderField(field: TemplateAdminField) {
|
|
if (field.input === "textarea")
|
|
return (
|
|
<Input.TextArea
|
|
rows={3}
|
|
maxLength={field.maxLength}
|
|
showCount={!!field.maxLength}
|
|
placeholder={field.placeholder}
|
|
/>
|
|
);
|
|
if (field.input === "image")
|
|
return <ImageUrlField {...(field.placeholder ? { placeholder: field.placeholder } : {})} />;
|
|
if (field.input === "number")
|
|
return <InputNumber style={{ width: "100%" }} placeholder={field.placeholder} />;
|
|
if (field.input === "boolean")
|
|
return (
|
|
<Select
|
|
options={[
|
|
{ value: true, label: "是" },
|
|
{ value: false, label: "否" },
|
|
]}
|
|
/>
|
|
);
|
|
if (field.input === "select" || field.input === "multi-select")
|
|
return (
|
|
<Select
|
|
{...(field.input === "multi-select" ? { mode: "multiple" as const } : {})}
|
|
options={field.options ?? []}
|
|
/>
|
|
);
|
|
return (
|
|
<Input
|
|
type={field.input === "phone" ? "tel" : "text"}
|
|
maxLength={field.maxLength}
|
|
placeholder={field.placeholder}
|
|
/>
|
|
);
|
|
}
|
|
|
|
function previewTargetSource(
|
|
section: TemplateAdminSection,
|
|
targetKey: string,
|
|
): "brand" | "offering" | "trust" | "inquiry" {
|
|
const fields = section.fields.filter((field) => field.previewTarget === targetKey);
|
|
if (fields.some((field) => field.binding.startsWith("offering."))) return "offering";
|
|
if (fields.some((field) => field.binding.startsWith("trust."))) return "trust";
|
|
if (fields.some((field) => field.binding.startsWith("brand."))) return "brand";
|
|
return section.target;
|
|
}
|
|
|
|
export function PresentationEditor({
|
|
templateContent,
|
|
previewWorkspace,
|
|
config,
|
|
brand,
|
|
loading,
|
|
error,
|
|
initialSelection,
|
|
}: PresentationEditorProps) {
|
|
const [form] = Form.useForm<PresentationValues>();
|
|
const queryClient = useQueryClient();
|
|
const navigate = useNavigate();
|
|
const breakpoints = Grid.useBreakpoint();
|
|
const [activePage, setActivePage] = useState<string>();
|
|
const [selection, setSelection] = useState<PreviewSelection | undefined>(initialSelection);
|
|
const [hasInitializedSelection, setHasInitializedSelection] = useState(Boolean(initialSelection));
|
|
const [detailOfferingId, setDetailOfferingId] = useState<string>();
|
|
const [recordDraft, setRecordDraft] = useState<{
|
|
source: "offering" | "trust";
|
|
id: string;
|
|
values: PresentationValues;
|
|
}>();
|
|
const [previewConfig, setPreviewConfig] = useState(config ?? {});
|
|
const [previewBrandValues, setPreviewBrandValues] = useState<Record<string, unknown>>({});
|
|
const [inquiryDraft, setInquiryDraft] =
|
|
useState<RuntimeWorkspace["inquiryForms"][number]["fields"]>();
|
|
const [brandDirty, setBrandDirty] = useState(false);
|
|
const guardDraft = useEditorDraft(brandDirty || !!recordDraft || !!inquiryDraft);
|
|
const inquiryRuntime = previewWorkspace?.inquiryForms.find((item) => item.target === "brand");
|
|
const sections = useMemo(() => {
|
|
const declaredTemplateFields = (templateContent?.templateFields ?? []).map((field) => ({
|
|
...field,
|
|
binding: field.binding ?? `brand.config.templateData.${field.key}`,
|
|
}));
|
|
return [
|
|
...[...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,
|
|
fields: section.fields.filter(
|
|
(field) =>
|
|
field.binding.startsWith("brand.config.") ||
|
|
[
|
|
"brand.name",
|
|
"brand.tagline",
|
|
"brand.avatarUrl",
|
|
"brand.description",
|
|
"brand.contactPhone",
|
|
"brand.contactWechat",
|
|
"brand.address",
|
|
].includes(field.binding),
|
|
),
|
|
}));
|
|
}, [templateContent]);
|
|
const pageOptions = useMemo(() => {
|
|
const pages = new Map<string, string>();
|
|
for (const item of templateContent?.navigation ?? []) pages.set(item.page, item.label);
|
|
for (const section of templateContent?.admin.sections ?? []) {
|
|
const navigation = templateContent?.navigation.find((item) => item.page === section.page);
|
|
if (!pages.has(section.page))
|
|
pages.set(section.page, navigation?.label ?? pageTitle(section.page));
|
|
}
|
|
return [...pages].map(([value, label]) => ({ value, label }));
|
|
}, [templateContent]);
|
|
const selectedPage = pageOptions.some((item) => item.value === activePage)
|
|
? activePage
|
|
: pageOptions[0]?.value;
|
|
const visibleSections = sections.filter((section) => section.page === selectedPage);
|
|
const recordSections = useMemo(
|
|
() =>
|
|
(templateContent?.trustContentTypes ?? [])
|
|
.filter((item) => item.pages?.includes(selectedPage ?? ""))
|
|
.map((item) => trustSection(item, selectedPage ?? "home")),
|
|
[templateContent, selectedPage],
|
|
);
|
|
const visibleTargets = useMemo<PreviewTarget[]>(
|
|
() =>
|
|
[...(templateContent?.admin.sections ?? []), ...recordSections]
|
|
.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(
|
|
selectedPage === "inquiry" && inquiryRuntime
|
|
? inquiryRuntime.fields.map((field) => ({
|
|
key: `inquiry-field:${field.key}`,
|
|
label: field.label,
|
|
page: "inquiry",
|
|
source: "inquiry" as const,
|
|
}))
|
|
: [],
|
|
),
|
|
[templateContent, selectedPage, recordSections, inquiryRuntime],
|
|
);
|
|
const activeTarget = selection?.targetKey;
|
|
const selectedTarget = visibleTargets.find((target) => target.key === activeTarget);
|
|
const selectedSection = [...(templateContent?.admin.sections ?? []), ...recordSections].find(
|
|
(section) =>
|
|
section.page === selectedPage &&
|
|
section.previewTargets?.some((target) => target.key === selectedTarget?.key),
|
|
);
|
|
const selectedRecordSource =
|
|
selectedTarget?.source === "offering" || selectedTarget?.source === "trust"
|
|
? selectedTarget.source
|
|
: undefined;
|
|
const offeringsQuery = useQuery({
|
|
queryKey: ["admin-offerings"],
|
|
queryFn: () => createApiClient().listAdminOfferings(),
|
|
enabled: selectedRecordSource === "offering",
|
|
refetchOnMount: "always",
|
|
});
|
|
const trustQuery = useQuery({
|
|
queryKey: ["trust-content"],
|
|
queryFn: () => createApiClient().listAdminTrustContent(),
|
|
enabled: selectedRecordSource === "trust",
|
|
});
|
|
const selectedRecord =
|
|
selectedRecordSource === "offering"
|
|
? offeringsQuery.data?.find((item) => item.id === selection?.recordId)
|
|
: selectedRecordSource === "trust"
|
|
? trustQuery.data?.find((item) => item.id === selection?.recordId)
|
|
: undefined;
|
|
const editableSections = filterSectionsForPreviewTarget(
|
|
visibleSections.filter((section) =>
|
|
section.fields.some((field) => field.binding.startsWith("brand.")),
|
|
),
|
|
selectedTarget?.key,
|
|
);
|
|
const recordSection = useMemo(
|
|
() =>
|
|
selectedSection && selection?.targetKey
|
|
? {
|
|
...selectedSection,
|
|
fields: selectedSection.fields.filter(
|
|
(field) => field.previewTarget === selection.targetKey,
|
|
),
|
|
}
|
|
: undefined,
|
|
[selectedSection, selection?.targetKey],
|
|
);
|
|
const handleTargetSelected = useCallback(
|
|
(next?: PreviewSelection) => {
|
|
const select = () =>
|
|
setSelection(
|
|
next
|
|
? resolvePreviewSelection(
|
|
next.targetKey,
|
|
next.recordId,
|
|
visibleTargets,
|
|
selectedPage ?? "home",
|
|
)
|
|
: undefined,
|
|
);
|
|
if (recordDraft || inquiryDraft)
|
|
guardDraft(() => {
|
|
setInquiryDraft(undefined);
|
|
setRecordDraft(undefined);
|
|
select();
|
|
});
|
|
else select();
|
|
},
|
|
[visibleTargets, selectedPage, recordDraft, inquiryDraft, guardDraft],
|
|
);
|
|
const relatedTargets = [
|
|
...(templateContent?.admin.sections ?? [])
|
|
.filter((section) => section.page === selectedPage && section.target !== "brand")
|
|
.map((section) => section.target),
|
|
...((templateContent?.trustContentTypes ?? []).some((item) =>
|
|
item.pages?.includes(selectedPage ?? ""),
|
|
)
|
|
? ["trust"]
|
|
: []),
|
|
];
|
|
const pageLinks = [...new Set(relatedTargets)].flatMap((target) =>
|
|
target === "trust"
|
|
? (templateContent?.trustContentTypes ?? [])
|
|
.filter((item) => item.pages?.includes(selectedPage ?? ""))
|
|
.map((item) => ({
|
|
target: `trust:${item.type}`,
|
|
label: `编辑${item.label}`,
|
|
}))
|
|
: [
|
|
{
|
|
target,
|
|
label: (
|
|
{ offering: "管理此页展示的业务内容", inquiry: "管理此页的咨询表单" } as Record<
|
|
string,
|
|
string
|
|
>
|
|
)[target],
|
|
},
|
|
],
|
|
);
|
|
const relevantPageLinks = selectedSection
|
|
? pageLinks.filter((item) => {
|
|
if (item.target.startsWith("trust:")) return selectedSection.target === "trust";
|
|
return item.target === selectedSection.target;
|
|
})
|
|
: pageLinks;
|
|
const initialValues = useMemo(() => {
|
|
const values: PresentationValues = {};
|
|
for (const section of sections) {
|
|
for (const field of section.fields) {
|
|
values[presentationFieldName(section.key, field.key)] = readPresentationValue(
|
|
config,
|
|
field.binding,
|
|
field,
|
|
brand as unknown as Record<string, unknown>,
|
|
);
|
|
}
|
|
}
|
|
return values;
|
|
}, [config, sections]);
|
|
|
|
const save = useMutation({
|
|
mutationFn: (values: PresentationValues) => {
|
|
const nextConfig = applyPresentationValues(config, sections, values);
|
|
// 页面内容编辑不提交模板选择,避免触发切换模板的权限校验。
|
|
const contentConfig = { ...nextConfig };
|
|
delete contentConfig.visualTemplateKey;
|
|
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({
|
|
...changed,
|
|
config: contentConfig,
|
|
});
|
|
},
|
|
onSuccess: () => {
|
|
setBrandDirty(false);
|
|
message.success("页面内容已保存为草稿,发布后小程序才会更新");
|
|
window.dispatchEvent(new Event("template-preview-refresh"));
|
|
void queryClient.invalidateQueries({ queryKey: ["admin-brand"] });
|
|
void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] });
|
|
void queryClient.invalidateQueries({ queryKey: ["admin-preview-workspace"] });
|
|
void queryClient.invalidateQueries({ queryKey: ["public-runtime-workspace"] });
|
|
void queryClient.invalidateQueries({ queryKey: ["publication-readiness"] });
|
|
},
|
|
});
|
|
|
|
useEffect(() => {
|
|
form.setFieldsValue(initialValues);
|
|
}, [form, initialValues]);
|
|
|
|
useEffect(() => {
|
|
const initialPage =
|
|
typeof initialSelection === "object" && "page" in initialSelection
|
|
? String(initialSelection.page)
|
|
: undefined;
|
|
if (initialPage) {
|
|
setActivePage(
|
|
pageOptions.some((item) => item.value === initialPage)
|
|
? initialPage
|
|
: pageOptions[0]?.value,
|
|
);
|
|
return;
|
|
}
|
|
if (!activePage && pageOptions.length) setActivePage(pageOptions[0]?.value);
|
|
}, [activePage, initialSelection, pageOptions]);
|
|
useEffect(() => {
|
|
if (hasInitializedSelection) return;
|
|
if (!pageOptions.length) return;
|
|
if (!initialSelection) return;
|
|
const requestedPage =
|
|
typeof initialSelection === "object" && "page" in initialSelection
|
|
? String(initialSelection.page)
|
|
: undefined;
|
|
const resolvedPage =
|
|
requestedPage && pageOptions.some((item) => item.value === requestedPage)
|
|
? requestedPage
|
|
: (pageOptions[0]?.value ?? "home");
|
|
setSelection(
|
|
resolvePreviewSelection(
|
|
initialSelection.targetKey,
|
|
initialSelection.recordId,
|
|
visibleTargets,
|
|
resolvedPage,
|
|
),
|
|
);
|
|
setActivePage(resolvedPage);
|
|
setHasInitializedSelection(true);
|
|
}, [hasInitializedSelection, initialSelection, pageOptions, selectedPage, visibleTargets]);
|
|
useEffect(() => {
|
|
if (!initialSelection) setSelection(undefined);
|
|
}, [initialSelection, selectedPage, templateContent?.displayName]);
|
|
useEffect(() => {
|
|
if (!selection?.recordId) return;
|
|
const records =
|
|
selectedTarget?.source === "trust"
|
|
? previewWorkspace?.trustItems
|
|
: previewWorkspace?.offerings;
|
|
if (records && !records.some((item) => item.id === selection.recordId)) setSelection(undefined);
|
|
}, [
|
|
previewWorkspace?.offerings,
|
|
previewWorkspace?.trustItems,
|
|
selectedTarget?.source,
|
|
selection,
|
|
]);
|
|
useEffect(
|
|
() => setRecordDraft(undefined),
|
|
[selection?.targetKey, selection?.recordId, selectedPage],
|
|
);
|
|
useEffect(() => {
|
|
setPreviewConfig(config ?? {});
|
|
setPreviewBrandValues({});
|
|
}, [config]);
|
|
const previewBrand = useMemo(
|
|
() => ({ ...brand, ...previewBrandValues, config: previewConfig }),
|
|
[brand, previewBrandValues, previewConfig],
|
|
);
|
|
const handleValuesChange = useCallback(
|
|
(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,
|
|
synchronized,
|
|
);
|
|
setPreviewConfig((next.config as Record<string, unknown>) ?? {});
|
|
setPreviewBrandValues(next);
|
|
},
|
|
[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,
|
|
),
|
|
brand: projectBrandAliases(
|
|
{
|
|
...previewWorkspace.brand,
|
|
...(previewBrand as unknown as Record<string, unknown>),
|
|
config: previewConfig,
|
|
},
|
|
previewConfig,
|
|
),
|
|
};
|
|
if (!recordDraft || !selectedRecord || !recordSection || recordDraft.id !== selectedRecord.id)
|
|
return next;
|
|
const values = recordDraft.values;
|
|
const config = applyPresentationValues(selectedRecord.config, [recordSection], values);
|
|
const standard = applyStandardEntityValues(
|
|
selectedRecord as unknown as Record<string, unknown>,
|
|
recordDraft.source,
|
|
[recordSection],
|
|
values,
|
|
);
|
|
if (recordDraft.source === "trust") {
|
|
next.trustItems = next.trustItems.map((item) =>
|
|
item.id === recordDraft.id
|
|
? {
|
|
...item,
|
|
title: typeof standard.title === "string" ? standard.title : item.title,
|
|
summary: typeof standard.summary === "string" ? standard.summary : item.summary,
|
|
content: typeof standard.content === "string" ? standard.content : item.content,
|
|
config,
|
|
}
|
|
: item,
|
|
);
|
|
next.trustPresentation = projectTrustPresentation(
|
|
next.trustItems,
|
|
templateContent?.trustContentTypes ?? [],
|
|
);
|
|
} else {
|
|
next.offerings = next.offerings.map((item) =>
|
|
item.id === recordDraft.id
|
|
? {
|
|
...item,
|
|
name: typeof standard.name === "string" ? standard.name : item.name,
|
|
summary:
|
|
typeof standard.summary === "string"
|
|
? standard.summary
|
|
: typeof standard.description === "string"
|
|
? standard.description
|
|
: item.summary,
|
|
priceText: typeof standard.priceText === "string" ? standard.priceText : null,
|
|
coverUrl: typeof standard.coverUrl === "string" ? standard.coverUrl : null,
|
|
presentation: {
|
|
...item.presentation,
|
|
...config,
|
|
...(config.presentation && typeof config.presentation === "object"
|
|
? (config.presentation as Record<string, unknown>)
|
|
: {}),
|
|
...(typeof config.categoryLabel === "string"
|
|
? { categoryLabel: config.categoryLabel }
|
|
: {}),
|
|
...(typeof config.area === "string" ? { area: config.area } : {}),
|
|
...(typeof config.series === "string" ? { series: config.series } : {}),
|
|
...(typeof config.spaceType === "string" ? { spaceType: config.spaceType } : {}),
|
|
heroImage:
|
|
typeof config.detailHero === "string"
|
|
? config.detailHero
|
|
: typeof standard.coverUrl === "string"
|
|
? standard.coverUrl
|
|
: null,
|
|
details: {
|
|
...item.presentation.details,
|
|
...(config.details &&
|
|
typeof config.details === "object" &&
|
|
"background" in config.details
|
|
? (config.details as RuntimeWorkspace["offerings"][number]["presentation"]["details"])
|
|
: {}),
|
|
},
|
|
},
|
|
}
|
|
: item,
|
|
);
|
|
}
|
|
return next;
|
|
}, [
|
|
previewWorkspace,
|
|
previewBrand,
|
|
previewConfig,
|
|
recordDraft,
|
|
selectedRecord,
|
|
recordSection,
|
|
templateContent,
|
|
inquiryDraft,
|
|
inquiryRuntime,
|
|
]);
|
|
const selectedDetailOfferingId =
|
|
detailOfferingId && draftWorkspace?.offerings.some((item) => item.id === detailOfferingId)
|
|
? detailOfferingId
|
|
: draftWorkspace?.offerings[0]?.id;
|
|
|
|
return (
|
|
<section className="presentation-editor" aria-labelledby="presentation-editor-title">
|
|
<div className="presentation-editor__header">
|
|
<div>
|
|
<Typography.Title id="presentation-editor-title" level={3}>
|
|
小程序页面配置
|
|
</Typography.Title>
|
|
<Typography.Paragraph type="secondary">
|
|
按页面查找内容。这里修改的是草稿,发布后访客才会看到更新。
|
|
</Typography.Paragraph>
|
|
</div>
|
|
{templateContent ? <Tag color="blue">{templateContent.displayName}</Tag> : null}
|
|
</div>
|
|
{loading ? <Alert type="info" showIcon message="正在读取当前模板的编辑项目" /> : null}
|
|
{error ? (
|
|
<Alert type="warning" showIcon message="模板编辑项目读取失败,请刷新后重试。" />
|
|
) : null}
|
|
{!loading && !error && !templateContent ? (
|
|
<Alert type="warning" showIcon message="当前模板暂未提供可编辑的页面内容。" />
|
|
) : null}
|
|
{templateContent ? (
|
|
<>
|
|
<Space className="presentation-editor__navigation" wrap>
|
|
{breakpoints.xs ? (
|
|
<Select
|
|
aria-label="选择小程序页面"
|
|
value={selectedPage}
|
|
options={pageOptions}
|
|
onChange={(value) =>
|
|
guardDraft(() => {
|
|
setBrandDirty(false);
|
|
form.setFieldsValue(initialValues);
|
|
setPreviewConfig(config ?? {});
|
|
setPreviewBrandValues({});
|
|
setRecordDraft(undefined);
|
|
setInquiryDraft(undefined);
|
|
setActivePage(value);
|
|
})
|
|
}
|
|
style={{ minWidth: 180 }}
|
|
/>
|
|
) : (
|
|
<Segmented
|
|
value={selectedPage}
|
|
options={pageOptions}
|
|
onChange={(value) =>
|
|
guardDraft(() => {
|
|
setBrandDirty(false);
|
|
form.setFieldsValue(initialValues);
|
|
setPreviewConfig(config ?? {});
|
|
setPreviewBrandValues({});
|
|
setRecordDraft(undefined);
|
|
setInquiryDraft(undefined);
|
|
setActivePage(String(value));
|
|
})
|
|
}
|
|
/>
|
|
)}
|
|
</Space>
|
|
<div className="presentation-editor__workspace">
|
|
<div className="presentation-editor__preview">
|
|
<Typography.Title level={5}>页面预览</Typography.Title>
|
|
{selectedPage === "detail" && draftWorkspace?.offerings.length ? (
|
|
<Select
|
|
aria-label="选择详情页业务内容"
|
|
style={{ width: "100%", marginBottom: 12 }}
|
|
value={selectedDetailOfferingId}
|
|
options={draftWorkspace.offerings.map((item) => ({
|
|
value: item.id,
|
|
label: item.name,
|
|
}))}
|
|
onChange={(value) => {
|
|
setDetailOfferingId(value);
|
|
setSelection(undefined);
|
|
}}
|
|
/>
|
|
) : null}
|
|
<TemplatePreview
|
|
brand={previewBrand}
|
|
{...(draftWorkspace ? { workspace: draftWorkspace } : {})}
|
|
page={selectedPage ?? "home"}
|
|
{...(selectedPage === "detail" && selectedDetailOfferingId
|
|
? { offeringId: selectedDetailOfferingId }
|
|
: {})}
|
|
{...(activeTarget ? { targetKey: activeTarget } : {})}
|
|
{...(selection ? { selection } : {})}
|
|
targets={visibleTargets}
|
|
onTargetSelected={handleTargetSelected}
|
|
/>
|
|
</div>
|
|
<aside className="presentation-editor__outline" aria-label="页面区域">
|
|
<Typography.Title level={5}>页面区域</Typography.Title>
|
|
<Typography.Paragraph type="secondary">选择区域,编辑对应内容</Typography.Paragraph>
|
|
{visibleTargets.length ? (
|
|
<Space className="presentation-editor__regions" wrap style={{ marginTop: 16 }}>
|
|
{visibleTargets.map((target) => (
|
|
<Button
|
|
key={target.key}
|
|
className={
|
|
visibleTargets.some((parent) => parent.children?.includes(target.key))
|
|
? "is-child-region"
|
|
: ""
|
|
}
|
|
aria-pressed={activeTarget === target.key}
|
|
type={activeTarget === target.key ? "primary" : "default"}
|
|
onClick={() => {
|
|
const recordId =
|
|
target.source === "offering"
|
|
? selectedPage === "detail"
|
|
? selectedDetailOfferingId
|
|
: draftWorkspace?.offerings[0]?.id
|
|
: target.source === "trust"
|
|
? previewTrustRecordId(
|
|
draftWorkspace?.trustItems ?? [],
|
|
target.trustType,
|
|
)
|
|
: undefined;
|
|
handleTargetSelected({
|
|
targetKey: target.key,
|
|
...(recordId ? { recordId } : {}),
|
|
});
|
|
}}
|
|
>
|
|
{target.label}
|
|
</Button>
|
|
))}
|
|
</Space>
|
|
) : null}
|
|
{!visibleTargets.length ? (
|
|
<Typography.Text type="secondary">此页为动态预览</Typography.Text>
|
|
) : null}
|
|
</aside>
|
|
<div className="presentation-editor__inspector">
|
|
<Typography.Title level={5}>
|
|
{selectedTarget?.label ?? pageTitle(selectedPage ?? "home")}
|
|
</Typography.Title>
|
|
<Tag color={brandDirty || recordDraft || inquiryDraft ? "orange" : "default"}>
|
|
{save.isPending
|
|
? "正在保存"
|
|
: save.isError
|
|
? "保存失败,请重试"
|
|
: brandDirty || recordDraft || inquiryDraft
|
|
? "有未保存修改"
|
|
: "草稿已同步"}
|
|
</Tag>
|
|
{selectedPage === "detail" && !selectedTarget ? (
|
|
<Alert
|
|
type="info"
|
|
showIcon
|
|
message="详情页由所选业务内容动态生成"
|
|
description="选择业务内容预览详情,记录内容在业务提供项中维护;公共标题和按钮文案可从左侧区域列表配置,对全部详情生效。"
|
|
action={
|
|
<Button
|
|
onClick={() => {
|
|
void navigate("/admin/services");
|
|
}}
|
|
>
|
|
管理业务内容
|
|
</Button>
|
|
}
|
|
/>
|
|
) : activeTarget?.startsWith("inquiry-field:") && inquiryRuntime ? (
|
|
<InquiryPreviewEditor
|
|
runtime={
|
|
draftWorkspace?.inquiryForms.find((item) => item.key === inquiryRuntime.key) ??
|
|
inquiryRuntime
|
|
}
|
|
fieldKey={activeTarget.slice("inquiry-field:".length)}
|
|
onDraft={setInquiryDraft}
|
|
onSaved={() => setInquiryDraft(undefined)}
|
|
/>
|
|
) : selectedTarget?.composite ? (
|
|
<Alert
|
|
type="info"
|
|
showIcon
|
|
message="这是一个组合区域"
|
|
description={
|
|
<Space wrap>
|
|
{(selectedTarget.children ?? []).map((childKey) => {
|
|
const child = visibleTargets.find((item) => item.key === childKey);
|
|
if (!child) return null;
|
|
return (
|
|
<Button
|
|
key={child.key}
|
|
onClick={() => {
|
|
const recordId =
|
|
child.source === "offering"
|
|
? selectedPage === "detail"
|
|
? selectedDetailOfferingId
|
|
: draftWorkspace?.offerings[0]?.id
|
|
: child.source === "trust"
|
|
? previewTrustRecordId(
|
|
draftWorkspace?.trustItems ?? [],
|
|
child.trustType,
|
|
)
|
|
: undefined;
|
|
handleTargetSelected({
|
|
targetKey: child.key,
|
|
...(recordId ? { recordId } : {}),
|
|
});
|
|
}}
|
|
>
|
|
编辑{child.label}
|
|
</Button>
|
|
);
|
|
})}
|
|
</Space>
|
|
}
|
|
/>
|
|
) : selectedRecordSource && selection?.recordId && selectedSection ? (
|
|
selectedRecord ? (
|
|
<RecordContentEditor
|
|
key={`${selectedRecordSource}:${selectedRecord.id}:${selectedSection.key}`}
|
|
source={selectedRecordSource}
|
|
record={selectedRecord}
|
|
section={recordSection!}
|
|
context="visual"
|
|
onDraft={(values) =>
|
|
setRecordDraft({
|
|
source: selectedRecordSource,
|
|
id: selectedRecord.id,
|
|
values,
|
|
})
|
|
}
|
|
onSaved={() => setRecordDraft(undefined)}
|
|
/>
|
|
) : (
|
|
<Alert
|
|
type={
|
|
(
|
|
selectedRecordSource === "offering"
|
|
? offeringsQuery.isError
|
|
: trustQuery.isError
|
|
)
|
|
? "error"
|
|
: "info"
|
|
}
|
|
message={
|
|
(
|
|
selectedRecordSource === "offering"
|
|
? offeringsQuery.isError
|
|
: trustQuery.isError
|
|
)
|
|
? "内容读取失败"
|
|
: (
|
|
selectedRecordSource === "offering"
|
|
? offeringsQuery.isSuccess
|
|
: trustQuery.isSuccess
|
|
)
|
|
? "对应内容已不存在,请重新选择页面中的区域"
|
|
: "正在读取对应内容"
|
|
}
|
|
{...((
|
|
selectedRecordSource === "offering"
|
|
? offeringsQuery.isError
|
|
: trustQuery.isError
|
|
)
|
|
? {
|
|
action: (
|
|
<Button
|
|
size="small"
|
|
onClick={() => {
|
|
if (selectedRecordSource === "offering")
|
|
void offeringsQuery.refetch();
|
|
else void trustQuery.refetch();
|
|
}}
|
|
>
|
|
重试
|
|
</Button>
|
|
),
|
|
}
|
|
: {})}
|
|
/>
|
|
)
|
|
) : 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>
|
|
<Typography.Paragraph type="secondary">
|
|
点击预览中的内容,或使用区域列表,查看并修改对应字段。
|
|
</Typography.Paragraph>
|
|
</div>
|
|
) : (
|
|
<Form
|
|
form={form}
|
|
layout="vertical"
|
|
initialValues={initialValues}
|
|
onValuesChange={handleValuesChange}
|
|
onFinish={() => save.mutate(form.getFieldsValue(true) as PresentationValues)}
|
|
style={{ minHeight: 0, display: "flex", flexDirection: "column" }}
|
|
>
|
|
<div style={{ flex: 1 }}>
|
|
{editableSections.map((section, index) => (
|
|
<section key={section.key} aria-labelledby={`section-${section.key}`}>
|
|
{selectedTarget && index > 0 ? <Divider /> : null}
|
|
<Typography.Title id={`section-${section.key}`} level={5}>
|
|
{selectedTarget
|
|
? fieldTargetTitle(section, selectedTarget.label)
|
|
: section.title}
|
|
</Typography.Title>
|
|
{!selectedTarget ? (
|
|
<Typography.Paragraph type="secondary">
|
|
{section.description}
|
|
</Typography.Paragraph>
|
|
) : null}
|
|
<Row gutter={16}>
|
|
{section.fields.map((field) => (
|
|
<Col key={field.key} xs={24} md={24}>
|
|
{field.input === "list" ? (
|
|
<TemplateListField
|
|
field={field}
|
|
name={presentationFieldName(section.key, field.key)}
|
|
/>
|
|
) : (
|
|
<Form.Item
|
|
name={presentationFieldName(section.key, field.key)}
|
|
label={field.label}
|
|
{...(field.help ? { help: field.help } : {})}
|
|
{...(field.required
|
|
? {
|
|
rules: [
|
|
{ required: true, message: `请填写${field.label}` },
|
|
],
|
|
}
|
|
: {})}
|
|
>
|
|
{renderField(field)}
|
|
</Form.Item>
|
|
)}
|
|
</Col>
|
|
))}
|
|
</Row>
|
|
</section>
|
|
))}
|
|
{relevantPageLinks.length ? (
|
|
<Alert
|
|
type="info"
|
|
showIcon
|
|
style={{ marginBottom: 24 }}
|
|
message="这个页面还有关联内容"
|
|
description={
|
|
<Space wrap>
|
|
{relevantPageLinks.map((item) => (
|
|
<Button
|
|
key={item.target}
|
|
type="link"
|
|
onClick={() => {
|
|
if (item.target.startsWith("trust:")) {
|
|
void navigate(
|
|
`/admin/trust?type=${encodeURIComponent(item.target.slice(6))}`,
|
|
);
|
|
return;
|
|
}
|
|
void navigate(
|
|
item.target === "offering"
|
|
? "/admin/services"
|
|
: "/admin/workspace",
|
|
);
|
|
}}
|
|
style={{ padding: 0 }}
|
|
>
|
|
{item.label}
|
|
</Button>
|
|
))}
|
|
</Space>
|
|
}
|
|
/>
|
|
) : null}
|
|
{!editableSections.some((section) => section.fields.length) &&
|
|
!relevantPageLinks.length ? (
|
|
<Typography.Paragraph type="secondary">
|
|
这个页面没有单独的品牌文案配置。
|
|
</Typography.Paragraph>
|
|
) : null}
|
|
{selectedTarget &&
|
|
!editableSections.some((section) => section.fields.length) &&
|
|
relevantPageLinks.length ? (
|
|
<Typography.Paragraph type="secondary">
|
|
此区域内容由对应的业务管理页面维护。
|
|
</Typography.Paragraph>
|
|
) : null}
|
|
</div>
|
|
<Space direction="vertical" style={{ width: "100%", marginTop: 24 }}>
|
|
<MutationError error={save.error} />
|
|
<Button type="primary" htmlType="submit" loading={save.isPending}>
|
|
保存页面内容
|
|
</Button>
|
|
</Space>
|
|
</Form>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</>
|
|
) : null}
|
|
</section>
|
|
);
|
|
}
|
|
|
|
function pageTitle(page: string): string {
|
|
return (
|
|
(
|
|
{
|
|
home: "首页",
|
|
offerings: "业务展示",
|
|
service: "服务说明",
|
|
detail: "详情页",
|
|
inquiry: "咨询页",
|
|
success: "提交成功页",
|
|
records: "记录页",
|
|
profile: "主理人页",
|
|
} as Record<string, string>
|
|
)[page] ?? page
|
|
);
|
|
}
|
|
|
|
function fieldTargetTitle(section: { title: string }, targetLabel: string): string {
|
|
return section.title === targetLabel ? targetLabel : `${targetLabel} · ${section.title}`;
|
|
}
|