diff --git a/packages/node_modules/@node-red/editor-client/src/js/ui/view-tools.js b/packages/node_modules/@node-red/editor-client/src/js/ui/view-tools.js index f503beecb..22cb1eecd 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/ui/view-tools.js +++ b/packages/node_modules/@node-red/editor-client/src/js/ui/view-tools.js @@ -1102,18 +1102,27 @@ RED.view.tools = (function() { const paletteLabel = RED.utils.getPaletteLabel(n.type, nodeDef) const defaultNodeNameRE = new RegExp('^'+paletteLabel.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')+' (\\d+)$') if (!typeIndex.hasOwnProperty(n.type)) { - const existingNodes = RED.nodes.filterNodes({type: n.type}) - let maxNameNumber = 0; - existingNodes.forEach(n => { - let match = defaultNodeNameRE.exec(n.name) + const existingNodes = RED.nodes.filterNodes({ type: n.type }); + const existingIds = existingNodes.reduce((ids, node) => { + let match = defaultNodeNameRE.exec(node.name); if (match) { - let nodeNumber = parseInt(match[1]) - if (nodeNumber > maxNameNumber) { - maxNameNumber = nodeNumber + const nodeNumber = parseInt(match[1], 10); + if (!ids.includes(nodeNumber)) { + ids.push(nodeNumber); } } - }) - typeIndex[n.type] = maxNameNumber + 1 + return ids; + }, []).sort((a, b) => a - b); + + let availableNameNumber = 1; + for (let i = 0; i < existingIds.length; i++) { + if (existingIds[i] !== availableNameNumber) { + break; + } + availableNameNumber++; + } + + typeIndex[n.type] = availableNameNumber; } if ((options.renameBlank && n.name === '') || (options.renameClash && defaultNodeNameRE.test(n.name))) { if (generateHistory) {