fix(editor): Fix unique names for node duplication (#6134)

* 🐛 Fix unique names for node duplication

* 🐛 Fix i18n references
This commit is contained in:
Iván Ovejero
2023-04-28 15:53:59 +02:00
committed by GitHub
parent 20a72bb28b
commit 71ae6c66ef
9 changed files with 227 additions and 102 deletions

View File

@@ -210,6 +210,8 @@ import useGlobalLinkActions from '@/composables/useGlobalLinkActions';
import useCanvasMouseSelect from '@/composables/useCanvasMouseSelect';
import { showMessage } from '@/mixins/showMessage';
import { useTitleChange } from '@/composables/useTitleChange';
import { useUniqueNodeName } from '@/composables/useUniqueNodeName';
import { useI18n } from '@/composables/useI18n';
import { workflowHelpers } from '@/mixins/workflowHelpers';
import { workflowRun } from '@/mixins/workflowRun';
@@ -341,6 +343,8 @@ export default mixins(
...useCanvasMouseSelect(),
...useGlobalLinkActions(),
...useTitleChange(),
...useUniqueNodeName(),
...useI18n(),
};
},
errorCaptured: (err, vm, info) => {
@@ -477,12 +481,6 @@ export default mixins(
currentUser(): IUser | null {
return this.usersStore.currentUser;
},
defaultLocale(): string {
return this.rootStore.defaultLocale;
},
isEnglishLocale(): boolean {
return this.defaultLocale === 'en';
},
activeNode(): INodeUi | null {
return this.ndvStore.activeNode;
},
@@ -678,78 +676,6 @@ export default mixins(
this.workflowsStore.workflowExecutionData = null;
this.updateNodesExecutionIssues();
},
translateName(type: string, originalName: string) {
return this.$locale.headerText({
key: `headers.${this.$locale.shortNodeType(type)}.displayName`,
fallback: originalName,
});
},
getUniqueNodeName({
originalName,
additionalUsedNames = [],
type = '',
}: {
originalName: string;
additionalUsedNames?: string[];
type?: string;
}) {
const allNodeNamesOnCanvas = this.workflowsStore.allNodes.map((n: INodeUi) => n.name);
originalName = this.isEnglishLocale ? originalName : this.translateName(type, originalName);
if (
!allNodeNamesOnCanvas.includes(originalName) &&
!additionalUsedNames.includes(originalName)
) {
return originalName; // already unique
}
let natives: string[] = this.nativelyNumberSuffixedDefaults;
natives = this.isEnglishLocale
? 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 = 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 (
allNodeNamesOnCanvas.includes(uniqueName) ||
additionalUsedNames.includes(uniqueName)
) {
uniqueName = baseName + index++;
}
return uniqueName;
},
async onSaveKeyboardShortcut(e: KeyboardEvent) {
let saved = await this.saveCurrentWorkflow();
if (saved) await this.settingsStore.fetchPromptsData();
@@ -1931,11 +1857,9 @@ export default mixins(
newNodeData.position = NodeViewUtils.getNewNodePosition(this.nodes, position);
}
// Check if node-name is unique else find one that is
newNodeData.name = this.getUniqueNodeName({
originalName: newNodeData.name,
type: newNodeData.type,
});
const localizedName = this.localizeNodeName(newNodeData.name, newNodeData.type);
newNodeData.name = this.uniqueNodeName(localizedName);
if (nodeTypeData.webhooks && nodeTypeData.webhooks.length) {
newNodeData.webhookId = uuid();
@@ -2732,11 +2656,9 @@ export default mixins(
const newNodeData = deepCopy(this.getNodeDataToSave(node));
newNodeData.id = uuid();
// Check if node-name is unique else find one that is
newNodeData.name = this.getUniqueNodeName({
originalName: newNodeData.name,
type: newNodeData.type,
});
const localizedName = this.localizeNodeName(newNodeData.name, newNodeData.type);
newNodeData.name = this.uniqueNodeName(localizedName);
newNodeData.position = NodeViewUtils.getNewNodePosition(
this.nodes,
@@ -3116,10 +3038,7 @@ export default mixins(
this.renamingActive = true;
}
// Check if node-name is unique else find one that is
newName = this.getUniqueNodeName({
originalName: newName,
});
newName = this.uniqueNodeName(newName);
// Rename the node and update the connections
const workflow = this.getCurrentWorkflow(true);
@@ -3386,11 +3305,10 @@ export default mixins(
}
oldName = node.name;
newName = this.getUniqueNodeName({
originalName: node.name,
additionalUsedNames: newNodeNames,
type: node.type,
});
const localized = this.localizeNodeName(node.name, node.type);
newName = this.uniqueNodeName(localized, newNodeNames);
newNodeNames.push(newName);
nodeNameTable[oldName] = newName;