diff --git a/packages/core/src/kiwi/fig/parse/transfer.ts b/packages/core/src/kiwi/fig/parse/transfer.ts index 8e1283434..3a2718373 100644 --- a/packages/core/src/kiwi/fig/parse/transfer.ts +++ b/packages/core/src/kiwi/fig/parse/transfer.ts @@ -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) diff --git a/packages/core/src/library/serialization.ts b/packages/core/src/library/serialization.ts index ab0415de9..a11f05d98 100644 --- a/packages/core/src/library/serialization.ts +++ b/packages/core/src/library/serialization.ts @@ -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)]) )