-
Notifications
You must be signed in to change notification settings - Fork 7
Set up React Flow and add a base Diagram component #10 #47
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
Open
handreyrc
wants to merge
4
commits into
serverlessworkflow:main
Choose a base branch
from
handreyrc:add-react-flow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
107 changes: 107 additions & 0 deletions
107
packages/serverless-workflow-diagram-editor/src/react-flow/diagram/Diagram.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,107 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import * as React from "react"; | ||
| import * as RF from "@xyflow/react"; | ||
| import "@xyflow/react/dist/style.css"; | ||
| import "./Diagram.css"; | ||
handreyrc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const FIT_VIEW_OPTIONS: RF.FitViewOptions = { maxZoom: 1, minZoom: 0.1, duration: 400 }; | ||
|
|
||
| // TODO: Nodes and Edges are hardcoded for now to generate a renderable basic workflow | ||
| // It shall be replaced by the actual implementation based on graph structure | ||
| const initialNodes: RF.Node[] = [ | ||
| { id: "n1", position: { x: 100, y: 0 }, data: { label: "Node 1" } }, | ||
| { id: "n2", position: { x: 100, y: 100 }, data: { label: "Node 2" } }, | ||
| { id: "n3", position: { x: 0, y: 200 }, data: { label: "Node 3" } }, | ||
| { id: "n4", position: { x: 200, y: 200 }, data: { label: "Node 4" } }, | ||
| { id: "n5", position: { x: 100, y: 300 }, data: { label: "Node 5" } }, | ||
| ]; | ||
| const initialEdges: RF.Edge[] = [ | ||
| { id: "n1-n2", source: "n1", target: "n2" }, | ||
| { id: "n2-n3", source: "n2", target: "n3" }, | ||
| { id: "n2-n4", source: "n2", target: "n4" }, | ||
| { id: "n3-n5", source: "n3", target: "n5" }, | ||
| { id: "n4-n5", source: "n4", target: "n5" }, | ||
| ]; | ||
|
|
||
| /** | ||
| * Diagram component API | ||
| */ | ||
| export type DiagramRef = { | ||
| doSomething: () => void; // TODO: to be implemented, it is just a placeholder | ||
| }; | ||
|
|
||
| export type DiagramProps = { | ||
| divRef?: React.RefObject<HTMLDivElement | null>; | ||
| ref?: React.Ref<DiagramRef>; | ||
| }; | ||
|
|
||
| export const Diagram = ({ divRef, ref }: DiagramProps) => { | ||
handreyrc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const [minimapVisible, setMinimapVisible] = React.useState(false); | ||
|
|
||
| const [nodes, setNodes] = React.useState<RF.Node[]>(initialNodes); | ||
| const [edges, setEdges] = React.useState<RF.Edge[]>(initialEdges); | ||
|
|
||
| const onNodesChange = React.useCallback<RF.OnNodesChange>( | ||
| (changes) => setNodes((nodesSnapshot) => RF.applyNodeChanges(changes, nodesSnapshot)), | ||
| [], | ||
| ); | ||
| const onEdgesChange = React.useCallback<RF.OnEdgesChange>( | ||
| (changes) => setEdges((edgesSnapshot) => RF.applyEdgeChanges(changes, edgesSnapshot)), | ||
| [], | ||
| ); | ||
|
|
||
| React.useImperativeHandle( | ||
| ref, | ||
| () => ({ | ||
| doSomething: () => { | ||
| // TODO: to be implemented, it is just a placeholder | ||
| }, | ||
| }), | ||
| [], | ||
| ); | ||
|
|
||
| return ( | ||
| <div ref={divRef} className={"diagram-container"} data-testid={"diagram-container"}> | ||
| <RF.ReactFlow | ||
| nodes={nodes} | ||
| edges={edges} | ||
| onNodesChange={onNodesChange} | ||
| onEdgesChange={onEdgesChange} | ||
| onlyRenderVisibleElements={true} | ||
| zoomOnDoubleClick={false} | ||
| elementsSelectable={true} | ||
| panOnScroll={true} | ||
| zoomOnScroll={false} | ||
| preventScrolling={true} | ||
| selectionOnDrag={true} | ||
| fitView | ||
| > | ||
| {minimapVisible && <RF.MiniMap pannable zoomable position={"top-right"} />} | ||
|
|
||
| <RF.Controls | ||
| fitViewOptions={FIT_VIEW_OPTIONS} | ||
| position={"bottom-right"} | ||
| showInteractive={false} | ||
| > | ||
| <RF.ControlButton onClick={() => setMinimapVisible(!minimapVisible)}>M</RF.ControlButton> | ||
| </RF.Controls> | ||
| <RF.Background className="diagram-background" variant={RF.BackgroundVariant.Cross} /> | ||
| </RF.ReactFlow> | ||
| </div> | ||
| ); | ||
| }; | ||
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
41 changes: 41 additions & 0 deletions
41
packages/serverless-workflow-diagram-editor/tests/react-flow/diagram/Diagram.test.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,41 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { render, screen } from "@testing-library/react"; | ||
| import { Diagram } from "../../../src/react-flow/diagram/Diagram"; | ||
| import { vi, test, expect, afterEach, describe } from "vitest"; | ||
|
|
||
| describe("Diagram Component", () => { | ||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| test("Renders react flow nodes", async () => { | ||
| render(<Diagram />); | ||
|
|
||
| const node1 = screen.getByText("Node 1"); | ||
| const node2 = screen.getByText("Node 2"); | ||
| const node3 = screen.getByText("Node 3"); | ||
| const node4 = screen.getByText("Node 4"); | ||
| const node5 = screen.getByText("Node 5"); | ||
|
|
||
| expect(node1).toBeInTheDocument(); | ||
| expect(node2).toBeInTheDocument(); | ||
| expect(node3).toBeInTheDocument(); | ||
| expect(node4).toBeInTheDocument(); | ||
| expect(node5).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
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.