Skip to content

AI-native design editor. Open-source Figma alternative.

fix(instances): preserve ambiguous imported extras

Danila Poyarkovf8f5f0fc4eb0
diff --git a/packages/fig/src/node-change/index.ts b/packages/fig/src/node-change/index.ts
index 3c347ccb0..68841c7cb 100644
--- a/packages/fig/src/node-change/index.ts
+++ b/packages/fig/src/node-change/index.ts
@@ -7,6 +7,7 @@ export * from './export-node'
export * from './export-runtime'
export * from './font/features'
export * from './font/style'
+export * from './font/variations'
export * from './instance-linkage'
export * from './paint'
export * from './path/commands'
diff --git a/packages/scene-graph/src/instances.ts b/packages/scene-graph/src/instances.ts
index d238824da..d4bdd0cdc 100644
--- a/packages/scene-graph/src/instances.ts
+++ b/packages/scene-graph/src/instances.ts
@@ -151,6 +151,14 @@ function matchFallbackChildren(
usedInstChildIds: Set<string>
): void {
const fallbackByType = new Map<SceneNode['type'], Map<string, SceneNode[]>>()
+ const unmatchedComponentCount = new Map<SceneNode['type'], number>()
+ for (const compChildId of compParent.childIds) {
+ if (instChildMap.has(compChildId)) continue
+ const child = graph.nodes.get(compChildId)
+ if (child) {
+ unmatchedComponentCount.set(child.type, (unmatchedComponentCount.get(child.type) ?? 0) + 1)
+ }
+ }
for (const childId of instParent.childIds) {
const child = graph.nodes.get(childId)
if (!child || usedInstChildIds.has(child.id)) continue
@@ -169,7 +177,13 @@ function matchFallbackChildren(
if (instChildMap.has(compChildId)) continue
const compChild = graph.nodes.get(compChildId)
if (!compChild) continue
- const match = fallbackByType.get(compChild.type)?.get(compChild.name)?.shift()
+ const candidatesByName = fallbackByType.get(compChild.type)
+ const candidateCount = [...(candidatesByName?.values() ?? [])].reduce(
+ (count, queue) => count + queue.length,
+ 0
+ )
+ if (candidateCount > (unmatchedComponentCount.get(compChild.type) ?? 0)) continue
+ const match = candidatesByName?.get(compChild.name)?.shift()
if (!match) continue
instChildMap.set(compChildId, match)
usedInstChildIds.add(match.id)
diff --git a/tests/engine/editor/clipboard/import-nodes/paste-scope.test.ts b/tests/engine/editor/clipboard/import-nodes/paste-scope.test.ts
index e79321c38..3f5a6b14c 100644
--- a/tests/engine/editor/clipboard/import-nodes/paste-scope.test.ts
+++ b/tests/engine/editor/clipboard/import-nodes/paste-scope.test.ts
@@ -72,14 +72,16 @@ describe('importClipboardNodes: paste linkage scoping', () => {
expect(inst.childIds[0]).toBe(badge.id)
expect(inst.childIds[1]).toBe(instItem1.id)
- // A later sync links instItem1 by name+type (fallback) and leaves badge alone.
+ // A later sync preserves both ambiguous same-type children and clones the
+ // missing component child rather than risking data loss.
graph.syncInstances(comp.id)
expect(badge.name).toBe('Badge')
expect(badge.componentId).toBeNull()
- expect(instItem1.componentId).toBe(compItem1.id)
- expect(inst.childIds.length).toBe(2) // no duplication
- expect(inst.childIds[0]).toBe(instItem1.id) // mapped sorts first
- expect(inst.childIds[1]).toBe(badge.id) // unmapped extra sorts last
+ expect(instItem1.componentId).toBeNull()
+ const mapped = graph.getChildren(inst.id).filter((child) => child.componentId === compItem1.id)
+ expect(mapped).toHaveLength(1)
+ expect(inst.childIds.length).toBe(3)
+ expect(inst.childIds).toEqual(expect.arrayContaining([badge.id, instItem1.id, mapped[0].id]))
})
it('links pasted instances but not pre-existing instances in the same paste', () => {
diff --git a/tests/engine/scene-graph/instance-sync-duplication.test.ts b/tests/engine/scene-graph/instance-sync-duplication.test.ts
index 94ebabadb..abe5157c9 100644
--- a/tests/engine/scene-graph/instance-sync-duplication.test.ts
+++ b/tests/engine/scene-graph/instance-sync-duplication.test.ts
@@ -261,6 +261,29 @@ describe('instance synchronization regressions from review triage', () => {
expect(labelB.text).toBe('B-changed')
})
+ test('same-name extra beside a renamed imported child remains untouched', () => {
+ const graph = new SceneGraph()
+ const page = graph.addPage('Page')
+ const component = graph.createNode('COMPONENT', page.id, { name: 'List' })
+ const row = graph.createNode('FRAME', component.id, { name: 'Row', width: 100 })
+ const instance = graph.createNode('INSTANCE', page.id, {
+ name: 'List',
+ componentId: component.id
+ })
+ const extra = graph.createNode('FRAME', instance.id, { name: 'Row', width: 20 })
+ const renamed = graph.createNode('FRAME', instance.id, { name: 'Row v2', width: 100 })
+
+ graph.syncInstances(component.id)
+
+ expect(extra.width).toBe(20)
+ expect(extra.componentId).toBeNull()
+ expect(renamed.name).toBe('Row v2')
+ expect(renamed.componentId).toBeNull()
+ const mapped = graph.getChildren(instance.id).filter((child) => child.componentId === row.id)
+ expect(mapped).toHaveLength(1)
+ expect(mapped[0]?.id).not.toBe(extra.id)
+ })
+
// V2+V3: extra unmatched instance child is not co-opted by positional or type-only fallback.
test('extra unmatched instance child is not co-opted or renamed by fallback matching', () => {
const graph = new SceneGraph()