Skip to content

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

fix(scene-graph): avoid id collisions after import

Danila Poyarkovf25e5cfe0a1f
diff --git a/packages/core/src/editor/components/instances.ts b/packages/core/src/editor/components/instances.ts
index 23f699c03..97a5cdadb 100644
--- a/packages/core/src/editor/components/instances.ts
+++ b/packages/core/src/editor/components/instances.ts
@@ -18,6 +18,7 @@ export function createComponentInstanceActions(ctx: EditorContext) {
const component = ctx.graph.getNode(componentId)
if (component?.type !== 'COMPONENT') return null
+ const previousSelection = new Set(ctx.state.selectedIds)
const instance = ctx.graph.createInstance(componentId, parentId, {
x: x ?? component.x + component.width + 40,
y: y ?? component.y
@@ -36,7 +37,7 @@ export function createComponentInstanceActions(ctx: EditorContext) {
},
inverse: () => {
ctx.graph.deleteNode(instanceId)
- ctx.setSelectedIds(new Set([componentId]))
+ ctx.setSelectedIds(new Set(previousSelection))
}
})
return instanceId
diff --git a/packages/core/src/scene-graph/index.ts b/packages/core/src/scene-graph/index.ts
index d9df54aac..0b9e8d778 100644
--- a/packages/core/src/scene-graph/index.ts
+++ b/packages/core/src/scene-graph/index.ts
@@ -257,8 +257,14 @@ export class SceneGraph {
}
}
+ private generateNodeId(): string {
+ let id = generateId()
+ while (this.nodes.has(id)) id = generateId()
+ return id
+ }
+
createNode(type: NodeType, parentId: string, overrides: Partial<SceneNode> = {}): SceneNode {
- const node = createDefaultNode(generateId, type, overrides)
+ const node = createDefaultNode(() => this.generateNodeId(), type, overrides)
node.parentId = parentId
this.nodes.set(node.id, node)
diff --git a/tests/engine/editor/undo/create-instance.test.ts b/tests/engine/editor/undo/create-instance.test.ts
new file mode 100644
index 000000000..14358d569
--- /dev/null
+++ b/tests/engine/editor/undo/create-instance.test.ts
@@ -0,0 +1,32 @@
+import { describe, expect, test } from 'bun:test'
+
+import { createEditor } from '@open-pencil/core/editor'
+
+import { expectDefined } from '#tests/helpers/assert'
+
+describe('create instance undo/redo', () => {
+ test('undo restores the previous selection instead of selecting the source component', () => {
+ const editor = createEditor()
+ const pageId = editor.state.currentPageId
+ const component = editor.graph.createNode('COMPONENT', pageId, {
+ name: 'Library Button',
+ width: 100,
+ height: 40,
+ internalOnly: true
+ })
+ const previous = editor.graph.createNode('RECTANGLE', pageId, { name: 'Previous selection' })
+
+ editor.select([previous.id])
+ const instanceId = expectDefined(
+ editor.createInstanceFromComponent(component.id, 200, 100, pageId),
+ 'instanceId'
+ )
+
+ expect([...editor.state.selectedIds]).toEqual([instanceId])
+
+ editor.undo.undo()
+
+ expect(editor.graph.getNode(instanceId)).toBeUndefined()
+ expect([...editor.state.selectedIds]).toEqual([previous.id])
+ })
+})
diff --git a/tests/engine/scene-graph/basic/graph.test.ts b/tests/engine/scene-graph/basic/graph.test.ts
index 019fe3689..625d78fed 100644
--- a/tests/engine/scene-graph/basic/graph.test.ts
+++ b/tests/engine/scene-graph/basic/graph.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, test } from 'bun:test'
-import { SceneGraph } from '@open-pencil/core'
+import { generateId, SceneGraph } from '@open-pencil/core'
import { expectDefined } from '#tests/helpers/assert'
@@ -17,6 +17,25 @@ describe('SceneGraph', () => {
expect(node.width).toBe(200)
})
+ test('create node skips imported ids that collide with the local generator', () => {
+ const graph = new SceneGraph()
+ const page = pageId(graph)
+ const probe = generateId()
+ const nextLocalId = `0:${Number(probe.split(':')[1]) + 1}`
+ const imported = graph.createNode('RECTANGLE', page, {
+ id: nextLocalId,
+ name: 'Imported node'
+ })
+ const countBefore = graph.nodes.size
+
+ const created = graph.createNode('RECTANGLE', page, { name: 'Fresh node' })
+
+ expect(created.id).not.toBe(imported.id)
+ expect(graph.nodes.size).toBe(countBefore + 1)
+ expect(graph.getNode(imported.id)?.name).toBe('Imported node')
+ expect(graph.getNode(created.id)?.name).toBe('Fresh node')
+ })
+
test('create and delete', () => {
const graph = new SceneGraph()
const id = rect(graph, 'R')