Integrate number suffix fix

This commit is contained in:
Iván Ovejero
2021-12-02 17:51:50 +01:00
parent e1ac523797
commit 0baace0a5c
4 changed files with 96 additions and 117 deletions

View File

@@ -597,69 +597,6 @@ export const getZoomToFit = (nodes: INodeUi[]): {offset: XYPosition, zoomLevel:
};
};
export const getUniqueNodeName = ({
nodes,
originalName,
additionalUsedNames,
nativelyNumberSuffixed,
} : {
nodes: INodeUi[],
originalName: string,
additionalUsedNames?: string[],
nativelyNumberSuffixed: string[],
}) => {
// Check if node-name is unique else find one that is
additionalUsedNames = additionalUsedNames || [];
// 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) && !additionalUsedNames.includes(originalName)) {
return originalName;
}
const found = nativelyNumberSuffixed.find((n) => originalName.startsWith(n));
let ignore, baseName, nameIndex, uniqueName;
let index = 1;
if (found) {
nameIndex = originalName.split(found).pop();
if (nameIndex) {
index = parseInt(nameIndex, 10);
}
baseName = uniqueName = found;
} else {
const nameMatch = originalName.match(/(.*\D+)(\d*)/);
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) ||
additionalUsedNames.includes(uniqueName)
) {
uniqueName = baseName + (index++);
}
return uniqueName;
};
export const showDropConnectionState = (connection: Connection, targetEndpoint?: Endpoint) => {
if (connection && connection.connector) {
if (targetEndpoint) {