Feature: Draggable edge
This commit is contained in:
@@ -67,6 +67,37 @@ export const addEdge = (edgeParams: Edge | Connection, elements: Elements): Elem
|
||||
return elements.concat(edge);
|
||||
};
|
||||
|
||||
export const updateEdge = (oldEdge: Edge, newConnection: Connection, elements: Elements): Elements => {
|
||||
if (!newConnection.source || !newConnection.target) {
|
||||
throw new Error("Can't create new edge. An edge needs a source and a target.");
|
||||
}
|
||||
|
||||
// make sure that there is node with the target and one with the source id
|
||||
[newConnection.source, newConnection.target].forEach((id) => {
|
||||
const nodeId = id.includes('__') ? id.split('__')[0] : id;
|
||||
|
||||
if (!elements.find((e) => isNode(e) && e.id === nodeId)) {
|
||||
throw new Error(`Can't create edge. Node with id=${nodeId} does not exist.`);
|
||||
}
|
||||
});
|
||||
|
||||
const foundEdge = elements.find(e => isEdge(e) && e.id === oldEdge.id) as Edge;
|
||||
if (!foundEdge) {
|
||||
throw new Error(`The old edge with id=${oldEdge.id} does not exist.`);
|
||||
}
|
||||
|
||||
// Remove old edge and create the new edge with parameters of old edge.
|
||||
const edge = {
|
||||
...oldEdge,
|
||||
id: getEdgeId(newConnection),
|
||||
source: newConnection.source,
|
||||
target: newConnection.target,
|
||||
} as Edge;
|
||||
|
||||
return elements.filter(e => e.id !== oldEdge.id).concat(edge);
|
||||
};
|
||||
|
||||
|
||||
export const pointToRendererPoint = (
|
||||
{ x, y }: XYPosition,
|
||||
[tx, ty, tScale]: Transform,
|
||||
|
||||
Reference in New Issue
Block a user