feat: 在场景编辑器中新增场景 JSON 规范化处理逻辑,优化场景加载条件,提升数据解析的健壮性
This commit is contained in:
parent
3295c02122
commit
f41e6cf1d3
@ -26,11 +26,33 @@ const props = defineProps<Props>();
|
|||||||
const { t } = useI18n();
|
const { t } = useI18n();
|
||||||
const { isConverting, convertSmapToScene, exportSceneToSmap, convertSceneToIray } = useMapConversion();
|
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 接口
|
//#region 接口
|
||||||
const readScene = async () => {
|
const readScene = async () => {
|
||||||
const res = await getSceneById(props.id);
|
const res = await getSceneById(props.id);
|
||||||
title.value = res?.label ?? '';
|
title.value = res?.label ?? '';
|
||||||
let sceneJson = res?.json;
|
const sceneJson = normalizeSceneJson(res?.json);
|
||||||
|
|
||||||
requireImportTransform.value = false;
|
requireImportTransform.value = false;
|
||||||
// 适配不同的场景数据格式
|
// 适配不同的场景数据格式
|
||||||
@ -49,7 +71,7 @@ const readScene = async () => {
|
|||||||
previousFloorIndex.value = 0;
|
previousFloorIndex.value = 0;
|
||||||
editor.value?.load('{}', editable.value, undefined, requireImportTransform.value);
|
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];
|
floorScenes.value = [sceneJson];
|
||||||
currentFloorIndex.value = 0;
|
currentFloorIndex.value = 0;
|
||||||
@ -69,6 +91,12 @@ type SaveScenePayload = {
|
|||||||
png?: string;
|
png?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type FloorViewState = {
|
||||||
|
scale: number;
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
};
|
||||||
|
|
||||||
const saveScene = async (payload?: SaveScenePayload) => {
|
const saveScene = async (payload?: SaveScenePayload) => {
|
||||||
const currentJson = payload?.json ?? editor.value?.save();
|
const currentJson = payload?.json ?? editor.value?.save();
|
||||||
if (currentJson) {
|
if (currentJson) {
|
||||||
@ -422,12 +450,7 @@ const handleFloorChange = async (value: any) => {
|
|||||||
// 加载新楼层
|
// 加载新楼层
|
||||||
currentFloorIndex.value = newFloorIndex;
|
currentFloorIndex.value = newFloorIndex;
|
||||||
previousFloorIndex.value = newFloorIndex;
|
previousFloorIndex.value = newFloorIndex;
|
||||||
await editor.value.load(
|
await editor.value.load(floorScenes.value[newFloorIndex], editable.value, undefined, requireImportTransform.value);
|
||||||
floorScenes.value[newFloorIndex],
|
|
||||||
editable.value,
|
|
||||||
undefined,
|
|
||||||
requireImportTransform.value,
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
currentFloorIndex.value = newFloorIndex;
|
currentFloorIndex.value = newFloorIndex;
|
||||||
previousFloorIndex.value = newFloorIndex;
|
previousFloorIndex.value = newFloorIndex;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user