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

Merge pull request #3478 from node-red/auto-gen-name

Allow node name to be auto-generated when added
This commit is contained in:
Nick O'Leary 2022-03-14 19:35:58 +00:00 committed by GitHub
commit 3649f10600
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 94 additions and 14 deletions

View File

@ -224,14 +224,7 @@ RED.palette = (function() {
var d = $('<div>',{class:"red-ui-palette-node"}).attr("data-palette-type",nt).data('category',rootCategory);
var label = nt;///^(.*?)([ -]in|[ -]out)?$/.exec(nt)[1];
if (typeof def.paletteLabel !== "undefined") {
try {
label = (typeof def.paletteLabel === "function" ? def.paletteLabel.call(def) : def.paletteLabel)||"";
} catch(err) {
console.log("Definition error: "+nt+".paletteLabel",err);
}
}
var label = RED.utils.getPaletteLabel(nt, def);///^(.*?)([ -]in|[ -]out)?$/.exec(nt)[1];
$('<div/>', {
class: "red-ui-palette-label"+(((!def.align && def.inputs !== 0 && def.outputs === 0) || "right" === def.align) ? " red-ui-palette-label-right" : "")

View File

@ -1109,6 +1109,18 @@ RED.utils = (function() {
return RED.text.bidi.enforceTextDirectionWithUCC(l);
}
function getPaletteLabel(nodeType, def) {
var label = nodeType;
if (typeof def.paletteLabel !== "undefined") {
try {
label = (typeof def.paletteLabel === "function" ? def.paletteLabel.call(def) : def.paletteLabel)||"";
} catch(err) {
console.log("Definition error: "+nodeType+".paletteLabel",err);
}
}
return label
}
var nodeColorCache = {};
function clearNodeColorCache() {
nodeColorCache = {};
@ -1394,6 +1406,7 @@ RED.utils = (function() {
getNodeIcon: getNodeIcon,
getNodeLabel: getNodeLabel,
getNodeColor: getNodeColor,
getPaletteLabel: getPaletteLabel,
clearNodeColorCache: clearNodeColorCache,
addSpinnerOverlay: addSpinnerOverlay,
decodeObject: decodeObject,

View File

@ -811,7 +811,7 @@ RED.view.tools = (function() {
/**
* 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) {
let wiresToSplit = wires || RED.view.selection().links;
@ -868,7 +868,7 @@ RED.view.tools = (function() {
nodeSrcMap[linkOutMapId] = nnLinkOut;
let yOffset = 0;
if(nSrc.outputs > 1) {
const CENTER_PORT = (((nSrc.outputs-1) / 2) + 1);
const offsetCount = Math.abs(CENTER_PORT - (srcPort + 1));
yOffset = (_gridSize * 2 * offsetCount);
@ -918,7 +918,7 @@ RED.view.tools = (function() {
t: 'add',
links: [link],
});
}
}
//connect the link out/link in virtual wires
if(nnLinkIn.links.indexOf(nnLinkOut.id) == -1) {
@ -937,9 +937,9 @@ RED.view.tools = (function() {
}
//add all history events to stack
RED.history.push(history);
//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) });
selectConnected("down");
@ -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',{