feat: 建立组件化模板预览基线并规划可视化编辑
This commit is contained in:
1 parent
c5af790554
commit
0b14b5a814
46 files changed
+1880
-427
No files matched your search
@@ -19,14 +19,21 @@ import {
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import type { AdminTemplateContent, TemplateAdminField } from "@personal-brand/api-client";
|
||||
import type {
|
||||
AdminTemplateContent,
|
||||
RuntimeWorkspace,
|
||||
TemplateAdminField,
|
||||
} from "@personal-brand/api-client";
|
||||
import type { BrandProfile } from "@personal-brand/api-client";
|
||||
import { projectBrandAliases } 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 { TemplatePreview } from "./TemplatePreview.js";
|
||||
import {
|
||||
applyBrandPreviewValues,
|
||||
applyStandardBrandValues,
|
||||
applyPresentationValues,
|
||||
filterSectionsForPreviewTarget,
|
||||
presentationFieldName,
|
||||
@@ -36,6 +43,7 @@ import {
|
||||
|
||||
interface PresentationEditorProps {
|
||||
templateContent: AdminTemplateContent | null | undefined;
|
||||
previewWorkspace?: RuntimeWorkspace | undefined;
|
||||
config: Record<string, unknown> | null | undefined;
|
||||
brand: BrandProfile;
|
||||
loading?: boolean;
|
||||
@@ -83,6 +91,7 @@ function renderField(field: TemplateAdminField) {
|
||||
|
||||
export function PresentationEditor({
|
||||
templateContent,
|
||||
previewWorkspace,
|
||||
config,
|
||||
brand,
|
||||
loading,
|
||||
@@ -94,6 +103,8 @@ export function PresentationEditor({
|
||||
const breakpoints = Grid.useBreakpoint();
|
||||
const [activePage, setActivePage] = useState<string>();
|
||||
const [activeTarget, setActiveTarget] = useState<string>();
|
||||
const [previewConfig, setPreviewConfig] = useState(config ?? {});
|
||||
const [previewBrandValues, setPreviewBrandValues] = useState<Record<string, unknown>>({});
|
||||
const sections = useMemo(() => {
|
||||
const declaredTemplateFields = (templateContent?.templateFields ?? []).map((field) => ({
|
||||
...field,
|
||||
@@ -113,22 +124,20 @@ export function PresentationEditor({
|
||||
]
|
||||
: []),
|
||||
...(templateContent?.admin.sections ?? []),
|
||||
]
|
||||
.map((section) => ({
|
||||
...section,
|
||||
fields: section.fields.filter(
|
||||
(field) =>
|
||||
field.binding.startsWith("brand.config.") ||
|
||||
[
|
||||
"brand.avatarUrl",
|
||||
"brand.description",
|
||||
"brand.contactPhone",
|
||||
"brand.contactWechat",
|
||||
"brand.address",
|
||||
].includes(field.binding),
|
||||
),
|
||||
}))
|
||||
.filter((section) => section.fields.length > 0);
|
||||
].map((section) => ({
|
||||
...section,
|
||||
fields: section.fields.filter(
|
||||
(field) =>
|
||||
field.binding.startsWith("brand.config.") ||
|
||||
[
|
||||
"brand.avatarUrl",
|
||||
"brand.description",
|
||||
"brand.contactPhone",
|
||||
"brand.contactWechat",
|
||||
"brand.address",
|
||||
].includes(field.binding),
|
||||
),
|
||||
}));
|
||||
}, [templateContent]);
|
||||
const pageOptions = useMemo(() => {
|
||||
const pages = new Map<string, string>();
|
||||
@@ -144,11 +153,23 @@ export function PresentationEditor({
|
||||
? activePage
|
||||
: pageOptions[0]?.value;
|
||||
const visibleSections = sections.filter((section) => section.page === selectedPage);
|
||||
const visibleTargets = visibleSections.flatMap((section) =>
|
||||
(section.previewTargets ?? []).map((target) => ({ ...target, page: section.page })),
|
||||
);
|
||||
const visibleTargets = (templateContent?.admin.sections ?? [])
|
||||
.filter((section) => section.page === selectedPage)
|
||||
.flatMap((section) =>
|
||||
(section.previewTargets ?? []).map((target) => ({
|
||||
...target,
|
||||
page: section.page,
|
||||
source: section.target,
|
||||
})),
|
||||
);
|
||||
const selectedTarget = visibleTargets.find((target) => target.key === activeTarget);
|
||||
const editableSections = filterSectionsForPreviewTarget(visibleSections, selectedTarget?.key);
|
||||
const selectedSection = (templateContent?.admin.sections ?? []).find((section) =>
|
||||
section.previewTargets?.some((target) => target.key === selectedTarget?.key),
|
||||
);
|
||||
const editableSections = filterSectionsForPreviewTarget(
|
||||
visibleSections.filter((section) => section.target === "brand"),
|
||||
selectedTarget?.key,
|
||||
);
|
||||
const handleTargetSelected = useCallback((key: string) => setActiveTarget(key), []);
|
||||
const relatedTargets = [
|
||||
...(templateContent?.admin.sections ?? [])
|
||||
@@ -180,6 +201,12 @@ export function PresentationEditor({
|
||||
},
|
||||
],
|
||||
);
|
||||
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) {
|
||||
@@ -188,6 +215,7 @@ export function PresentationEditor({
|
||||
config,
|
||||
field.binding,
|
||||
field,
|
||||
brand as unknown as Record<string, unknown>,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -197,7 +225,10 @@ export function PresentationEditor({
|
||||
const save = useMutation({
|
||||
mutationFn: (values: PresentationValues) => {
|
||||
const nextConfig = applyPresentationValues(config, sections, values);
|
||||
return createApiClient().updateAdminBrand({ config: nextConfig });
|
||||
return createApiClient().updateAdminBrand({
|
||||
config: nextConfig,
|
||||
...applyStandardBrandValues(brand as unknown as Record<string, unknown>, sections, values),
|
||||
});
|
||||
},
|
||||
onSuccess: () => {
|
||||
message.success("页面内容已保存为草稿,发布后小程序才会更新");
|
||||
@@ -218,6 +249,27 @@ export function PresentationEditor({
|
||||
setActivePage(pageOptions[0]?.value);
|
||||
}, [activePage, pageOptions]);
|
||||
useEffect(() => setActiveTarget(undefined), [selectedPage]);
|
||||
useEffect(() => {
|
||||
setPreviewConfig(config ?? {});
|
||||
setPreviewBrandValues({});
|
||||
}, [config]);
|
||||
const previewBrand = useMemo(
|
||||
() => ({ ...brand, ...previewBrandValues, config: previewConfig }),
|
||||
[brand, previewBrandValues, previewConfig],
|
||||
);
|
||||
const handleValuesChange = useCallback(
|
||||
(_changed: PresentationValues, allValues: PresentationValues) => {
|
||||
const next = applyBrandPreviewValues(
|
||||
brand as unknown as Record<string, unknown>,
|
||||
config,
|
||||
sections,
|
||||
allValues,
|
||||
);
|
||||
setPreviewConfig((next.config as Record<string, unknown>) ?? {});
|
||||
setPreviewBrandValues(next);
|
||||
},
|
||||
[brand, config, sections],
|
||||
);
|
||||
|
||||
return (
|
||||
<Card
|
||||
@@ -259,9 +311,25 @@ export function PresentationEditor({
|
||||
<Col xs={24} xl={12}>
|
||||
<Typography.Title level={5}>页面预览</Typography.Title>
|
||||
<TemplatePreview
|
||||
brand={brand}
|
||||
brand={previewBrand}
|
||||
{...(previewWorkspace
|
||||
? {
|
||||
workspace: {
|
||||
...previewWorkspace,
|
||||
brand: {
|
||||
...projectBrandAliases(
|
||||
{
|
||||
...previewWorkspace.brand,
|
||||
...(previewBrand as unknown as Record<string, unknown>),
|
||||
config: previewConfig,
|
||||
},
|
||||
previewConfig,
|
||||
),
|
||||
},
|
||||
},
|
||||
}
|
||||
: {})}
|
||||
page={selectedPage ?? "home"}
|
||||
route={`/pages/${encodeURIComponent(selectedPage ?? "home")}/index`}
|
||||
{...(activeTarget ? { targetKey: activeTarget } : {})}
|
||||
onTargetSelected={handleTargetSelected}
|
||||
/>
|
||||
@@ -287,6 +355,7 @@ export function PresentationEditor({
|
||||
form={form}
|
||||
layout="vertical"
|
||||
initialValues={initialValues}
|
||||
onValuesChange={handleValuesChange}
|
||||
onFinish={(values) => save.mutate(values)}
|
||||
style={{ minHeight: 540, display: "flex", flexDirection: "column" }}
|
||||
>
|
||||
@@ -333,7 +402,7 @@ export function PresentationEditor({
|
||||
</Row>
|
||||
</section>
|
||||
))}
|
||||
{pageLinks.length ? (
|
||||
{relevantPageLinks.length ? (
|
||||
<Alert
|
||||
type="info"
|
||||
showIcon
|
||||
@@ -341,7 +410,7 @@ export function PresentationEditor({
|
||||
message="这个页面还有关联内容"
|
||||
description={
|
||||
<Space wrap>
|
||||
{pageLinks.map((item) => (
|
||||
{relevantPageLinks.map((item) => (
|
||||
<Button
|
||||
key={item.target}
|
||||
type="link"
|
||||
@@ -367,11 +436,19 @@ export function PresentationEditor({
|
||||
}
|
||||
/>
|
||||
) : null}
|
||||
{!editableSections.length && !pageLinks.length ? (
|
||||
{!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} />
|
||||
|
||||
Reference in new issue
Block a user