122 lines
4.5 KiB
TypeScript
122 lines
4.5 KiB
TypeScript
import type {
|
|
AdminTemplateContent,
|
|
Offering,
|
|
RuntimeWorkspace,
|
|
TemplateAdminField,
|
|
} from "@personal-brand/api-client";
|
|
import type { PreviewTarget } from "../brand/preview-selection.js";
|
|
import { applyPresentationValues } from "../../lib/presentation/presentation-bindings.js";
|
|
|
|
export function previewTargetsByPage(
|
|
sections: AdminTemplateContent["admin"]["sections"],
|
|
): Record<"offerings" | "detail", PreviewTarget[]> {
|
|
const targets = sections.flatMap((section) =>
|
|
(section.previewTargets ?? []).map((target) => ({
|
|
...target,
|
|
page: section.page,
|
|
source: section.fields.some(
|
|
(field) => field.binding.startsWith("offering.") && field.previewTarget === target.key,
|
|
)
|
|
? ("offering" as const)
|
|
: section.target,
|
|
})),
|
|
);
|
|
const pages = [...new Set(sections.map((section) => section.page))];
|
|
const offeringPages = pages.filter(
|
|
(page) =>
|
|
page !== "detail" &&
|
|
targets.some((target) => target.page === page && target.source === "offering"),
|
|
);
|
|
// 业务卡片不一定在“服务页”:独立工坊把案例卡片放在首页。优先使用专门的
|
|
// offerings 页,没有时退回第一个包含业务记录的页面。
|
|
const listPage = offeringPages.find((page) => page === "offerings") ?? offeringPages[0];
|
|
return {
|
|
offerings: listPage ? targets.filter((target) => target.page === listPage) : [],
|
|
detail: targets.filter((target) => target.page === "detail"),
|
|
};
|
|
}
|
|
|
|
export function previewFieldsByTarget(
|
|
sections: AdminTemplateContent["admin"]["sections"],
|
|
): Record<string, TemplateAdminField[]> {
|
|
const result: Record<string, TemplateAdminField[]> = {};
|
|
for (const section of sections) {
|
|
for (const field of section.fields) {
|
|
if (!field.previewTarget) continue;
|
|
(result[field.previewTarget] ??= []).push(field);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export function buildOfferingPreviewWorkspace(options: {
|
|
previewWorkspace: RuntimeWorkspace;
|
|
editingOffering: Offering;
|
|
templateFields: TemplateAdminField[];
|
|
formValues: Record<string, unknown>;
|
|
}): RuntimeWorkspace {
|
|
const { previewWorkspace, editingOffering, templateFields, formValues } = options;
|
|
const config = applyPresentationValues(
|
|
editingOffering.config,
|
|
[{ key: "offering", fields: templateFields }],
|
|
formValues,
|
|
);
|
|
const offeringValues = {
|
|
name: formValues.name,
|
|
summary: formValues.summary,
|
|
description: formValues.description,
|
|
priceText: formValues.priceText,
|
|
coverUrl: formValues.coverUrl,
|
|
};
|
|
return {
|
|
...previewWorkspace,
|
|
offerings: previewWorkspace.offerings.map((item) =>
|
|
item.id === editingOffering.id
|
|
? {
|
|
...item,
|
|
name:
|
|
typeof offeringValues.name === "string" && offeringValues.name.trim()
|
|
? offeringValues.name
|
|
: item.name,
|
|
summary:
|
|
typeof offeringValues.summary === "string" ? offeringValues.summary : item.summary,
|
|
priceText:
|
|
typeof offeringValues.priceText === "string"
|
|
? offeringValues.priceText
|
|
: item.priceText,
|
|
coverUrl:
|
|
typeof offeringValues.coverUrl === "string" && offeringValues.coverUrl.trim()
|
|
? offeringValues.coverUrl
|
|
: item.coverUrl,
|
|
config,
|
|
presentation: {
|
|
...item.presentation,
|
|
...config,
|
|
...(config.presentation && typeof config.presentation === "object"
|
|
? (config.presentation as Record<string, unknown>)
|
|
: {}),
|
|
...(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 offeringValues.coverUrl === "string" && offeringValues.coverUrl.trim()
|
|
? offeringValues.coverUrl
|
|
: (item.coverUrl ?? null),
|
|
details: {
|
|
...item.presentation.details,
|
|
...(config.details && typeof config.details === "object"
|
|
? {
|
|
...item.presentation.details,
|
|
...(config.details as Record<string, unknown>),
|
|
}
|
|
: {}),
|
|
},
|
|
},
|
|
}
|
|
: item,
|
|
),
|
|
};
|
|
}
|