feat: 完善模板字段编辑与分类治理
This commit is contained in:
1 parent
91322fa9a0
commit
26bcb2b66e
23 files changed
+631
-250
No files matched your search
@@ -114,7 +114,18 @@
|
||||
}
|
||||
|
||||
.presentation-editor__inspector .ant-form-item {
|
||||
margin-bottom: 24px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.presentation-editor__inspector form > section {
|
||||
padding: 16px;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 12px;
|
||||
background: #f8fafc;
|
||||
}
|
||||
|
||||
.presentation-editor__inspector form > section + section {
|
||||
margin-top: 16px;
|
||||
}
|
||||
|
||||
.presentation-editor__inspector form > .ant-space:last-child {
|
||||
|
||||
@@ -5,8 +5,6 @@ import {
|
||||
Col,
|
||||
Form,
|
||||
Grid,
|
||||
Input,
|
||||
InputNumber,
|
||||
Row,
|
||||
Select,
|
||||
Segmented,
|
||||
@@ -27,8 +25,8 @@ import type {
|
||||
import type { BrandProfile } from "@personal-brand/api-client";
|
||||
import { projectBrandAliases, projectTrustPresentation } from "@personal-brand/api-client";
|
||||
import { createApiClient } from "../../lib/api.js";
|
||||
import { ImageUrlField } from "../../components/ImageUrlField.js";
|
||||
import { TemplateListField } from "../../components/TemplateListField.js";
|
||||
import { TemplateFieldRenderer } from "../../components/TemplateFieldRenderer.js";
|
||||
import { MutationError } from "../../components/QueryState.js";
|
||||
import { RecordContentEditor } from "../../components/RecordContentEditor.js";
|
||||
import { TemplatePreview } from "./TemplatePreview.js";
|
||||
@@ -118,45 +116,6 @@ function trustSection(
|
||||
};
|
||||
}
|
||||
|
||||
function renderField(field: TemplateAdminField) {
|
||||
if (field.input === "textarea")
|
||||
return (
|
||||
<Input.TextArea
|
||||
rows={3}
|
||||
maxLength={field.maxLength}
|
||||
showCount={!!field.maxLength}
|
||||
placeholder={field.placeholder}
|
||||
/>
|
||||
);
|
||||
if (field.input === "image")
|
||||
return <ImageUrlField {...(field.placeholder ? { placeholder: field.placeholder } : {})} />;
|
||||
if (field.input === "number")
|
||||
return <InputNumber style={{ width: "100%" }} placeholder={field.placeholder} />;
|
||||
if (field.input === "boolean")
|
||||
return (
|
||||
<Select
|
||||
options={[
|
||||
{ value: true, label: "是" },
|
||||
{ value: false, label: "否" },
|
||||
]}
|
||||
/>
|
||||
);
|
||||
if (field.input === "select" || field.input === "multi-select")
|
||||
return (
|
||||
<Select
|
||||
{...(field.input === "multi-select" ? { mode: "multiple" as const } : {})}
|
||||
options={field.options ?? []}
|
||||
/>
|
||||
);
|
||||
return (
|
||||
<Input
|
||||
type={field.input === "phone" ? "tel" : "text"}
|
||||
maxLength={field.maxLength}
|
||||
placeholder={field.placeholder}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function previewTargetSource(
|
||||
section: TemplateAdminSection,
|
||||
targetKey: string,
|
||||
@@ -497,11 +456,28 @@ export function PresentationEditor({
|
||||
useEffect(() => {
|
||||
setPreviewConfig(config ?? {});
|
||||
setPreviewBrandValues({});
|
||||
}, [config]);
|
||||
}, [config, initialValues]);
|
||||
const previewBrand = useMemo(
|
||||
() => ({ ...brand, ...previewBrandValues, config: previewConfig }),
|
||||
[brand, previewBrandValues, previewConfig],
|
||||
);
|
||||
const recordPreview = useMemo(() => {
|
||||
if (!recordDraft || !selectedRecord || !recordSection) return undefined;
|
||||
const config = applyPresentationValues(
|
||||
selectedRecord.config,
|
||||
[recordSection],
|
||||
recordDraft.values,
|
||||
);
|
||||
return {
|
||||
config,
|
||||
standard: applyStandardEntityValues(
|
||||
selectedRecord as unknown as Record<string, unknown>,
|
||||
recordDraft.source,
|
||||
[recordSection],
|
||||
recordDraft.values,
|
||||
),
|
||||
};
|
||||
}, [recordDraft, selectedRecord, recordSection]);
|
||||
const handleValuesChange = useCallback(
|
||||
(changed: PresentationValues, allValues: PresentationValues) => {
|
||||
setBrandDirty(true);
|
||||
@@ -559,14 +535,9 @@ export function PresentationEditor({
|
||||
};
|
||||
if (!recordDraft || !selectedRecord || !recordSection || recordDraft.id !== selectedRecord.id)
|
||||
return next;
|
||||
const values = recordDraft.values;
|
||||
const config = applyPresentationValues(selectedRecord.config, [recordSection], values);
|
||||
const standard = applyStandardEntityValues(
|
||||
selectedRecord as unknown as Record<string, unknown>,
|
||||
recordDraft.source,
|
||||
[recordSection],
|
||||
values,
|
||||
);
|
||||
const config = recordPreview?.config;
|
||||
const standard = recordPreview?.standard;
|
||||
if (!config || !standard) return next;
|
||||
if (recordDraft.source === "trust") {
|
||||
next.trustItems = next.trustItems.map((item) =>
|
||||
item.id === recordDraft.id
|
||||
@@ -588,24 +559,27 @@ export function PresentationEditor({
|
||||
item.id === recordDraft.id
|
||||
? {
|
||||
...item,
|
||||
name: typeof standard.name === "string" ? standard.name : item.name,
|
||||
name:
|
||||
(recordDraft.values[presentationFieldName(recordSection.key, "name")] as string) ||
|
||||
item.name,
|
||||
summary:
|
||||
typeof standard.summary === "string"
|
||||
? standard.summary
|
||||
: typeof standard.description === "string"
|
||||
? standard.description
|
||||
: item.summary,
|
||||
priceText: typeof standard.priceText === "string" ? standard.priceText : null,
|
||||
coverUrl: typeof standard.coverUrl === "string" ? standard.coverUrl : null,
|
||||
(recordDraft.values[
|
||||
presentationFieldName(recordSection.key, "summary")
|
||||
] as string) || item.summary,
|
||||
priceText:
|
||||
(recordDraft.values[
|
||||
presentationFieldName(recordSection.key, "priceText")
|
||||
] as string) || null,
|
||||
coverUrl:
|
||||
(recordDraft.values[
|
||||
presentationFieldName(recordSection.key, "coverUrl")
|
||||
] as string) || null,
|
||||
presentation: {
|
||||
...item.presentation,
|
||||
...config,
|
||||
...(config.presentation && typeof config.presentation === "object"
|
||||
? (config.presentation as Record<string, unknown>)
|
||||
: {}),
|
||||
...(typeof config.categoryLabel === "string"
|
||||
? { categoryLabel: config.categoryLabel }
|
||||
: {}),
|
||||
...(typeof config.area === "string" ? { area: config.area } : {}),
|
||||
...(typeof config.series === "string" ? { series: config.series } : {}),
|
||||
...(typeof config.spaceType === "string" ? { spaceType: config.spaceType } : {}),
|
||||
@@ -636,6 +610,7 @@ export function PresentationEditor({
|
||||
recordDraft,
|
||||
selectedRecord,
|
||||
recordSection,
|
||||
recordPreview,
|
||||
templateContent,
|
||||
inquiryDraft,
|
||||
inquiryRuntime,
|
||||
@@ -992,7 +967,7 @@ export function PresentationEditor({
|
||||
}
|
||||
: {})}
|
||||
>
|
||||
{renderField(field)}
|
||||
<TemplateFieldRenderer field={field} />
|
||||
</Form.Item>
|
||||
)}
|
||||
</Col>
|
||||
|
||||
@@ -38,15 +38,35 @@
|
||||
min-width: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
max-height: 72vh;
|
||||
padding-right: 4px;
|
||||
}
|
||||
|
||||
.service-offering-editor__form-scroll {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
flex: none;
|
||||
max-height: 66vh;
|
||||
overflow-y: auto;
|
||||
padding-right: 4px;
|
||||
padding: 20px 16px 24px;
|
||||
border-radius: 12px;
|
||||
background: #f8fafc;
|
||||
border: 1px solid #e5e7eb;
|
||||
}
|
||||
|
||||
.service-offering-editor__form-scroll > .ant-typography:first-child {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
.service-offering-editor__form-scroll .ant-form-item {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.service-offering-editor__form-scroll .ant-typography h5.ant-typography {
|
||||
margin: 28px 0 4px;
|
||||
padding-bottom: 8px;
|
||||
border-bottom: 1px solid #e5e7eb;
|
||||
}
|
||||
|
||||
.service-offering-editor__form-scroll > .ant-typography-secondary {
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.service-offering-editor__preview .ant-alert {
|
||||
|
||||
@@ -45,6 +45,7 @@ export function ServicesPage() {
|
||||
const [editingOffering, setEditingOffering] = useState<Offering | null>(null);
|
||||
const [offeringDraftVersion, setOfferingDraftVersion] = useState(0);
|
||||
const [previewPage, setPreviewPage] = useState<"offerings" | "detail">("detail");
|
||||
const [formValues, setFormValues] = useState<Record<string, unknown>>({});
|
||||
const offerings = useQuery({
|
||||
queryKey: ["admin-offerings"],
|
||||
queryFn: () => client.listAdminOfferings(),
|
||||
@@ -139,7 +140,9 @@ export function ServicesPage() {
|
||||
: [...detailFields, ...otherFields.filter((field) => field.page === "detail")];
|
||||
const previewWorkspaceDraft = useMemo<RuntimeWorkspace | undefined>(() => {
|
||||
if (!previewWorkspace.data || !editingOffering) return undefined;
|
||||
const values = offeringForm.getFieldsValue(true) as Record<string, unknown>;
|
||||
const values: Record<string, unknown> = Object.keys(formValues).length
|
||||
? formValues
|
||||
: (offeringForm.getFieldsValue(true) as Record<string, unknown>);
|
||||
const config = applyPresentationValues(
|
||||
editingOffering.config,
|
||||
[{ key: "offering", fields: templateFields }],
|
||||
@@ -179,9 +182,6 @@ export function ServicesPage() {
|
||||
...(config.presentation && typeof config.presentation === "object"
|
||||
? (config.presentation as Record<string, unknown>)
|
||||
: {}),
|
||||
...(typeof config.categoryLabel === "string"
|
||||
? { categoryLabel: config.categoryLabel }
|
||||
: {}),
|
||||
...(typeof config.area === "string" ? { area: config.area } : {}),
|
||||
...(typeof config.series === "string" ? { series: config.series } : {}),
|
||||
...(typeof config.spaceType === "string" ? { spaceType: config.spaceType } : {}),
|
||||
@@ -206,7 +206,14 @@ export function ServicesPage() {
|
||||
),
|
||||
brand: previewWorkspace.data.brand,
|
||||
};
|
||||
}, [previewWorkspace.data, editingOffering, templateFields, offeringDraftVersion]);
|
||||
}, [
|
||||
previewWorkspace.data,
|
||||
editingOffering,
|
||||
templateFields,
|
||||
formValues,
|
||||
offeringDraftVersion,
|
||||
offeringForm,
|
||||
]);
|
||||
const saveOffering = useMutation({
|
||||
mutationFn: (values: Record<string, unknown>) => {
|
||||
const fields = Object.fromEntries(
|
||||
@@ -250,6 +257,7 @@ export function ServicesPage() {
|
||||
});
|
||||
const beginOffering = (item?: Offering) => {
|
||||
setEditingOffering(item ?? null);
|
||||
setPreviewPage("detail");
|
||||
setOfferingOpen(true);
|
||||
offeringForm.setFieldsValue(
|
||||
item
|
||||
@@ -263,10 +271,19 @@ export function ServicesPage() {
|
||||
sortOrder: item.sortOrder,
|
||||
isEnabled: item.isEnabled,
|
||||
...Object.fromEntries(
|
||||
templateFields.map((field) => [
|
||||
presentationFieldName("offering", field.key),
|
||||
readPresentationValue(item.config, field.binding, field),
|
||||
]),
|
||||
templateFields.map((field) => {
|
||||
const value = readPresentationValue(item.config, field.binding, field);
|
||||
return [
|
||||
presentationFieldName("offering", field.key),
|
||||
value === undefined || value === null || value === ""
|
||||
? field.input === "select"
|
||||
? field.options?.[0]?.value
|
||||
: field.input === "list"
|
||||
? []
|
||||
: undefined
|
||||
: value,
|
||||
];
|
||||
}),
|
||||
),
|
||||
}
|
||||
: {
|
||||
@@ -281,6 +298,7 @@ export function ServicesPage() {
|
||||
),
|
||||
},
|
||||
);
|
||||
setFormValues(offeringForm.getFieldsValue(true) as Record<string, unknown>);
|
||||
};
|
||||
return (
|
||||
<QueryState
|
||||
@@ -442,7 +460,10 @@ export function ServicesPage() {
|
||||
className="service-offering-editor__form"
|
||||
form={offeringForm}
|
||||
layout="vertical"
|
||||
onValuesChange={() => setOfferingDraftVersion((version) => version + 1)}
|
||||
onValuesChange={(_, allValues) => {
|
||||
setFormValues(allValues as Record<string, unknown>);
|
||||
setOfferingDraftVersion((version) => version + 1);
|
||||
}}
|
||||
onFinish={(values) => saveOffering.mutate(values as Record<string, unknown>)}
|
||||
>
|
||||
<div className="service-offering-editor__form-scroll">
|
||||
@@ -507,6 +528,7 @@ export function ServicesPage() {
|
||||
...templateFieldSection,
|
||||
fields: activeTemplateFields,
|
||||
}}
|
||||
sectionKey="offering"
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
@@ -36,25 +36,16 @@ const inquiryFieldTypes = [
|
||||
{ value: "multi-select", label: "多项选择" },
|
||||
];
|
||||
|
||||
function optionsToText(options: unknown): string {
|
||||
if (!Array.isArray(options)) return "";
|
||||
return options
|
||||
.map((option) => {
|
||||
if (!option || typeof option !== "object") return "";
|
||||
const label = (option as { label?: unknown }).label;
|
||||
return typeof label === "string" ? label : "";
|
||||
})
|
||||
.filter(Boolean)
|
||||
.join("\n");
|
||||
function readOptionLabel(option: unknown): string {
|
||||
const value = option as { label?: unknown } | null | undefined;
|
||||
return typeof value?.label === "string" ? value.label : "";
|
||||
}
|
||||
|
||||
function optionsFromText(value: unknown): Array<{ label: string; value: string }> {
|
||||
if (typeof value !== "string") return [];
|
||||
return value
|
||||
.split(/\r?\n|、|,/)
|
||||
.map((item) => item.trim())
|
||||
.filter(Boolean)
|
||||
.map((item) => ({ label: item, value: item }));
|
||||
function normalizeOptionLabels(options: unknown): Array<{ label: string; value: string }> {
|
||||
return (Array.isArray(options) ? options : [])
|
||||
.map(readOptionLabel)
|
||||
.filter((label) => label.trim())
|
||||
.map((label) => ({ label, value: label }));
|
||||
}
|
||||
|
||||
function supportsIndustry(
|
||||
@@ -541,11 +532,11 @@ export function WorkspacePage() {
|
||||
name: item.name,
|
||||
purpose: item.purpose,
|
||||
offeringId: item.offeringId ?? undefined,
|
||||
fieldsSnapshot: item.fieldsSnapshot.map((field) => {
|
||||
fieldsSnapshot: item.fieldsSnapshot.map((field: unknown) => {
|
||||
const value = field as Record<string, unknown>;
|
||||
return {
|
||||
...value,
|
||||
optionsText: optionsToText(value.options),
|
||||
options: normalizeOptionLabels(value.options),
|
||||
};
|
||||
}),
|
||||
});
|
||||
@@ -719,12 +710,15 @@ export function WorkspacePage() {
|
||||
fieldsSnapshot: (Array.isArray(values.fieldsSnapshot)
|
||||
? values.fieldsSnapshot
|
||||
: []
|
||||
).map((field) => {
|
||||
).map((field: unknown) => {
|
||||
const item = field as Record<string, unknown>;
|
||||
const { optionsText, ...rest } = item;
|
||||
const { options, ...rest } = item;
|
||||
const type = typeof rest.type === "string" ? rest.type : "text";
|
||||
return ["select", "radio", "multi-select"].includes(type)
|
||||
? { ...rest, options: optionsFromText(optionsText) }
|
||||
? {
|
||||
...rest,
|
||||
options: normalizeOptionLabels(options),
|
||||
}
|
||||
: rest;
|
||||
}),
|
||||
};
|
||||
@@ -824,18 +818,54 @@ export function WorkspacePage() {
|
||||
["select", "radio", "multi-select"].includes(
|
||||
String(getFieldValue(["fieldsSnapshot", name, "type"])),
|
||||
) ? (
|
||||
<Form.Item
|
||||
{...restField}
|
||||
name={[name, "optionsText"]}
|
||||
label="可选内容"
|
||||
help="每行填写一个选项,也可以用顿号分隔。"
|
||||
rules={[{ required: true, message: "请至少填写一个选项" }]}
|
||||
<Form.List
|
||||
name={[name, "options"]}
|
||||
rules={[
|
||||
{
|
||||
validator: (_, value: unknown) =>
|
||||
Array.isArray(value) &&
|
||||
value.some((option: unknown) => readOptionLabel(option).trim())
|
||||
? Promise.resolve()
|
||||
: Promise.reject(new Error("请至少添加一个选项")),
|
||||
},
|
||||
]}
|
||||
>
|
||||
<Input.TextArea
|
||||
rows={2}
|
||||
placeholder="例如:预算内、需要沟通、暂不确定"
|
||||
/>
|
||||
</Form.Item>
|
||||
{(optionFields, { add, remove }, { errors }) => (
|
||||
<>
|
||||
{optionFields.map((optionField) => (
|
||||
<Space
|
||||
key={optionField.key}
|
||||
align="center"
|
||||
style={{ display: "flex" }}
|
||||
>
|
||||
<Form.Item
|
||||
{...optionField}
|
||||
name={[optionField.name, "label"]}
|
||||
initialValue=""
|
||||
rules={[{ required: true, message: "请填写选项内容" }]}
|
||||
style={{ flex: 1, minWidth: 240, marginBottom: 8 }}
|
||||
>
|
||||
<Input placeholder="例如:预算内" />
|
||||
</Form.Item>
|
||||
<Button
|
||||
danger
|
||||
onClick={() => remove(optionField.name)}
|
||||
style={{ marginBottom: 8 }}
|
||||
>
|
||||
删除
|
||||
</Button>
|
||||
</Space>
|
||||
))}
|
||||
<Button
|
||||
onClick={() => add({ label: "", value: "" })}
|
||||
style={{ marginBottom: 8 }}
|
||||
>
|
||||
新增选项
|
||||
</Button>
|
||||
<Form.ErrorList errors={errors} />
|
||||
</>
|
||||
)}
|
||||
</Form.List>
|
||||
) : null
|
||||
}
|
||||
</Form.Item>
|
||||
|
||||
Reference in new issue
Block a user