feat: 增加可视化模板页面编辑器
This commit is contained in:
1 parent
9d2038cc93
commit
c5af790554
24 files changed
+773
-129
No files matched your search
@@ -38,7 +38,7 @@ export function BrandPage() {
|
||||
extra={<Tag color="blue">{template.data?.name ?? "当前模板"}</Tag>}
|
||||
/>
|
||||
<Row gutter={[24, 24]} align="top">
|
||||
<Col xs={24} xl={15}>
|
||||
<Col xs={24} xl={16}>
|
||||
<Form
|
||||
form={form}
|
||||
layout="vertical"
|
||||
@@ -146,30 +146,12 @@ export function BrandPage() {
|
||||
<PresentationEditor
|
||||
templateContent={workspace.data?.templateContent}
|
||||
config={brand.data.config}
|
||||
brand={brand.data}
|
||||
loading={workspace.isLoading}
|
||||
error={workspace.error}
|
||||
/>
|
||||
</Col>
|
||||
<Col xs={24} xl={9}>
|
||||
<Card title="公开表达预览" style={{ position: "sticky", top: 24 }}>
|
||||
<Typography.Text type="secondary">
|
||||
这些资料会用于小程序首页和联系区域
|
||||
</Typography.Text>
|
||||
<Typography.Title level={3}>{brand.data.name || "品牌名称"}</Typography.Title>
|
||||
<Typography.Title level={5}>{brand.data.tagline || "一句话定位"}</Typography.Title>
|
||||
<Typography.Paragraph>
|
||||
{brand.data.description || "品牌简介将在这里展示。"}
|
||||
</Typography.Paragraph>
|
||||
<Typography.Text strong>合作方式</Typography.Text>
|
||||
<Typography.Paragraph type="secondary">
|
||||
{brand.data.deliveryNote || "补充你的响应时间和合作预期。"}
|
||||
</Typography.Paragraph>
|
||||
<Space wrap>
|
||||
{brand.data.contactPhone && <Tag>电话:{brand.data.contactPhone}</Tag>}
|
||||
{brand.data.contactWechat && <Tag>微信:{brand.data.contactWechat}</Tag>}
|
||||
{brand.data.hours && <Tag>时间:{brand.data.hours}</Tag>}
|
||||
</Space>
|
||||
</Card>
|
||||
<Col xs={24} xl={8}>
|
||||
{template.data && (
|
||||
<Card title="模板说明" style={{ marginTop: 24 }}>
|
||||
<Typography.Paragraph>{template.data.description}</Typography.Paragraph>
|
||||
|
||||
@@ -17,15 +17,18 @@ import {
|
||||
message,
|
||||
} from "antd";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import type { AdminTemplateContent, TemplateAdminField } from "@personal-brand/api-client";
|
||||
import type { BrandProfile } 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 {
|
||||
applyPresentationValues,
|
||||
filterSectionsForPreviewTarget,
|
||||
presentationFieldName,
|
||||
readPresentationValue,
|
||||
type PresentationValues,
|
||||
@@ -34,6 +37,7 @@ import {
|
||||
interface PresentationEditorProps {
|
||||
templateContent: AdminTemplateContent | null | undefined;
|
||||
config: Record<string, unknown> | null | undefined;
|
||||
brand: BrandProfile;
|
||||
loading?: boolean;
|
||||
error?: unknown;
|
||||
}
|
||||
@@ -80,6 +84,7 @@ function renderField(field: TemplateAdminField) {
|
||||
export function PresentationEditor({
|
||||
templateContent,
|
||||
config,
|
||||
brand,
|
||||
loading,
|
||||
error,
|
||||
}: PresentationEditorProps) {
|
||||
@@ -88,6 +93,7 @@ export function PresentationEditor({
|
||||
const navigate = useNavigate();
|
||||
const breakpoints = Grid.useBreakpoint();
|
||||
const [activePage, setActivePage] = useState<string>();
|
||||
const [activeTarget, setActiveTarget] = useState<string>();
|
||||
const sections = useMemo(() => {
|
||||
const declaredTemplateFields = (templateContent?.templateFields ?? []).map((field) => ({
|
||||
...field,
|
||||
@@ -138,6 +144,12 @@ 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 selectedTarget = visibleTargets.find((target) => target.key === activeTarget);
|
||||
const editableSections = filterSectionsForPreviewTarget(visibleSections, selectedTarget?.key);
|
||||
const handleTargetSelected = useCallback((key: string) => setActiveTarget(key), []);
|
||||
const relatedTargets = [
|
||||
...(templateContent?.admin.sections ?? [])
|
||||
.filter((section) => section.page === selectedPage && section.target !== "brand")
|
||||
@@ -189,6 +201,7 @@ export function PresentationEditor({
|
||||
},
|
||||
onSuccess: () => {
|
||||
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: ["public-runtime-workspace"] });
|
||||
@@ -204,6 +217,7 @@ export function PresentationEditor({
|
||||
if (!pageOptions.some((item) => item.value === activePage))
|
||||
setActivePage(pageOptions[0]?.value);
|
||||
}, [activePage, pageOptions]);
|
||||
useEffect(() => setActiveTarget(undefined), [selectedPage]);
|
||||
|
||||
return (
|
||||
<Card
|
||||
@@ -241,97 +255,133 @@ export function PresentationEditor({
|
||||
/>
|
||||
)}
|
||||
</Space>
|
||||
<Form
|
||||
form={form}
|
||||
layout="vertical"
|
||||
initialValues={initialValues}
|
||||
onFinish={(values) => save.mutate(values)}
|
||||
style={{ minHeight: 540, display: "flex", flexDirection: "column" }}
|
||||
>
|
||||
<div style={{ flex: 1 }}>
|
||||
{visibleSections.map((section, index) => (
|
||||
<section key={section.key} aria-labelledby={`section-${section.key}`}>
|
||||
{index > 0 ? <Divider /> : null}
|
||||
<Typography.Title id={`section-${section.key}`} level={5}>
|
||||
{section.title}
|
||||
</Typography.Title>
|
||||
<Typography.Paragraph type="secondary">
|
||||
{section.description}
|
||||
</Typography.Paragraph>
|
||||
<Row gutter={16}>
|
||||
{section.fields.map((field) => (
|
||||
<Col
|
||||
key={field.key}
|
||||
xs={24}
|
||||
md={field.input === "textarea" || field.input === "list" ? 24 : 12}
|
||||
>
|
||||
{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}` }] }
|
||||
: {})}
|
||||
<Row gutter={[24, 24]} align="top">
|
||||
<Col xs={24} xl={12}>
|
||||
<Typography.Title level={5}>页面预览</Typography.Title>
|
||||
<TemplatePreview
|
||||
brand={brand}
|
||||
page={selectedPage ?? "home"}
|
||||
route={`/pages/${encodeURIComponent(selectedPage ?? "home")}/index`}
|
||||
{...(activeTarget ? { targetKey: activeTarget } : {})}
|
||||
onTargetSelected={handleTargetSelected}
|
||||
/>
|
||||
{visibleTargets.length ? (
|
||||
<Space wrap style={{ marginTop: 16 }}>
|
||||
{visibleTargets.map((target) => (
|
||||
<Button
|
||||
key={target.key}
|
||||
type={activeTarget === target.key ? "primary" : "default"}
|
||||
onClick={() => setActiveTarget(target.key)}
|
||||
>
|
||||
{target.label}
|
||||
</Button>
|
||||
))}
|
||||
</Space>
|
||||
) : null}
|
||||
</Col>
|
||||
<Col xs={24} xl={12}>
|
||||
<Typography.Title level={5}>
|
||||
{selectedTarget?.label ?? pageTitle(selectedPage ?? "home")}
|
||||
</Typography.Title>
|
||||
<Form
|
||||
form={form}
|
||||
layout="vertical"
|
||||
initialValues={initialValues}
|
||||
onFinish={(values) => save.mutate(values)}
|
||||
style={{ minHeight: 540, 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={field.input === "textarea" || field.input === "list" ? 24 : 12}
|
||||
>
|
||||
{renderField(field)}
|
||||
</Form.Item>
|
||||
)}
|
||||
</Col>
|
||||
))}
|
||||
</Row>
|
||||
</section>
|
||||
))}
|
||||
{pageLinks.length ? (
|
||||
<Alert
|
||||
type="info"
|
||||
showIcon
|
||||
style={{ marginBottom: 24 }}
|
||||
message="这个页面还有关联内容"
|
||||
description={
|
||||
<Space wrap>
|
||||
{pageLinks.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}
|
||||
{!visibleSections.length && !pageLinks.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>
|
||||
{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>
|
||||
))}
|
||||
{pageLinks.length ? (
|
||||
<Alert
|
||||
type="info"
|
||||
showIcon
|
||||
style={{ marginBottom: 24 }}
|
||||
message="这个页面还有关联内容"
|
||||
description={
|
||||
<Space wrap>
|
||||
{pageLinks.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.length && !pageLinks.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>
|
||||
</Col>
|
||||
</Row>
|
||||
</>
|
||||
) : null}
|
||||
</Card>
|
||||
@@ -354,3 +404,7 @@ function pageTitle(page: string): string {
|
||||
)[page] ?? page
|
||||
);
|
||||
}
|
||||
|
||||
function fieldTargetTitle(section: { title: string }, targetLabel: string): string {
|
||||
return section.title === targetLabel ? targetLabel : `${targetLabel} · ${section.title}`;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
.template-preview {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
height: min(72vh, 760px);
|
||||
min-height: 560px;
|
||||
overflow: hidden;
|
||||
border: 1px solid #e8e8e8;
|
||||
background: #f3f4f6;
|
||||
}
|
||||
|
||||
.template-preview iframe {
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
border: 0;
|
||||
background: #fff;
|
||||
transition: opacity 120ms ease;
|
||||
}
|
||||
|
||||
.template-preview__loading {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
z-index: 1;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
background: #fff;
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { Alert, Spin } from "antd";
|
||||
import type {
|
||||
BrandProfile,
|
||||
EditorPreviewFrameMessage,
|
||||
EditorPreviewHostMessage,
|
||||
} from "@personal-brand/api-client";
|
||||
import { templatePreviewUrl } from "../../lib/template-preview.js";
|
||||
import "./TemplatePreview.css";
|
||||
|
||||
export function TemplatePreview({
|
||||
brand,
|
||||
page,
|
||||
targetKey,
|
||||
route,
|
||||
onTargetSelected,
|
||||
}: {
|
||||
brand: BrandProfile;
|
||||
page: string;
|
||||
targetKey?: string;
|
||||
route: string;
|
||||
onTargetSelected: (key: string) => void;
|
||||
}) {
|
||||
const frame = useRef<HTMLIFrameElement>(null);
|
||||
const [ready, setReady] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
const [reloadKey, setReloadKey] = useState(0);
|
||||
const config = brand.config ?? {};
|
||||
const templateKey =
|
||||
typeof config.visualTemplateKey === "string" ? config.visualTemplateKey : brand.templateKey;
|
||||
const url = useMemo(
|
||||
() =>
|
||||
templatePreviewUrl(
|
||||
templateKey,
|
||||
brand.slug,
|
||||
page,
|
||||
import.meta.env.VITE_TEMPLATE_PREVIEW_URLS,
|
||||
window.location.origin,
|
||||
route,
|
||||
),
|
||||
[brand.slug, page, reloadKey, route, templateKey],
|
||||
);
|
||||
const origin = useMemo(() => (url ? new URL(url).origin : ""), [url]);
|
||||
const brandDraft = useMemo(
|
||||
() => ({
|
||||
name: brand.name,
|
||||
tagline: brand.tagline,
|
||||
description: brand.description,
|
||||
avatarUrl: brand.avatarUrl,
|
||||
coverUrl: brand.coverUrl,
|
||||
hours: brand.hours,
|
||||
address: brand.address,
|
||||
deliveryNote: brand.deliveryNote,
|
||||
contactPhone: brand.contactPhone,
|
||||
contactWechat: brand.contactWechat,
|
||||
config: brand.config,
|
||||
}),
|
||||
[brand],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
setReady(false);
|
||||
setError("");
|
||||
}, [url]);
|
||||
|
||||
useEffect(() => {
|
||||
const onMessage = (event: MessageEvent<EditorPreviewFrameMessage>) => {
|
||||
if (event.source !== frame.current?.contentWindow || event.origin !== origin) return;
|
||||
const message = event.data;
|
||||
if (message?.protocol !== "personal-brand-editor-preview" || message.version !== 1) return;
|
||||
if (message.type === "ready") setReady(true);
|
||||
if (message.type === "targetSelected" && message.page === page)
|
||||
onTargetSelected(message.targetKey);
|
||||
};
|
||||
window.addEventListener("message", onMessage);
|
||||
return () => window.removeEventListener("message", onMessage);
|
||||
}, [onTargetSelected, origin, page]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!ready || !frame.current?.contentWindow || !origin) return;
|
||||
const send = (message: EditorPreviewHostMessage) =>
|
||||
frame.current?.contentWindow?.postMessage(message, origin);
|
||||
send({
|
||||
protocol: "personal-brand-editor-preview",
|
||||
version: 1,
|
||||
type: "brandDraft",
|
||||
brand: brandDraft,
|
||||
});
|
||||
if (targetKey)
|
||||
send({
|
||||
protocol: "personal-brand-editor-preview",
|
||||
version: 1,
|
||||
type: "focusTarget",
|
||||
targetKey,
|
||||
});
|
||||
}, [brandDraft, origin, ready, targetKey]);
|
||||
|
||||
useEffect(() => {
|
||||
const refresh = () => setReloadKey((value) => value + 1);
|
||||
window.addEventListener("template-preview-refresh", refresh);
|
||||
return () => window.removeEventListener("template-preview-refresh", refresh);
|
||||
}, []);
|
||||
|
||||
if (!url)
|
||||
return (
|
||||
<Alert
|
||||
type="warning"
|
||||
showIcon
|
||||
message="尚未配置模板预览服务"
|
||||
description="请在 Admin 环境变量 VITE_TEMPLATE_PREVIEW_URLS 中为模板配置 H5 预览地址。"
|
||||
/>
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="template-preview">
|
||||
{!ready && !error ? (
|
||||
<div className="template-preview__loading">
|
||||
<Spin tip="正在加载页面预览" />
|
||||
</div>
|
||||
) : null}
|
||||
{error ? (
|
||||
<Alert type="error" showIcon message="页面预览加载失败" description={error} />
|
||||
) : null}
|
||||
<iframe
|
||||
key={url}
|
||||
ref={frame}
|
||||
title="小程序页面预览"
|
||||
src={url}
|
||||
referrerPolicy="origin"
|
||||
onError={() => setError("请检查预览服务地址和网络连接。")}
|
||||
style={{ opacity: ready ? 1 : 0 }}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -12,6 +12,18 @@ export type PresentationSection = {
|
||||
|
||||
export type PresentationValues = Record<string, unknown>;
|
||||
|
||||
export function filterSectionsForPreviewTarget<
|
||||
T extends { fields: Array<{ previewTarget?: string }> },
|
||||
>(sections: T[], targetKey?: string): T[] {
|
||||
if (!targetKey) return sections;
|
||||
return sections
|
||||
.map((section) => ({
|
||||
...section,
|
||||
fields: section.fields.filter((field) => field.previewTarget === targetKey),
|
||||
}))
|
||||
.filter((section) => section.fields.length > 0);
|
||||
}
|
||||
|
||||
function asObject(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === "object" && !Array.isArray(value)
|
||||
? (value as Record<string, unknown>)
|
||||
|
||||
Reference in new issue
Block a user