Skip to content

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

perf(editor): use repaint previews while dragging

Danila Poyarkov4e646443851e
diff --git a/packages/core/src/editor/selection/overlays.ts b/packages/core/src/editor/selection/overlays.ts
index 616931949..83baecbc0 100644
--- a/packages/core/src/editor/selection/overlays.ts
+++ b/packages/core/src/editor/selection/overlays.ts
@@ -25,11 +25,13 @@ export function createSelectionOverlayActions(ctx: EditorContext) {
}
function setDropTarget(id: string | null) {
+ if (ctx.state.dropTargetId === id) return
ctx.state.dropTargetId = id
ctx.requestRepaint()
}
function setLayoutInsertIndicator(indicator: typeof ctx.state.layoutInsertIndicator) {
+ if (ctx.state.layoutInsertIndicator === indicator) return
ctx.state.layoutInsertIndicator = indicator
ctx.requestRepaint()
}
diff --git a/packages/core/src/scene-graph/index.ts b/packages/core/src/scene-graph/index.ts
index 0b9e8d778..0c60b3b58 100644
--- a/packages/core/src/scene-graph/index.ts
+++ b/packages/core/src/scene-graph/index.ts
@@ -357,6 +357,15 @@ export class SceneGraph {
'height'
])
+ updateNodePositionPreview(id: string, x: number, y: number): void {
+ const node = this.nodes.get(id)
+ if (!node) return
+ if (node.x === x && node.y === y) return
+ this.absPosCache.clear()
+ node.x = x
+ node.y = y
+ }
+
updateNode(id: string, changes: Partial<SceneNode>): void {
const node = this.nodes.get(id)
if (!node) return
diff --git a/packages/vue/src/shared/input/move.ts b/packages/vue/src/shared/input/move.ts
index 5b91bae1d..26d72c4e0 100644
--- a/packages/vue/src/shared/input/move.ts
+++ b/packages/vue/src/shared/input/move.ts
@@ -83,11 +83,11 @@ export function handleMoveMove(d: DragMove, cx: number, cy: number, editor: Edit
dy = snapped.dy
for (const [id, orig] of d.originals) {
- editor.graph.updateNode(id, { x: Math.round(orig.x + dx), y: Math.round(orig.y + dy) })
+ editor.graph.updateNodePositionPreview(id, Math.round(orig.x + dx), Math.round(orig.y + dy))
}
editor.setDropTarget(dropTarget?.id ?? null)
- editor.requestRender()
+ editor.requestRepaint()
}
function getMoveDistance(d: DragMove) {
diff --git a/tests/e2e/editor/drag-performance.spec.ts b/tests/e2e/editor/drag-performance.spec.ts
index 35ed919cd..8edfd5d07 100644
--- a/tests/e2e/editor/drag-performance.spec.ts
+++ b/tests/e2e/editor/drag-performance.spec.ts
@@ -2,7 +2,7 @@ import { expect, test } from '@playwright/test'
import { CanvasHelper } from '#tests/helpers/canvas'
-test('dragging a nested card does not recompute layout on every pointer move', async ({ page }) => {
+test('dragging a nested card uses repaint-only position previews', async ({ page }) => {
await page.goto('/')
const canvas = new CanvasHelper(page)
await canvas.waitForInit()
@@ -49,23 +49,50 @@ test('dragging a nested card does not recompute layout on every pointer move', a
await page.evaluate(() => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
- const original = store.updateNode.bind(store)
- let count = 0
+ const originalStoreUpdate = store.updateNode.bind(store)
+ const originalGraphUpdate = store.graph.updateNode.bind(store.graph)
+ let storeUpdateCount = 0
+ let graphUpdateCount = 0
+ let renderCount = 0
+ let repaintCount = 0
store.updateNode = ((id, changes) => {
- count++
- return original(id, changes)
+ storeUpdateCount++
+ return originalStoreUpdate(id, changes)
}) as typeof store.updateNode
- Object.assign(window, { __openPencilUpdateNodeCount: () => count })
+ store.graph.updateNode = ((id, changes) => {
+ graphUpdateCount++
+ return originalGraphUpdate(id, changes)
+ }) as typeof store.graph.updateNode
+ store.onEditorEvent('render:requested', () => {
+ renderCount++
+ })
+ store.onEditorEvent('repaint:requested', () => {
+ repaintCount++
+ })
+ Object.assign(window, {
+ __openPencilDragCounters: () => ({ storeUpdateCount, graphUpdateCount, renderCount, repaintCount })
+ })
})
await canvas.drag(110, 110, 210, 150, 20)
- const updateNodeCalls = await page.evaluate(() => {
- const getCount = (window as typeof window & { __openPencilUpdateNodeCount?: () => number })
- .__openPencilUpdateNodeCount
- return getCount?.() ?? 0
+ const counters = await page.evaluate(() => {
+ const getCounters = (
+ window as typeof window & {
+ __openPencilDragCounters?: () => {
+ storeUpdateCount: number
+ graphUpdateCount: number
+ renderCount: number
+ repaintCount: number
+ }
+ }
+ ).__openPencilDragCounters
+ return getCounters?.() ?? null
})
- expect(updateNodeCalls).toBe(0)
+ expect(counters?.storeUpdateCount).toBe(0)
+ expect(counters?.graphUpdateCount).toBe(0)
+ expect(counters?.renderCount).toBe(0)
+ expect(counters?.repaintCount).toBeGreaterThan(0)
canvas.assertNoErrors()
})