Add option to RED.nodes.createCompleteNodeSet to include node dimensions

This allows the linter to use a flow json with more than just position
information.
This commit is contained in:
Nick O'Leary 2021-06-04 13:53:01 +01:00
parent e042ef05a4
commit e9e64f6a44
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 76 additions and 13 deletions

View File

@ -694,11 +694,23 @@ RED.nodes = (function() {
/** /**
* Converts a node to an exportable JSON Object * Converts a node to an exportable JSON Object
**/ **/
function convertNode(n, exportCreds) { function convertNode(n, opts) {
var exportCreds = true;
var exportDimensions = false;
if (opts === false) {
exportCreds = false;
} else if (typeof opts === "object") {
if (opts.hasOwnProperty("credentials")) {
exportCreds = opts.credentials;
}
if (opts.hasOwnProperty("dimensions")) {
exportDimensions = opts.dimensions;
}
}
if (n.type === 'tab') { if (n.type === 'tab') {
return convertWorkspace(n); return convertWorkspace(n);
} }
exportCreds = exportCreds || false;
var node = {}; var node = {};
node.id = n.id; node.id = n.id;
node.type = n.type; node.type = n.type;
@ -771,6 +783,19 @@ RED.nodes = (function() {
if (n._def.category != "config") { if (n._def.category != "config") {
node.x = n.x; node.x = n.x;
node.y = n.y; node.y = n.y;
if (exportDimensions) {
if (!n.hasOwnProperty('w')) {
// This node has not yet been drawn in the view. So we need
// to explicitly calculate its dimensions. Store the result
// on the node as if it had been drawn will save us doing
// it again
var dimensions = RED.view.calculateNodeDimensions(n);
n.w = dimensions[0];
n.h = dimensions[1];
}
node.w = n.w;
node.h = n.h;
}
node.wires = []; node.wires = [];
for(var i=0;i<n.outputs;i++) { for(var i=0;i<n.outputs;i++) {
node.wires.push([]); node.wires.push([]);
@ -810,7 +835,21 @@ RED.nodes = (function() {
return node; return node;
} }
function convertSubflow(n, exportCreds) { function convertSubflow(n, opts) {
var exportCreds = true;
var exportDimensions = false;
if (opts === false) {
exportCreds = false;
} else if (typeof opts === "object") {
if (opts.hasOwnProperty("credentials")) {
exportCreds = opts.credentials;
}
if (opts.hasOwnProperty("dimensions")) {
exportDimensions = opts.dimensions;
}
}
var node = {}; var node = {};
node.id = n.id; node.id = n.id;
node.type = n.type; node.type = n.type;
@ -982,11 +1021,10 @@ RED.nodes = (function() {
return nns; return nns;
} }
//TODO: rename this (createCompleteNodeSet) // Create the Flow JSON for the current configuration
function createCompleteNodeSet(exportCredentials) { // opts.credentials (whether to include (known) credentials) - default: true
if (exportCredentials === undefined) { // opts.dimensions (whether to include node dimensions) - default: false
exportCredentials = true; function createCompleteNodeSet(opts) {
}
var nns = []; var nns = [];
var i; var i;
for (i=0;i<workspacesOrder.length;i++) { for (i=0;i<workspacesOrder.length;i++) {
@ -996,22 +1034,22 @@ RED.nodes = (function() {
} }
for (i in subflows) { for (i in subflows) {
if (subflows.hasOwnProperty(i)) { if (subflows.hasOwnProperty(i)) {
nns.push(convertSubflow(subflows[i], exportCredentials)); nns.push(convertSubflow(subflows[i], opts));
} }
} }
for (i in groups) { for (i in groups) {
if (groups.hasOwnProperty(i)) { if (groups.hasOwnProperty(i)) {
nns.push(convertNode(groups[i])); nns.push(convertNode(groups[i], opts));
} }
} }
for (i in configNodes) { for (i in configNodes) {
if (configNodes.hasOwnProperty(i)) { if (configNodes.hasOwnProperty(i)) {
nns.push(convertNode(configNodes[i], exportCredentials)); nns.push(convertNode(configNodes[i], opts));
} }
} }
for (i in nodes) { for (i in nodes) {
if (nodes.hasOwnProperty(i)) { if (nodes.hasOwnProperty(i)) {
nns.push(convertNode(nodes[i], exportCredentials)); nns.push(convertNode(nodes[i], opts));
} }
} }
return nns; return nns;

View File

@ -5044,6 +5044,30 @@ RED.view = (function() {
} }
return selection; return selection;
} }
function calculateNodeDimensions(node) {
var result = [node_width,node_height];
try {
var isLink = (node.type === "link in" || node.type === "link out")
var hideLabel = node.hasOwnProperty('l')?!node.l : isLink;
var label = RED.utils.getNodeLabel(node, node.type);
var labelParts = getLabelParts(label, "red-ui-flow-node-label");
if (hideLabel) {
result[1] = Math.max(node_height,(node.outputs || 0) * 15);
} else {
result[1] = Math.max(6+24*labelParts.lines.length,(node.outputs || 0) * 15, 30);
}
if (hideLabel) {
result[0] = node_height;
} else {
result[0] = Math.max(node_width,20*(Math.ceil((labelParts.width+50+(node._def.inputs>0?7:0))/20)) );
}
}catch(err) {
console.log("Error",node);
}
return result;
}
return { return {
init: init, init: init,
state:function(state) { state:function(state) {
@ -5275,6 +5299,7 @@ RED.view = (function() {
return clipboard return clipboard
}, },
redrawStatus: redrawStatus, redrawStatus: redrawStatus,
showQuickAddDialog:showQuickAddDialog showQuickAddDialog:showQuickAddDialog,
calculateNodeDimensions: calculateNodeDimensions
}; };
})(); })();