Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow node name to be auto-generated when added #3478

Merged
merged 2 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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" : "")
Expand Down
13 changes: 13 additions & 0 deletions packages/node_modules/@node-red/editor-client/src/js/ui/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,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 = {};
Expand Down Expand Up @@ -1388,6 +1400,7 @@ RED.utils = (function() {
getNodeIcon: getNodeIcon,
getNodeLabel: getNodeLabel,
getNodeColor: getNodeColor,
getPaletteLabel: getPaletteLabel,
clearNodeColorCache: clearNodeColorCache,
addSpinnerOverlay: addSpinnerOverlay,
decodeObject: decodeObject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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");

Expand Down Expand Up @@ -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); })
Expand Down Expand Up @@ -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() })
},
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
* @param {string} type The node type to create
* @param {number} [x] (optional) The horizontal position on the workspace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
});
})();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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',{
Expand Down