fix(editor): Fix node positions update event in new canvas (no-changelog) (#10528)

This commit is contained in:
Alex Grozav 2024-08-26 10:33:11 +03:00 committed by GitHub
parent 51c70b5cff
commit 9d156d3703
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 50 additions and 15 deletions

View file

@ -85,7 +85,7 @@ describe('Canvas', () => {
expect(container.querySelector(`[data-id="${connections[0].id}"]`)).toBeInTheDocument(); expect(container.querySelector(`[data-id="${connections[0].id}"]`)).toBeInTheDocument();
}); });
it('should handle node drag stop event', async () => { it('should handle `update:nodes:position` event', async () => {
const nodes = [createCanvasNodeElement()]; const nodes = [createCanvasNodeElement()];
const { container, emitted } = renderComponent({ const { container, emitted } = renderComponent({
props: { props: {
@ -99,14 +99,47 @@ describe('Canvas', () => {
await fireEvent.mouseDown(node, { view: window }); await fireEvent.mouseDown(node, { view: window });
await fireEvent.mouseMove(node, { await fireEvent.mouseMove(node, {
view: window, view: window,
clientX: 96, clientX: 20,
clientY: 96, clientY: 20,
});
await fireEvent.mouseMove(node, {
view: window,
clientX: 40,
clientY: 40,
}); });
await fireEvent.mouseUp(node, { view: window }); await fireEvent.mouseUp(node, { view: window });
// Snap to 20px grid: 96 -> 100
expect(emitted()['update:nodes:position']).toEqual([ expect(emitted()['update:nodes:position']).toEqual([
[[{ id: '1', position: { x: 100, y: 100 } }]], [
[
{
id: '1',
type: 'position',
dragging: true,
from: {
x: 100,
y: 100,
z: 0,
},
position: { x: 80, y: 80 },
},
],
],
[
[
{
id: '1',
type: 'position',
dragging: true,
from: {
x: 100,
y: 100,
z: 0,
},
position: { x: 120, y: 120 },
},
],
],
]); ]);
}); });
}); });

View file

@ -2,10 +2,11 @@
import type { CanvasConnection, CanvasNode, CanvasNodeMoveEvent, ConnectStartEvent } from '@/types'; import type { CanvasConnection, CanvasNode, CanvasNodeMoveEvent, ConnectStartEvent } from '@/types';
import type { import type {
EdgeMouseEvent, EdgeMouseEvent,
NodeDragEvent,
Connection, Connection,
XYPosition, XYPosition,
ViewportTransform, ViewportTransform,
NodeChange,
NodePositionChange,
} from '@vue-flow/core'; } from '@vue-flow/core';
import { useVueFlow, VueFlow, PanelPosition } from '@vue-flow/core'; import { useVueFlow, VueFlow, PanelPosition } from '@vue-flow/core';
import { Background } from '@vue-flow/background'; import { Background } from '@vue-flow/background';
@ -134,11 +135,7 @@ function onClickNodeAdd(id: string, handle: string) {
emit('click:node:add', id, handle); emit('click:node:add', id, handle);
} }
function onNodeDragStop(e: NodeDragEvent) { function onUpdateNodesPosition(events: NodePositionChange[]) {
onUpdateNodesPosition(e.nodes.map((node) => ({ id: node.id, position: node.position })));
}
function onUpdateNodesPosition(events: CanvasNodeMoveEvent[]) {
emit('update:nodes:position', events); emit('update:nodes:position', events);
} }
@ -146,8 +143,14 @@ function onUpdateNodePosition(id: string, position: XYPosition) {
emit('update:node:position', id, position); emit('update:node:position', id, position);
} }
function onSelectionDragStop(e: NodeDragEvent) { function onNodesChange(events: NodeChange[]) {
onNodeDragStop(e); const isPositionChangeEvent = (event: NodeChange): event is NodePositionChange =>
event.type === 'position' && 'position' in event;
const positionChangeEndEvents = events.filter(isPositionChangeEvent);
if (positionChangeEndEvents.length > 0) {
onUpdateNodesPosition(positionChangeEndEvents);
}
} }
function onSetNodeActive(id: string) { function onSetNodeActive(id: string) {
@ -414,8 +417,6 @@ provide(CanvasKey, {
:max-zoom="4" :max-zoom="4"
:class="[$style.canvas, { [$style.visible]: paneReady }]" :class="[$style.canvas, { [$style.visible]: paneReady }]"
data-test-id="canvas" data-test-id="canvas"
@node-drag-stop="onNodeDragStop"
@selection-drag-stop="onSelectionDragStop"
@edge-mouse-enter="onMouseEnterEdge" @edge-mouse-enter="onMouseEnterEdge"
@edge-mouse-leave="onMouseLeaveEdge" @edge-mouse-leave="onMouseLeaveEdge"
@connect-start="onConnectStart" @connect-start="onConnectStart"
@ -424,6 +425,7 @@ provide(CanvasKey, {
@pane-click="onClickPane" @pane-click="onClickPane"
@contextmenu="onOpenContextMenu" @contextmenu="onOpenContextMenu"
@viewport-change="onViewportChange" @viewport-change="onViewportChange"
@nodes-change="onNodesChange"
> >
<template #node-canvas-node="canvasNodeProps"> <template #node-canvas-node="canvasNodeProps">
<Node <Node