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

resolve PR issues...

- change default keymap to `ALT-L L`
- Move `RED.nodes.createNode` to `RED.view.createNode`
- replace `selectedLinks` with `selection().links`
This commit is contained in:
Steve-Mcl 2022-03-05 11:06:13 +00:00
parent 62315fd478
commit fce4f0c116
3 changed files with 95 additions and 10 deletions

View File

@ -91,6 +91,6 @@
"alt-a c": "core:align-selection-to-center", "alt-a c": "core:align-selection-to-center",
"alt-a h": "core:distribute-selection-horizontally", "alt-a h": "core:distribute-selection-horizontally",
"alt-a v": "core:distribute-selection-vertically", "alt-a v": "core:distribute-selection-vertically",
"alt-l": "core:split-wire-with-link-nodes" "alt-l l": "core:split-wire-with-link-nodes"
} }
} }

View File

@ -814,7 +814,7 @@ RED.view.tools = (function() {
* @param {Object || Object[]} wires The wire(s) to split and replace with link-out, link-in nodes. * @param {Object || Object[]} wires The wire(s) to split and replace with link-out, link-in nodes.
*/ */
function splitWiresWithLinkNodes(wires) { function splitWiresWithLinkNodes(wires) {
let wiresToSplit = wires || RED.view.selectedLinks.toArray(); let wiresToSplit = wires || RED.view.selection().links;
if (!Array.isArray(wiresToSplit)) { if (!Array.isArray(wiresToSplit)) {
wiresToSplit = [wiresToSplit]; wiresToSplit = [wiresToSplit];
} }
@ -863,7 +863,7 @@ RED.view.tools = (function() {
let nnLinkOut = nodeSrcMap[linkOutMapId]; let nnLinkOut = nodeSrcMap[linkOutMapId];
//Create a Link Out if one is not already present //Create a Link Out if one is not already present
if(!nnLinkOut) { if(!nnLinkOut) {
const nLinkOut = RED.nodes.createNode("link out"); //create link node const nLinkOut = RED.view.createNode("link out"); //create link node
nnLinkOut = nLinkOut.node; nnLinkOut = nLinkOut.node;
nodeSrcMap[linkOutMapId] = nnLinkOut; nodeSrcMap[linkOutMapId] = nnLinkOut;
let yOffset = 0; let yOffset = 0;
@ -899,7 +899,7 @@ RED.view.tools = (function() {
let nnLinkIn = nodeTrgMap[nTrg.id]; let nnLinkIn = nodeTrgMap[nTrg.id];
//Create a Link In if one is not already present //Create a Link In if one is not already present
if(!nnLinkIn) { if(!nnLinkIn) {
const nLinkIn = RED.nodes.createNode("link in"); //create link node const nLinkIn = RED.view.createNode("link in"); //create link node
nnLinkIn = nLinkIn.node; nnLinkIn = nLinkIn.node;
nodeTrgMap[nTrg.id] = nnLinkIn; nodeTrgMap[nTrg.id] = nnLinkIn;
updateNewNodePosXY(nTrg, nnLinkIn, true, RED.view.snapGrid, 0); updateNewNodePosXY(nTrg, nnLinkIn, true, RED.view.snapGrid, 0);
@ -1031,7 +1031,7 @@ RED.view.tools = (function() {
RED.actions.add("core:wire-series-of-nodes", function() { wireSeriesOfNodes() }) RED.actions.add("core:wire-series-of-nodes", function() { wireSeriesOfNodes() })
RED.actions.add("core:wire-node-to-multiple", function() { wireNodeToMultiple() }) RED.actions.add("core:wire-node-to-multiple", function() { wireNodeToMultiple() })
RED.actions.add("core:split-wire-with-link-nodes",function(){splitWiresWithLinkNodes(RED.view.selectedLinks.length() ? RED.view.selectedLinks.toArray() : null);}); RED.actions.add("core:split-wire-with-link-nodes", function () { splitWiresWithLinkNodes() });
// RED.actions.add("core:add-node", function() { addNode() }) // RED.actions.add("core:add-node", function() { addNode() })
}, },

View File

@ -449,7 +449,7 @@ RED.view = (function() {
drop: function( event, ui ) { drop: function( event, ui ) {
d3.event = event; d3.event = event;
var selected_tool = $(ui.draggable[0]).attr("data-palette-type"); var selected_tool = $(ui.draggable[0]).attr("data-palette-type");
var result = RED.nodes.createNode(selected_tool); var result = createNode(selected_tool);
if (!result) { if (!result) {
return; return;
} }
@ -1107,7 +1107,7 @@ RED.view = (function() {
keepAdding = false; keepAdding = false;
resetMouseVars(); resetMouseVars();
} }
var result = RED.nodes.createNode(type); var result = createNode(type);
if (!result) { if (!result) {
return; return;
} }
@ -5411,6 +5411,93 @@ RED.view = (function() {
return selection; return selection;
} }
/**
* Create a node from a type string.
* **NOTE:** Can throw on error - use `try` `catch` block when calling
* @param {string} type The node type to create
* @param {number} [x] (optional) The horizontal position on the workspace
* @param {number} [y] (optional)The vertical on the workspace
* @param {string} [z] (optional) The flow tab this node will belong to. Defaults to active workspace.
* @returns An object containing the `node` and a `historyEvent`
* @private
*/
function createNode(type, x, y, z) {
var m = /^subflow:(.+)$/.exec(type);
var activeSubflow = z ? RED.nodes.subflow(z) : null;
if (activeSubflow && m) {
var subflowId = m[1];
if (subflowId === activeSubflow.id) {
throw new Error(RED._("notification.error", { message: RED._("notification.errors.cannotAddSubflowToItself") }))
}
if (RED.nodes.subflowContains(m[1], activeSubflow.id)) {
throw new Error(RED._("notification.error", { message: RED._("notification.errors.cannotAddCircularReference") }))
}
}
var nn = { id: RED.nodes.id(), z: z || RED.workspaces.active() };
nn.type = type;
nn._def = RED.nodes.getType(nn.type);
if (!m) {
nn.inputs = nn._def.inputs || 0;
nn.outputs = nn._def.outputs;
for (var d in nn._def.defaults) {
if (nn._def.defaults.hasOwnProperty(d)) {
if (nn._def.defaults[d].value !== undefined) {
nn[d] = JSON.parse(JSON.stringify(nn._def.defaults[d].value));
}
}
}
if (nn._def.onadd) {
try {
nn._def.onadd.call(nn);
} catch (err) {
console.log("Definition error: " + nn.type + ".onadd:", err);
}
}
} else {
var subflow = RED.nodes.subflow(m[1]);
nn.name = "";
nn.inputs = subflow.in.length;
nn.outputs = subflow.out.length;
}
nn.changed = true;
nn.moved = true;
nn.w = RED.view.node_width;
nn.h = Math.max(RED.view.node_height, (nn.outputs || 0) * 15);
nn.resize = true;
if (x != null && typeof x == "number" && x >= 0) {
nn.x = x;
}
if (y != null && typeof y == "number" && y >= 0) {
nn.y = y;
}
var historyEvent = {
t: "add",
nodes: [nn.id],
dirty: RED.nodes.dirty()
}
if (activeSubflow) {
var subflowRefresh = RED.subflow.refresh(true);
if (subflowRefresh) {
historyEvent.subflow = {
id: activeSubflow.id,
changed: activeSubflow.changed,
instances: subflowRefresh.instances
}
}
}
return {
node: nn,
historyEvent: historyEvent
}
}
function calculateNodeDimensions(node) { function calculateNodeDimensions(node) {
var result = [node_width,node_height]; var result = [node_width,node_height];
try { try {
@ -5495,9 +5582,7 @@ RED.view = (function() {
}, },
selection: getSelection, selection: getSelection,
clearSelection: clearSelection, clearSelection: clearSelection,
get selectedLinks() { createNode: createNode,
return selectedLinks;
},
/** default node width */ /** default node width */
get node_width() { get node_width() {
return node_width; return node_width;