Further improve docs and comments

This commit is contained in:
GogoVega 2024-11-17 14:22:34 +01:00
parent 7ba7333310
commit 4aff2ae08a
No known key found for this signature in database
GPG Key ID: E1E048B63AC5AC2B

View File

@ -114,7 +114,7 @@ RED.history = (function () {
}
}
// Remove each links
// Remove each link
if (ev.links) {
inverseEv.links = [];
for (let i = 0; i < ev.links.length; i++) {
@ -177,13 +177,14 @@ RED.history = (function () {
}
}
// TODO: why?
// Mark each subflow node as changed (because node added)
if (ev.subflow) {
inverseEv.subflow = {};
if (ev.subflow.instances) {
inverseEv.subflow.instances = [];
ev.subflow.instances.forEach(function (n) {
inverseEv.subflow.instances.push(n);
// TODO: no need getNode, n is the node
const node = RED.nodes.node(n.id);
if (node) {
node.changed = n.changed;
@ -192,6 +193,7 @@ RED.history = (function () {
});
}
// Set the changed prop of subflow (tab)
if (ev.subflow.hasOwnProperty("changed")) {
const subflow = RED.nodes.subflow(ev.subflow.id);
if (subflow) {
@ -224,7 +226,7 @@ RED.history = (function () {
* @property {Array<Junction>} [junctions] An array with removed junctions
* @property {Array<Link>} [links] An array with removed links
* @property {Array<Node>} [nodes] An array with removed nodes
* @property {{ id?: string; instances?: Array<Subflow>; status?: string;}} [subflow]
* @property {{ id?: string; instances?: Array<Node>; status?: string;}} [subflow]
* @property {Array<SubflowNode>} [subflowInputs] An array with removed subflow input
* @property {Array<SubflowNode>} [subflowOutputs] An array with removed subflow outputs
* @property {Array<Subflow>} [subflows] An array with removed subflows (tabs)
@ -253,6 +255,7 @@ RED.history = (function () {
}
}
// Add each subflow (tab)
if (ev.subflows) {
inverseEv.subflows = [];
for (let i = 0; i < ev.subflows.length; i++) {
@ -262,12 +265,14 @@ RED.history = (function () {
}
}
// Update the input array - 0 or one element
if (ev.subflowInputs && ev.subflowInputs.length > 0) {
subflow = RED.nodes.subflow(ev.subflowInputs[0].z);
subflow.in.push(ev.subflowInputs[0]);
subflow.in[0].dirty = true;
}
// Update the output array - 0 or more elements
if (ev.subflowOutputs && ev.subflowOutputs.length > 0) {
subflow = RED.nodes.subflow(ev.subflowOutputs[0].z);
ev.subflowOutputs.sort(function (a, b) { return a.i - b.i });
@ -288,13 +293,14 @@ RED.history = (function () {
}
}
// TODO: why?
if (ev.subflow) {
inverseEv.subflow = {};
// Mark each subflow node as changed
if (ev.subflow.hasOwnProperty("instances")) {
inverseEv.subflow.instances = [];
ev.subflow.instances.forEach(function (n) {
inverseEv.subflow.instances.push(n);
// TODO: Use n instead of getNode
const node = RED.nodes.node(n.id);
if (node) {
node.changed = n.changed;
@ -303,6 +309,7 @@ RED.history = (function () {
});
}
// Add the status (redo)
if (ev.subflow.hasOwnProperty("status")) {
const subflow = RED.nodes.subflow(ev.subflow.id);
subflow.status = ev.subflow.status;
@ -310,6 +317,7 @@ RED.history = (function () {
}
if (subflow) {
// TODO: use subflow.instances
RED.nodes.filterNodes({ type: "subflow:" + subflow.id }).forEach(function (n) {
n.inputs = subflow.in.length;
n.outputs = subflow.out.length;
@ -443,7 +451,7 @@ RED.history = (function () {
dirty: RED.nodes.dirty()
};
// Move each nodes
// Move each node
for (let i = 0; i < ev.nodes.length; i++) {
const n = ev.nodes[i];
const rn = { n: n.n, ox: n.n.x, oy: n.n.y, dirty: true, moved: n.n.moved };
@ -472,13 +480,13 @@ RED.history = (function () {
}
}
// Remove each nodes from the group
// Remove each node from the group
if (ev.addToGroup) {
RED.group.removeFromGroup(ev.addToGroup, ev.nodes.map(function (n) { return n.n }), false);
inverseEv.removeFromGroup = ev.addToGroup;
}
// Add each nodes to the group
// Add each node to the group
if (ev.removeFromGroup) {
RED.group.addToGroup(ev.removeFromGroup, ev.nodes.map(function (n) { return n.n }));
inverseEv.addToGroup = ev.removeFromGroup;
@ -534,7 +542,7 @@ RED.history = (function () {
}
}
// Remove each links (from the conversion - inside the subflow)
// Remove each link (from the conversion - inside the subflow)
if (ev.links) {
inverseEv.links = [];
for (let i = 0; i < ev.links.length; i++) {
@ -548,7 +556,7 @@ RED.history = (function () {
RED.nodes.removeSubflow(ev.subflow.subflow);
RED.workspaces.remove(ev.subflow.subflow);
// Add each links (from the undo of conversion - active workspace)
// Add each link (from the undo of conversion - active workspace)
if (ev.removedLinks) {
inverseEv.createdLinks = [];
for (let i = 0; i < ev.removedLinks.length; i++) {
@ -621,7 +629,7 @@ RED.history = (function () {
});
}
// Add each links (from redo of conversion - inside the subflow)
// Add each link (from redo of conversion - inside the subflow)
if (ev.links) {
inverseEv.links = [];
for (let i = 0; i < ev.links.length; i++) {
@ -630,7 +638,7 @@ RED.history = (function () {
}
}
// Remove each links (from redo of conversion - on active workspace)
// Remove each link (from redo of conversion - on active workspace)
if (ev.createdLinks) {
inverseEv.removedLinks = [];
for (let i = 0; i < ev.createdLinks.length; i++) {
@ -648,7 +656,9 @@ RED.history = (function () {
* @typedef {object} ReorderEvent
* @property {"reorder"} t The history event type
* @property {{ from: string; to: string; z: string; }} [nodes]
* @property {{ from: Array<string>; to: Array<string>; }} [workspaces]
* @property {object} [workspaces]
* @property {Array<string>} workspaces[].from Ordered array of workspace ids
* @property {Array<string>} workspaces[].to Ordered array of workspace ids
*
* @param {ReorderEvent} ev The history event to undo
* @returns {ReorderEvent} The generated history event to redo
@ -667,6 +677,7 @@ RED.history = (function () {
RED.workspaces.order(ev.workspaces.from);
}
// TODO: still used? Maybe replace by convertToSubflow
if (ev.nodes) {
inverseEv.nodes = {
z: ev.nodes.z,
@ -696,7 +707,7 @@ RED.history = (function () {
groups: []
};
// For each group, remove each nodes from the group then delete the group
// For each group, remove each node from the group then delete the group
if (ev.groups) {
for (let i = 0; i < ev.groups.length; i++) {
inverseEv.groups.push(ev.groups[i]);
@ -731,7 +742,7 @@ RED.history = (function () {
ev.groups[i].nodes = [];
// Create the group
RED.nodes.addGroup(ev.groups[i]);
// Add each nodes to the created group
// Add each node to the created group
RED.group.addToGroup(ev.groups[i], nodes);
if (ev.groups[i].g) {
// If the created group has a parent group, add to it
@ -754,7 +765,7 @@ RED.history = (function () {
* @property {Group} group The group in which remove nodes
* @property {Array<Node> | Node} [nodes] An array of nodes or one node
* to remove from the group
* @property {boolean} [reparent]
* @property {boolean} [reparent] Either to re-add to parent group
*
* @param {AddToGroupEvent} ev The history event to undo
* @returns {RemoveFromGroupEvent} The generated history event to redo
@ -768,7 +779,7 @@ RED.history = (function () {
reparent: ev.reparent
};
// Remove each nodes from the group
// Remove each node from the group
if (ev.nodes) {
RED.group.removeFromGroup(ev.group, ev.nodes, (ev.hasOwnProperty("reparent") && ev.hasOwnProperty("reparent") !== undefined) ? ev.reparent : true);
}
@ -784,7 +795,7 @@ RED.history = (function () {
* @property {Group} group The group in which add nodes
* @property {Array<Node> | Node} [nodes] An array of nodes or one node
* to add to the group
* @property {boolean} [reparent]
* @property {boolean} [reparent] Either to re-add to parent group
*
* @param {RemoveFromGroupEvent} ev The history event to undo
* @returns {AddToGroupEvent} The generated history event to redo
@ -798,7 +809,7 @@ RED.history = (function () {
reparent: ev.reparent
};
// Add each nodes to the group
// Add each node to the group
if (ev.nodes) {
RED.group.addToGroup(ev.group, ev.nodes);
}
@ -815,10 +826,10 @@ RED.history = (function () {
* @property {object} changes An object with previous node properties value
* @property {Array<Link>} [createdLinks] An array with links to create (redo)
* @property {Array<Link>} [links] An array with removed links
* @property {Node} node The current node
* @property {Node} node The current node/subflow
* @property {object} [outputMap]
* @property {{ instances?: Array<Subflow>; inputCount?: number;
* outputCount?: number; status?: string; }} [subflow]
* @property {{ instances?: Array<Node>; inputCount?: number;
* outputCount?: number; status?: string; }} [subflow] Subflow properties
*
* @param {EditEvent} ev The history event to undo
* @returns {EditEvent} The generated history event to redo
@ -834,6 +845,8 @@ RED.history = (function () {
// Apply old values to the current node
// And saves values changed by old values for the redo
// TODO: Create a function like `RED.editor.updateNodeproperties`?
// => `RED.nodes.updateNodeproperties(node, changes)`
for (const i in ev.changes) {
if (ev.changes.hasOwnProperty(i)) {
inverseEv.changes[i] = ev.node[i];
@ -844,6 +857,8 @@ RED.history = (function () {
nodeList = [nodeList];
}
// Remove the node from each config node users
// TODO: remove only if ot used
nodeList.forEach(function (id) {
const currentConfigNode = RED.nodes.node(id);
if (currentConfigNode && currentConfigNode._def.category === "config") {
@ -857,6 +872,7 @@ RED.history = (function () {
nodeList = [nodeList];
}
// Add the node to each config node users
nodeList.forEach(function (id) {
const newConfigNode = RED.nodes.node(id);
if (newConfigNode && newConfigNode._def.category === "config") {
@ -898,6 +914,7 @@ RED.history = (function () {
if (ev.subflow) {
inverseEv.subflow = {};
// Update the input array - 0 or one element
if (ev.subflow.hasOwnProperty("inputCount")) {
inverseEv.subflow.inputCount = ev.node.in.length;
if (ev.node.in.length > ev.subflow.inputCount) {
@ -908,6 +925,7 @@ RED.history = (function () {
}
}
// Update the outputs array - 0 or more elements
if (ev.subflow.hasOwnProperty("outputCount")) {
inverseEv.subflow.outputCount = ev.node.out.length;
if (ev.node.out.length > ev.subflow.outputCount) {
@ -918,10 +936,12 @@ RED.history = (function () {
}
}
// Mark each subflow node as changed
if (ev.subflow.hasOwnProperty("instances")) {
inverseEv.subflow.instances = [];
ev.subflow.instances.forEach(function (n) {
inverseEv.subflow.instances.push(n);
// TODO: why check if the node exist?
const node = RED.nodes.node(n.id);
if (node) {
node.changed = n.changed;
@ -940,6 +960,7 @@ RED.history = (function () {
// Validate the subflow (tab)
RED.editor.validateNode(ev.node);
// Update each subflow node and validate it
// TODO: instances prop can be used here
RED.nodes.filterNodes({ type: "subflow:" + ev.node.id }).forEach(function (n) {
n.inputs = ev.node.in.length;
n.outputs = ev.node.out.length;
@ -987,18 +1008,26 @@ RED.history = (function () {
/**
* Called on the `replace` history event
*
* @typedef {object} ReplaceEvent
* @typedef {object} CompleteReplaceEvent
* @property {"replace"} t The history event type
* @property {Record<string, boolean>} [changed] An object with a node id
* @property {Record<string, boolean>} changed An object with a node id
* as key and the node changed property as value
* @property {boolean} [complete] If the {@link ReplaceEvent.config}
* @property {true} complete Either the {@link ReplaceEvent.config}
* property contains the complete flows
* @property {Array<Node>} config An array with the complete flows
* @property {boolean} dirty The dirty state before replacement
* @property {Record<string, boolean>} moved An object with a node id
* as key and the node moved property as value
* @property {string} rev A revision version
*
* @typedef {object} IncompleteReplaceEvent
* @property {"replace"} t The history event type
* @property {false} [complete] Either the {@link ReplaceEvent.config}
* property contains the complete flows
* @property {Array<Node>} config An array with config nodes and/or
* subflow definitions to replace. Can be the complete flows too
* @property {boolean} [dirty] The dirty state before replacement
* @property {Record<string, boolean>} [moved] An object with a node id
* as key and the node moved property as value
* @property {string} [rev] A revision version
* subflow definitions to replace.
*
* @typedef {CompleteReplaceEvent|IncompleteReplaceEvent} ReplaceEvent
*
* @param {ReplaceEvent} ev The history event to undo
* @returns {ReplaceEvent} The generated history event to redo
@ -1141,6 +1170,7 @@ RED.history = (function () {
}
if (ev.callback && typeof ev.callback === "function") {
inverseEv = inverseEv || {};
inverseEv.callback = ev.callback;
ev.callback(ev);
}