diff --git a/apps/admin/.env.example b/apps/admin/.env.example index 16f18e9..ffe8b28 100644 --- a/apps/admin/.env.example +++ b/apps/admin/.env.example @@ -2,3 +2,4 @@ VITE_APP_BASE_PATH=/admin/ VITE_API_BASE_URL=/api VITE_DEV_API_TARGET=http://localhost:3001 ADMIN_PORT=3000 +VITE_TEMPLATE_PREVIEW_URLS={"acheng-campus-food":"http://localhost:3002","linjing-editorial":"http://localhost:3003"} diff --git a/apps/admin/src/features/brand/BrandPage.tsx b/apps/admin/src/features/brand/BrandPage.tsx index 488d894..c366c0f 100644 --- a/apps/admin/src/features/brand/BrandPage.tsx +++ b/apps/admin/src/features/brand/BrandPage.tsx @@ -38,7 +38,7 @@ export function BrandPage() { extra={{template.data?.name ?? "当前模板"}} /> - +
- - - - 这些资料会用于小程序首页和联系区域 - - {brand.data.name || "品牌名称"} - {brand.data.tagline || "一句话定位"} - - {brand.data.description || "品牌简介将在这里展示。"} - - 合作方式 - - {brand.data.deliveryNote || "补充你的响应时间和合作预期。"} - - - {brand.data.contactPhone && 电话:{brand.data.contactPhone}} - {brand.data.contactWechat && 微信:{brand.data.contactWechat}} - {brand.data.hours && 时间:{brand.data.hours}} - - + {template.data && ( {template.data.description} diff --git a/apps/admin/src/features/brand/PresentationEditor.tsx b/apps/admin/src/features/brand/PresentationEditor.tsx index 30d0bdf..fd535fe 100644 --- a/apps/admin/src/features/brand/PresentationEditor.tsx +++ b/apps/admin/src/features/brand/PresentationEditor.tsx @@ -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 | 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(); + const [activeTarget, setActiveTarget] = useState(); 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 ( )} - save.mutate(values)} - style={{ minHeight: 540, display: "flex", flexDirection: "column" }} - > -
- {visibleSections.map((section, index) => ( -
- {index > 0 ? : null} - - {section.title} - - - {section.description} - - - {section.fields.map((field) => ( - - {field.input === "list" ? ( - - ) : ( - + + 页面预览 + + {visibleTargets.length ? ( + + {visibleTargets.map((target) => ( + + ))} + + ) : null} + + + + {selectedTarget?.label ?? pageTitle(selectedPage ?? "home")} + + save.mutate(values)} + style={{ minHeight: 540, display: "flex", flexDirection: "column" }} + > +
+ {editableSections.map((section, index) => ( +
+ {selectedTarget && index > 0 ? : null} + + {selectedTarget + ? fieldTargetTitle(section, selectedTarget.label) + : section.title} + + {!selectedTarget ? ( + + {section.description} + + ) : null} + + {section.fields.map((field) => ( + - {renderField(field)} - - )} - - ))} - -
- ))} - {pageLinks.length ? ( - - {pageLinks.map((item) => ( - - ))} - - } - /> - ) : null} - {!visibleSections.length && !pageLinks.length ? ( - - 这个页面没有单独的品牌文案配置。 - - ) : null} -
- - - - - + {field.input === "list" ? ( + + ) : ( + + {renderField(field)} + + )} + + ))} +
+
+ ))} + {pageLinks.length ? ( + + {pageLinks.map((item) => ( + + ))} + + } + /> + ) : null} + {!editableSections.length && !pageLinks.length ? ( + + 这个页面没有单独的品牌文案配置。 + + ) : null} +
+ + + + + + +
) : null} @@ -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}`; +} diff --git a/apps/admin/src/features/brand/TemplatePreview.css b/apps/admin/src/features/brand/TemplatePreview.css new file mode 100644 index 0000000..b1ed3ea --- /dev/null +++ b/apps/admin/src/features/brand/TemplatePreview.css @@ -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; +} diff --git a/apps/admin/src/features/brand/TemplatePreview.tsx b/apps/admin/src/features/brand/TemplatePreview.tsx new file mode 100644 index 0000000..ba12ffe --- /dev/null +++ b/apps/admin/src/features/brand/TemplatePreview.tsx @@ -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(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) => { + 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 ( + + ); + + return ( +
+ {!ready && !error ? ( +
+ +
+ ) : null} + {error ? ( + + ) : null} +