feat(utils): add getIncomers function closes #461

This commit is contained in:
moklick
2020-09-10 23:54:53 +02:00
parent 801f773e9a
commit 7f64ffe173
3 changed files with 28 additions and 3 deletions
+11 -1
View File
@@ -1,6 +1,7 @@
import { Store } from 'easy-peasy';
import store, { StoreModel } from '../store';
import { ElementId, Node, Edge, Elements, Transform, XYPosition, Rect, FitViewParams, Box, Connection } from '../types';
import { Store } from 'easy-peasy';
export const isEdge = (element: Node | Connection | Edge): element is Edge =>
'id' in element && 'source' in element && 'target' in element;
@@ -17,6 +18,15 @@ export const getOutgoers = (node: Node, elements: Elements): Node[] => {
return elements.filter((e) => outgoerIds.includes(e.id)) as Node[];
};
export const getIncomers = (node: Node, elements: Elements): Node[] => {
if (!isNode(node)) {
return [];
}
const incomersIds = elements.filter((e) => isEdge(e) && e.target === node.id).map((e) => (e as Edge).source);
return elements.filter((e) => incomersIds.includes(e.id)) as Node[];
};
export const removeElements = (elementsToRemove: Elements, elements: Elements): Elements => {
const nodeIdsToRemove = elementsToRemove.map((n) => n.id);