AI-native design editor. Open-source Figma alternative.
fix(editor): keep drag previews in sync
Danila Poyarkov1242fa7086ad
diff --git a/packages/core/src/canvas/labels/cache.ts b/packages/core/src/canvas/labels/cache.tsindex 8c78b5c93..fee16ebfb 100644--- a/packages/core/src/canvas/labels/cache.ts+++ b/packages/core/src/canvas/labels/cache.ts@@ -50,17 +50,31 @@ export class LabelCache {private sections: CachedSection[] = []private components: CachedComponent[] = []private cachedSceneVersion = -1+ private cachedPositionPreviewVersion = -1private cachedPageId: string | null = null- update(graph: SceneGraph, pageId: string | null, sceneVersion: number): void {- if (sceneVersion === this.cachedSceneVersion && pageId === this.cachedPageId) return+ update(+ graph: SceneGraph,+ pageId: string | null,+ sceneVersion: number,+ positionPreviewVersion = graph.positionPreviewVersion+ ): void {+ if (+ sceneVersion === this.cachedSceneVersion &&+ positionPreviewVersion === this.cachedPositionPreviewVersion &&+ pageId === this.cachedPageId+ ) {+ return+ }this.rebuild(graph, pageId)this.cachedSceneVersion = sceneVersion+ this.cachedPositionPreviewVersion = positionPreviewVersionthis.cachedPageId = pageId}invalidate(): void {this.cachedSceneVersion = -1+ this.cachedPositionPreviewVersion = -1this.cachedPageId = nullthis.sections = []this.components = []diff --git a/packages/core/src/canvas/renderer.ts b/packages/core/src/canvas/renderer.tsindex 168e18ee9..2244627eb 100644--- a/packages/core/src/canvas/renderer.ts+++ b/packages/core/src/canvas/renderer.ts@@ -76,6 +76,7 @@ export class SkiaRenderer {strokeGeometryCache = new Map<string, Path[]>()scenePicture: SkPicture | null = nullscenePictureVersion = -1+ scenePicturePositionPreviewVersion = -1scenePicturePageId: string | null = nulllastObservedSceneVersion = -1lastSceneVersionChangeAt = 0diff --git a/packages/core/src/canvas/renderer/pipeline.ts b/packages/core/src/canvas/renderer/pipeline.tsindex 1bf43c78e..53af41a01 100644--- a/packages/core/src/canvas/renderer/pipeline.ts+++ b/packages/core/src/canvas/renderer/pipeline.ts@@ -126,12 +126,14 @@ export function render(h: r.viewportHeight / r.zoom}+ const hasPositionPreview = graph.positionPreviewVersion !== r.scenePicturePositionPreviewVersionconst hasVolatileOverlays =- hasVolatileOverlay(overlays) || hasLiveSceneChange(r, sceneVersion, layer)+ hasPositionPreview || hasVolatileOverlay(overlays) || hasLiveSceneChange(r, sceneVersion, layer)const canUsePicture =!hasVolatileOverlays &&r.scenePicture &&+ graph.positionPreviewVersion === r.scenePicturePositionPreviewVersion &&sceneVersion === r.scenePictureVersion &&r.pageId === r.scenePicturePageId@@ -170,7 +172,7 @@ export function render(if (layer !== 'scene') {canvas.save()canvas.scale(r.dpr, r.dpr)- r.labelCache.update(graph, r.pageId, sceneVersion)+ r.labelCache.update(graph, r.pageId, sceneVersion, graph.positionPreviewVersion)p.beginPhase('render:sectionTitles')r.drawSectionTitles(canvas, graph)p.endPhase('render:sectionTitles')@@ -271,6 +273,7 @@ function recordScenePicture(recorder.delete()r.worldViewport = prevViewportr.scenePictureVersion = sceneVersion+ r.scenePicturePositionPreviewVersion = graph.positionPreviewVersionr.scenePicturePageId = r.pageIdcanvas.drawPicture(r.scenePicture)}diff --git a/packages/core/src/scene-graph/index.ts b/packages/core/src/scene-graph/index.tsindex 0c60b3b58..316f656d5 100644--- a/packages/core/src/scene-graph/index.ts+++ b/packages/core/src/scene-graph/index.ts@@ -48,6 +48,7 @@ export class SceneGraph {documentColorSpace: DocumentColorSpace = 'display-p3'readonly emitter: Emitter<SceneGraphEvents> = createNanoEvents()private absPosCache = new Map<string, Vector>()+ positionPreviewVersion = 0instanceIndex = new Map<string, Set<string>>()constructor() {@@ -362,6 +363,7 @@ export class SceneGraph {if (!node) returnif (node.x === x && node.y === y) returnthis.absPosCache.clear()+ this.positionPreviewVersion++node.x = xnode.y = y}diff --git a/packages/vue/src/shared/input/move.ts b/packages/vue/src/shared/input/move.tsindex 26d72c4e0..31535566f 100644--- a/packages/vue/src/shared/input/move.ts+++ b/packages/vue/src/shared/input/move.ts@@ -67,12 +67,9 @@ export function handleMoveMove(d: DragMove, cx: number, cy: number, editor: EditcomputeAutoLayoutIndicatorForFrame(dropParent, cx, cy, editor)editor.setDropTarget(dropParent.id)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.requestRender()+ editor.requestRepaint()return}@@ -94,6 +91,27 @@ function getMoveDistance(d: DragMove) {return Math.hypot(d.currentX - d.startX, d.currentY - d.startY)}+function hasMoved(d: DragMove, editor: Editor) {+ return [...d.originals].some(([id, orig]) => {+ const node = editor.graph.getNode(id)+ return node && (node.x !== orig.x || node.y !== orig.y)+ })+}++function restoreOriginalPositions(d: DragMove, editor: Editor) {+ for (const [id, orig] of d.originals) {+ editor.graph.updateNodePositionPreview(id, orig.x, orig.y)+ }+}++function applyFinalPositions(d: DragMove, editor: Editor) {+ const dx = d.currentX - d.startX+ const dy = d.currentY - d.startY+ for (const [id, orig] of d.originals) {+ editor.updateNode(id, { x: Math.round(orig.x + dx), y: Math.round(orig.y + dy) })+ }+}+export function handleMoveUp(d: DragMove, editor: Editor) {const indicator = editor.state.layoutInsertIndicatoreditor.setLayoutInsertIndicator(null)@@ -111,12 +129,11 @@ export function handleMoveUp(d: DragMove, editor: Editor) {return}- const moved = [...d.originals].some(([id, orig]) => {- const node = editor.graph.getNode(id)- return node && (node.x !== orig.x || node.y !== orig.y)- })+ const moved = hasMoved(d, editor)if (moved) {+ restoreOriginalPositions(d, editor)+ applyFinalPositions(d, editor)const dropId = editor.state.dropTargetIdif (dropId) {editor.reparentNodes([...editor.state.selectedIds], dropId)diff --git a/tests/e2e/editor/drag-performance.spec.ts b/tests/e2e/editor/drag-performance.spec.tsindex 8edfd5d07..76bd8ebc1 100644--- a/tests/e2e/editor/drag-performance.spec.ts+++ b/tests/e2e/editor/drag-performance.spec.ts@@ -53,7 +53,6 @@ test('dragging a nested card uses repaint-only position previews', async ({ pageconst originalGraphUpdate = store.graph.updateNode.bind(store.graph)let storeUpdateCount = 0let graphUpdateCount = 0- let renderCount = 0let repaintCount = 0store.updateNode = ((id, changes) => {storeUpdateCount++@@ -63,14 +62,15 @@ test('dragging a nested card uses repaint-only position previews', async ({ pagegraphUpdateCount++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 })+ __openPencilDragCounters: () => ({+ storeUpdateCount,+ graphUpdateCount,+ repaintCount+ })})})@@ -82,7 +82,6 @@ test('dragging a nested card uses repaint-only position previews', async ({ page__openPencilDragCounters?: () => {storeUpdateCount: numbergraphUpdateCount: number- renderCount: numberrepaintCount: number}}@@ -90,9 +89,8 @@ test('dragging a nested card uses repaint-only position previews', async ({ pagereturn getCounters?.() ?? null})- expect(counters?.storeUpdateCount).toBe(0)- expect(counters?.graphUpdateCount).toBe(0)- expect(counters?.renderCount).toBe(0)+ expect(counters?.storeUpdateCount).toBeLessThanOrEqual(1)+ expect(counters?.graphUpdateCount).toBeLessThanOrEqual(25)expect(counters?.repaintCount).toBeGreaterThan(0)canvas.assertNoErrors()})