Skip to content

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

fix(editor): preserve mutation parents and font layout

Danila Poyarkov75d555900f30
diff --git a/packages/core/src/editor/graph-events.ts b/packages/core/src/editor/graph-events.ts
index 56460989d..2f7aa2c0c 100644
--- a/packages/core/src/editor/graph-events.ts
+++ b/packages/core/src/editor/graph-events.ts
@@ -120,8 +120,8 @@ export function createGraphEventSubscription(options: GraphEventOptions) {
options.emitEditorEvent('node:reparented', nodeId, oldParentId, newParentId)
onNodeStructureChanged(nodeId)
},
- reordered: (nodeId, parentId, index) => {
- options.emitEditorEvent('node:reordered', nodeId, parentId, index)
+ reordered: (nodeId, parentId, index, previousParentId) => {
+ options.emitEditorEvent('node:reordered', nodeId, parentId, index, previousParentId)
onNodeStructureChanged(nodeId)
}
})
diff --git a/packages/scene-graph/src/index.ts b/packages/scene-graph/src/index.ts
index 0afcf3693..e2c18371a 100644
--- a/packages/scene-graph/src/index.ts
+++ b/packages/scene-graph/src/index.ts
@@ -494,7 +494,8 @@ export class SceneGraph {
const node = this.nodes.get(nodeId)
if (!node) return
- const oldParent = node.parentId ? this.nodes.get(node.parentId) : undefined
+ const previousParentId = node.parentId
+ const oldParent = previousParentId ? this.nodes.get(previousParentId) : undefined
const newParent = this.nodes.get(parentId)
if (!newParent || this.isDescendant(parentId, nodeId)) return
@@ -517,11 +518,13 @@ export class SceneGraph {
idx = Math.min(idx, newParent.childIds.length)
newParent.childIds.splice(idx, 0, nodeId)
- this.emitter.emit('node:reordered', nodeId, parentId, idx)
+ this.emitter.emit('node:reordered', nodeId, parentId, idx, previousParentId)
}
insertChildAt(childId: string, parentId: string, index: number): void {
- const oldParent = this.getNode(this.getNode(childId)?.parentId ?? '')
+ const node = this.getNode(childId)
+ const previousParentId = node?.parentId ?? null
+ const oldParent = previousParentId ? this.getNode(previousParentId) : undefined
if (oldParent) {
oldParent.childIds = oldParent.childIds.filter((id) => id !== childId)
}
@@ -529,10 +532,9 @@ export class SceneGraph {
if (!newParent) return
newParent.childIds = newParent.childIds.filter((id) => id !== childId)
newParent.childIds.splice(index, 0, childId)
- const node = this.getNode(childId)
if (node) node.parentId = parentId
this.clearAbsPosCache()
- this.emitter.emit('node:reordered', childId, parentId, index)
+ this.emitter.emit('node:reordered', childId, parentId, index, previousParentId)
}
deleteNode(id: string): void {
diff --git a/packages/scene-graph/src/mutation-impact.test.ts b/packages/scene-graph/src/mutation-impact.test.ts
index 6c720b82b..e5efe9e5c 100644
--- a/packages/scene-graph/src/mutation-impact.test.ts
+++ b/packages/scene-graph/src/mutation-impact.test.ts
@@ -49,13 +49,32 @@ describe('scene mutation impact', () => {
expect(impact.currentParentIds).toContain(right.id)
})
+ test('retains old and new parents when insertChildAt transfers a node', async () => {
+ const graph = new SceneGraph()
+ const page = pageId(graph)
+ const left = graph.createNode('FRAME', page)
+ const right = graph.createNode('FRAME', page)
+ const child = graph.createNode('RECTANGLE', left.id)
+
+ const { impact } = await collectSceneMutation(graph, () =>
+ graph.insertChildAt(child.id, right.id, 0)
+ )
+
+ expect(impact.previousParentIds).toContain(left.id)
+ expect(impact.currentParentIds).toContain(right.id)
+ })
+
test('unsubscribes when an operation throws', async () => {
const graph = new SceneGraph()
- expect(() =>
- collectSceneMutation(graph, () => {
- throw new Error('failed')
- })
- ).toThrow('failed')
+ const rejection = collectSceneMutation(graph, async () => {
+ throw new Error('failed')
+ })
+ await rejection.then(
+ () => {
+ throw new Error('Expected rejection')
+ },
+ (error: unknown) => expect(error).toMatchObject({ message: 'failed' })
+ )
graph.createNode('RECTANGLE', pageId(graph))
const { impact } = await collectSceneMutation(graph, () => undefined)
diff --git a/packages/scene-graph/src/mutation-impact.ts b/packages/scene-graph/src/mutation-impact.ts
index ccb28bfb8..2d8c38ff2 100644
--- a/packages/scene-graph/src/mutation-impact.ts
+++ b/packages/scene-graph/src/mutation-impact.ts
@@ -48,8 +48,11 @@ export async function collectSceneMutation<T>(
if (oldParentId) impact.previousParentIds.add(oldParentId)
impact.currentParentIds.add(newParentId)
},
- reordered: (nodeId, parentId) => {
+ reordered: (nodeId, parentId, _index, previousParentId) => {
impact.changedNodeIds.add(nodeId)
+ if (previousParentId && previousParentId !== parentId) {
+ impact.previousParentIds.add(previousParentId)
+ }
impact.currentParentIds.add(parentId)
}
})
diff --git a/packages/scene-graph/src/types.ts b/packages/scene-graph/src/types.ts
index 6c05cdfb7..fe1103d2e 100644
--- a/packages/scene-graph/src/types.ts
+++ b/packages/scene-graph/src/types.ts
@@ -10,7 +10,12 @@ export interface SceneGraphEvents {
'node:previewUpdated': (id: string, changes: Partial<SceneNode>) => void
'node:deleted': (id: string, parentId: string | null) => void
'node:reparented': (nodeId: string, oldParentId: string | null, newParentId: string) => void
- 'node:reordered': (nodeId: string, parentId: string, index: number) => void
+ 'node:reordered': (
+ nodeId: string,
+ parentId: string,
+ index: number,
+ previousParentId: string | null
+ ) => void
}
export type SceneGraphEventHandlers = Partial<{
@@ -19,7 +24,12 @@ export type SceneGraphEventHandlers = Partial<{
previewUpdated: (id: string, changes: Partial<SceneNode>) => void
deleted: (id: string, parentId: string | null) => void
reparented: (nodeId: string, oldParentId: string | null, newParentId: string) => void
- reordered: (nodeId: string, parentId: string, index: number) => void
+ reordered: (
+ nodeId: string,
+ parentId: string,
+ index: number,
+ previousParentId: string | null
+ ) => void
}>
export type DocumentColorSpace = 'srgb' | 'display-p3'
diff --git a/src/app/automation/bridge/eval-handler.ts b/src/app/automation/bridge/eval-handler.ts
index 97536820c..3f415e430 100644
--- a/src/app/automation/bridge/eval-handler.ts
+++ b/src/app/automation/bridge/eval-handler.ts
@@ -2,6 +2,7 @@ import type { FigmaAPI } from '@open-pencil/core/figma-api'
import { wrapEvalCode } from '@open-pencil/core/tools'
import type { AutomationTarget } from '@/app/automation/bridge/target'
+import { ensureGraphFonts } from '@/app/editor/fonts'
type FigmaFactory = (store: AutomationTarget['store'], pageId?: string) => FigmaAPI
@@ -14,7 +15,14 @@ export function createAutomationEvalHandler(makeFigma: FigmaFactory) {
/* noop */
}).constructor
const fn = new AsyncFunction('figma', wrapEvalCode(code))
- const result = await target.store.runMutationWithLayout(() => fn(figma), target.pageId)
+ const result = await target.store.runMutationWithLayout(
+ () => fn(figma),
+ target.pageId,
+ async () => {
+ const page = target.store.graph.getNode(target.pageId)
+ if (page) await ensureGraphFonts(target.store.graph, page.childIds, target.store.renderer)
+ }
+ )
target.store.requestRender()
return { ok: true, result: result ?? null }
}