Skip to content

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

fix: persist imported FIG coordinates after reparenting (#1586)

fikyair6ade38db49a5
diff --git a/changes/1586/fixed.md b/changes/1586/fixed.md
new file mode 100644
index 000000000..2b1f7dc62
--- /dev/null
+++ b/changes/1586/fixed.md
@@ -0,0 +1 @@
+- Layers from an opened `.fig` file that are grouped or moved into a frame keep their place after the file is saved and reopened.
diff --git a/packages/core/tests/io/formats/fig/roundtrip/source-metadata.test.ts b/packages/core/tests/io/formats/fig/roundtrip/source-metadata.test.ts
index 7e6d36a5e..b73e0b657 100644
--- a/packages/core/tests/io/formats/fig/roundtrip/source-metadata.test.ts
+++ b/packages/core/tests/io/formats/fig/roundtrip/source-metadata.test.ts
@@ -177,6 +177,50 @@ describe('fig roundtrip source metadata', () => {
}
})
+ test('exports current child coordinates after reparenting imported nodes', async () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const child = graph.createNode('RECTANGLE', page.id, {
+ name: 'Reparented imported rect',
+ x: 31,
+ y: 282,
+ width: 40,
+ height: 40
+ })
+ child.source.format = 'fig'
+ child.source.id = '4:508'
+ child.source.fig.rawTransform = {
+ m00: 1,
+ m01: 0,
+ m02: 31,
+ m10: 0,
+ m11: 1,
+ m12: 282
+ }
+
+ const group = graph.createNode('GROUP', page.id, {
+ name: 'New group',
+ x: 20,
+ y: 200,
+ width: 100,
+ height: 100
+ })
+ graph.reparentNode(child.id, group.id)
+
+ expect(child.x).toBe(11)
+ expect(child.y).toBe(82)
+ expect(child.source.editedFields).toEqual(['x', 'y'])
+
+ const reopened = await parseFigFile((await exportFigFile(graph)).slice().buffer)
+ const exportedChild = [...reopened.getAllNodes()].find((node) => node.name === child.name)
+
+ expect(exportedChild?.parentId).toBe(
+ [...reopened.getAllNodes()].find((node) => node.name === group.name)?.id
+ )
+ expect(exportedChild?.x).toBe(11)
+ expect(exportedChild?.y).toBe(82)
+ })
+
test('does not restore cleared imported size constraints from raw metadata', async () => {
const graph = new SceneGraph()
const page = graph.getPages()[0]
diff --git a/packages/scene-graph/src/graph.ts b/packages/scene-graph/src/graph.ts
index e1c339e9c..d01621bcb 100644
--- a/packages/scene-graph/src/graph.ts
+++ b/packages/scene-graph/src/graph.ts
@@ -1,5 +1,5 @@
/* eslint-disable max-lines -- SceneGraph is the facade over the domain modules */
-import { omitBy } from 'es-toolkit/object'
+import { omitBy, pick } from 'es-toolkit/object'
import { isUndefined } from 'es-toolkit/predicate'
import { removeStaleBindings } from './bindings'
@@ -13,6 +13,7 @@ import {
} from './coordinate'
import { cloneNodeProps, type NodeCloneMode } from './copy'
import { bindNodeEvents } from './events'
+import { TRANSFORM_FIELDS } from './fields/geometry'
import { LAYOUT_AFFECTING_KEYS } from './fields/layout'
import { withGeneratedFrameSize } from './generated'
import * as HitTest from './hit-test'
@@ -576,9 +577,7 @@ export class SceneGraph {
if (changes.componentId) this.indexInstance(changes.componentId, id)
}
if (node.type === 'TEXT') invalidateTextCaches(node, changes)
- if (this.scopeDepths.sourceMetadata === 0 && !this.isApplyingLayout) {
- markSourceFieldsEdited(node, Object.keys(changes))
- }
+ this.markEdited(node, Object.keys(changes))
if (changes.vectorNetwork) {
changes = { ...changes, vectorNetwork: normalizeVectorNetwork(changes.vectorNetwork) }
}
@@ -594,6 +593,13 @@ export class SceneGraph {
this.publishNodeUpdate(id, changes)
}
+ /** Marks fields an edit changed, unless metadata is being restored or layout is writing them. */
+ private markEdited(node: SceneNode, keys: string[]): void {
+ if (this.scopeDepths.sourceMetadata === 0 && !this.isApplyingLayout) {
+ markSourceFieldsEdited(node, keys)
+ }
+ }
+
/** Publishes an update now, or leaves it to the running layout, which publishes once it ends. */
private publishNodeUpdate(id: string, changes: Partial<SceneNode>): void {
if (!this.layoutWrites) this.emitter.emit('node:updated', id, changes)
@@ -625,6 +631,7 @@ export class SceneGraph {
node.parentId = newParentId
newParent.childIds.push(nodeId)
+ const before = pick(node, TRANSFORM_FIELDS)
if (isTranslationOnly(oldParentWorld) && isTranslationOnly(newParentWorld)) {
node.x += oldParentWorld[2] - newParentWorld[2]
node.y += oldParentWorld[5] - newParentWorld[5]
@@ -633,6 +640,11 @@ export class SceneGraph {
const local = localTransformFromWorld(node, world, newParentWorld)
if (local) Object.assign(node, local)
}
+ // The node keeps its place on the canvas by new coordinates, which a save must write.
+ this.markEdited(
+ node,
+ TRANSFORM_FIELDS.filter((key) => node[key] !== before[key])
+ )
this.emitter.emit('node:reparented', nodeId, oldParentId, newParentId)
}
diff --git a/packages/scene-graph/tests/reparent-transform.test.ts b/packages/scene-graph/tests/reparent-transform.test.ts
index 04b6adb17..148c863a7 100644
--- a/packages/scene-graph/tests/reparent-transform.test.ts
+++ b/packages/scene-graph/tests/reparent-transform.test.ts
@@ -109,3 +109,24 @@ describe('reparentNode keeps the node where it is drawn', () => {
expect(node?.rotation).toBe(45)
})
})
+
+// A save writes the fields an edit changed and keeps the rest as the file had them.
+describe('reparentNode records what it changes', () => {
+ test('marks the coordinates it moved', () => {
+ const { graph, pageId } = setup()
+ const frame = graph.createNode('FRAME', pageId, { x: 20, y: 200, width: 100, height: 100 })
+ const child = graph.createNode('RECTANGLE', pageId, { x: 31, y: 282, width: 10, height: 10 })
+ graph.reparentNode(child.id, frame.id)
+ expect(child.source.editedFields).toEqual(['x', 'y'])
+ })
+
+ test('marks nothing when the new parent sits where the old one did', () => {
+ const { graph, pageId } = setup()
+ const first = graph.createNode('FRAME', pageId, { x: 20, y: 20, width: 100, height: 100 })
+ const second = graph.createNode('FRAME', pageId, { x: 20, y: 20, width: 100, height: 100 })
+ const child = graph.createNode('RECTANGLE', first.id, { x: 5, y: 5, width: 10, height: 10 })
+ child.source.editedFields = []
+ graph.reparentNode(child.id, second.id)
+ expect(child.source.editedFields).toEqual([])
+ })
+})