AI-native design editor. Open-source Figma alternative.
fix(fig): keep a moved component under its new parent when slot content loads (#1247)
Niraj Sangani706c87f707b3
diff --git a/changes/1247/fixed.md b/changes/1247/fixed.mdnew file mode 100644index 000000000..56c58c481--- /dev/null+++ b/changes/1247/fixed.md@@ -0,0 +1 @@+- Saving a `.fig` after moving a component that has an instance in slot content no longer writes the component twice, so the file opens again.diff --git a/packages/core/tests/io/formats/fig/roundtrip/moved-component.test.ts b/packages/core/tests/io/formats/fig/roundtrip/moved-component.test.tsnew file mode 100644index 000000000..feb21615d--- /dev/null+++ b/packages/core/tests/io/formats/fig/roundtrip/moved-component.test.ts@@ -0,0 +1,63 @@+import { expect, test } from 'bun:test'++import { expectDefined } from '#core-tests/helpers/assert'++import { exportFigFile, parseFigFile, populateAllFigPages } from '@open-pencil/core/io/formats/fig'+import { parseFigBuffer } from '@open-pencil/fig'+import { initCodec } from '@open-pencil/kiwi/fig/codec'+import { guidToString } from '@open-pencil/kiwi/fig/guid'+import { SceneGraph } from '@open-pencil/scene-graph'+import { slotPropertyId } from '@open-pencil/scene-graph/slots'++/** An Icon component, and a Card instance whose slot holds an instance of Icon. */+async function savedDocument(): Promise<Uint8Array> {+ await initCodec()+ const graph = new SceneGraph()+ const pageId = graph.getPages()[0].id+ const icon = graph.createNode('COMPONENT', pageId, { name: 'Icon' })+ const card = graph.createNode('COMPONENT', pageId, {+ name: 'Card',+ componentPropertyDefinitions: [+ { id: 'card:body', name: 'Body', type: 'SLOT', defaultValue: '' }+ ]+ })+ const body = graph.createNode('FRAME', card.id, {+ name: 'Body',+ componentPropertyReferences: [{ propertyId: 'card:body', field: 'SLOT_CONTENT' }]+ })+ graph.createNode('TEXT', body.id, { name: 'Default', text: 'Default' })+ const instance = expectDefined(graph.createInstance(card.id, pageId))+ const slot = expectDefined(graph.getChildren(instance.id).find((child) => slotPropertyId(child)))+ expectDefined(graph.createInstance(icon.id, slot.id))+ graph.updateNode(instance.id, { componentPropertyAssignments: { 'card:body': '' } })+ return exportFigFile(graph)+}++test('a component moved to another parent is saved once when an instance of it sits in slot content', async () => {+ const graph = await parseFigFile((await savedDocument()).slice().buffer)+ populateAllFigPages(graph)+ const icon = expectDefined(+ [...graph.nodes.values()].find((node) => node.type === 'COMPONENT' && node.name === 'Icon')+ )+ const frame = graph.createNode('FRAME', graph.getPages()[0].id, { name: 'Moved to' })+ graph.reparentNode(icon.id, frame.id)++ const saved = await exportFigFile(graph)+ const records = parseFigBuffer(saved.slice().buffer).nodeChanges+ const guids = records.flatMap((record) => (record.guid ? [guidToString(record.guid)] : []))+ expect(guids.filter((guid, index) => guids.indexOf(guid) !== index)).toEqual([])+ const record = expectDefined(+ records.find((candidate) => candidate.guid && guidToString(candidate.guid) === icon.id)+ )+ expect(guidToString(expectDefined(record.parentIndex?.guid))).toBe(frame.id)++ const reopened = await parseFigFile(saved.slice().buffer)+ populateAllFigPages(reopened)+ const moved = expectDefined(+ [...reopened.nodes.values()].find((node) => node.type === 'COMPONENT' && node.name === 'Icon')+ )+ expect(reopened.getNode(expectDefined(moved.parentId))?.name).toBe('Moved to')+ expect(reopened.getChildren(reopened.getPages()[0].id).map((node) => node.name)).not.toContain(+ 'Icon'+ )+})diff --git a/packages/fig/src/document/materialize.ts b/packages/fig/src/document/materialize.tsindex 0029e05e4..ab14e427e 100644--- a/packages/fig/src/document/materialize.ts+++ b/packages/fig/src/document/materialize.ts@@ -496,7 +496,8 @@ function materializeReaderInto(populateInstances(child)const id = sources.get(child.sourceId)if (!id) throw new Error(`Missing assembled child ${child.sourceId}`)- ordered.push(id)+ // A layer moved live to another parent stays there; this only orders what the parent holds.+ if (graph.getNode(id)?.parentId === parentId) ordered.push(id)}const parent = graph.getNode(parentId)if (parent)