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

@ -196,21 +196,14 @@ export default mixins(externalHooks, nodeBase, nodeHelpers, renderText, workflow
return this.$shortNodeType(this.data.type); return this.$shortNodeType(this.data.type);
}, },
nodeTitle (): string { nodeTitle (): string {
const node = this.data; if (this.data.name === 'Start') {
return this.$headerText({
const nodeName = this.$headerText({ key: `headers.start.displayName`,
key: `headers.${this.$shortNodeType(node.type)}.displayName`, fallback: 'Start',
fallback: node.name, });
}); }
if (!/\d$/.test(node.name)) return nodeName;
const nativeDuplicateSuffix = this.getDuplicateSuffix(node, { fromNative: true });
if (nativeDuplicateSuffix) return nodeName + nativeDuplicateSuffix;
return nodeName + this.getDuplicateSuffix(node, { fromStandard: true });
return this.data.name;
}, },
waiting (): string | undefined { waiting (): string | undefined {
const workflowExecution = this.$store.getters.getWorkflowExecution; const workflowExecution = this.$store.getters.getWorkflowExecution;
@ -343,34 +336,6 @@ export default mixins(externalHooks, nodeBase, nodeHelpers, renderText, workflow
}); });
}, },
/**
* Extract the duplicate number suffix from a node name:
* - from a node name natively ending in a number, e.g. `'S31'` `'1'`
* - from a standard node name, e.g. `'GitHub1'` `'1'`
*/
getDuplicateSuffix(
node: INodeUi,
{ fromNative, fromStandard }: { fromNative?: true; fromStandard?: true; },
) {
if (fromNative) {
const { nativelyNumberSuffixedNodeNames: natives } = this.$store.getters;
const found = natives.find((native: string) => node.name.includes(native));
if (!found) return null;
return node.name.split(found).pop()!;
}
if (fromStandard) {
const match = node.name.match(/(.*)(?<duplicateSuffix>\d)$/);
if (!match || !match.groups || !match.groups.duplicateSuffix) return null;
return match.groups.duplicateSuffix;
}
throw new Error('Either "fromNative" or "fromStandard" must be specified');
},
setNodeActive () { setNodeActive () {
this.$store.commit('setActiveNode', this.data.name); this.$store.commit('setActiveNode', this.data.name);
}, },

View file

@ -270,6 +270,9 @@ export default mixins(
defaultLocale (): string { defaultLocale (): string {
return this.$store.getters.defaultLocale; return this.$store.getters.defaultLocale;
}, },
englishLocale(): boolean {
return this.defaultLocale === 'en';
},
...mapGetters(['nativelyNumberSuffixedDefaults']), ...mapGetters(['nativelyNumberSuffixedDefaults']),
activeNode (): INodeUi | null { activeNode (): INodeUi | null {
return this.$store.getters.activeNode; return this.$store.getters.activeNode;
@ -354,6 +357,76 @@ export default mixins(
this.$store.commit('setWorkflowExecutionData', null); this.$store.commit('setWorkflowExecutionData', null);
this.updateNodesExecutionIssues(); this.updateNodesExecutionIssues();
}, },
translateName(type: string, originalName: string) {
return this.$headerText({
key: `headers.${this.$shortNodeType(type)}.displayName`,
fallback: originalName,
});
},
getUniqueNodeName({
originalName,
additionalUsedNames = [],
type = '',
} : {
originalName: string,
additionalUsedNames?: string[],
type?: string,
}) {
const allNodeNamesOnCanvas = this.$store.getters.allNodes.map((n: INodeUi) => n.name);
originalName = this.englishLocale ? originalName : this.translateName(type, originalName);
if (
!allNodeNamesOnCanvas.includes(originalName) &&
!additionalUsedNames.includes(originalName)
) {
return originalName; // already unique
}
let natives: string[] = this.nativelyNumberSuffixedDefaults;
natives = this.englishLocale ? natives : natives.map(name => {
const type = name.toLowerCase().replace('_', '');
return this.translateName(type, name);
});
const found = natives.find((n) => originalName.startsWith(n));
let ignore, baseName, nameIndex, uniqueName;
let index = 1;
if (found) {
// name natively ends with number
nameIndex = originalName.split(found).pop();
if (nameIndex) {
index = parseInt(nameIndex, 10);
}
baseName = uniqueName = originalName;
} 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 = originalName;
}
}
while (
allNodeNamesOnCanvas.includes(uniqueName) ||
additionalUsedNames.includes(uniqueName)
) {
uniqueName = baseName + (index++);
}
return uniqueName;
},
openNodeCreator (source: string) { openNodeCreator (source: string) {
this.createNodeActive = true; this.createNodeActive = true;
this.$externalHooks().run('nodeView.createNodeActiveChanged', { source, createNodeActive: this.createNodeActive }); this.$externalHooks().run('nodeView.createNodeActiveChanged', { source, createNodeActive: this.createNodeActive });
@ -1265,11 +1338,11 @@ export default mixins(
newNodeData.position = CanvasHelpers.getNewNodePosition(this.nodes, this.lastClickPosition); newNodeData.position = CanvasHelpers.getNewNodePosition(this.nodes, this.lastClickPosition);
} }
// Check if node-name is unique else find one that is // Check if node-name is unique else find one that is
newNodeData.name = CanvasHelpers.getUniqueNodeName({ newNodeData.name = this.getUniqueNodeName({
nodes: this.$store.getters.allNodes,
originalName: newNodeData.name, originalName: newNodeData.name,
nativelyNumberSuffixed: this.nativelyNumberSuffixedDefaults, type: newNodeData.type,
}); });
if (nodeTypeData.webhooks && nodeTypeData.webhooks.length) { if (nodeTypeData.webhooks && nodeTypeData.webhooks.length) {
@ -1862,10 +1935,9 @@ export default mixins(
const newNodeData = JSON.parse(JSON.stringify(this.getNodeDataToSave(node))); const newNodeData = JSON.parse(JSON.stringify(this.getNodeDataToSave(node)));
// Check if node-name is unique else find one that is // Check if node-name is unique else find one that is
newNodeData.name = CanvasHelpers.getUniqueNodeName({ newNodeData.name = this.getUniqueNodeName({
nodes: this.$store.getters.allNodes,
originalName: newNodeData.name, originalName: newNodeData.name,
nativelyNumberSuffixed: this.nativelyNumberSuffixedDefaults, type: newNodeData.type,
}); });
newNodeData.position = CanvasHelpers.getNewNodePosition( newNodeData.position = CanvasHelpers.getNewNodePosition(
@ -2110,10 +2182,8 @@ export default mixins(
return; return;
} }
// Check if node-name is unique else find one that is // Check if node-name is unique else find one that is
newName = CanvasHelpers.getUniqueNodeName({ newName = this.getUniqueNodeName({
nodes: this.$store.getters.allNodes,
originalName: newName, originalName: newName,
nativelyNumberSuffixed: this.nativelyNumberSuffixedDefaults,
}); });
// Rename the node and update the connections // Rename the node and update the connections
@ -2333,11 +2403,10 @@ export default mixins(
} }
oldName = node.name; oldName = node.name;
newName = CanvasHelpers.getUniqueNodeName({ newName = this.getUniqueNodeName({
nodes: this.$store.getters.allNodes,
originalName: node.name, originalName: node.name,
additionalUsedNames: newNodeNames, additionalUsedNames: newNodeNames,
nativelyNumberSuffixed: this.nativelyNumberSuffixedDefaults, type: node.type,
}); });
newNodeNames.push(newName); newNodeNames.push(newName);

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) => { export const showDropConnectionState = (connection: Connection, targetEndpoint?: Endpoint) => {
if (connection && connection.connector) { if (connection && connection.connector) {
if (targetEndpoint) { if (targetEndpoint) {

View file

@ -0,0 +1,8 @@
{
"signl4": {
"header": {
"displayName": "🇩🇪 SIGNL4",
"description": "🇩🇪 Some description"
}
}
}