|
| 1 | +/* |
| 2 | + * Copyright 2021-Present The Serverless Workflow Specification Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as React from "react"; |
| 18 | +import * as RF from "@xyflow/react"; |
| 19 | +import "@xyflow/react/dist/style.css"; |
| 20 | +import "./Diagram.css"; |
| 21 | + |
| 22 | +const FIT_VIEW_OPTIONS: RF.FitViewOptions = { maxZoom: 1, minZoom: 0.1, duration: 400 }; |
| 23 | + |
| 24 | +// TODO: Nodes and Edges are hardcoded for now to generate a renderable basic workflow |
| 25 | +// It shall be replaced by the actual implementation based on graph structure |
| 26 | +const initialNodes: RF.Node[] = [ |
| 27 | + { id: "n1", position: { x: 100, y: 0 }, data: { label: "Node 1" } }, |
| 28 | + { id: "n2", position: { x: 100, y: 100 }, data: { label: "Node 2" } }, |
| 29 | + { id: "n3", position: { x: 0, y: 200 }, data: { label: "Node 3" } }, |
| 30 | + { id: "n4", position: { x: 200, y: 200 }, data: { label: "Node 4" } }, |
| 31 | + { id: "n5", position: { x: 100, y: 300 }, data: { label: "Node 5" } }, |
| 32 | +]; |
| 33 | +const initialEdges: RF.Edge[] = [ |
| 34 | + { id: "n1-n2", source: "n1", target: "n2" }, |
| 35 | + { id: "n2-n3", source: "n2", target: "n3" }, |
| 36 | + { id: "n2-n4", source: "n2", target: "n4" }, |
| 37 | + { id: "n3-n5", source: "n3", target: "n5" }, |
| 38 | + { id: "n4-n5", source: "n4", target: "n5" }, |
| 39 | +]; |
| 40 | + |
| 41 | +/** |
| 42 | + * Diagram component API |
| 43 | + */ |
| 44 | +export type DiagramRef = { |
| 45 | + doSomething: () => void; // TODO: to be implemented, it is just a placeholder |
| 46 | +}; |
| 47 | + |
| 48 | +export type DiagramProps = { |
| 49 | + divRef?: React.RefObject<HTMLDivElement | null>; |
| 50 | + ref?: React.Ref<DiagramRef>; |
| 51 | +}; |
| 52 | + |
| 53 | +export const Diagram = ({ divRef, ref }: DiagramProps) => { |
| 54 | + const [minimapVisible, setMinimapVisible] = React.useState(false); |
| 55 | + |
| 56 | + const [nodes, setNodes] = React.useState<RF.Node[]>(initialNodes); |
| 57 | + const [edges, setEdges] = React.useState<RF.Edge[]>(initialEdges); |
| 58 | + |
| 59 | + const onNodesChange = React.useCallback<RF.OnNodesChange>( |
| 60 | + (changes) => setNodes((nodesSnapshot) => RF.applyNodeChanges(changes, nodesSnapshot)), |
| 61 | + [], |
| 62 | + ); |
| 63 | + const onEdgesChange = React.useCallback<RF.OnEdgesChange>( |
| 64 | + (changes) => setEdges((edgesSnapshot) => RF.applyEdgeChanges(changes, edgesSnapshot)), |
| 65 | + [], |
| 66 | + ); |
| 67 | + |
| 68 | + React.useImperativeHandle( |
| 69 | + ref, |
| 70 | + () => ({ |
| 71 | + doSomething: () => { |
| 72 | + // TODO: to be implemented, it is just a placeholder |
| 73 | + }, |
| 74 | + }), |
| 75 | + [], |
| 76 | + ); |
| 77 | + |
| 78 | + return ( |
| 79 | + <div ref={divRef} className={"diagram-container"} data-testid={"diagram-container"}> |
| 80 | + <RF.ReactFlow |
| 81 | + nodes={nodes} |
| 82 | + edges={edges} |
| 83 | + onNodesChange={onNodesChange} |
| 84 | + onEdgesChange={onEdgesChange} |
| 85 | + onlyRenderVisibleElements={true} |
| 86 | + zoomOnDoubleClick={false} |
| 87 | + elementsSelectable={true} |
| 88 | + panOnScroll={true} |
| 89 | + zoomOnScroll={false} |
| 90 | + preventScrolling={true} |
| 91 | + selectionOnDrag={true} |
| 92 | + fitView |
| 93 | + > |
| 94 | + {minimapVisible && <RF.MiniMap pannable zoomable position={"top-right"} />} |
| 95 | + |
| 96 | + <RF.Controls |
| 97 | + fitViewOptions={FIT_VIEW_OPTIONS} |
| 98 | + position={"bottom-right"} |
| 99 | + showInteractive={false} |
| 100 | + > |
| 101 | + <RF.ControlButton onClick={() => setMinimapVisible(!minimapVisible)}>M</RF.ControlButton> |
| 102 | + </RF.Controls> |
| 103 | + <RF.Background className="diagram-background" variant={RF.BackgroundVariant.Cross} /> |
| 104 | + </RF.ReactFlow> |
| 105 | + </div> |
| 106 | + ); |
| 107 | +}; |
0 commit comments