Skip to content

Integrate ELKjs for automatic diagram layout#152

Open
handreyrc wants to merge 6 commits into
serverlessworkflow:mainfrom
handreyrc:add-elkjs-engine
Open

Integrate ELKjs for automatic diagram layout#152
handreyrc wants to merge 6 commits into
serverlessworkflow:mainfrom
handreyrc:add-elkjs-engine

Conversation

@handreyrc
Copy link
Copy Markdown
Contributor

Closes #54

Summary

This PR replaces the dummy auto-layout placeholder for an actual layout engine capable of layout nodes and edges.

Changes

  • Added latest ELKjs library as a dependency.
  • Added core function to consume ELK layout workers as an asynchronous operation.
  • Replaced dummy layout implemention in autoLayout.ts for an actual implementation that takes react flow nodes and edges, transforms it to elkGraph, calculate the layout then applies it back to react flow nodes and edges.
  • Integrated diagramBuilder and auto-layout in the Diagram component in order to render layouted workflows.
  • Tests for graph manipulation and auto-layout integration.
image

Copilot AI review requested due to automatic review settings May 20, 2026 21:47
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Integrates ELK-based auto-layout into the diagram editor and expands test coverage around layout graph conversion, application, and error handling.

Changes:

  • Add elkjs dependency and introduce processElkLayout core helper.
  • Replace the previous placeholder auto-layout with ELK graph building + layout application (including edge bendpoints).
  • Update Diagram rendering/tests to use ReactFlowProvider and add extensive layout-focused tests.

Reviewed changes

Copilot reviewed 11 out of 12 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
pnpm-workspace.yaml Adds elkjs to the workspace catalog for consistent versioning.
packages/serverless-workflow-diagram-editor/package.json Consumes elkjs from the workspace catalog.
packages/serverless-workflow-diagram-editor/src/core/index.ts Re-exports new ELK layout helper from core.
packages/serverless-workflow-diagram-editor/src/core/elkjs.ts Adds processElkLayout wrapper around ELK with error handling.
packages/serverless-workflow-diagram-editor/src/react-flow/diagram/autoLayout.ts Implements ELK graph conversion, layout application, and bendpoint mapping.
packages/serverless-workflow-diagram-editor/src/react-flow/diagram/diagramBuilder.ts Stops applying auto-layout inside the builder; returns raw nodes/edges.
packages/serverless-workflow-diagram-editor/src/react-flow/diagram/Diagram.tsx Applies auto-layout asynchronously on model changes and fits view.
packages/serverless-workflow-diagram-editor/src/diagram-editor/DiagramEditor.tsx Adds ReactFlowProvider to support useReactFlow() usage.
packages/serverless-workflow-diagram-editor/tests/react-flow/diagram/Diagram.test.tsx Wraps Diagram with ReactFlowProvider in tests.
packages/serverless-workflow-diagram-editor/tests/react-flow/diagram/autoLayout.integration.test.ts Replaces old placeholder assertions with detailed auto-layout unit/integration coverage.
packages/serverless-workflow-diagram-editor/tests/core/elkjs.test.ts Adds unit tests for processElkLayout success and error scenarios.
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/serverless-workflow-diagram-editor/src/react-flow/diagram/autoLayout.ts Outdated
Comment thread packages/serverless-workflow-diagram-editor/src/react-flow/diagram/autoLayout.ts Outdated
Comment thread packages/serverless-workflow-diagram-editor/src/react-flow/diagram/Diagram.tsx Outdated
Comment thread packages/serverless-workflow-diagram-editor/tests/core/elkjs.test.ts Outdated
Copy link
Copy Markdown
Member

@fantonangeli fantonangeli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@handreyrc I left some comment. Please let me know what you think

if (isActive) {
setNodes(nodes);
setEdges(edges);
reactFlowInstance.fitView();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because useStates are async, reactFlowInstance.fitView(); will be executed before nodes and edges are updated.
I would suggest to move fitView() in a separate useEffect() bounded to nodes and edges

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I too agree with this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fantonangeli @kumaradityaraj ,

This one is interesting because we cannot move fitView() to a separate useEffect(). It would be executed on every change on nodes and edges such as dragging a node (x/y changes).
I digged a bit about this matter and the recommended approach for cases like that is to create a microtask with setTimeout, so fitView will be queued to run after React updates the DOM.
e.g.
setTimeout(() => reactFlowInstance.fitView() , 0);

I fixed it this way.

WDYT?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also add a test to verify applyAutoLayout() is called. Wdyt?

});

// Helper function to setup ELK mock with success or error response
function setupElkMock(returnValue: ElkNode | Error | string | null | undefined | object) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • have you tried to take this function out from the describe, taking elkMockLayout as an input?
  • returnValue: ElkNode | Error | string | null | undefined | object I think is just like returnValue: any

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

}

// Test data factory for simple graphs
function createSimpleGraph(nodeCount: number = 2): ElkNode {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as my previous comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

Copilot AI review requested due to automatic review settings May 21, 2026 13:57
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 13 changed files in this pull request and generated 4 comments.

Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

Comment thread packages/serverless-workflow-diagram-editor/tests/core/elkjs.test.ts Outdated
Comment thread packages/serverless-workflow-diagram-editor/src/react-flow/diagram/Diagram.tsx Outdated
Comment thread pnpm-workspace.yaml Outdated
@handreyrc handreyrc requested a review from fantonangeli May 21, 2026 14:44
@handreyrc
Copy link
Copy Markdown
Contributor Author

@fantonangeli @kumaradityaraj,

This PR is ready for reviews again.

Thanks!

handreyrc added 5 commits May 21, 2026 17:16
Signed-off-by: handreyrc <handrey.cunha@gmail.com>
Signed-off-by: handreyrc <handrey.cunha@gmail.com>

# Conflicts:
#	packages/serverless-workflow-diagram-editor/src/diagram-editor/DiagramEditor.tsx
Signed-off-by: handreyrc <handrey.cunha@gmail.com>
Signed-off-by: handreyrc <handrey.cunha@gmail.com>
Signed-off-by: handreyrc <handrey.cunha@gmail.com>
Copilot AI review requested due to automatic review settings May 21, 2026 21:29
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 13 changed files in this pull request and generated 7 comments.

Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

Comment thread packages/serverless-workflow-diagram-editor/vitest.config.ts
Comment thread packages/serverless-workflow-diagram-editor/vitest.config.ts
Comment thread packages/serverless-workflow-diagram-editor/src/core/elkjs.ts
Comment thread packages/serverless-workflow-diagram-editor/src/react-flow/diagram/Diagram.tsx Outdated
Signed-off-by: handreyrc <handrey.cunha@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Integrate ELKjs for automatic diagram layout

4 participants