AI-native design editor. Open-source Figma alternative.
fix(fig): preserve swapped instance appearance
Danila Poyarkovb61957dff9f6
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8f9ab5739..2dc46ab37 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -28,7 +28,7 @@- Keep desktop text visible across the scene and overlay canvases, refresh it after local fonts load, and preserve rendering when a requested italic face is unavailable (#395).- Honor node-scoped variable modes in `.fig` files so light and dark component examples keep their intended colors.-- Preserve nested instance text, visibility, and paint overrides across repeated children and component swaps in `.fig` files.+- Preserve nested instance text, visibility, paint, geometry, and clipping across repeated children and component swaps in `.fig` files.- Improve `.fig` import and rendering fidelity for groups, booleans, instances, rotated vectors, complex text fills, auto-sized text, layout grids, page guides, patterns, noise effects, masks, and canvas backgrounds.- Preserve pages, components, prototype and library metadata, export settings, unsupported effects, and other unrelated Figma data when editing and resaving `.fig` files.- Prevent duplicate generated IDs from corrupting `.fig` round trips.diff --git a/packages/fig/src/instance-overrides/resolve.ts b/packages/fig/src/instance-overrides/resolve.tsindex 17acc9593..0a788e108 100644--- a/packages/fig/src/instance-overrides/resolve.ts+++ b/packages/fig/src/instance-overrides/resolve.ts@@ -1,6 +1,6 @@import { guidToString } from '@open-pencil/fig/node-change'import type { GUID } from '@open-pencil/kiwi/fig/codec'-import type { SceneNode } from '@open-pencil/scene-graph'+import { copyInstanceComponentProps, type SceneNode } from '@open-pencil/scene-graph'import { copyStrokes } from '@open-pencil/scene-graph/copy'import {@@ -448,6 +448,18 @@ function componentInstanceName(ctx: OverrideContext, component: SceneNode | undereturn parent?.type === 'COMPONENT_SET' ? parent.name : component.name}+function swappedRootProps(node: SceneNode, component: SceneNode): Partial<SceneNode> {+ const props = copyInstanceComponentProps(component)+ props.width = node.width+ props.height = node.height+ props.boundVariables = { ...props.boundVariables }+ for (const field of ['width', 'height'] as const) {+ const binding = node.boundVariables[field]+ if (binding) props.boundVariables[field] = binding+ }+ return props+}+export function repopulateInstance(ctx: OverrideContext, nodeId: string, compId: string): void {const node = ctx.graph.getNode(nodeId)if (node?.type !== 'INSTANCE') return@@ -458,7 +470,9 @@ export function repopulateInstance(ctx: OverrideContext, nodeId: string, compId:const rootComp = rootCompId ? ctx.graph.getNode(rootCompId) : undefinedfor (const childId of Array.from(node.childIds)) ctx.graph.deleteNode(childId)const comp = ctx.graph.getNode(compId)- const updates: Partial<SceneNode> = { componentId: compId }+ const updates: Partial<SceneNode> = comp+ ? { ...swappedRootProps(node, comp), componentId: compId }+ : { componentId: compId }const previousName = componentInstanceName(ctx, rootComp)const nextName = componentInstanceName(ctx, comp)if (nextName && previousName && (node.name === previousName || node.name === rootComp?.name)) {diff --git a/packages/scene-graph/src/index.ts b/packages/scene-graph/src/index.tsindex 5d94cc23f..623bc59ef 100644--- a/packages/scene-graph/src/index.ts+++ b/packages/scene-graph/src/index.ts@@ -1,6 +1,7 @@/* eslint-disable max-lines -- SceneGraph exposes a stable facade over domain modules */export * from './images'export * from './copy'+export { copyInstanceComponentProps } from './instances'export * from './snap'export * from './export-scale'export * from './coordinate'diff --git a/packages/scene-graph/src/instances.ts b/packages/scene-graph/src/instances.tsindex d29665ac8..ad059a626 100644--- a/packages/scene-graph/src/instances.ts+++ b/packages/scene-graph/src/instances.ts@@ -176,6 +176,12 @@ function syncChildren(})}+export function copyInstanceComponentProps(component: SceneNode): Partial<SceneNode> {+ const props: Partial<SceneNode> = {}+ for (const key of INSTANCE_SYNC_PROPS) copyProp(props, component, key)+ return props+}+export function createInstance(graph: SceneGraph,componentId: string,@@ -185,9 +191,10 @@ export function createInstance(const component = graph.nodes.get(componentId)if (component?.type !== 'COMPONENT') return null- const props: Partial<SceneNode> = { name: component.name, componentId }- for (const key of INSTANCE_SYNC_PROPS) {- copyProp(props, component, key)+ const props: Partial<SceneNode> = {+ ...copyInstanceComponentProps(component),+ name: component.name,+ componentId}const instance = graph.createNode('INSTANCE', parentId, { ...props, ...overrides })diff --git a/tests/engine/io/fig/import/edge-cases.test.ts b/tests/engine/io/fig/import/edge-cases.test.tsindex 98c2a831f..b705f1003 100644--- a/tests/engine/io/fig/import/edge-cases.test.ts+++ b/tests/engine/io/fig/import/edge-cases.test.ts@@ -228,7 +228,9 @@ describe('edge cases', () => {type: 'SYMBOL',name: 'IconB',phase: 'CREATED',- size: { x: 24, y: 24 },+ size: { x: 40, y: 40 },+ cornerRadius: 12,+ frameMaskDisabled: false,transform: { m00: 1, m01: 0, m02: 0, m10: 0, m11: 1, m12: 0 }} as NodeChange,{@@ -383,6 +385,10 @@ describe('edge cases', () => {const iconClone = graph.getChildren(buttonChildren[1].id)[0]expect(iconClone).toBeDefined()expect(iconClone.name).toBe('icon')+ expect(iconClone.width).toBe(24)+ expect(iconClone.height).toBe(24)+ expect(iconClone.cornerRadius).toBe(12)+ expect(iconClone.clipsContent).toBe(true)// Icon should have IconB's children, not IconA's child. The later text// override must resolve through the same path prefix after the swap.