feat(editor): Add remove node and connections functionality to canvas v2 (#9602)

This commit is contained in:
Alex Grozav
2024-06-04 15:36:27 +03:00
committed by GitHub
parent 202c91e7ed
commit f6a466cd87
13 changed files with 876 additions and 125 deletions

View File

@@ -1,7 +1,10 @@
import type { IConnections, INodeTypeDescription } from 'n8n-workflow';
import type { IConnection, IConnections, INodeTypeDescription } from 'n8n-workflow';
import type { INodeUi } from '@/Interface';
import type { CanvasConnection, CanvasConnectionPortType, CanvasConnectionPort } from '@/types';
import type { Connection } from '@vue-flow/core';
import { v4 as uuid } from 'uuid';
import { isValidNodeConnectionType } from '@/utils/typeGuards';
import { NodeConnectionType } from 'n8n-workflow';
export function mapLegacyConnectionsToCanvasConnections(
legacyConnections: IConnections,
@@ -49,6 +52,53 @@ export function mapLegacyConnectionsToCanvasConnections(
return mappedConnections;
}
export function parseCanvasConnectionHandleString(handle: string | null | undefined) {
const [, type, index] = (handle ?? '').split('/');
const resolvedType = isValidNodeConnectionType(type) ? type : NodeConnectionType.Main;
let resolvedIndex = parseInt(index, 10);
if (isNaN(resolvedIndex)) {
resolvedIndex = 0;
}
return {
type: resolvedType,
index: resolvedIndex,
};
}
export function mapCanvasConnectionToLegacyConnection(
sourceNode: INodeUi,
targetNode: INodeUi,
connection: Connection,
): [IConnection, IConnection] {
// Output
const sourceNodeName = sourceNode?.name ?? '';
const { type: sourceType, index: sourceIndex } = parseCanvasConnectionHandleString(
connection.sourceHandle,
);
// Input
const targetNodeName = targetNode?.name ?? '';
const { type: targetType, index: targetIndex } = parseCanvasConnectionHandleString(
connection.targetHandle,
);
return [
{
node: sourceNodeName,
type: sourceType,
index: sourceIndex,
},
{
node: targetNodeName,
type: targetType,
index: targetIndex,
},
];
}
export function mapLegacyEndpointsToCanvasConnectionPort(
endpoints: INodeTypeDescription['inputs'],
): CanvasConnectionPort[] {