From 4b0187e7e8f8f6a764dac42aba073f341878c66b Mon Sep 17 00:00:00 2001 From: Alex Grozav Date: Thu, 3 Oct 2024 21:20:57 +0300 Subject: [PATCH] feat(editor): Add logic for renaming duplicate nodes in new canvas (no-changelog) (#11081) --- .../src/composables/useCanvasOperations.ts | 20 ++++++++---- .../src/utils/__tests__/canvasUtilsV2.spec.ts | 31 ------------------- packages/editor-ui/src/utils/canvasUtilsV2.ts | 16 ---------- 3 files changed, 14 insertions(+), 53 deletions(-) diff --git a/packages/editor-ui/src/composables/useCanvasOperations.ts b/packages/editor-ui/src/composables/useCanvasOperations.ts index 9ae616c6c0..c4fc09954b 100644 --- a/packages/editor-ui/src/composables/useCanvasOperations.ts +++ b/packages/editor-ui/src/composables/useCanvasOperations.ts @@ -58,7 +58,6 @@ import type { import { CanvasConnectionMode } from '@/types'; import { createCanvasConnectionHandleString, - getUniqueNodeName, mapCanvasConnectionToLegacyConnection, mapLegacyConnectionsToCanvasConnections, mapLegacyConnectionToCanvasConnection, @@ -96,6 +95,7 @@ import { v4 as uuid } from 'uuid'; import { computed, nextTick, ref } from 'vue'; import type { useRouter } from 'vue-router'; import { useClipboard } from '@/composables/useClipboard'; +import { useUniqueNodeName } from '@/composables/useUniqueNodeName'; import { isPresent } from '../utils/typesUtils'; type AddNodeData = Partial & { @@ -201,12 +201,12 @@ export function useCanvasOperations({ router }: { router: ReturnType ({ name: node.type, version: node.typeVersion })), ); + const { uniqueNodeName } = useUniqueNodeName(); + data.nodes.forEach((node) => { if (nodeTypesCount[node.type] !== undefined) { if (nodeTypesCount[node.type].exist >= nodeTypesCount[node.type].max) { @@ -1503,7 +1506,7 @@ export function useCanvasOperations({ router }: { router: ReturnType { }); }); -describe('getUniqueNodeName', () => { - it('should return the original name if it is unique', () => { - const name = 'Node A'; - const existingNames = new Set(['Node B', 'Node C']); - const result = getUniqueNodeName(name, existingNames); - expect(result).toBe(name); - }); - - it('should append a number to the name if it already exists', () => { - const name = 'Node A'; - const existingNames = new Set(['Node A', 'Node B']); - const result = getUniqueNodeName(name, existingNames); - expect(result).toBe('Node A 1'); - }); - - it('should find the next available number for the name', () => { - const name = 'Node A'; - const existingNames = new Set(['Node A', 'Node A 1', 'Node A 2']); - const result = getUniqueNodeName(name, existingNames); - expect(result).toBe('Node A 3'); - }); - - it('should use UUID if more than 99 variations exist', () => { - const name = 'Node A'; - const existingNames = new Set([...Array(100).keys()].map((i) => `Node A ${i}`).concat([name])); - const result = getUniqueNodeName(name, existingNames); - expect(result).toBe('Node A mock-uuid'); - }); -}); - describe('checkOverlap', () => { it('should return true when nodes overlap', () => { const node1 = { x: 0, y: 0, width: 10, height: 10 }; diff --git a/packages/editor-ui/src/utils/canvasUtilsV2.ts b/packages/editor-ui/src/utils/canvasUtilsV2.ts index 4feeb6d2d6..c4a49d8c94 100644 --- a/packages/editor-ui/src/utils/canvasUtilsV2.ts +++ b/packages/editor-ui/src/utils/canvasUtilsV2.ts @@ -3,7 +3,6 @@ import type { INodeUi } from '@/Interface'; import type { BoundingBox, CanvasConnection, CanvasConnectionPort } from '@/types'; import { CanvasConnectionMode } from '@/types'; import type { Connection } from '@vue-flow/core'; -import { v4 as uuid } from 'uuid'; import { isValidCanvasConnectionMode, isValidNodeConnectionType } from '@/utils/typeGuards'; import { NodeConnectionType } from 'n8n-workflow'; @@ -195,21 +194,6 @@ export function mapLegacyEndpointsToCanvasConnectionPort( }); } -export function getUniqueNodeName(name: string, existingNames: Set): string { - if (!existingNames.has(name)) { - return name; - } - - for (let i = 1; i < 100; i++) { - const newName = `${name} ${i}`; - if (!existingNames.has(newName)) { - return newName; - } - } - - return `${name} ${uuid()}`; -} - export function checkOverlap(node1: BoundingBox, node2: BoundingBox) { return !( // node1 is completely to the left of node2