feat: 完善模板区域预览与详情页编辑边界

This commit is contained in:
que01 committed 2026-10-10 09:51:12 +08:00
1 parent df2b1f6031
commit 849ae5df05
11 files changed
+236 -28

No files matched your search

@@ -104,6 +104,20 @@
box-shadow: 0 0 0 3px rgba(22, 119, 255, 0.16);
}
.component-template-preview__composite-select {
position: absolute;
z-index: 7;
width: auto !important;
padding: 2px 8px !important;
border: 1px solid #1677ff !important;
border-radius: 4px !important;
background: #fff !important;
color: #0958d9 !important;
font-size: 12px !important;
line-height: 20px !important;
cursor: pointer;
}
.component-template-preview__outline span {
position: absolute;
bottom: 100%;
@@ -32,15 +32,19 @@ export function ComponentTemplatePreview({
const [hovered, setHovered] = useState<HTMLElement | null>(null);
const [hoverOutline, setHoverOutline] = useState<Outline | null>(null);
const [selectedOutline, setSelectedOutline] = useState<Outline | null>(null);
const [compositeOutlines, setCompositeOutlines] = useState<Array<Outline & { key: string }>>([]);
const targetFor = useCallback(
(element: Element | null): HTMLElement | null => {
let candidate = element?.closest<HTMLElement>("[data-editor-target]");
let deepest: HTMLElement | null = null;
let composite: HTMLElement | null = null;
while (candidate && root.current?.contains(candidate)) {
if (targets.some((target) => target.key === candidate?.dataset.editorTarget))
return candidate;
const target = targets.find((item) => item.key === candidate?.dataset.editorTarget);
if (target && !deepest) deepest = candidate;
if (target?.composite) composite = candidate;
candidate = candidate.parentElement?.closest<HTMLElement>("[data-editor-target]") ?? null;
}
return null;
return composite ?? deepest;
},
[targets],
);
@@ -81,6 +85,17 @@ export function ComponentTemplatePreview({
frame = requestAnimationFrame(() => {
setSelectedOutline(measure(selectedElement()));
setHoverOutline(measure(hovered));
setCompositeOutlines(
targets
.filter((target) => target.composite)
.flatMap((target) => {
const element = viewport.querySelector<HTMLElement>(
`[data-editor-target="${CSS.escape(target.key)}"]`,
);
const outline = measure(element);
return outline ? [{ ...outline, key: target.key }] : [];
}),
);
});
};
update();
@@ -136,7 +151,13 @@ export function ComponentTemplatePreview({
onPointerLeave={() => setHovered(null)}
onClickCapture={(event) => {
if (mode !== "edit") return;
const element = targetFor(event.target instanceof Element ? event.target : null);
const canvas = root.current?.querySelector(".component-template-preview__canvas");
if (!event.target || !(event.target instanceof Node) || !canvas?.contains(event.target))
return;
const pointTarget = document.elementFromPoint(event.clientX, event.clientY);
const element = targetFor(
pointTarget && canvas.contains(pointTarget) ? pointTarget : null,
);
event.preventDefault();
event.stopPropagation();
onTargetSelected(
@@ -154,6 +175,20 @@ export function ComponentTemplatePreview({
<div className="component-template-preview__canvas">
<Renderer workspace={workspace} page={page} {...(offeringId ? { offeringId } : {})} />
</div>
{mode === "edit"
? compositeOutlines.map((outline) => (
<button
key={outline.key}
type="button"
className="component-template-preview__composite-select"
style={{ top: outline.top + 4, left: outline.left + 4 }}
aria-label={`选择${targets.find((target) => target.key === outline.key)?.label ?? outline.label}`}
onClick={() => onTargetSelected({ targetKey: outline.key })}
>
选择{targets.find((target) => target.key === outline.key)?.label ?? outline.label}
</button>
))
: null}
{mode === "edit" && hoverOutline ? (
<div
className="component-template-preview__outline is-hovered"
@@ -35,7 +35,11 @@ import { TemplateListField } from "../../components/TemplateListField.js";
import { MutationError } from "../../components/QueryState.js";
import { TemplatePreview } from "./TemplatePreview.js";
import { InquiryPreviewEditor } from "./InquiryPreviewEditor.js";
import { resolvePreviewSelection, type PreviewSelection } from "./preview-selection.js";
import {
resolvePreviewSelection,
type PreviewSelection,
type PreviewTarget,
} from "./preview-selection.js";
import {
applyBrandPreviewValues,
applyStandardBrandValues,
@@ -219,7 +223,12 @@ export function PresentationEditor({
const selectedPage = pageOptions.some((item) => item.value === activePage)
? activePage
: pageOptions[0]?.value;
const visibleSections = sections.filter((section) => section.page === selectedPage);
const visibleSections = sections.filter(
(section) =>
section.page === selectedPage &&
// 详情页内容来自所选业务记录;不再展示一套额外的品牌级详情编辑器。
!(selectedPage === "detail" && section.target === "brand"),
);
const recordSections = useMemo(
() =>
(templateContent?.trustContentTypes ?? [])
@@ -227,10 +236,14 @@ export function PresentationEditor({
.map((item) => trustSection(item, selectedPage ?? "home")),
[templateContent, selectedPage],
);
const visibleTargets = useMemo(
const visibleTargets = useMemo<PreviewTarget[]>(
() =>
[...(templateContent?.admin.sections ?? []), ...recordSections]
.filter((section) => section.page === selectedPage)
.filter(
(section) =>
section.page === selectedPage &&
!(selectedPage === "detail" && section.target === "brand"),
)
.flatMap((section) =>
(section.previewTargets ?? []).map((target) => ({
...target,
@@ -621,7 +634,14 @@ export function PresentationEditor({
<Typography.Title level={5}>
{selectedTarget?.label ?? pageTitle(selectedPage ?? "home")}
</Typography.Title>
{activeTarget?.startsWith("inquiry-field:") && inquiryRuntime ? (
{selectedPage === "detail" ? (
<Alert
type="info"
showIcon
message="详情页由所选业务内容动态生成"
description="选择不同的业务内容即可预览对应详情;内容请在业务提供项中维护。"
/>
) : activeTarget?.startsWith("inquiry-field:") && inquiryRuntime ? (
<InquiryPreviewEditor
runtime={
draftWorkspace?.inquiryForms.find((item) => item.key === inquiryRuntime.key) ??
@@ -630,6 +650,43 @@ export function PresentationEditor({
fieldKey={activeTarget.slice("inquiry-field:".length)}
onDraft={setInquiryDraft}
/>
) : 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"
? draftWorkspace?.trustItems.find(
(item) => child.key === `trust:${item.type}`,
)?.id
: undefined;
handleTargetSelected({
targetKey: child.key,
...(recordId ? { recordId } : {}),
});
}}
>
编辑{child.label}
</Button>
);
})}
</Space>
}
/>
) : selectedRecordSource && selection?.recordId && selectedSection ? (
selectedRecord ? (
<RecordEditor
@@ -5,6 +5,8 @@ export type PreviewTarget = {
label: string;
source: "brand" | "offering" | "trust" | "inquiry";
page: string;
composite?: boolean;
children?: string[];
};
export function resolvePreviewSelection(