fix(io): normalize legacy guide arrays safely
- Restore missing or malformed guides as empty arrays during graph hydration - Keep library revision clones independent while preserving valid guides
This commit is contained in:
parent
9ef2f63d57
commit
8e3f904ed4
|
|
@ -1,6 +1,6 @@
|
|||
import type { InstanceNodeChange } from '@open-pencil/fig/instance-overrides'
|
||||
import { SceneGraph } from '@open-pencil/scene-graph'
|
||||
import type { EnabledLibraryBinding } from '@open-pencil/scene-graph'
|
||||
import type { EnabledLibraryBinding, SceneNode } from '@open-pencil/scene-graph'
|
||||
|
||||
import { getLazyFigImportContext, setLazyFigImportContext } from '#core/kiwi/fig/lazy-import'
|
||||
import type { PortableSceneGraphData } from '#core/kiwi/fig/parse/portable-data'
|
||||
|
|
@ -112,12 +112,14 @@ export function cloneSceneGraphForFigExport(graph: SceneGraph): SceneGraph {
|
|||
return cloned
|
||||
}
|
||||
|
||||
function normalizeNodeGuides(node: SceneNode): SceneNode {
|
||||
return Array.isArray(node.guides) ? node : { ...node, guides: [] }
|
||||
}
|
||||
|
||||
export function deserializeSceneGraph(data: SerializedSceneGraph): SceneGraph {
|
||||
const graph = new SceneGraph()
|
||||
graph.rootId = data.rootId
|
||||
graph.nodes = new Map(
|
||||
data.nodes.map(([id, node]) => [id, { ...node, guides: node.guides ?? [] }])
|
||||
)
|
||||
graph.nodes = new Map(data.nodes.map(([id, node]) => [id, normalizeNodeGuides(node)]))
|
||||
graph.images = new Map(data.images)
|
||||
graph.variables = new Map(data.variables)
|
||||
graph.variableCollections = new Map(data.variableCollections)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { SceneGraph } from '@open-pencil/scene-graph'
|
||||
import type { SceneNode } from '@open-pencil/scene-graph'
|
||||
|
||||
import type { PortableSceneGraphData } from '#core/kiwi/fig/parse/portable-data'
|
||||
|
||||
|
|
@ -34,18 +35,17 @@ export function serializeLibraryRevision(
|
|||
}
|
||||
}
|
||||
|
||||
function cloneLibraryNode(node: SceneNode): SceneNode {
|
||||
const cloned = structuredClone(node)
|
||||
return Array.isArray(cloned.guides) ? cloned : { ...cloned, guides: [] }
|
||||
}
|
||||
|
||||
export function deserializeLibraryRevision(
|
||||
revision: SerializedComponentLibraryRevision
|
||||
): ComponentLibraryRevision {
|
||||
const graph = new SceneGraph()
|
||||
graph.rootId = revision.graph.rootId
|
||||
graph.nodes = new Map(
|
||||
revision.graph.nodes.map(([id, node]) => {
|
||||
const cloned = structuredClone(node)
|
||||
cloned.guides ??= []
|
||||
return [id, cloned]
|
||||
})
|
||||
)
|
||||
graph.nodes = new Map(revision.graph.nodes.map(([id, node]) => [id, cloneLibraryNode(node)]))
|
||||
graph.images = new Map(
|
||||
revision.graph.images.map(([hash, bytes]) => [hash, new Uint8Array(bytes)])
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue