From 0baace0a5c16c07d7cd34ee9baa848a9b3c3c1df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Thu, 2 Dec 2021 17:51:50 +0100 Subject: [PATCH] :zap: Integrate number suffix fix --- packages/editor-ui/src/components/Node.vue | 49 ++-------- packages/editor-ui/src/views/NodeView.vue | 93 ++++++++++++++++--- packages/editor-ui/src/views/canvasHelpers.ts | 63 ------------- .../nodes/Signl4/translations/de.json | 8 ++ 4 files changed, 96 insertions(+), 117 deletions(-) create mode 100644 packages/nodes-base/nodes/Signl4/translations/de.json diff --git a/packages/editor-ui/src/components/Node.vue b/packages/editor-ui/src/components/Node.vue index 6282278982..6d69bed7ac 100644 --- a/packages/editor-ui/src/components/Node.vue +++ b/packages/editor-ui/src/components/Node.vue @@ -196,21 +196,14 @@ export default mixins(externalHooks, nodeBase, nodeHelpers, renderText, workflow return this.$shortNodeType(this.data.type); }, nodeTitle (): string { - const node = this.data; - - const nodeName = this.$headerText({ - key: `headers.${this.$shortNodeType(node.type)}.displayName`, - 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 }); + if (this.data.name === 'Start') { + return this.$headerText({ + key: `headers.start.displayName`, + fallback: 'Start', + }); + } + return this.data.name; }, waiting (): string | undefined { 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(/(.*)(?\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 () { this.$store.commit('setActiveNode', this.data.name); }, diff --git a/packages/editor-ui/src/views/NodeView.vue b/packages/editor-ui/src/views/NodeView.vue index b7c1f293c0..46e499e3b3 100644 --- a/packages/editor-ui/src/views/NodeView.vue +++ b/packages/editor-ui/src/views/NodeView.vue @@ -270,6 +270,9 @@ export default mixins( defaultLocale (): string { return this.$store.getters.defaultLocale; }, + englishLocale(): boolean { + return this.defaultLocale === 'en'; + }, ...mapGetters(['nativelyNumberSuffixedDefaults']), activeNode (): INodeUi | null { return this.$store.getters.activeNode; @@ -354,6 +357,76 @@ export default mixins( this.$store.commit('setWorkflowExecutionData', null); 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) { this.createNodeActive = true; this.$externalHooks().run('nodeView.createNodeActiveChanged', { source, createNodeActive: this.createNodeActive }); @@ -1265,11 +1338,11 @@ export default mixins( newNodeData.position = CanvasHelpers.getNewNodePosition(this.nodes, this.lastClickPosition); } + // Check if node-name is unique else find one that is - newNodeData.name = CanvasHelpers.getUniqueNodeName({ - nodes: this.$store.getters.allNodes, + newNodeData.name = this.getUniqueNodeName({ originalName: newNodeData.name, - nativelyNumberSuffixed: this.nativelyNumberSuffixedDefaults, + type: newNodeData.type, }); if (nodeTypeData.webhooks && nodeTypeData.webhooks.length) { @@ -1862,10 +1935,9 @@ export default mixins( const newNodeData = JSON.parse(JSON.stringify(this.getNodeDataToSave(node))); // Check if node-name is unique else find one that is - newNodeData.name = CanvasHelpers.getUniqueNodeName({ - nodes: this.$store.getters.allNodes, + newNodeData.name = this.getUniqueNodeName({ originalName: newNodeData.name, - nativelyNumberSuffixed: this.nativelyNumberSuffixedDefaults, + type: newNodeData.type, }); newNodeData.position = CanvasHelpers.getNewNodePosition( @@ -2110,10 +2182,8 @@ export default mixins( return; } // Check if node-name is unique else find one that is - newName = CanvasHelpers.getUniqueNodeName({ - nodes: this.$store.getters.allNodes, + newName = this.getUniqueNodeName({ originalName: newName, - nativelyNumberSuffixed: this.nativelyNumberSuffixedDefaults, }); // Rename the node and update the connections @@ -2333,11 +2403,10 @@ export default mixins( } oldName = node.name; - newName = CanvasHelpers.getUniqueNodeName({ - nodes: this.$store.getters.allNodes, + newName = this.getUniqueNodeName({ originalName: node.name, additionalUsedNames: newNodeNames, - nativelyNumberSuffixed: this.nativelyNumberSuffixedDefaults, + type: node.type, }); newNodeNames.push(newName); diff --git a/packages/editor-ui/src/views/canvasHelpers.ts b/packages/editor-ui/src/views/canvasHelpers.ts index 8b3532cfb9..8e9178034f 100644 --- a/packages/editor-ui/src/views/canvasHelpers.ts +++ b/packages/editor-ui/src/views/canvasHelpers.ts @@ -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) { diff --git a/packages/nodes-base/nodes/Signl4/translations/de.json b/packages/nodes-base/nodes/Signl4/translations/de.json new file mode 100644 index 0000000000..bcbaa71f2c --- /dev/null +++ b/packages/nodes-base/nodes/Signl4/translations/de.json @@ -0,0 +1,8 @@ +{ + "signl4": { + "header": { + "displayName": "🇩🇪 SIGNL4", + "description": "🇩🇪 Some description" + } + } +} \ No newline at end of file