-
Notifications
You must be signed in to change notification settings - Fork 1
feature/MIG-6602 Implement floating edges #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Position, useNodes } from '@xyflow/react'; | ||
import { ComponentProps } from 'react'; | ||
|
||
import { EMPLOYEES_NODE, ORDERS_NODE } from '@/mocks/datasets/nodes'; | ||
import { render, screen } from '@/mocks/testing-utils'; | ||
import { FloatingEdge } from '@/components/edge/floating-edge'; | ||
|
||
vi.mock('@xyflow/react', async () => { | ||
const actual = await vi.importActual<typeof import('@xyflow/react')>('@xyflow/react'); | ||
return { | ||
...actual, | ||
useNodes: vi.fn(), | ||
}; | ||
}); | ||
|
||
describe('floating-edge', () => { | ||
beforeEach(() => { | ||
const nodes = [ | ||
{ ...ORDERS_NODE, data: { title: ORDERS_NODE.title, fields: ORDERS_NODE.fields } }, | ||
{ ...EMPLOYEES_NODE, data: { title: EMPLOYEES_NODE.title, fields: EMPLOYEES_NODE.fields } }, | ||
]; | ||
const mockedNodes = vi.mocked(useNodes); | ||
mockedNodes.mockReturnValue(nodes); | ||
}); | ||
|
||
const renderComponent = (props?: Partial<ComponentProps<typeof FloatingEdge>>) => { | ||
return render( | ||
<FloatingEdge | ||
sourceX={100} | ||
sourceY={100} | ||
targetX={500} | ||
targetY={500} | ||
sourcePosition={Position.Left} | ||
targetPosition={Position.Top} | ||
id={'orders-to-employees'} | ||
source={'orders'} | ||
target={'employees'} | ||
{...props} | ||
/>, | ||
); | ||
}; | ||
|
||
it('Should render edge', () => { | ||
renderComponent(); | ||
const path = screen.getByTestId('floating-edge-orders-to-employees'); | ||
expect(path).toHaveAttribute('id', 'orders-to-employees'); | ||
expect(path).toHaveAttribute( | ||
'd', | ||
'M240 136L240 156L 240,213Q 240,218 245,218L 381,218Q 386,218 386,223L386 280L386 300', | ||
); | ||
}); | ||
|
||
it('Should not render edge if source does not exist', () => { | ||
renderComponent({ source: 'unknown' }); | ||
expect(screen.queryByTestId('floating-edge-orders-to-employees')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('Should not render edge if target does not exist', () => { | ||
renderComponent({ target: 'unknown' }); | ||
expect(screen.queryByTestId('floating-edge-orders-to-employees')).not.toBeInTheDocument(); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { EdgeProps, getSmoothStepPath, useNodes } from '@xyflow/react'; | ||
import { useMemo } from 'react'; | ||
|
||
import { getEdgeParams } from '@/utilities/get-edge-params'; | ||
import { InternalNode } from '@/types/internal'; | ||
|
||
export const FloatingEdge = ({ id, source, target }: EdgeProps) => { | ||
const nodes = useNodes<InternalNode>(); | ||
|
||
const { sourceNode, targetNode } = useMemo(() => { | ||
const sourceNode = nodes.find(n => n.id === source); | ||
const targetNode = nodes.find(n => n.id === target); | ||
return { sourceNode, targetNode }; | ||
}, [nodes, source, target]); | ||
|
||
if (!sourceNode || !targetNode) return null; | ||
|
||
const { sx, sy, tx, ty, sourcePos, targetPos } = getEdgeParams(sourceNode, targetNode); | ||
|
||
const [path] = getSmoothStepPath({ | ||
sourceX: sx, | ||
sourceY: sy, | ||
sourcePosition: sourcePos, | ||
targetPosition: targetPos, | ||
targetX: tx, | ||
targetY: ty, | ||
}); | ||
|
||
return <path data-testId={`floating-edge-${id}`} className="react-flow__edge-path" d={path} id={id} />; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Edge } from '@/types'; | ||
|
||
export const ORDERS_TO_EMPLOYEES_EDGE: Edge = { | ||
id: 'employees-to-orders', | ||
type: 'floatingEdge', | ||
source: 'employees', | ||
target: 'orders', | ||
markerEnd: 'one', | ||
markerStart: 'one', | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { getEdgeParams } from '@/utilities/get-edge-params'; | ||
import { EMPLOYEES_NODE, ORDERS_NODE } from '@/mocks/datasets/nodes'; | ||
|
||
describe('get-edge-params', () => { | ||
it('Should get parameters', () => { | ||
const result = getEdgeParams( | ||
{ ...ORDERS_NODE, data: { title: ORDERS_NODE.title, fields: ORDERS_NODE.fields } }, | ||
{ ...EMPLOYEES_NODE, data: { title: EMPLOYEES_NODE.title, fields: EMPLOYEES_NODE.fields } }, | ||
); | ||
expect(result).toEqual({ | ||
sourcePos: 'bottom', | ||
sx: 240, | ||
sy: 136, | ||
targetPos: 'top', | ||
tx: 386, | ||
ty: 300, | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Position, XYPosition } from '@xyflow/react'; | ||
|
||
import { InternalNode } from '@/types/internal'; | ||
|
||
/** | ||
* Returns the coordinates where a line connecting the centers of the source and target nodes intersects | ||
* the edges of those nodes. This implementation is copied from: | ||
* https://github.com/xyflow/xyflow/blob/main/examples/react/src/examples/FloatingEdges/utils.ts | ||
* | ||
* @param intersectionNode The source node | ||
* @param targetNode The target node | ||
*/ | ||
const getNodeIntersection = (intersectionNode: InternalNode, targetNode: InternalNode): XYPosition => { | ||
const { width: intersectionNodeWidth, height: intersectionNodeHeight } = intersectionNode.measured ?? { | ||
width: 0, | ||
height: 0, | ||
}; | ||
const targetPosition = targetNode.position; | ||
|
||
const w = (intersectionNodeWidth ?? 0) / 2; | ||
const h = (intersectionNodeHeight ?? 0) / 2; | ||
|
||
const x2 = intersectionNode.position.x + w; | ||
const y2 = intersectionNode.position.y + h; | ||
const x1 = targetPosition.x + w; | ||
const y1 = targetPosition.y + h; | ||
|
||
const xx1 = (x1 - x2) / (2 * w) - (y1 - y2) / (2 * h); | ||
const yy1 = (x1 - x2) / (2 * w) + (y1 - y2) / (2 * h); | ||
const a = 1 / (Math.abs(xx1) + Math.abs(yy1)); | ||
const xx3 = a * xx1; | ||
const yy3 = a * yy1; | ||
const x = w * (xx3 + yy3) + x2; | ||
const y = h * (-xx3 + yy3) + y2; | ||
|
||
return { x, y }; | ||
}; | ||
|
||
/** | ||
* Normalises the edge position based on co-ordinates of the edge | ||
* @param node The node where the edge is pointing from. This implementation is copied from: | ||
* https://github.com/xyflow/xyflow/blob/main/examples/react/src/examples/FloatingEdges/utils.ts | ||
* | ||
* @param intersectionPoint The position of the edge | ||
*/ | ||
const getEdgePosition = (node: InternalNode, intersectionPoint: XYPosition) => { | ||
const n = { ...node.position, ...node }; | ||
const nx = Math.round(n.x); | ||
const ny = Math.round(n.y); | ||
const px = Math.round(intersectionPoint.x); | ||
const py = Math.round(intersectionPoint.y); | ||
|
||
if (px <= nx + 1) { | ||
return Position.Left; | ||
} | ||
if (px >= nx + (n.measured?.width ?? 0) - 1) { | ||
return Position.Right; | ||
} | ||
if (py <= ny + 1) { | ||
return Position.Top; | ||
} | ||
if (py >= n.y + (n.measured?.height ?? 0) - 1) { | ||
return Position.Bottom; | ||
} | ||
|
||
return Position.Top; | ||
}; | ||
|
||
/** | ||
* Returns the coordinates where a line connecting the centers of the source and target nodes intersects | ||
* the edges of those nodes. This implementation is copied from: | ||
* https://github.com/xyflow/xyflow/blob/main/examples/react/src/examples/FloatingEdges/utils.ts | ||
* | ||
* @param source The source node | ||
* @param target The target node | ||
*/ | ||
export const getEdgeParams = (source: InternalNode, target: InternalNode) => { | ||
const sourceIntersectionPoint = getNodeIntersection(source, target); | ||
const targetIntersectionPoint = getNodeIntersection(target, source); | ||
|
||
const sourcePos = getEdgePosition(source, sourceIntersectionPoint); | ||
const targetPos = getEdgePosition(target, targetIntersectionPoint); | ||
|
||
return { | ||
sx: sourceIntersectionPoint.x, | ||
sy: sourceIntersectionPoint.y, | ||
tx: targetIntersectionPoint.x, | ||
ty: targetIntersectionPoint.y, | ||
sourcePos, | ||
targetPos, | ||
}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,10 +30,10 @@ | |
}, | ||
"include": [ | ||
"src", | ||
".storybook" | ||
], | ||
"exclude": [ | ||
"node_modules", | ||
"src/**/*.d.ts", | ||
".storybook" | ||
] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.