Skip to content

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

fix(clipboard): preserve component dependency references

Danila Poyarkov4b47ffefbbb8
diff --git a/packages/core/src/editor/clipboard.ts b/packages/core/src/editor/clipboard.ts
index ea22fa7fe..9391e8f9a 100644
--- a/packages/core/src/editor/clipboard.ts
+++ b/packages/core/src/editor/clipboard.ts
@@ -119,7 +119,7 @@ export function createClipboardActions(ctx: EditorContext) {
created = pasteOpenPencilNodes(
dependencies.nodes,
snapshot.images,
- snapshot.componentDependencies,
+ dependencies.componentDependencies,
cursorPos,
options
)
diff --git a/packages/core/src/editor/clipboard/dependencies.ts b/packages/core/src/editor/clipboard/dependencies.ts
index d1b4ca9b3..464dd16fe 100644
--- a/packages/core/src/editor/clipboard/dependencies.ts
+++ b/packages/core/src/editor/clipboard/dependencies.ts
@@ -7,6 +7,7 @@ import { importClipboardVariables, remapClipboardVariableBindings } from './vari
export interface ClipboardDependencyImport {
nodes: ClipboardSnapshot['nodes']
+ componentDependencies: ClipboardSnapshot['componentDependencies']
styleSnapshots: SceneNode[]
applyVariables?: () => void
revertVariables?: () => void
@@ -17,7 +18,14 @@ export function importClipboardDependencies(
snapshot: ClipboardSnapshot
): ClipboardDependencyImport {
const nodes = structuredClone(snapshot.nodes)
- if (snapshot.sourceRootId === ctx.graph.rootId) return { nodes, styleSnapshots: [] }
+ const componentDependencies = structuredClone(snapshot.componentDependencies)
+ if (snapshot.sourceRootId === ctx.graph.rootId) {
+ return {
+ nodes,
+ componentDependencies: componentDependencies.filter((node) => !ctx.graph.getNode(node.id)),
+ styleSnapshots: []
+ }
+ }
const styleIds = new Map<string, string>()
const styleSnapshots: SceneNode[] = []
for (const definition of snapshot.styleDefinitions) {
@@ -49,10 +57,11 @@ export function importClipboardDependencies(
)
for (const child of node.children ?? []) remap(child)
}
- for (const node of nodes) remap(node)
+ for (const node of [...nodes, ...componentDependencies]) remap(node)
variables.apply()
return {
nodes,
+ componentDependencies,
styleSnapshots,
applyVariables: variables.apply,
revertVariables: variables.revert
diff --git a/tests/engine/app/clipboard/dependency-bindings.test.ts b/tests/engine/app/clipboard/dependency-bindings.test.ts
new file mode 100644
index 000000000..acab10833
--- /dev/null
+++ b/tests/engine/app/clipboard/dependency-bindings.test.ts
@@ -0,0 +1,78 @@
+import { expect, test } from 'bun:test'
+
+import { createEditorStore } from '@/app/editor/session/create'
+
+test('same-document instance paste retains its existing component', async () => {
+ const editor = createEditorStore()
+ const component = editor.graph.createNode('COMPONENT', editor.state.currentPageId)
+ const instance = editor.graph.createNode('INSTANCE', editor.state.currentPageId, {
+ componentId: component.id
+ })
+ editor.select([instance.id])
+ const { snapshot } = await editor.prepareCopy()
+ if (!snapshot) throw new Error('Missing snapshot')
+ await editor.pasteSnapshot(snapshot)
+ const pasted = editor.graph.getNode([...editor.state.selectedIds][0])
+ expect(pasted?.componentId).toBe(component.id)
+ if (!pasted) throw new Error('Missing pasted instance')
+ editor.undo.undo()
+ expect(editor.graph.getNode(pasted.id)).toBeUndefined()
+ expect(editor.graph.getNode(component.id)).toBe(component)
+ editor.undo.redo()
+ expect(editor.graph.getNode(pasted.id)?.componentId).toBe(component.id)
+})
+
+test('cross-document component dependency bindings resolve in the target', async () => {
+ const source = createEditorStore()
+ const collection = source.graph.createCollection('Spacing')
+ const variable = source.graph.createVariable('Radius', 'FLOAT', collection.id, 12)
+ const component = source.graph.createNode('COMPONENT', source.state.currentPageId, {
+ boundVariables: { cornerRadius: variable.id }
+ })
+ source.graph.createNode('RECTANGLE', component.id, {
+ boundVariables: { cornerRadius: variable.id }
+ })
+ const instance = source.graph.createNode('INSTANCE', source.state.currentPageId, {
+ componentId: component.id
+ })
+ source.select([instance.id])
+ const { snapshot } = await source.prepareCopy()
+ if (!snapshot) throw new Error('Missing snapshot')
+ const target = createEditorStore()
+ await target.pasteSnapshot(snapshot)
+ const pasted = target.graph.getNode([...target.state.selectedIds][0])
+ const dependency = target.graph.getNode(pasted?.componentId ?? '')
+ expect(target.graph.variables.has(dependency?.boundVariables.cornerRadius ?? '')).toBe(true)
+ if (!pasted || !dependency) throw new Error('Missing imported instance or component')
+ const child = target.graph.getNode(dependency.childIds[0])
+ expect(child?.boundVariables.cornerRadius).toBe(dependency.boundVariables.cornerRadius)
+ expect(snapshot.componentDependencies[0].boundVariables.cornerRadius).toBe(variable.id)
+ target.undo.undo()
+ expect(target.graph.getNode(dependency.id)).toBeUndefined()
+ expect(target.graph.variables.size).toBe(0)
+ target.undo.redo()
+ expect(target.graph.getNode(pasted.id)?.componentId).toBe(dependency.id)
+ expect(target.graph.variables.has(dependency.boundVariables.cornerRadius)).toBe(true)
+})
+
+test('same-document paste restores a component deleted after copying', async () => {
+ const editor = createEditorStore()
+ const component = editor.graph.createNode('COMPONENT', editor.state.currentPageId)
+ const instance = editor.graph.createNode('INSTANCE', editor.state.currentPageId, {
+ componentId: component.id
+ })
+ editor.select([instance.id])
+ const { snapshot } = await editor.prepareCopy()
+ if (!snapshot) throw new Error('Missing snapshot')
+ editor.graph.deleteNode(component.id)
+ await editor.pasteSnapshot(snapshot)
+ const pasted = editor.graph.getNode([...editor.state.selectedIds][0])
+ const restored = editor.graph.getNode(pasted?.componentId ?? '')
+ expect(restored?.type).toBe('COMPONENT')
+ expect(restored?.id).not.toBe(component.id)
+ if (!restored) throw new Error('Missing restored component')
+ editor.undo.undo()
+ expect(editor.graph.getNode(restored.id)).toBeUndefined()
+ editor.undo.redo()
+ expect(editor.graph.getNode(restored.id)?.type).toBe('COMPONENT')
+})
diff --git a/tests/engine/app/clipboard/styles.test.ts b/tests/engine/app/clipboard/styles.test.ts
index b3dd997e0..870146f3c 100644
--- a/tests/engine/app/clipboard/styles.test.ts
+++ b/tests/engine/app/clipboard/styles.test.ts
@@ -2,42 +2,53 @@ import { expect, test } from 'bun:test'
import { createEditorStore } from '@/app/editor/session/create'
-test('cross-document paste imports and remaps referenced shared styles', async () => {
- const source = createEditorStore()
- const definition = source.graph.createNode('RECTANGLE', source.state.currentPageId, {
- name: 'Brand/Primary',
- sharedStyleType: 'FILL',
- internalOnly: true
- })
- source.graph.preserveSourceMetadataDuring(() =>
- source.graph.updateNode(definition.id, {
- source: { ...definition.source, format: 'fig', id: 'source:style' }
+test.each(['selection', 'component dependency'] as const)(
+ 'cross-document paste imports and remaps styles on %s',
+ async (location) => {
+ const source = createEditorStore()
+ const definition = source.graph.createNode('RECTANGLE', source.state.currentPageId, {
+ name: 'Brand/Primary',
+ sharedStyleType: 'FILL',
+ internalOnly: true
})
- )
- const node = source.graph.createNode('RECTANGLE', source.state.currentPageId, {
- fillStyleId: 'source:style'
- })
- source.select([node.id])
- const payload = await source.prepareCopy()
- if (!payload.snapshot) throw new Error('Missing snapshot')
- const target = createEditorStore()
- await target.pasteSnapshot(payload.snapshot)
- const pastedId = [...target.state.selectedIds][0]
- const styleId = target.graph.getNode(pastedId)?.fillStyleId
- expect(styleId).toBeString()
- expect(styleId).not.toBe('source:style')
- const imported = [...target.graph.getAllNodes()].find(
- (candidate) => candidate.source.id === styleId
- )
- expect(imported?.name).toBe('Brand/Primary')
- target.undo.undo()
- expect(target.graph.getNode(pastedId)).toBeUndefined()
- expect([...target.graph.getAllNodes()].some((candidate) => candidate.source.id === styleId)).toBe(
- false
- )
- target.undo.redo()
- expect(target.graph.getNode(pastedId)?.fillStyleId).toBe(styleId)
- expect([...target.graph.getAllNodes()].some((candidate) => candidate.source.id === styleId)).toBe(
- true
- )
-})
+ source.graph.preserveSourceMetadataDuring(() =>
+ source.graph.updateNode(definition.id, {
+ source: { ...definition.source, format: 'fig', id: 'source:style' }
+ })
+ )
+ const node = source.graph.createNode(
+ location === 'selection' ? 'RECTANGLE' : 'COMPONENT',
+ source.state.currentPageId,
+ { fillStyleId: 'source:style' }
+ )
+ const selection =
+ location === 'selection'
+ ? node
+ : source.graph.createNode('INSTANCE', source.state.currentPageId, { componentId: node.id })
+ source.select([selection.id])
+ const payload = await source.prepareCopy()
+ if (!payload.snapshot) throw new Error('Missing snapshot')
+ const target = createEditorStore()
+ await target.pasteSnapshot(payload.snapshot)
+ const pastedId = [...target.state.selectedIds][0]
+ const pasted = target.graph.getNode(pastedId)
+ const styledId = location === 'selection' ? pastedId : (pasted?.componentId ?? '')
+ const styleId = target.graph.getNode(styledId)?.fillStyleId
+ expect(styleId).toBeString()
+ expect(styleId).not.toBe('source:style')
+ const imported = [...target.graph.getAllNodes()].find(
+ (candidate) => candidate.source.id === styleId
+ )
+ expect(imported?.name).toBe('Brand/Primary')
+ target.undo.undo()
+ expect(target.graph.getNode(pastedId)).toBeUndefined()
+ expect(
+ [...target.graph.getAllNodes()].some((candidate) => candidate.source.id === styleId)
+ ).toBe(false)
+ target.undo.redo()
+ expect(target.graph.getNode(styledId)?.fillStyleId).toBe(styleId)
+ expect(
+ [...target.graph.getAllNodes()].some((candidate) => candidate.source.id === styleId)
+ ).toBe(true)
+ }
+)