feat(utils): add getHandleBounds

This commit is contained in:
moklick
2023-03-14 13:15:36 +01:00
parent 13e7cc340c
commit 03fd2b9568
+34
View File
@@ -7,6 +7,8 @@ import type {
BaseNode,
BaseEdge,
NodeOrigin,
HandleElement,
Position,
} from '@reactflow/system';
import { getConnectedEdgesBase } from './graph';
@@ -193,3 +195,35 @@ export const getPositionWithOrigin = ({
y: y - height * origin[1],
};
};
export const getHandleBounds = (
selector: string,
nodeElement: HTMLDivElement,
zoom: number,
nodeOrigin: NodeOrigin = [0, 0]
): HandleElement[] | null => {
const handles = nodeElement.querySelectorAll(selector);
if (!handles || !handles.length) {
return null;
}
const handlesArray = Array.from(handles) as HTMLDivElement[];
const nodeBounds = nodeElement.getBoundingClientRect();
const nodeOffset = {
x: nodeBounds.width * nodeOrigin[0],
y: nodeBounds.height * nodeOrigin[1],
};
return handlesArray.map((handle): HandleElement => {
const handleBounds = handle.getBoundingClientRect();
return {
id: handle.getAttribute('data-handleid'),
position: handle.getAttribute('data-handlepos') as unknown as Position,
x: (handleBounds.left - nodeBounds.left - nodeOffset.x) / zoom,
y: (handleBounds.top - nodeBounds.top - nodeOffset.y) / zoom,
...getDimensions(handle),
};
});
};