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

Merge pull request #3538 from node-red/fix-name-generator

Fix name auto-generator to leave blank names alone on copy/paste
This commit is contained in:
Stephen McLaughlin 2022-04-25 20:33:55 +01:00 committed by GitHub
commit 143b807e9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 20 deletions

View File

@ -977,18 +977,31 @@ RED.view.tools = (function() {
* 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;
function generateNodeNames(node, options) {
options = options || {
renameBlank: true,
renameClash: true,
generateHistory: true
}
let nodes = node;
if (node) {
if (!Array.isArray(node)) {
nodes = [ node ]
}
} else {
nodes = 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 generateHistory = options.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 nodeDef = n._def || RED.nodes.getType(n.type)
if (nodeDef && nodeDef.defaults && nodeDef.defaults.name) {
const paletteLabel = RED.utils.getPaletteLabel(n.type, nodeDef)
const defaultNodeNameRE = new RegExp('^'+paletteLabel+' (\\d+)$')
if (!typeIndex.hasOwnProperty(n.type)) {
const existingNodes = RED.nodes.filterNodes({type: n.type})
@ -1004,7 +1017,7 @@ RED.view.tools = (function() {
})
typeIndex[n.type] = maxNameNumber + 1
}
if (n.name === '') {
if ((options.renameBlank && n.name === '') || (options.renameClash && defaultNodeNameRE.test(n.name))) {
if (generateHistory) {
historyEvents.push({
t:'edit',

View File

@ -455,7 +455,7 @@ RED.view = (function() {
}
});
//add search to status-toolbar
//add search to status-toolbar
RED.statusBar.add({
id: "view-search-tools",
align: "left",
@ -604,7 +604,7 @@ RED.view = (function() {
RED.actions.add("core:copy-selection-to-internal-clipboard",copySelection);
RED.actions.add("core:cut-selection-to-internal-clipboard",function(){copySelection();deleteSelection();});
RED.actions.add("core:paste-from-internal-clipboard",function(){importNodes(clipboard,{generateIds: true});});
RED.actions.add("core:paste-from-internal-clipboard",function(){importNodes(clipboard,{generateIds: true, generateDefaultNames: true});});
RED.actions.add("core:detach-selected-nodes", function() { detachSelectedNodes() })
@ -3480,7 +3480,6 @@ RED.view = (function() {
enterActiveGroup(ag);
activeGroup.selected = true;
}
console.log(d3.event);
var cnodes = RED.nodes.getAllFlowNodes(mousedown_node);
for (var n=0;n<cnodes.length;n++) {
if (!cnodes[n].selected) {
@ -5403,12 +5402,16 @@ RED.view = (function() {
* - addFlow - whether to import nodes to a new tab
* - touchImport - whether this is a touch import. If not, imported nodes are
* attachedto mouse for placing - "IMPORT_DRAGGING" state
* - generateIds - whether to automatically generate new ids for all imported nodes
* - generateDefaultNames - whether to automatically update any nodes with clashing
* default names
*/
function importNodes(newNodesObj,options) {
options = options || {
addFlow: false,
touchImport: false,
generateIds: false
generateIds: false,
generateDefaultNames: false
}
var addNewFlow = options.addFlow
var touchImport = options.touchImport;
@ -5436,7 +5439,13 @@ RED.view = (function() {
if (!$.isArray(nodesToImport)) {
nodesToImport = [nodesToImport];
}
if (options.generateDefaultNames) {
RED.actions.invoke("core:generate-node-names", nodesToImport, {
renameBlank: false,
renameClash: true,
generateHistory: false
})
}
try {
var activeSubflowChanged;

View File

@ -74,7 +74,7 @@
RED.nodes.registerType('debug',{
category: 'common',
defaults: {
name: {value:""},
name: {value:"_DEFAULT_"},
active: {value:true},
tosidebar: {value:true},
console: {value:false},
@ -546,7 +546,10 @@
$("#node-input-statusVal").val($("#node-input-typed-status").typedInput('value'));
},
onadd: function() {
RED.actions.invoke("core:generate-node-names",this)
if (this.name === '_DEFAULT_') {
this.name = ''
RED.actions.invoke("core:generate-node-names", this)
}
}
});
})();

View File

@ -219,6 +219,10 @@
}
function onAdd() {
if (this.name === '_DEFAULT_') {
this.name = ''
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) {
@ -231,7 +235,7 @@
category: 'common',
color:"#ddd",//"#87D8CF",
defaults: {
name: {value:""},
name: { value: "_DEFAULT_" },
links: { value: [], type:"link out[]" }
},
inputs:0,
@ -267,9 +271,9 @@
category: 'common',
color:"#ddd",//"#87D8CF",
defaults: {
name: {value:""},
links: { value: [], type:"link in[]"},
linkType: {value:"static"},
name: { value: "" },
links: { value: [], type:"link in[]" },
linkType: { value:"static" },
timeout: { value: "30", validate:RED.validators.number(true) }
},
inputs: 1,
@ -322,9 +326,9 @@
category: 'common',
color:"#ddd",//"#87D8CF",
defaults: {
name: {value:""},
name: { value:"_DEFAULT_" },
mode: { value: "link" },// link || return
links: { value: [], type:"link in[]"}
links: { value: [], type:"link in[]" }
},
align:"right",
inputs:1,

View File

@ -355,7 +355,7 @@
color:"#fdd0a2",
category: 'function',
defaults: {
name: {value:""},
name: {value:"_DEFAULT_"},
func: {value:"\nreturn msg;"},
outputs: {value:1},
noerr: {value:0,required:true,validate:function(v) { return !v; }},
@ -605,6 +605,12 @@
this.finalizeEditor.resize();
$("#node-input-libs-container").css("height", (height - 192)+"px");
},
onadd: function() {
if (this.name === '_DEFAULT_') {
this.name = ''
RED.actions.invoke("core:generate-node-names", this)
}
}
});
})();