From f478b111818f3d8871cc2fa19e472df76e2472ba Mon Sep 17 00:00:00 2001 From: Alexandre Rousseau Date: Fri, 20 Dec 2024 14:02:36 +0100 Subject: [PATCH] feat(ui): enable multiples move in workflow - WF-148 --- src/ui/src/builder/builderManager.ts | 2 +- src/ui/src/builder/useComponentActions.ts | 18 +++++--- .../workflows/WorkflowsWorkflow.vue | 41 +++++++++++++------ 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/ui/src/builder/builderManager.ts b/src/ui/src/builder/builderManager.ts index ead8f7ec..c5929d37 100644 --- a/src/ui/src/builder/builderManager.ts +++ b/src/ui/src/builder/builderManager.ts @@ -1,4 +1,4 @@ -import { computed, ref, Ref, watch } from "vue"; +import { computed, ref, Ref } from "vue"; import { Component, ClipboardOperation } from "@/writerTypes"; export const CANDIDATE_CONFIRMATION_DELAY_MS = 1500; diff --git a/src/ui/src/builder/useComponentActions.ts b/src/ui/src/builder/useComponentActions.ts index af31c41c..0ed52efa 100644 --- a/src/ui/src/builder/useComponentActions.ts +++ b/src/ui/src/builder/useComponentActions.ts @@ -806,7 +806,11 @@ export function useComponentActions(wf: Core, ssbm: BuilderManager) { if (!component) return; const transactionId = `change-${componentId}-coordinates`; - ssbm.openMutationTransaction(transactionId, "Change coordinates", false); + ssbm.openMutationTransaction( + transactionId, + "Change coordinates", + false, + ); ssbm.registerPreMutation(component); component.x = Math.floor(x); @@ -821,14 +825,18 @@ export function useComponentActions(wf: Core, ssbm: BuilderManager) { * Change the coordinates of multiple components. */ function changeCoordinatesMultiple( - coordinates: Record + coordinates: Record, ) { const transactionId = "change-multiple-coordinates"; - ssbm.openMutationTransaction(transactionId, "Change coordinates", false); + ssbm.openMutationTransaction( + transactionId, + "Change coordinates", + false, + ); - const entries = Object.entries(coordinates); + const entries = Object.entries(coordinates); if (entries.length == 0) return; - entries.forEach(([componentId, {x, y}]) => { + entries.forEach(([componentId, { x, y }]) => { const component = wf.getComponentById(componentId); if (!component) return; ssbm.registerPreMutation(component); diff --git a/src/ui/src/components/workflows/WorkflowsWorkflow.vue b/src/ui/src/components/workflows/WorkflowsWorkflow.vue index a43b38b6..542b654f 100644 --- a/src/ui/src/components/workflows/WorkflowsWorkflow.vue +++ b/src/ui/src/components/workflows/WorkflowsWorkflow.vue @@ -19,8 +19,8 @@ :is-selected="selectedArrow == arrowId" :is-engaged=" selectedArrow == arrowId || - wfbm.isComponentIdSelected(arrow.fromNodeId) || - wfbm.isComponentIdSelected(arrow.toNodeId) + wfbm.getSelectedId() == arrow.fromNodeId || + wfbm.getSelectedId() == arrow.toNodeId " @click="handleArrowClick($event, arrowId)" @delete="handleArrowDeleteClick(arrow)" @@ -137,6 +137,7 @@ import { onMounted, onUnmounted, ref, + shallowRef, } from "vue"; import { useComponentActions } from "@/builder/useComponentActions"; import { useDragDropComponent } from "@/builder/useDragDropComponent"; @@ -170,7 +171,6 @@ const { createAndInsertComponent, addOut, removeOut, - changeCoordinates, changeCoordinatesMultiple, } = useComponentActions(wf, wfbm); const { getComponentInfoFromDrag } = useDragDropComponent(wf); @@ -484,12 +484,8 @@ function clearActiveOperations() { } function saveNodeMove() { - const { nodeId } = activeNodeMove.value; - const tempXY = temporaryNodeCoordinates.value?.[nodeId]; - if (!tempXY) return; - const { x, y } = tempXY; - changeCoordinates(nodeId, x, y); - temporaryNodeCoordinates.value[nodeId] = null; + changeCoordinatesMultiple(temporaryNodeCoordinates.value); + temporaryNodeCoordinates.value = {}; } function moveNode(ev: MouseEvent) { @@ -500,9 +496,30 @@ function moveNode(ev: MouseEvent) { const newX = Math.floor(x - offset.x); const newY = Math.floor(y - offset.y); - temporaryNodeCoordinates.value[nodeId] = { - x: newX, - y: newY, + const component = wf.getComponentById(nodeId); + + const translationX = newX - component.x; + const translationY = newY - component.y; + + // apply the same vector to other selected components + const otherSelectedComponents = wfbm + .getSelection() + .map((c) => wf.getComponentById(c.componentId)) + .filter( + (c) => c.id !== nodeId && c.x !== undefined && c.y !== undefined, + ) + .reduce>((acc, component) => { + acc[component.id] = { + x: component.x + translationX, + y: component.y + translationY, + }; + return acc; + }, {}); + + temporaryNodeCoordinates.value = { + ...temporaryNodeCoordinates.value, + ...otherSelectedComponents, + [nodeId]: { x: newX, y: newY }, }; }