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

@ -811,7 +811,7 @@ RED.view.tools = (function() {
/** /**
* Splits selected wires and re-joins them with link-out+link-in * Splits selected wires and re-joins them with link-out+link-in
* @param {Object || Object[]} wires The wire(s) to split and replace with link-out, link-in nodes. * @param {Object || Object[]} wires The wire(s) to split and replace with link-out, link-in nodes.
*/ */
function splitWiresWithLinkNodes(wires) { function splitWiresWithLinkNodes(wires) {
let wiresToSplit = wires || RED.view.selection().links; let wiresToSplit = wires || RED.view.selection().links;
@ -868,7 +868,7 @@ RED.view.tools = (function() {
nodeSrcMap[linkOutMapId] = nnLinkOut; nodeSrcMap[linkOutMapId] = nnLinkOut;
let yOffset = 0; let yOffset = 0;
if(nSrc.outputs > 1) { if(nSrc.outputs > 1) {
const CENTER_PORT = (((nSrc.outputs-1) / 2) + 1); const CENTER_PORT = (((nSrc.outputs-1) / 2) + 1);
const offsetCount = Math.abs(CENTER_PORT - (srcPort + 1)); const offsetCount = Math.abs(CENTER_PORT - (srcPort + 1));
yOffset = (_gridSize * 2 * offsetCount); yOffset = (_gridSize * 2 * offsetCount);
@ -918,7 +918,7 @@ RED.view.tools = (function() {
t: 'add', t: 'add',
links: [link], links: [link],
}); });
} }
//connect the link out/link in virtual wires //connect the link out/link in virtual wires
if(nnLinkIn.links.indexOf(nnLinkOut.id) == -1) { if(nnLinkIn.links.indexOf(nnLinkOut.id) == -1) {
@ -937,9 +937,9 @@ RED.view.tools = (function() {
} }
//add all history events to stack //add all history events to stack
RED.history.push(history); RED.history.push(history);
//select all downstream of new link-in nodes so user can drag to new location //select all downstream of new link-in nodes so user can drag to new location
RED.view.clearSelection(); RED.view.clearSelection();
RED.view.select({nodes: Object.values(nodeTrgMap) }); RED.view.select({nodes: Object.values(nodeTrgMap) });
selectConnected("down"); selectConnected("down");
@ -970,6 +970,70 @@ RED.view.tools = (function() {
return gridOffset; 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 { return {
init: function() { init: function() {
RED.actions.add("core:show-selected-node-labels", function() { setSelectedNodeLabelState(true); }) 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:split-wire-with-link-nodes", function () { splitWiresWithLinkNodes() });
RED.actions.add("core:generate-node-names", generateNodeNames )
// RED.actions.add("core:add-node", function() { addNode() }) // RED.actions.add("core:add-node", function() { addNode() })
}, },
/** /**

View File

@ -5419,7 +5419,7 @@ RED.view = (function() {
} }
/** /**
* Create a node from a type string. * Create a node from a type string.
* **NOTE:** Can throw on error - use `try` `catch` block when calling * **NOTE:** Can throw on error - use `try` `catch` block when calling
* @param {string} type The node type to create * @param {string} type The node type to create
* @param {number} [x] (optional) The horizontal position on the workspace * @param {number} [x] (optional) The horizontal position on the workspace

View File

@ -507,6 +507,9 @@
$("#node-input-complete").val($("#node-input-typed-complete").typedInput('value')); $("#node-input-complete").val($("#node-input-typed-complete").typedInput('value'));
} }
$("#node-input-statusVal").val($("#node-input-typed-status").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() { function onAdd() {
RED.actions.invoke("core:generate-node-names",this)
for (var i=0;i<this.links.length;i++) { for (var i=0;i<this.links.length;i++) {
var n = RED.nodes.node(this.links[i]); var n = RED.nodes.node(this.links[i]);
if (n && n.links.indexOf(this.id) === -1) { if (n && n.links.indexOf(this.id) === -1) {
@ -286,7 +288,10 @@
oneditsave: function() { oneditsave: function() {
onEditSave(this); onEditSave(this);
}, },
oneditresize: resizeNodeList oneditresize: resizeNodeList,
onadd: function() {
RED.actions.invoke("core:generate-node-names",this)
}
}); });
RED.nodes.registerType('link out',{ RED.nodes.registerType('link out',{