-
Notifications
You must be signed in to change notification settings - Fork 65
[DS 2.0] Use the measured source-node height for self-loop edges #121
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@workflowbuilder/sdk': minor | ||
| --- | ||
|
|
||
| Self-connecting edges use the measured source-node height for loop geometry and label placement. `SelfConnectingEdge` reads that height from the React Flow store when `nodeHeight` is omitted, so a custom edge that delegates to it no longer draws the loop as if the node had no height; `useSelfLoopNodeHeight` is exported alongside it. |
87 changes: 87 additions & 0 deletions
87
packages/sdk/src/features/diagram/edges/label-edge/label-edge.spec.tsx
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,87 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import type { EdgeProps } from '@xyflow/react'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import type { WorkflowBuilderEdge } from '../../../../node/node-data'; | ||
| import { LabelEdge } from './label-edge'; | ||
|
|
||
| const { nodeLookup } = vi.hoisted(() => ({ nodeLookup: new Map<string, unknown>() })); | ||
|
|
||
| vi.mock('@xyflow/react', () => ({ | ||
| getSmoothStepPath: () => ['M 0 0', 0, 0], | ||
| useStore: (selector: (state: { nodeLookup: Map<string, unknown> }) => unknown) => selector({ nodeLookup }), | ||
| })); | ||
|
|
||
| vi.mock('../edge-label-renderer/edge-label-renderer', () => ({ | ||
| EdgeLabel: ({ id, labelY }: { id: string; labelY: number }) => ( | ||
| <span data-testid="edge-label" data-edge-label-id={id} data-label-y={labelY} /> | ||
| ), | ||
| })); | ||
|
|
||
| vi.mock('../enhanced-base-edge/enhanced-base-edge', () => ({ | ||
| EnhancedBaseEdge: ({ id, path }: { id: string; path: string }) => ( | ||
| <svg> | ||
| <path data-edge-id={id} d={path} /> | ||
| </svg> | ||
| ), | ||
| })); | ||
|
|
||
| vi.mock('./use-label-edge-hover', () => ({ | ||
| useLabelEdgeHover: () => ({ | ||
| style: {}, | ||
| hovered: false, | ||
| onMouseEnter: vi.fn(), | ||
| onMouseLeave: vi.fn(), | ||
| }), | ||
| })); | ||
|
|
||
| const selfConnectingEdgeProps = { | ||
| id: 'self-loop', | ||
| source: 'node-1', | ||
| target: 'node-1', | ||
| sourceX: 100, | ||
| sourceY: 300, | ||
| targetX: 200, | ||
| targetY: 300, | ||
| sourcePosition: 'right', | ||
| targetPosition: 'left', | ||
| data: { label: 'Loop' }, | ||
| } as EdgeProps<WorkflowBuilderEdge>; | ||
|
|
||
| beforeEach(() => { | ||
| nodeLookup.clear(); | ||
| }); | ||
|
|
||
| describe('LabelEdge', () => { | ||
| it('uses the measured source height for self-loop geometry and label placement', () => { | ||
| nodeLookup.set('node-1', { measured: { height: 80 } }); | ||
|
|
||
| const { container } = render(<LabelEdge {...selfConnectingEdgeProps} />); | ||
|
|
||
| expect(container.querySelector('[data-edge-id="self-loop"]')?.getAttribute('d')).toContain('Q 125 120 109 120'); | ||
| expect(screen.getByTestId('edge-label').dataset.labelY).toBe('120'); | ||
| }); | ||
|
|
||
| it('falls back to the explicit node height when no measurement exists', () => { | ||
| nodeLookup.set('node-1', { measured: {}, height: 40 }); | ||
|
|
||
| render(<LabelEdge {...selfConnectingEdgeProps} />); | ||
|
|
||
| expect(screen.getByTestId('edge-label').dataset.labelY).toBe('160'); | ||
| }); | ||
|
|
||
| it('does not read the node lookup for a regular edge', () => { | ||
| const get = vi.spyOn(nodeLookup, 'get'); | ||
|
|
||
| render(<LabelEdge {...selfConnectingEdgeProps} target="node-2" />); | ||
|
|
||
| expect(get).not.toHaveBeenCalled(); | ||
| expect(screen.getByTestId('edge-label').dataset.labelY).toBe('0'); | ||
| }); | ||
|
|
||
| it('treats an unknown source node as zero height', () => { | ||
| render(<LabelEdge {...selfConnectingEdgeProps} />); | ||
|
|
||
| expect(screen.getByTestId('edge-label').dataset.labelY).toBe('200'); | ||
| }); | ||
| }); | ||
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
62 changes: 62 additions & 0 deletions
62
packages/sdk/src/features/diagram/edges/self-connecting-edge/self-connecting-edge.spec.tsx
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 { render } from '@testing-library/react'; | ||
| import type { EdgeProps } from '@xyflow/react'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import type { WorkflowBuilderEdge } from '../../../../node/node-data'; | ||
| import { SelfConnectingEdge } from './self-connecting-edge'; | ||
|
|
||
| const { nodeLookup } = vi.hoisted(() => ({ nodeLookup: new Map<string, unknown>() })); | ||
|
|
||
| vi.mock('@xyflow/react', () => ({ | ||
| useStore: (selector: (state: { nodeLookup: Map<string, unknown> }) => unknown) => selector({ nodeLookup }), | ||
| })); | ||
|
|
||
| vi.mock('@workflowbuilder/ui', () => ({ | ||
| useEdgeStyle: () => ({}), | ||
| })); | ||
|
|
||
| vi.mock('../enhanced-base-edge/enhanced-base-edge', () => ({ | ||
| EnhancedBaseEdge: ({ id, path }: { id: string; path: string }) => ( | ||
| <svg> | ||
| <path data-edge-id={id} d={path} /> | ||
| </svg> | ||
| ), | ||
| })); | ||
|
|
||
| const loopProps = { | ||
| id: 'self-loop', | ||
| source: 'node-1', | ||
| target: 'node-1', | ||
| sourceX: 100, | ||
| sourceY: 300, | ||
| targetX: 200, | ||
| targetY: 300, | ||
| hovered: false, | ||
| } as unknown as EdgeProps<WorkflowBuilderEdge> & { hovered: boolean }; | ||
|
|
||
| function loopApexY(container: HTMLElement) { | ||
| const d = container.querySelector('[data-edge-id="self-loop"]')?.getAttribute('d') ?? ''; | ||
| return Math.min(...[...d.matchAll(/L \d+ (\d+)/g)].map((match) => Number(match[1]))); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| nodeLookup.clear(); | ||
| }); | ||
|
|
||
| describe('SelfConnectingEdge', () => { | ||
| it('reads the measured source height when nodeHeight is omitted', () => { | ||
| nodeLookup.set('node-1', { measured: { height: 80 } }); | ||
|
|
||
| const { container } = render(<SelfConnectingEdge {...loopProps} />); | ||
|
|
||
| expect(loopApexY(container)).toBe(300 - (80 + 100)); | ||
| }); | ||
|
|
||
| it('prefers an explicit nodeHeight over the store', () => { | ||
| nodeLookup.set('node-1', { measured: { height: 80 } }); | ||
|
|
||
| const { container } = render(<SelfConnectingEdge {...loopProps} nodeHeight={20} />); | ||
|
|
||
| expect(loopApexY(container)).toBe(300 - (20 + 100)); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.