feat: 改造了组件的渲染逻辑

This commit is contained in:
2026-06-04 10:57:28 +08:00
parent 19b97fc326
commit e5ec442500
5 changed files with 267 additions and 219 deletions

View File

@@ -8,6 +8,12 @@ export const LONG_TEXT_KEYS = {
keyFacts: "key_facts",
sceneImage: "scene_image",
contentImage: "content_image",
viewSectionTitle: "view_section_title",
viewSectionItems: "view_section_items",
suggestionSectionTitle: "suggestion_section_title",
suggestionSectionContent: "suggestion_section_content",
lightReminderTitle: "light_reminder_title",
lightReminderItems: "light_reminder_items",
checklistOrSteps: "checklist_or_steps",
bestTimeOrPeople: "best_time_or_people",
avoidPitfalls: "avoid_pitfalls",
@@ -50,6 +56,12 @@ export const LONG_TEXT_FIELD_CONFIG = [
{ key: LONG_TEXT_KEYS.keyFacts },
{ key: LONG_TEXT_KEYS.sceneImage },
{ key: LONG_TEXT_KEYS.contentImage },
{ key: LONG_TEXT_KEYS.viewSectionTitle },
{ key: LONG_TEXT_KEYS.viewSectionItems },
{ key: LONG_TEXT_KEYS.suggestionSectionTitle },
{ key: LONG_TEXT_KEYS.suggestionSectionContent },
{ key: LONG_TEXT_KEYS.lightReminderTitle },
{ key: LONG_TEXT_KEYS.lightReminderItems },
{ key: LONG_TEXT_KEYS.checklistOrSteps },
{ key: LONG_TEXT_KEYS.bestTimeOrPeople },
{ key: LONG_TEXT_KEYS.avoidPitfalls },
@@ -121,112 +133,61 @@ const parseMaybeJSON = (value) => {
return parsed.ok ? parsed.value : value;
};
const toTrimmedText = (value) => {
if (value === undefined || value === null) return "";
return String(value).trim();
const isLongTextArrayValue = (value) => Array.isArray(value);
const isLongTextObjectValue = (value) => {
return value !== null && typeof value === "object" && !Array.isArray(value);
};
const toFiniteNumber = (value) => {
if (value === undefined || value === null || value === "") return null;
const numberValue = Number(value);
return Number.isFinite(numberValue) ? numberValue : null;
export const parseLongTextDisplayValue = (value) => {
return parseMaybeJSON(value);
};
const pickFirstValue = (...values) => {
return values.find((value) => value !== undefined && value !== null && value !== "");
};
export const sanitizeLongTextDisplayValue = (value, ignoredKeys = []) => {
const parsedValue = parseLongTextDisplayValue(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: "",
};
if (isLongTextArrayValue(parsedValue)) {
return parsedValue.map((item) => sanitizeLongTextDisplayValue(item, ignoredKeys));
}
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)),
};
if (isLongTextObjectValue(parsedValue)) {
return Object.keys(parsedValue).reduce((result, key) => {
if (ignoredKeys.includes(key)) return result;
result[key] = sanitizeLongTextDisplayValue(parsedValue[key], ignoredKeys);
return result;
}, {});
}
return parsedValue;
};
export const normalizeLongTextQuestionSuggest = (value) => {
const parsedValue = parseMaybeJSON(value);
const rawList = Array.isArray(parsedValue)
? parsedValue
: [parsedValue?.questions, parsedValue?.items, parsedValue?.list].find(Array.isArray) || [];
export const hasLongTextDisplayValue = (value, ignoredKeys = []) => {
const displayValue = sanitizeLongTextDisplayValue(value, ignoredKeys);
return rawList
.map((item) => toTrimmedText(item))
.filter(Boolean);
if (displayValue === undefined || displayValue === null) return false;
if (typeof displayValue === "string") return !!displayValue.trim();
if (isLongTextArrayValue(displayValue)) {
return displayValue.some((item) => hasLongTextDisplayValue(item, ignoredKeys));
}
if (isLongTextObjectValue(displayValue)) {
return Object.keys(displayValue).some((key) =>
hasLongTextDisplayValue(displayValue[key], ignoredKeys)
);
}
return true;
};
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 formatLongTextDisplayValue = (value, ignoredKeys = []) => {
if (value === undefined || value === null) return "";
if (typeof value === "boolean") return value ? "是" : "否";
if (typeof value === "object") {
try {
return JSON.stringify(sanitizeLongTextDisplayValue(value, ignoredKeys));
} catch (e) {
return String(value);
}
}
return String(value);
};
export const appendLongTextChunk = (target, chunk = {}) => {