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
* @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:"_"},
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 === '_') {
this.name = ''
RED.actions.invoke("core:generate-node-names", this)
}
}
});
})();

View File

@ -218,7 +218,10 @@
}
function onAdd() {
RED.actions.invoke("core:generate-node-names",this)
if (this.name === '_') {
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]);
@ -232,7 +235,7 @@
category: 'common',
color:"#ddd",//"#87D8CF",
defaults: {
name: {value:""},
name: { value: "_" },
links: { value: [], type:"link out[]" }
},
inputs:0,
@ -268,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,
@ -318,7 +321,10 @@
},
oneditresize: resizeNodeList,
onadd: function() {
RED.actions.invoke("core:generate-node-names",this)
if (this.name === '_') {
this.name = ''
RED.actions.invoke("core:generate-node-names", this)
}
}
});
@ -326,9 +332,9 @@
category: 'common',
color:"#ddd",//"#87D8CF",
defaults: {
name: {value:""},
name: { value:"_" },
mode: { value: "link" },// link || return
links: { value: [], type:"link in[]"}
links: { value: [], type:"link in[]" }
},
align:"right",
inputs:1,