Compare commits

...
Author SHA1 Message Date
que01 45a5e4d666 refactor: 抽取业务预览投影规则并补充单测
CI / Browser smoke (pull_request) Successful in 47s
CI / Static checks (pull_request) Successful in 1m0s
CI / API integration (pull_request) Successful in 1m7s
CI / Production build (pull_request) Successful in 1m24s
2026-10-29 15:09:53 +08:00
que01 b0265f3be3 Merge pull request 'refactor: 抽取共享 getFullFormValues 表单读取工具' (#16) from refactor/form-values-helper into main
CI / Browser smoke (push) Has been skipped
CI / Static checks (push) Successful in 48s
CI / API integration (push) Successful in 1m9s
CI / Production build (push) Successful in 1m26s
Reviewed-on: #16
2026-10-29 06:32:31 +00:00
que01 aa2b6f35b0 refactor: 抽取共享 getFullFormValues 表单读取工具
CI / Browser smoke (pull_request) Successful in 47s
CI / Static checks (pull_request) Successful in 58s
CI / API integration (pull_request) Successful in 1m7s
CI / Production build (pull_request) Successful in 1m24s
2026-10-29 14:24:23 +08:00
11 changed files with 224 additions and 58 deletions

No files matched your search

@@ -1,7 +1,7 @@
{
"schemaVersion": 1,
"scope": "模板完成证据登记,保留命令、日志与产物哈希",
"generatedAt": "2026-10-29T05:47:32.979Z",
"generatedAt": "2026-10-29T07:09:56.748Z",
"templates": {
"acheng-campus-food": {
"real-manifest-interaction": {
@@ -256,5 +256,5 @@
}
}
},
"sourceTreeHash": "3f8f7d8b0063a1d21d4cf757d7c1b5ea83c3b3a162fd390183b1cb8e76879907"
"sourceTreeHash": "6c984c7a56aeef55ed6af2b9f95ec5a8fb2073405b14846fdc79e056c8fe2a65"
}
@@ -12,6 +12,7 @@ import {
type PresentationValues,
} from "../../lib/presentation/presentation-bindings.js";
import type { RecordContentSource } from "./types.js";
import { getFullFormValues } from "../../lib/form-values.js";
function createInitialValues(
record: Offering | TrustContent,
@@ -96,12 +97,12 @@ export function useRecordContentEditorState({
});
const submit = (values: PresentationValues) => {
const currentValues = form.getFieldsValue(true) as PresentationValues;
const currentValues = getFullFormValues<PresentationValues>(form);
save.mutate({ ...draftValues, ...values, ...currentValues });
};
const handleValuesChange = (_values: PresentationValues) => {
const currentValues = form.getFieldsValue(true) as PresentationValues;
const currentValues = getFullFormValues<PresentationValues>(form);
const next = { ...draftValues, ..._values, ...currentValues };
setDraftValues(next);
onDraft?.(next);
@@ -11,6 +11,7 @@ import {
} from "../../lib/presentation/presentation-bindings.js";
import { fieldTargetTitle } from "./presentation-editor-helpers.js";
import type { PreviewTarget } from "./preview-selection.js";
import { getFullFormValues } from "../../lib/form-values.js";
interface PresentationFormPanelProps {
form: FormInstance<PresentationValues>;
@@ -44,7 +45,7 @@ export function PresentationFormPanel({
layout="vertical"
initialValues={initialValues}
onValuesChange={onValuesChange}
onFinish={() => save.mutate(form.getFieldsValue(true) as PresentationValues)}
onFinish={() => save.mutate(getFullFormValues<PresentationValues>(form))}
className="presentation-editor__form"
>
<div className="presentation-editor__form-body">
@@ -11,6 +11,7 @@ import type {
TrustContent,
} from "@personal-brand/api-client";
import { createApiClient } from "../../lib/api.js";
import { getFullFormValues } from "../../lib/form-values.js";
import {
applyBrandPreviewValues,
applyPresentationValues,
@@ -161,7 +162,7 @@ export function useBrandPreviewDraft({
const handleValuesChange = (changed: PresentationValues, allValues: PresentationValues) => {
onDirty();
const synchronized: PresentationValues = {
...(form.getFieldsValue(true) as PresentationValues),
...getFullFormValues<PresentationValues>(form),
...allValues,
};
for (const section of sections) {
@@ -12,6 +12,7 @@ import { ComponentTemplatePreview } from "../brand/ComponentTemplatePreview.js";
import type { PreviewSelection, PreviewTarget } from "../brand/preview-selection.js";
import type { FormInstance } from "antd";
import "./ServicesPage.css";
import { getFullFormValues } from "../../lib/form-values.js";
type OfferingEditorModalProps = {
open: boolean;
@@ -140,7 +141,7 @@ export function OfferingEditorModal({
onValuesChange={(_, allValues) => onValuesChange(allValues)}
// 动态隐藏的字段会被卸载,onFinish 默认只返回仍挂载的字段。
// 提交时读取完整表单存储,避免切换区块后隐藏字段的修改丢失。
onFinish={() => onFinish(form.getFieldsValue(true) as Record<string, unknown>)}
onFinish={() => onFinish(getFullFormValues(form))}
>
<div ref={formScroll} className="service-offering-editor__form-scroll">
{selectedTarget ? (
@@ -0,0 +1,63 @@
import type { RuntimeOffering } from "@personal-brand/api-client";
type RuntimePresentation = RuntimeOffering["presentation"];
function isFilledString(value: unknown): value is string {
return typeof value === "string" && value.trim() !== "";
}
/**
* 把表单草稿投影到单条 Runtime Offering 上。
* 该函数是 buildOfferingPreviewWorkspace 的核心合并规则:
* - name/coverUrl 等展示字段为空时保留原值,避免输入中间态清空预览;
* - summary/priceText 允许清空为空字符串;
* - presentation 由原值、模板 config、config.presentation 与事实字段逐层合并;
* - detailHero 优先于 coverUrl,两者都为空时保留原 heroImage。
*/
export function projectOfferingPreviewFields(options: {
item: Pick<RuntimeOffering, "name" | "summary" | "priceText" | "coverUrl" | "presentation">;
config: Record<string, unknown>;
formValues: Record<string, unknown>;
}): {
name: string;
summary: string;
priceText: string | null;
coverUrl: string | null;
presentation: RuntimePresentation;
} {
const { item, config, formValues } = options;
const nestedPresentation =
config.presentation && typeof config.presentation === "object"
? (config.presentation as Record<string, unknown>)
: {};
const configDetails =
config.details && typeof config.details === "object"
? (config.details as Record<string, unknown>)
: {};
const presentation: RuntimePresentation = {
...item.presentation,
...config,
...nestedPresentation,
...(typeof config.area === "string" ? { area: config.area } : {}),
...(typeof config.series === "string" ? { series: config.series } : {}),
...(typeof config.spaceType === "string" ? { spaceType: config.spaceType } : {}),
heroImage: isFilledString(config.detailHero)
? config.detailHero
: isFilledString(formValues.coverUrl)
? formValues.coverUrl
: (item.coverUrl ?? null),
details: {
...item.presentation.details,
...configDetails,
},
};
return {
name: isFilledString(formValues.name) ? formValues.name : item.name,
summary: typeof formValues.summary === "string" ? formValues.summary : item.summary,
priceText: typeof formValues.priceText === "string" ? formValues.priceText : item.priceText,
coverUrl: isFilledString(formValues.coverUrl) ? formValues.coverUrl : item.coverUrl,
presentation,
};
}
@@ -7,6 +7,7 @@ import type {
TemplateAdminField,
} from "@personal-brand/api-client";
import { applyPresentationValues } from "../../lib/presentation/presentation-bindings.js";
import { projectOfferingPreviewFields } from "./offering-preview-projection.js";
export function previewFieldsByTarget(
sections: AdminTemplateContent["admin"]["sections"],
@@ -33,60 +34,11 @@ export function buildOfferingPreviewWorkspace(options: {
[{ 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, ...projectOfferingPreviewFields({ item, config, formValues }) }
: item,
),
};
@@ -6,6 +6,7 @@ import type {
TemplateAdminSection,
} from "@personal-brand/api-client";
import { offeringDefaultFormValues, offeringEditFormValues } from "./offering-form.js";
import { getFullFormValues } from "../../lib/form-values.js";
export type OfferingPreviewPage = "offerings" | "detail";
@@ -40,7 +41,7 @@ export function useOfferingEditor(templateFields: TemplateAdminField[]) {
? offeringEditFormValues(item, templateFields)
: offeringDefaultFormValues(templateFields),
);
setFormValues(offeringForm.getFieldsValue(true) as Record<string, unknown>);
setFormValues(getFullFormValues(offeringForm));
};
const closeOffering = () => {
+7
View File
@@ -0,0 +1,7 @@
import type { FormInstance } from "antd";
// 动态隐藏的字段会被 Ant Design Form 卸载,onFinish 默认只返回仍挂载的字段。
// 提交时读取完整表单存储,避免切换区块后隐藏字段的修改丢失。
export function getFullFormValues<T = Record<string, unknown>>(form: FormInstance): T {
return form.getFieldsValue(true) as T;
}
@@ -0,0 +1,138 @@
import assert from "node:assert/strict";
import test from "node:test";
import { projectOfferingPreviewFields } from "../src/features/services/offering-preview-projection.ts";
const baseItem = {
name: "原名称",
summary: "原简介",
priceText: "原价格",
coverUrl: "/media/original-cover.jpg",
presentation: {
categoryKey: "portfolio",
categoryLabel: "品牌视觉",
heroImage: "/media/original-hero.jpg",
keywords: ["原始"],
media: [],
details: {
background: "原始背景",
designConcept: "",
crafts: [],
materialsList: [],
},
},
};
test("filled name and coverUrl override originals", () => {
const result = projectOfferingPreviewFields({
item: baseItem,
config: {},
formValues: {
name: "新名称",
coverUrl: "/media/new-cover.jpg",
summary: "",
priceText: "",
},
});
assert.equal(result.name, "新名称");
assert.equal(result.coverUrl, "/media/new-cover.jpg");
// summary/priceText 空字符串应覆盖原值(允许清空)。
assert.equal(result.summary, "");
assert.equal(result.priceText, "");
});
test("empty name and coverUrl preserve originals", () => {
const result = projectOfferingPreviewFields({
item: baseItem,
config: {},
formValues: {
name: " ",
coverUrl: "",
summary: "新简介",
},
});
assert.equal(result.name, "原名称");
assert.equal(result.coverUrl, "/media/original-cover.jpg");
assert.equal(result.summary, "新简介");
});
test("presentation merges base config and nested presentation", () => {
const result = projectOfferingPreviewFields({
item: baseItem,
config: {
area: "杭州",
series: "品牌焕新",
spaceType: "线上与线下",
presentation: { categoryLabel: "新分类" },
keywords: ["新关键词"],
},
formValues: {},
});
assert.equal(result.presentation.area, "杭州");
assert.equal(result.presentation.series, "品牌焕新");
assert.equal(result.presentation.spaceType, "线上与线下");
assert.equal(result.presentation.categoryLabel, "新分类");
assert.deepEqual(result.presentation.keywords, ["新关键词"]);
// categoryKey 不被覆盖时保留原值。
assert.equal(result.presentation.categoryKey, "portfolio");
});
test("heroImage follows detailHero, then coverUrl, then original", () => {
const hero = projectOfferingPreviewFields({
item: baseItem,
config: { detailHero: "/media/detail-hero.jpg" },
formValues: { coverUrl: "/media/new-cover.jpg" },
});
assert.equal(hero.presentation.heroImage, "/media/detail-hero.jpg");
const fallbackCover = projectOfferingPreviewFields({
item: baseItem,
config: { detailHero: "" },
formValues: { coverUrl: "/media/new-cover.jpg" },
});
assert.equal(fallbackCover.presentation.heroImage, "/media/new-cover.jpg");
const fallbackOriginal = projectOfferingPreviewFields({
item: baseItem,
config: { detailHero: "" },
formValues: { coverUrl: "" },
});
assert.equal(fallbackOriginal.presentation.heroImage, "/media/original-cover.jpg");
});
test("details merge preserves originals and applies updates", () => {
const result = projectOfferingPreviewFields({
item: baseItem,
config: {
details: {
background: "更新后的背景",
designConcept: "新的设计理念",
crafts: ["新交付"],
materialsList: [{ name: "策略", spec: "定位" }],
},
},
formValues: {},
});
assert.equal(result.presentation.details.background, "更新后的背景");
assert.equal(result.presentation.details.designConcept, "新的设计理念");
assert.deepEqual(result.presentation.details.crafts, ["新交付"]);
assert.deepEqual(result.presentation.details.materialsList, [{ name: "策略", spec: "定位" }]);
});
test("non-object config.presentation and details are ignored safely", () => {
const result = projectOfferingPreviewFields({
item: baseItem,
config: {
presentation: "invalid",
details: 42,
},
formValues: {},
});
assert.deepEqual(result.presentation.keywords, ["原始"]);
assert.equal(result.presentation.details.background, "原始背景");
assert.deepEqual(result.presentation.details.crafts, []);
});
+1
View File
@@ -68,6 +68,7 @@ export type { PublicOffering, BookingStatus, BrandTemplateKey } from "@personal-
export type {
EditorPreviewFrameMessage,
EditorPreviewHostMessage,
RuntimeOffering,
RuntimeWorkspace,
} from "@personal-brand/types";
export { isTrustContentComplete } from "@personal-brand/types";