refactor: 拆分小程序发布与后台发布页面职责
This commit is contained in:
1 parent
8973338c64
commit
7c0f22ad7a
19 files changed
+797
-538
No files matched your search
@@ -39,7 +39,7 @@ import {
|
||||
filterSectionsForPreviewTarget,
|
||||
presentationFieldName,
|
||||
type PresentationValues,
|
||||
} from "./presentation-bindings.js";
|
||||
} from "../../lib/presentation/presentation-bindings.js";
|
||||
import "./PresentationEditor.css";
|
||||
import { useEditorDraft } from "./use-editor-draft.js";
|
||||
import {
|
||||
|
||||
@@ -1,164 +0,0 @@
|
||||
export type PresentationField = {
|
||||
key: string;
|
||||
binding: string;
|
||||
input?: string;
|
||||
itemValueKey?: string;
|
||||
};
|
||||
|
||||
export type PresentationSection = {
|
||||
key: string;
|
||||
fields: PresentationField[];
|
||||
};
|
||||
|
||||
export type PresentationValues = Record<string, unknown>;
|
||||
|
||||
export function filterSectionsForPreviewTarget<
|
||||
T extends { fields: Array<{ previewTarget?: string }> },
|
||||
>(sections: T[], targetKey?: string): T[] {
|
||||
if (!targetKey) return sections;
|
||||
return sections
|
||||
.map((section) => ({
|
||||
...section,
|
||||
fields: section.fields.filter((field) => field.previewTarget === targetKey),
|
||||
}))
|
||||
.filter((section) => section.fields.length > 0);
|
||||
}
|
||||
|
||||
function asObject(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === "object" && !Array.isArray(value)
|
||||
? (value as Record<string, unknown>)
|
||||
: {};
|
||||
}
|
||||
|
||||
function safePath(binding: string): string[] | null {
|
||||
const prefixes = ["brand.config.", "offering.config.", "trust.config."];
|
||||
const prefix = prefixes.find((candidate) => binding.startsWith(candidate));
|
||||
const standardField =
|
||||
/^(brand\.(name|tagline|avatarUrl|description|contactPhone|contactWechat|address|hours|deliveryNote)|offering\.(name|summary|description|coverUrl|priceText)|trust\.(title|summary|content|coverUrl))$/.test(
|
||||
binding,
|
||||
);
|
||||
if (!prefix && !standardField) return null;
|
||||
const path = prefix ? binding.slice(prefix.length).split(".") : [binding.split(".")[1] ?? ""];
|
||||
if (path.some((part) => !part || ["__proto__", "prototype", "constructor"].includes(part)))
|
||||
return null;
|
||||
return path;
|
||||
}
|
||||
|
||||
export function presentationFieldName(sectionKey: string, fieldKey: string): string {
|
||||
return `${sectionKey}:${fieldKey}`;
|
||||
}
|
||||
|
||||
export function readPresentationValue(
|
||||
config: Record<string, unknown> | null | undefined,
|
||||
binding: string,
|
||||
field?: PresentationField,
|
||||
brand?: Record<string, unknown>,
|
||||
): unknown {
|
||||
const path = safePath(binding);
|
||||
if (!path) return undefined;
|
||||
const isConfigBinding =
|
||||
binding.startsWith("brand.config.") ||
|
||||
binding.startsWith("offering.config.") ||
|
||||
binding.startsWith("trust.config.");
|
||||
let value: unknown = isConfigBinding ? asObject(config) : asObject(brand);
|
||||
for (const part of path) value = asObject(value)[part];
|
||||
if (field?.input === "list") {
|
||||
if (!Array.isArray(value)) return [];
|
||||
return field.itemValueKey
|
||||
? (value as unknown[]).map((item) => ({ [field.itemValueKey as string]: item }))
|
||||
: value;
|
||||
}
|
||||
if (field?.input === "multi-select") return Array.isArray(value) ? value : [];
|
||||
return typeof value === "string" || typeof value === "number" || typeof value === "boolean"
|
||||
? value
|
||||
: undefined;
|
||||
}
|
||||
|
||||
function writePath(
|
||||
source: Record<string, unknown>,
|
||||
path: string[],
|
||||
value: unknown,
|
||||
): Record<string, unknown> {
|
||||
const [head, ...tail] = path;
|
||||
if (!head) return source;
|
||||
if (!tail.length) return { ...source, [head]: value ?? "" };
|
||||
return {
|
||||
...source,
|
||||
[head]: writePath(asObject(source[head]), tail, value),
|
||||
};
|
||||
}
|
||||
|
||||
export function applyPresentationValues(
|
||||
config: Record<string, unknown> | null | undefined,
|
||||
sections: PresentationSection[],
|
||||
values: PresentationValues,
|
||||
): Record<string, unknown> {
|
||||
let nextConfig = { ...asObject(config) };
|
||||
for (const section of sections) {
|
||||
for (const field of section.fields) {
|
||||
if (!field.binding.includes(".config.")) continue;
|
||||
const path = safePath(field.binding);
|
||||
if (!path) continue;
|
||||
const raw = values[presentationFieldName(section.key, field.key)];
|
||||
if (raw === undefined) continue;
|
||||
const value =
|
||||
field.input === "list" && Array.isArray(raw)
|
||||
? field.itemValueKey
|
||||
? raw
|
||||
.map((item) => asObject(item)[field.itemValueKey as string])
|
||||
.filter((item) => typeof item === "string" && item.trim())
|
||||
: raw
|
||||
: raw;
|
||||
nextConfig = writePath(nextConfig, path, value);
|
||||
}
|
||||
}
|
||||
return nextConfig;
|
||||
}
|
||||
|
||||
export function applyStandardBrandValues(
|
||||
brand: Record<string, unknown>,
|
||||
sections: PresentationSection[],
|
||||
values: PresentationValues,
|
||||
): Record<string, unknown> {
|
||||
const next = { ...brand };
|
||||
for (const section of sections) {
|
||||
for (const field of section.fields) {
|
||||
if (!field.binding.startsWith("brand.") || field.binding.startsWith("brand.config."))
|
||||
continue;
|
||||
const value = values[presentationFieldName(section.key, field.key)];
|
||||
if (value !== undefined) next[field.binding.slice("brand.".length)] = value;
|
||||
}
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
export function applyStandardEntityValues(
|
||||
entity: Record<string, unknown>,
|
||||
entityType: "offering" | "trust",
|
||||
sections: PresentationSection[],
|
||||
values: PresentationValues,
|
||||
): Record<string, unknown> {
|
||||
const next = { ...entity };
|
||||
for (const section of sections) {
|
||||
for (const field of section.fields) {
|
||||
if (!field.binding.startsWith(`${entityType}.`) || field.binding.includes(".config."))
|
||||
continue;
|
||||
const value = values[presentationFieldName(section.key, field.key)];
|
||||
if (value !== undefined) next[field.binding.slice(entityType.length + 1)] = value;
|
||||
}
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
export function applyBrandPreviewValues(
|
||||
brand: Record<string, unknown>,
|
||||
config: Record<string, unknown> | null | undefined,
|
||||
sections: PresentationSection[],
|
||||
values: PresentationValues,
|
||||
): Record<string, unknown> {
|
||||
const nextBrand = applyStandardBrandValues(brand, sections, values);
|
||||
return {
|
||||
...nextBrand,
|
||||
config: applyPresentationValues(config, sections, values),
|
||||
};
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
import { useMemo } from "react";
|
||||
import type { Offering, TrustContent } from "@personal-brand/api-client";
|
||||
import type { TemplateAdminSection } from "@personal-brand/api-client";
|
||||
import { applyPresentationValues, applyStandardEntityValues } from "./presentation-bindings.js";
|
||||
import type { PresentationValues } from "./presentation-bindings.js";
|
||||
import {
|
||||
applyPresentationValues,
|
||||
applyStandardEntityValues,
|
||||
} from "../../lib/presentation/presentation-bindings.js";
|
||||
import type { PresentationValues } from "../../lib/presentation/presentation-bindings.js";
|
||||
|
||||
export interface RecordDraft {
|
||||
source: "offering" | "trust";
|
||||
|
||||
@@ -18,7 +18,7 @@ import {
|
||||
presentationFieldName,
|
||||
readPresentationValue,
|
||||
type PresentationValues,
|
||||
} from "./presentation-bindings.js";
|
||||
} from "../../lib/presentation/presentation-bindings.js";
|
||||
|
||||
const standardBrandBindings = [
|
||||
"brand.name",
|
||||
|
||||
Reference in new issue
Block a user