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.tsindex 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 = falsefunction flushComponentSync() {const ids = pendingComponentSyncif (!ids) returnpendingComponentSync = 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) returnif (!pendingComponentSync) {pendingComponentSync = new Set()queueMicrotask(flushComponentSync)diff --git a/tests/engine/editor/structure/state.test.ts b/tests/engine/editor/structure/state.test.tsindex 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.currentPageIdconst 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)})})