AI-native design editor. Open-source Figma alternative.
fix(vue): ignore click jitter before moving selection
Danila Poyarkov8542c1db6536
diff --git a/packages/vue/src/canvas/tool-input/use.ts b/packages/vue/src/canvas/tool-input/use.tsindex d751da840..815ecdac1 100644--- a/packages/vue/src/canvas/tool-input/use.ts+++ b/packages/vue/src/canvas/tool-input/use.ts@@ -13,6 +13,8 @@ type ToolMouseDownOptions = {event: MouseEventcx: numbercy: number+ sx: number+ sy: numbereditor: EditorhitFns: HitTestFnscursorOverride: Ref<string | null>@@ -27,6 +29,8 @@ export function handleToolMouseDown({event,cx,cy,+ sx,+ sy,editor,hitFns,cursorOverride,@@ -42,7 +46,7 @@ export function handleToolMouseDown({}if (tool === 'SELECT') {- handleSelectDown(event, cx, cy, editor, hitFns, tryStartRotation, handleTextEditClick, setDrag)+ handleSelectDown(event, cx, cy, sx, sy, editor, hitFns, tryStartRotation, handleTextEditClick, setDrag)return}diff --git a/packages/vue/src/canvas/useCanvasInput.ts b/packages/vue/src/canvas/useCanvasInput.tsindex e287a2c75..32ec36164 100644--- a/packages/vue/src/canvas/useCanvasInput.ts+++ b/packages/vue/src/canvas/useCanvasInput.ts@@ -163,6 +163,8 @@ export function useCanvasInput(event: e,cx,cy,+ sx,+ sy,editor,hitFns,cursorOverride,@@ -202,14 +204,14 @@ export function useCanvasInput(return}- const { cx, cy } = getCoords(e)+ const { sx, sy, cx, cy } = getCoords(e)if (d.type === 'rotate') {handleRotateMove(d, cx, cy, e.shiftKey)return}if (d.type === 'move') {- handleMoveMove(d, cx, cy, editor)+ handleMoveMove(d, cx, cy, sx, sy, editor)return}if (d.type === 'text-select') {diff --git a/packages/vue/src/shared/input/duplicate-drag.ts b/packages/vue/src/shared/input/duplicate-drag.tsindex ca2fe0137..391b84925 100644--- a/packages/vue/src/shared/input/duplicate-drag.ts+++ b/packages/vue/src/shared/input/duplicate-drag.ts@@ -7,6 +7,8 @@ type DragOriginal = { x: number; y: number; parentId: string }export function duplicateAndDrag(cx: number,cy: number,+ sx: number,+ sy: number,editor: Editor): { originals: Map<string, DragOriginal>; drag: DragState } {const previousSelection = new Set(editor.state.selectedIds)@@ -35,6 +37,9 @@ export function duplicateAndDrag(startY: cy,currentX: cx,currentY: cy,+ startScreenX: sx,+ startScreenY: sy,+ dragStarted: true,originals: newOriginals,duplicated: true,duplicatedPreviousSelection: previousSelectiondiff --git a/packages/vue/src/shared/input/move.ts b/packages/vue/src/shared/input/move.tsindex 31535566f..0621828c9 100644--- a/packages/vue/src/shared/input/move.ts+++ b/packages/vue/src/shared/input/move.ts@@ -12,6 +12,7 @@ import type { DragMove } from '#vue/shared/input/types'const AUTO_LAYOUT_REORDER_CLICK_SLOP = 3const AUTO_LAYOUT_CROSS_AXIS_DRAG_TOLERANCE = 96+export const MOVE_DRAG_START_THRESHOLD_PX = 3function isInsideAutoLayoutDragBounds(parentId: string, cx: number, cy: number, editor: Editor) {const parent = editor.graph.getNode(parentId)@@ -44,10 +45,28 @@ export function detectAutoLayoutParent(editor: Editor): string | undefined {return undefined}-export function handleMoveMove(d: DragMove, cx: number, cy: number, editor: Editor) {+function isPastDragStartThreshold(d: DragMove, sx: number, sy: number) {+ const dx = sx - d.startScreenX+ const dy = sy - d.startScreenY+ return dx * dx + dy * dy >= MOVE_DRAG_START_THRESHOLD_PX * MOVE_DRAG_START_THRESHOLD_PX+}++export function handleMoveMove(+ d: DragMove,+ cx: number,+ cy: number,+ sx: number,+ sy: number,+ editor: Editor+) {d.currentX = cxd.currentY = cy+ if (!d.dragStarted) {+ if (!isPastDragStartThreshold(d, sx, sy)) return+ d.dragStarted = true+ }+let dx = cx - d.startXlet dy = cy - d.startY@@ -113,6 +132,13 @@ function applyFinalPositions(d: DragMove, editor: Editor) {}export function handleMoveUp(d: DragMove, editor: Editor) {+ if (!d.dragStarted) {+ editor.setLayoutInsertIndicator(null)+ editor.setSnapGuides([])+ editor.setDropTarget(null)+ return+ }+const indicator = editor.state.layoutInsertIndicatoreditor.setLayoutInsertIndicator(null)editor.setSnapGuides([])diff --git a/packages/vue/src/shared/input/select.ts b/packages/vue/src/shared/input/select.tsindex fe4eef436..15df49372 100644--- a/packages/vue/src/shared/input/select.ts+++ b/packages/vue/src/shared/input/select.ts@@ -21,6 +21,8 @@ export function handleSelectDown(e: MouseEvent,cx: number,cy: number,+ sx: number,+ sy: number,editor: Editor,fns: HitTestFns,tryStartRotation: (cx: number, cy: number) => boolean,@@ -62,5 +64,5 @@ export function handleSelectDown(if (selectionIsLocked(editor)) return- setDrag(createSelectionMoveDrag(cx, cy, editor, e.altKey))+ setDrag(createSelectionMoveDrag(cx, cy, sx, sy, editor, e.altKey))}diff --git a/packages/vue/src/shared/input/select/move.ts b/packages/vue/src/shared/input/select/move.tsindex beeabdb63..4e932e5bd 100644--- a/packages/vue/src/shared/input/select/move.ts+++ b/packages/vue/src/shared/input/select/move.ts@@ -59,10 +59,12 @@ function detectDragAutoLayoutParent(originals: Map<string, MoveOriginal>, editorexport function createSelectionMoveDrag(cx: number,cy: number,+ sx: number,+ sy: number,editor: Editor,duplicate: boolean): DragState {- if (duplicate && editor.state.selectedIds.size > 0) return duplicateAndDrag(cx, cy, editor).drag+ if (duplicate && editor.state.selectedIds.size > 0) return duplicateAndDrag(cx, cy, sx, sy, editor).dragconst originals = collectMoveOriginals(editor)@@ -72,6 +74,9 @@ export function createSelectionMoveDrag(startY: cy,currentX: cx,currentY: cy,+ startScreenX: sx,+ startScreenY: sy,+ dragStarted: false,originals,autoLayoutParentId: detectDragAutoLayoutParent(originals, editor)}diff --git a/packages/vue/src/shared/input/types.ts b/packages/vue/src/shared/input/types.tsindex 6f615d895..c063a1bf2 100644--- a/packages/vue/src/shared/input/types.ts+++ b/packages/vue/src/shared/input/types.ts@@ -19,6 +19,9 @@ export interface DragMove {startY: numbercurrentX: numbercurrentY: number+ startScreenX: number+ startScreenY: number+ dragStarted: booleanoriginals: Map<string, { x: number; y: number; parentId: string }>duplicated?: booleanduplicatedPreviousSelection?: Set<string>diff --git a/tests/engine/text/rtl-auto-layout-input.test.ts b/tests/engine/text/rtl-auto-layout-input.test.tsindex 29444c2ac..0b23d85a8 100644--- a/tests/engine/text/rtl-auto-layout-input.test.ts+++ b/tests/engine/text/rtl-auto-layout-input.test.ts@@ -74,6 +74,9 @@ describe('RTL auto-layout input', () => {startY: 40,currentX: 100,currentY: 40,+ startScreenX: 100,+ startScreenY: 40,+ dragStarted: true,originals: new Map([[second.id, { x: second.x, y: second.y, parentId: frame.id }]]),autoLayoutParentId: frame.id}diff --git a/tests/engine/vue/input/move-threshold.test.ts b/tests/engine/vue/input/move-threshold.test.tsnew file mode 100644index 000000000..4dbf0c953--- /dev/null+++ b/tests/engine/vue/input/move-threshold.test.ts@@ -0,0 +1,49 @@+import { describe, expect, test } from 'bun:test'++import { createEditor } from '@open-pencil/core/editor'++import { handleMoveMove, handleMoveUp, MOVE_DRAG_START_THRESHOLD_PX } from '#vue/shared/input/move'+import { createSelectionMoveDrag } from '#vue/shared/input/select/move'+import type { DragMove } from '#vue/shared/input/types'++function setupMoveDrag(): { editor: ReturnType<typeof createEditor>; drag: DragMove; nodeId: string } {+ const editor = createEditor()+ const pageId = editor.state.currentPageId+ const node = editor.graph.createNode('RECTANGLE', pageId, {+ name: 'Box',+ x: 10,+ y: 20,+ width: 100,+ height: 80+ })+ editor.select([node.id])+ const drag = createSelectionMoveDrag(10, 20, 100, 200, editor, false)+ if (drag.type !== 'move') throw new Error('Expected move drag')+ return { editor, drag, nodeId: node.id }+}++describe('selection move drag threshold', () => {+ test('does not move selected nodes for click jitter below threshold', () => {+ const { editor, drag, nodeId } = setupMoveDrag()++ handleMoveMove(drag, 11, 21, 100 + MOVE_DRAG_START_THRESHOLD_PX - 1, 200, editor)+ handleMoveUp(drag, editor)++ const node = editor.graph.getNode(nodeId)+ expect(node?.x).toBe(10)+ expect(node?.y).toBe(20)+ expect(drag.dragStarted).toBe(false)+ })++ test('moves selected nodes once pointer movement exceeds threshold', () => {+ const { editor, drag, nodeId } = setupMoveDrag()++ handleMoveMove(drag, 16, 27, 100 + MOVE_DRAG_START_THRESHOLD_PX + 1, 200, editor)+ handleMoveUp(drag, editor)++ const node = editor.graph.getNode(nodeId)+ expect(node?.x).toBe(16)+ expect(node?.y).toBe(27)+ expect(drag.dragStarted).toBe(true)+ })+})