feat: 填充了组件
This commit is contained in:
@@ -88,8 +88,6 @@ export const LONG_TEXT_PREVIEW_KEYS = [
|
||||
LONG_TEXT_KEYS.tag,
|
||||
];
|
||||
|
||||
const CONFIGURED_KEYS = LONG_TEXT_FIELD_CONFIG.map((item) => item.key);
|
||||
|
||||
export const createLongTextData = () => ({
|
||||
values: {},
|
||||
parsedValues: {},
|
||||
@@ -116,6 +114,121 @@ const tryParseJSON = (raw) => {
|
||||
}
|
||||
};
|
||||
|
||||
const parseMaybeJSON = (value) => {
|
||||
if (typeof value !== "string") return value;
|
||||
|
||||
const parsed = tryParseJSON(value.trim());
|
||||
return parsed.ok ? parsed.value : value;
|
||||
};
|
||||
|
||||
const toTrimmedText = (value) => {
|
||||
if (value === undefined || value === null) return "";
|
||||
return String(value).trim();
|
||||
};
|
||||
|
||||
const toFiniteNumber = (value) => {
|
||||
if (value === undefined || value === null || value === "") return null;
|
||||
const numberValue = Number(value);
|
||||
return Number.isFinite(numberValue) ? numberValue : null;
|
||||
};
|
||||
|
||||
const pickFirstValue = (...values) => {
|
||||
return values.find((value) => value !== undefined && value !== null && value !== "");
|
||||
};
|
||||
|
||||
const normalizeObjectKeys = (value) => {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return {};
|
||||
|
||||
return Object.keys(value).reduce((result, key) => {
|
||||
const normalizedKey = String(key).trim().replace(/[::]+$/, "");
|
||||
result[normalizedKey] = value[key];
|
||||
return result;
|
||||
}, {});
|
||||
};
|
||||
|
||||
export const normalizeLongTextContentImage = (value) => {
|
||||
const parsedValue = parseMaybeJSON(value);
|
||||
|
||||
if (typeof parsedValue === "string") {
|
||||
return {
|
||||
url: toTrimmedText(parsedValue),
|
||||
caption: "",
|
||||
};
|
||||
}
|
||||
|
||||
const valueObj = normalizeObjectKeys(parsedValue);
|
||||
return {
|
||||
url: toTrimmedText(
|
||||
pickFirstValue(
|
||||
valueObj.url,
|
||||
valueObj.image_url,
|
||||
valueObj.image,
|
||||
valueObj.image_id,
|
||||
valueObj.content_image
|
||||
)
|
||||
),
|
||||
caption: toTrimmedText(pickFirstValue(valueObj.caption, valueObj.image_caption)),
|
||||
};
|
||||
};
|
||||
|
||||
export const normalizeLongTextQuestionSuggest = (value) => {
|
||||
const parsedValue = parseMaybeJSON(value);
|
||||
const rawList = Array.isArray(parsedValue)
|
||||
? parsedValue
|
||||
: [parsedValue?.questions, parsedValue?.items, parsedValue?.list].find(Array.isArray) || [];
|
||||
|
||||
return rawList
|
||||
.map((item) => toTrimmedText(item))
|
||||
.filter(Boolean);
|
||||
};
|
||||
|
||||
export const normalizeLongTextSpotLocate = (value) => {
|
||||
const valueObj = normalizeObjectKeys(parseMaybeJSON(value));
|
||||
|
||||
return {
|
||||
name: toTrimmedText(
|
||||
pickFirstValue(
|
||||
valueObj.spot_name,
|
||||
valueObj.sopt_name,
|
||||
valueObj.name,
|
||||
valueObj.title
|
||||
)
|
||||
),
|
||||
description: toTrimmedText(
|
||||
pickFirstValue(
|
||||
valueObj.spot_description,
|
||||
valueObj.sopt_description,
|
||||
valueObj.description,
|
||||
valueObj.desc
|
||||
)
|
||||
),
|
||||
longitude: toFiniteNumber(
|
||||
pickFirstValue(
|
||||
valueObj.spot_longitude,
|
||||
valueObj.sopt_longitude,
|
||||
valueObj.longitude,
|
||||
valueObj.lng
|
||||
)
|
||||
),
|
||||
latitude: toFiniteNumber(
|
||||
pickFirstValue(
|
||||
valueObj.spot_latitude,
|
||||
valueObj.sopt_latitude,
|
||||
valueObj.latitude,
|
||||
valueObj.lat
|
||||
)
|
||||
),
|
||||
tag: toTrimmedText(
|
||||
pickFirstValue(
|
||||
valueObj.spot_tag,
|
||||
valueObj.sopt_tag,
|
||||
valueObj.tag,
|
||||
valueObj.type
|
||||
)
|
||||
),
|
||||
};
|
||||
};
|
||||
|
||||
export const appendLongTextChunk = (target, chunk = {}) => {
|
||||
if (!target || !chunk.contentKey) return target;
|
||||
|
||||
@@ -165,12 +278,7 @@ export const hasLongTextExtraSections = (data, previewKeys = LONG_TEXT_PREVIEW_K
|
||||
export const getLongTextSections = (data) => {
|
||||
if (!data || !data.values) return [];
|
||||
|
||||
const extraKeys = Object.keys(data.values).filter(
|
||||
(key) => !CONFIGURED_KEYS.includes(key),
|
||||
);
|
||||
|
||||
return [...CONFIGURED_KEYS, ...extraKeys]
|
||||
.filter((key) => Object.prototype.hasOwnProperty.call(data.values, key))
|
||||
return Object.keys(data.values)
|
||||
.map((key) => ({
|
||||
contentKey: key,
|
||||
contentValue: getLongTextValue(data, key),
|
||||
|
||||
Reference in New Issue
Block a user