Skip to content

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

fix(editor): relayout after component sync

Danila Poyarkov13314d6b6dfe
diff --git a/packages/core/src/editor/component-sync.ts b/packages/core/src/editor/component-sync.ts
index 664bde1d6..de121e572 100644
--- a/packages/core/src/editor/component-sync.ts
+++ b/packages/core/src/editor/component-sync.ts
@@ -1,34 +1,46 @@
import type { SceneGraph } from '@open-pencil/scene-graph'
+import { computeAllLayouts } from '#core/layout'
+
export function createComponentSyncScheduler(
getGraph: () => SceneGraph,
requestRender: () => void
) {
let pendingComponentSync: Set<string> | null = null
+ let isFlushingComponentSync = false
function flushComponentSync() {
const ids = pendingComponentSync
if (!ids) return
pendingComponentSync = null
- const graph = getGraph()
- const componentIds = new Set<string>()
- for (const id of ids) {
- let current = graph.getNode(id)
- while (current) {
- if (current.type === 'COMPONENT') {
- componentIds.add(current.id)
- break
+ isFlushingComponentSync = true
+ try {
+ const graph = getGraph()
+ const componentIds = new Set<string>()
+ for (const id of ids) {
+ let current = graph.getNode(id)
+ while (current) {
+ if (current.type === 'COMPONENT') {
+ componentIds.add(current.id)
+ break
+ }
+ current = current.parentId ? graph.getNode(current.parentId) : undefined
}
- current = current.parentId ? graph.getNode(current.parentId) : undefined
}
+ for (const compId of componentIds) {
+ graph.syncInstances(compId)
+ }
+ if (componentIds.size > 0) {
+ computeAllLayouts(graph)
+ requestRender()
+ }
+ } finally {
+ isFlushingComponentSync = false
}
- for (const compId of componentIds) {
- graph.syncInstances(compId)
- }
- if (componentIds.size > 0) requestRender()
}
function scheduleComponentSync(nodeId: string) {
+ if (isFlushingComponentSync) return
if (!pendingComponentSync) {
pendingComponentSync = new Set()
queueMicrotask(flushComponentSync)
diff --git a/tests/engine/editor/structure/state.test.ts b/tests/engine/editor/structure/state.test.ts
index 8c7597a2e..26a6a5738 100644
--- a/tests/engine/editor/structure/state.test.ts
+++ b/tests/engine/editor/structure/state.test.ts
@@ -7,7 +7,7 @@ import { getNodeOrThrow } from '#tests/helpers/assert'
import { rect } from '#tests/helpers/layout'
describe('structure state actions', () => {
- test('toggleNodeVisibility reflows HUG auto-layout instance slots', () => {
+ test('toggleNodeVisibility reflows HUG auto-layout instance slots', async () => {
const editor = createEditor()
const page = editor.state.currentPageId
const component = editor.graph.createNode('COMPONENT', page, {
@@ -37,5 +37,10 @@ describe('structure state actions', () => {
expect(getNodeOrThrow(editor.graph, instance.id).height).toBe(80)
expect(getNodeOrThrow(editor.graph, bottom.id).y).toBe(40)
+
+ await Promise.resolve()
+
+ expect(getNodeOrThrow(editor.graph, instance.id).height).toBe(80)
+ expect(getNodeOrThrow(editor.graph, bottom.id).y).toBe(40)
})
})