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

Fix name auto-generator to leave blank names alone on copy/paste

This commit is contained in:
Nick O'Leary 2022-04-19 19:17:41 +01:00
parent a7932da207
commit 908f9562f6
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
4 changed files with 52 additions and 21 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:"_"},
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 === '_') {
this.name = ''
RED.actions.invoke("core:generate-node-names", this) RED.actions.invoke("core:generate-node-names", this)
} }
}
}); });
})(); })();
</script> </script>

View File

@ -218,7 +218,10 @@
} }
function onAdd() { function onAdd() {
if (this.name === '_') {
this.name = ''
RED.actions.invoke("core:generate-node-names", this) 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]);
@ -232,7 +235,7 @@
category: 'common', category: 'common',
color:"#ddd",//"#87D8CF", color:"#ddd",//"#87D8CF",
defaults: { defaults: {
name: {value:""}, name: { value: "_" },
links: { value: [], type:"link out[]" } links: { value: [], type:"link out[]" }
}, },
inputs:0, inputs:0,
@ -268,7 +271,7 @@
category: 'common', category: 'common',
color:"#ddd",//"#87D8CF", color:"#ddd",//"#87D8CF",
defaults: { defaults: {
name: {value:""}, name: { value: "_" },
links: { value: [], type:"link in[]" }, links: { value: [], type:"link in[]" },
linkType: { value:"static" }, linkType: { value:"static" },
timeout: { value: "30", validate:RED.validators.number(true) } timeout: { value: "30", validate:RED.validators.number(true) }
@ -318,15 +321,18 @@
}, },
oneditresize: resizeNodeList, oneditresize: resizeNodeList,
onadd: function() { onadd: function() {
if (this.name === '_') {
this.name = ''
RED.actions.invoke("core:generate-node-names", this) RED.actions.invoke("core:generate-node-names", this)
} }
}
}); });
RED.nodes.registerType('link out',{ RED.nodes.registerType('link out',{
category: 'common', category: 'common',
color:"#ddd",//"#87D8CF", color:"#ddd",//"#87D8CF",
defaults: { defaults: {
name: {value:""}, name: { value:"_" },
mode: { value: "link" },// link || return mode: { value: "link" },// link || return
links: { value: [], type:"link in[]" } links: { value: [], type:"link in[]" }
}, },