feat: 在场景编辑器中新增场景 JSON 规范化处理逻辑,优化场景加载条件,提升数据解析的健壮性

This commit is contained in:
xudan 2025-10-15 08:59:46 +08:00
parent 3295c02122
commit f41e6cf1d3

View File

@ -26,11 +26,33 @@ const props = defineProps<Props>();
const { t } = useI18n();
const { isConverting, convertSmapToScene, exportSceneToSmap, convertSceneToIray } = useMapConversion();
const normalizeSceneJson = (raw: unknown): unknown => {
if (raw == null) return raw;
let result: unknown = raw;
const maxDepth = 2;
let depth = 0;
while (typeof result === 'string' && depth < maxDepth) {
const trimmed = (result as string).trim();
if (!trimmed) break;
try {
result = JSON.parse(trimmed);
} catch (error) {
console.warn('解析场景 JSON 失败:', error);
break;
}
depth += 1;
}
return result;
};
//#region
const readScene = async () => {
const res = await getSceneById(props.id);
title.value = res?.label ?? '';
let sceneJson = res?.json;
const sceneJson = normalizeSceneJson(res?.json);
requireImportTransform.value = false;
//
@ -49,7 +71,7 @@ const readScene = async () => {
previousFloorIndex.value = 0;
editor.value?.load('{}', editable.value, undefined, requireImportTransform.value);
}
} else if (sceneJson && Object.keys(sceneJson).length > 0) {
} else if (sceneJson && typeof sceneJson === 'object' && Object.keys(sceneJson).length > 0) {
//
floorScenes.value = [sceneJson];
currentFloorIndex.value = 0;
@ -69,6 +91,12 @@ type SaveScenePayload = {
png?: string;
};
type FloorViewState = {
scale: number;
x: number;
y: number;
};
const saveScene = async (payload?: SaveScenePayload) => {
const currentJson = payload?.json ?? editor.value?.save();
if (currentJson) {
@ -422,12 +450,7 @@ const handleFloorChange = async (value: any) => {
//
currentFloorIndex.value = newFloorIndex;
previousFloorIndex.value = newFloorIndex;
await editor.value.load(
floorScenes.value[newFloorIndex],
editable.value,
undefined,
requireImportTransform.value,
);
await editor.value.load(floorScenes.value[newFloorIndex], editable.value, undefined, requireImportTransform.value);
} else {
currentFloorIndex.value = newFloorIndex;
previousFloorIndex.value = newFloorIndex;