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 * 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 * @param {Object} node The node to set the name of - if not provided, uses current selection
*/ */
function generateNodeNames(node) { function generateNodeNames(node, options) {
const nodes = node?[node]:RED.view.selection().nodes; 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) { if (nodes && nodes.length > 0) {
// Generate history event if using the workspace selection, // Generate history event if using the workspace selection,
// or if the provided node already exists // 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 historyEvents = []
const typeIndex = {} const typeIndex = {}
let changed = false; let changed = false;
nodes.forEach(n => { nodes.forEach(n => {
if (n._def && n._def.defaults && n._def.defaults.name) { const nodeDef = n._def || RED.nodes.getType(n.type)
const paletteLabel = RED.utils.getPaletteLabel(n.type, n._def) if (nodeDef && nodeDef.defaults && nodeDef.defaults.name) {
const paletteLabel = RED.utils.getPaletteLabel(n.type, nodeDef)
const defaultNodeNameRE = new RegExp('^'+paletteLabel+' (\\d+)$') const defaultNodeNameRE = new RegExp('^'+paletteLabel+' (\\d+)$')
if (!typeIndex.hasOwnProperty(n.type)) { if (!typeIndex.hasOwnProperty(n.type)) {
const existingNodes = RED.nodes.filterNodes({type: n.type}) const existingNodes = RED.nodes.filterNodes({type: n.type})
@ -1004,7 +1017,7 @@ RED.view.tools = (function() {
}) })
typeIndex[n.type] = maxNameNumber + 1 typeIndex[n.type] = maxNameNumber + 1
} }
if (n.name === '') { if ((options.renameBlank && n.name === '') || (options.renameClash && defaultNodeNameRE.test(n.name))) {
if (generateHistory) { if (generateHistory) {
historyEvents.push({ historyEvents.push({
t:'edit', t:'edit',

View File

@ -604,7 +604,7 @@ RED.view = (function() {
RED.actions.add("core:copy-selection-to-internal-clipboard",copySelection); 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: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() }) RED.actions.add("core:detach-selected-nodes", function() { detachSelectedNodes() })
@ -3480,7 +3480,6 @@ RED.view = (function() {
enterActiveGroup(ag); enterActiveGroup(ag);
activeGroup.selected = true; activeGroup.selected = true;
} }
console.log(d3.event);
var cnodes = RED.nodes.getAllFlowNodes(mousedown_node); var cnodes = RED.nodes.getAllFlowNodes(mousedown_node);
for (var n=0;n<cnodes.length;n++) { for (var n=0;n<cnodes.length;n++) {
if (!cnodes[n].selected) { if (!cnodes[n].selected) {
@ -5403,12 +5402,16 @@ RED.view = (function() {
* - addFlow - whether to import nodes to a new tab * - addFlow - whether to import nodes to a new tab
* - touchImport - whether this is a touch import. If not, imported nodes are * - touchImport - whether this is a touch import. If not, imported nodes are
* attachedto mouse for placing - "IMPORT_DRAGGING" state * 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) { function importNodes(newNodesObj,options) {
options = options || { options = options || {
addFlow: false, addFlow: false,
touchImport: false, touchImport: false,
generateIds: false generateIds: false,
generateDefaultNames: false
} }
var addNewFlow = options.addFlow var addNewFlow = options.addFlow
var touchImport = options.touchImport; var touchImport = options.touchImport;
@ -5436,7 +5439,13 @@ RED.view = (function() {
if (!$.isArray(nodesToImport)) { if (!$.isArray(nodesToImport)) {
nodesToImport = [nodesToImport]; nodesToImport = [nodesToImport];
} }
if (options.generateDefaultNames) {
RED.actions.invoke("core:generate-node-names", nodesToImport, {
renameBlank: false,
renameClash: true,
generateHistory: false
})
}
try { try {
var activeSubflowChanged; var activeSubflowChanged;

View File

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

View File

@ -219,6 +219,10 @@
} }
function onAdd() { 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++) { 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) {
@ -231,7 +235,7 @@
category: 'common', category: 'common',
color:"#ddd",//"#87D8CF", color:"#ddd",//"#87D8CF",
defaults: { defaults: {
name: {value:""}, name: { value: "_DEFAULT_" },
links: { value: [], type:"link out[]" } links: { value: [], type:"link out[]" }
}, },
inputs:0, inputs:0,
@ -322,7 +326,7 @@
category: 'common', category: 'common',
color:"#ddd",//"#87D8CF", color:"#ddd",//"#87D8CF",
defaults: { defaults: {
name: {value:""}, name: { value:"_DEFAULT_" },
mode: { value: "link" },// link || return mode: { value: "link" },// link || return
links: { value: [], type:"link in[]" } links: { value: [], type:"link in[]" }
}, },

View File

@ -355,7 +355,7 @@
color:"#fdd0a2", color:"#fdd0a2",
category: 'function', category: 'function',
defaults: { defaults: {
name: {value:""}, name: {value:"_DEFAULT_"},
func: {value:"\nreturn msg;"}, func: {value:"\nreturn msg;"},
outputs: {value:1}, outputs: {value:1},
noerr: {value:0,required:true,validate:function(v) { return !v; }}, noerr: {value:0,required:true,validate:function(v) { return !v; }},
@ -605,6 +605,12 @@
this.finalizeEditor.resize(); this.finalizeEditor.resize();
$("#node-input-libs-container").css("height", (height - 192)+"px"); $("#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)
}
} }
}); });
})(); })();