1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add core:generate-node-names action

This commit is contained in:
Nick O'Leary 2022-03-09 11:32:37 +00:00
parent 93ff667df1
commit 3c0b74005b
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
4 changed files with 81 additions and 7 deletions

View File

@ -970,6 +970,70 @@ RED.view.tools = (function() {
return gridOffset;
}
/**
* Generate names for the select nodes.
* - it only sets the name if it is currently blank
* - it uses `<paletteLabel> <N>` - where N is the next available integer that
* doesn't clash with any existing nodes of that type
* @param {Object} node The node to set the name of - if not provided, uses current selection
*/
function generateNodeNames(node) {
const nodes = node?[node]:RED.view.selection().nodes;
if (nodes && nodes.length > 0) {
// Generate history event if using the workspace selection,
// or if the provided node already exists
const generateHistory = !node || !!RED.nodes.node(node.id)
const historyEvents = []
const typeIndex = {}
let changed = false;
nodes.forEach(n => {
if (n._def && n._def.defaults && n._def.defaults.name) {
const paletteLabel = RED.utils.getPaletteLabel(n.type, n._def)
const defaultNodeNameRE = new RegExp('^'+paletteLabel+' (\\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)
if (match) {
let nodeNumber = parseInt(match[1])
if (nodeNumber > maxNameNumber) {
maxNameNumber = nodeNumber
}
}
})
typeIndex[n.type] = maxNameNumber + 1
}
if (n.name === '') {
if (generateHistory) {
historyEvents.push({
t:'edit',
node: n,
changes: { name: n.name },
dirty: RED.nodes.dirty(),
changed: n.changed
})
}
n.name = paletteLabel+" "+typeIndex[n.type]
n.dirty = true
typeIndex[n.type]++
changed = true
}
}
})
if (changed) {
if (historyEvents.length > 0) {
RED.history.push({
t: 'multi',
events: historyEvents
})
}
RED.nodes.dirty(true)
RED.view.redraw()
}
}
}
return {
init: function() {
RED.actions.add("core:show-selected-node-labels", function() { setSelectedNodeLabelState(true); })
@ -1033,6 +1097,8 @@ RED.view.tools = (function() {
RED.actions.add("core:split-wire-with-link-nodes", function () { splitWiresWithLinkNodes() });
RED.actions.add("core:generate-node-names", generateNodeNames )
// RED.actions.add("core:add-node", function() { addNode() })
},
/**

View File

@ -507,6 +507,9 @@
$("#node-input-complete").val($("#node-input-typed-complete").typedInput('value'));
}
$("#node-input-statusVal").val($("#node-input-typed-status").typedInput('value'));
},
onadd: function() {
RED.actions.invoke("core:generate-node-names",this)
}
});
})();

View File

@ -209,6 +209,8 @@
}
function onAdd() {
RED.actions.invoke("core:generate-node-names",this)
for (var i=0;i<this.links.length;i++) {
var n = RED.nodes.node(this.links[i]);
if (n && n.links.indexOf(this.id) === -1) {
@ -286,7 +288,10 @@
oneditsave: function() {
onEditSave(this);
},
oneditresize: resizeNodeList
oneditresize: resizeNodeList,
onadd: function() {
RED.actions.invoke("core:generate-node-names",this)
}
});
RED.nodes.registerType('link out',{