diff --git a/packages/editor-ui/src/Interface.ts b/packages/editor-ui/src/Interface.ts index 44d03cadda..d47cc76afb 100644 --- a/packages/editor-ui/src/Interface.ts +++ b/packages/editor-ui/src/Interface.ts @@ -118,6 +118,7 @@ export interface INodeUi extends INode { color?: string; notes?: string; issues?: INodeIssues; + name: string; } export interface INodeTypesMaxCount { diff --git a/packages/editor-ui/src/views/NodeView.vue b/packages/editor-ui/src/views/NodeView.vue index 7c2269d657..a54f447422 100644 --- a/packages/editor-ui/src/views/NodeView.vue +++ b/packages/editor-ui/src/views/NodeView.vue @@ -1095,47 +1095,6 @@ export default mixins( this.$store.commit('setActiveNode', node.name); } }, - getUniqueNodeName (originalName: string, additinalUsedNames?: string[]) { - // Check if node-name is unique else find one that is - additinalUsedNames = additinalUsedNames || []; - - // Get all the names of the current nodes - const nodeNames = this.$store.getters.allNodes.map((node: INodeUi) => { - return node.name; - }); - - // Check first if the current name is already unique - if (!nodeNames.includes(originalName) && !additinalUsedNames.includes(originalName)) { - return originalName; - } - - const nameMatch = originalName.match(/(.*\D+)(\d*)/); - let ignore, baseName, nameIndex, uniqueName; - let index = 1; - - if (nameMatch === null) { - // Name is only a number - index = parseInt(originalName, 10); - baseName = ''; - uniqueName = baseName + index; - } else { - // Name is string or string/number combination - [ignore, baseName, nameIndex] = nameMatch; - if (nameIndex !== '') { - index = parseInt(nameIndex, 10); - } - uniqueName = baseName; - } - - while ( - nodeNames.includes(uniqueName) || - additinalUsedNames.includes(uniqueName) - ) { - uniqueName = baseName + (index++); - } - - return uniqueName; - }, showMaxNodeTypeError (nodeTypeData: INodeTypeDescription) { const maxNodes = nodeTypeData.maxNodes; this.$showMessage({ @@ -1211,7 +1170,7 @@ export default mixins( } // Check if node-name is unique else find one that is - newNodeData.name = this.getUniqueNodeName(newNodeData.name); + newNodeData.name = CanvasHelpers.getUniqueNodeName(this.$store.getters.allNodes, newNodeData.name); if (nodeTypeData.webhooks && nodeTypeData.webhooks.length) { newNodeData.webhookId = uuidv4(); @@ -1724,7 +1683,7 @@ export default mixins( const newNodeData = JSON.parse(JSON.stringify(this.getNodeDataToSave(node))); // Check if node-name is unique else find one that is - newNodeData.name = this.getUniqueNodeName(newNodeData.name); + newNodeData.name = CanvasHelpers.getUniqueNodeName(this.$store.getters.allNodes, newNodeData.name); newNodeData.position = CanvasHelpers.getNewNodePosition( this.nodes, @@ -1929,7 +1888,7 @@ export default mixins( return; } // Check if node-name is unique else find one that is - newName = this.getUniqueNodeName(newName); + newName = CanvasHelpers.getUniqueNodeName(this.$store.getters.allNodes, newName); // Rename the node and update the connections const workflow = this.getWorkflow(undefined, undefined, true); @@ -2141,7 +2100,7 @@ export default mixins( } oldName = node.name; - newName = this.getUniqueNodeName(node.name, newNodeNames); + newName = CanvasHelpers.getUniqueNodeName(this.$store.getters.allNodes, node.name, newNodeNames); newNodeNames.push(newName); nodeNameTable[oldName] = newName; diff --git a/packages/editor-ui/src/views/canvasHelpers.ts b/packages/editor-ui/src/views/canvasHelpers.ts index 65c747b406..4476cfa831 100644 --- a/packages/editor-ui/src/views/canvasHelpers.ts +++ b/packages/editor-ui/src/views/canvasHelpers.ts @@ -478,3 +478,45 @@ export const getZoomToFit = (nodes: INodeUi[]): {offset: XYPosition, zoomLevel: offset: [xOffset, yOffset], }; }; + +export const getUniqueNodeName = (nodes: INodeUi[], originalName: string, additinalUsedNames?: string[]) => { + // Check if node-name is unique else find one that is + additinalUsedNames = additinalUsedNames || []; + + // Get all the names of the current nodes + const nodeNames = nodes.map((node: INodeUi) => { + return node.name; + }); + + // Check first if the current name is already unique + if (!nodeNames.includes(originalName) && !additinalUsedNames.includes(originalName)) { + return originalName; + } + + const nameMatch = originalName.match(/(.*\D+)(\d*)/); + let ignore, baseName, nameIndex, uniqueName; + let index = 1; + + if (nameMatch === null) { + // Name is only a number + index = parseInt(originalName, 10); + baseName = ''; + uniqueName = baseName + index; + } else { + // Name is string or string/number combination + [ignore, baseName, nameIndex] = nameMatch; + if (nameIndex !== '') { + index = parseInt(nameIndex, 10); + } + uniqueName = baseName; + } + + while ( + nodeNames.includes(uniqueName) || + additinalUsedNames.includes(uniqueName) + ) { + uniqueName = baseName + (index++); + } + + return uniqueName; +};