diff --git a/src/pages/scene-editor.vue b/src/pages/scene-editor.vue index 3b60660..699b7c8 100644 --- a/src/pages/scene-editor.vue +++ b/src/pages/scene-editor.vue @@ -26,11 +26,33 @@ const props = defineProps(); 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;