Rename also expressions on pasted nodes and fix unique naming

issue
This commit is contained in:
Jan Oberhauser 2019-08-09 18:47:33 +02:00
parent b5b959fe08
commit fcb0982401
2 changed files with 63 additions and 37 deletions

View file

@ -1,6 +1,7 @@
import { PLACEHOLDER_EMPTY_WORKFLOW_ID } from '@/constants'; import { PLACEHOLDER_EMPTY_WORKFLOW_ID } from '@/constants';
import { import {
IConnections,
INode, INode,
INodeExecutionData, INodeExecutionData,
INodeIssues, INodeIssues,
@ -166,9 +167,9 @@ export const workflowHelpers = mixins(
}, },
// Returns a workflow instance. // Returns a workflow instance.
getWorkflow (copyData?: boolean): Workflow { getWorkflow (nodes?: INodeUi[], connections?: IConnections, copyData?: boolean): Workflow {
const nodes = this.getNodes(); nodes = nodes || this.getNodes();
const connections = this.$store.getters.allConnections; connections = connections || (this.$store.getters.allConnections as IConnections);
const nodeTypes: INodeTypes = { const nodeTypes: INodeTypes = {
nodeTypes: {}, nodeTypes: {},

View file

@ -132,7 +132,9 @@ import {
INodeIssues, INodeIssues,
INodeTypeDescription, INodeTypeDescription,
IRunData, IRunData,
NodeInputConnections,
NodeHelpers, NodeHelpers,
Workflow,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
IConnectionsUi, IConnectionsUi,
@ -862,6 +864,11 @@ export default mixins(
return node.name; 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(/(.*[a-zA-Z])(\d*)/); const nameMatch = originalName.match(/(.*[a-zA-Z])(\d*)/);
let ignore, baseName, nameIndex, uniqueName; let ignore, baseName, nameIndex, uniqueName;
let index = 1; let index = 1;
@ -1503,7 +1510,7 @@ export default mixins(
newName = this.getUniqueNodeName(newName); newName = this.getUniqueNodeName(newName);
// Rename the node and update the connections // Rename the node and update the connections
const workflow = this.getWorkflow(true); const workflow = this.getWorkflow(undefined, undefined, true);
workflow.renameNode(currentName, newName); workflow.renameNode(currentName, newName);
// Update also last selected node and exeuction data // Update also last selected node and exeuction data
@ -1634,7 +1641,8 @@ export default mixins(
// a max limit set already exist // a max limit set already exist
const nodeTypesCount = this.getNodeTypesMaxCount(); const nodeTypesCount = this.getNodeTypesMaxCount();
let oldName; let oldName: string;
let newName: string;
const createNodes: INode[] = []; const createNodes: INode[] = [];
data.nodes.forEach(node => { data.nodes.forEach(node => {
if (nodeTypesCount[node.type] !== undefined) { if (nodeTypesCount[node.type] !== undefined) {
@ -1654,52 +1662,69 @@ export default mixins(
} }
oldName = node.name; oldName = node.name;
node.name = this.getUniqueNodeName(node.name, newNodeNames); newName = this.getUniqueNodeName(node.name, newNodeNames);
newNodeNames.push(node.name); newNodeNames.push(newName);
nodeNameTable[oldName] = node.name; nodeNameTable[oldName] = newName;
createNodes.push(node); createNodes.push(node);
}); });
data.nodes = createNodes; // Get only the connections of the nodes that get created
const newConnections: IConnections = {};
const nameData = { old: '', new: '' }; const currentConnections = data.connections!;
for (oldName of Object.keys(nodeNameTable)) { const createNodeNames = createNodes.map((node) => node.name);
nameData.old = oldName;
nameData.new = nodeNameTable[oldName];
// More or less identical to "renameNode" in "Workflow.ts"
if (
nameData.old !== nameData.new &&
data.connections &&
data.connections.hasOwnProperty(nameData.old)
) {
data.connections[nameData.new] = data.connections[nameData.old];
delete data.connections[nameData.old];
}
// Rename all destination connections
let sourceNode, type, sourceIndex, connectionIndex, connectionData; let sourceNode, type, sourceIndex, connectionIndex, connectionData;
if (data.connections) { for (sourceNode of Object.keys(currentConnections)) {
for (sourceNode of Object.keys(data.connections)) { if (!createNodeNames.includes(sourceNode)) {
for (type of Object.keys(data.connections[sourceNode])) { // Node does not get created so skip output connections
for (sourceIndex = 0; sourceIndex < data.connections[sourceNode][type].length; sourceIndex++) { continue;
for (connectionIndex = 0; connectionIndex < data.connections[sourceNode][type][sourceIndex].length; connectionIndex++) {
connectionData = data.connections[sourceNode][type][sourceIndex][connectionIndex];
if (connectionData.node === nameData.old) {
connectionData.node = nameData.new;
}
}
} }
const connection: INodeConnections = {};
for (type of Object.keys(currentConnections[sourceNode])) {
connection[type] = [];
for (sourceIndex = 0; sourceIndex < currentConnections[sourceNode][type].length; sourceIndex++) {
const nodeSourceConnections = [];
for (connectionIndex = 0; connectionIndex < currentConnections[sourceNode][type][sourceIndex].length; connectionIndex++) {
const nodeConnection: NodeInputConnections = [];
connectionData = currentConnections[sourceNode][type][sourceIndex][connectionIndex];
if (!createNodeNames.includes(connectionData.node)) {
// Node does not get created so skip input connection
continue;
} }
nodeSourceConnections.push(connectionData);
// Add connection
} }
connection[type].push(nodeSourceConnections);
} }
} }
await this.addNodes(data.nodes, data.connections); newConnections[sourceNode] = connection;
}
return data; // Create a workflow with the new nodes and connections that we can use
// the rename method
const tempWorkflow: Workflow = this.getWorkflow(createNodes, newConnections);
// Rename all the nodes of which the name changed
for (oldName in nodeNameTable) {
if (oldName === nodeNameTable[oldName]) {
// Name did not change so skip
continue;
}
tempWorkflow.renameNode(oldName, nodeNameTable[oldName]);
}
// Add the nodes with the changed node names, expressions and connections
await this.addNodes(Object.values(tempWorkflow.nodes), tempWorkflow.connectionsBySourceNode);
return {
nodes: Object.values(tempWorkflow.nodes),
connections: tempWorkflow.connectionsBySourceNode,
};
}, },
getSelectedNodesToSave (): Promise<IWorkflowData> { getSelectedNodesToSave (): Promise<IWorkflowData> {
const data: IWorkflowData = { const data: IWorkflowData = {