mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-28 05:59:42 -08:00
refactor
This commit is contained in:
parent
b939f7fbb9
commit
828291df2c
|
@ -118,6 +118,7 @@ export interface INodeUi extends INode {
|
|||
color?: string;
|
||||
notes?: string;
|
||||
issues?: INodeIssues;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface INodeTypesMaxCount {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue