-
Notifications
You must be signed in to change notification settings - Fork 8
Add extended graph and dummy auto-layout #59 #77
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
34 changes: 34 additions & 0 deletions
34
packages/serverless-workflow-diagram-editor/src/core/autoLayout.ts
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,34 @@ | ||
| /* | ||
| * 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 { ExtendedGraph, Position, Size } from "./graph"; | ||
|
|
||
| export function applyAutoLayout(graph: ExtendedGraph): ExtendedGraph { | ||
| const graphClone = structuredClone(graph); | ||
|
|
||
| // TODO: This is just a temporary implementation until the actual auto-layout engine is integrated | ||
| const nodeSize: Size = { height: 50, width: 70 }; | ||
| let position: Position = { x: 0, y: 0 }; | ||
|
|
||
| // TODO: Containment is not supported for now. | ||
| graphClone.nodes.forEach((node) => { | ||
| node.size = { ...nodeSize }; | ||
| node.position = { ...position }; | ||
| position.y = position.y + 100; | ||
| }); | ||
|
|
||
| return graphClone; | ||
| } |
107 changes: 107 additions & 0 deletions
107
packages/serverless-workflow-diagram-editor/src/core/graph.ts
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 { Graph, GraphEdge, GraphNode, GraphNodeType } from "@serverlessworkflow/sdk"; | ||
|
|
||
| // Override / add multiple properties of a type in a generic way | ||
| export type Override<T, NewProps> = Omit<T, keyof NewProps> & NewProps; | ||
|
|
||
| // Supported edge types | ||
| export enum GraphEdgeType { | ||
| Default = "default", | ||
| Error = "error", | ||
| Condition = "condition", | ||
| } | ||
|
|
||
| export type Point = { | ||
| x: number; | ||
| y: number; | ||
| }; | ||
|
|
||
| export type Position = Point; | ||
|
|
||
| export type Size = { | ||
| height: number; | ||
| width: number; | ||
| }; | ||
|
|
||
| export type WayPoints = Point[]; | ||
|
|
||
| // Add extra properties to GraphNode | ||
| export type ExtendedGraphNode = Override< | ||
| GraphNode, | ||
| { | ||
| position?: Position; | ||
| size?: Size; | ||
| } | ||
| >; | ||
|
|
||
| // Add extra properties to GraphEdge | ||
| export type ExtendedGraphEdge = GraphEdge & { | ||
| type?: GraphEdgeType; | ||
| wayPoints?: WayPoints; | ||
| }; | ||
|
|
||
| export type ExtendedGraph = Override< | ||
| Graph, | ||
| { | ||
| parent?: ExtendedGraph | null; | ||
| nodes: ExtendedGraphNode[]; | ||
| edges: ExtendedGraphEdge[]; | ||
| entryNode: ExtendedGraphNode; | ||
| exitNode: ExtendedGraphNode; | ||
| } | ||
| >; | ||
|
|
||
| export function solveEdgeTypes(graph: ExtendedGraph): ExtendedGraph { | ||
| const graphClone = structuredClone(graph); | ||
|
|
||
| // root level | ||
| setEdgeTypes(graphClone); | ||
| // children n level | ||
| graphClone.nodes.flat().forEach((node) => setEdgeTypes(node as ExtendedGraph)); | ||
|
|
||
| return graphClone; | ||
| } | ||
|
|
||
| function setEdgeTypes(graph: ExtendedGraph): ExtendedGraph { | ||
| if (!graph.edges || !graph.nodes) { | ||
| return graph; | ||
| } | ||
|
|
||
| for (let i = 0; i < graph.nodes.length; i++) { | ||
| const graphNode = graph.nodes[i]! as ExtendedGraph; | ||
|
handreyrc marked this conversation as resolved.
|
||
|
|
||
| for (let j = 0; j < graph.edges.length; j++) { | ||
| const graphEdge = graph.edges[j]!; | ||
|
|
||
| if (graphNode.id === graphEdge.sourceId) { | ||
| switch (graphNode.type) { | ||
| case GraphNodeType.Raise: | ||
| graphEdge.type = GraphEdgeType.Error; | ||
| break; | ||
| case GraphNodeType.Switch: | ||
| graphEdge.type = GraphEdgeType.Condition; | ||
| break; | ||
|
handreyrc marked this conversation as resolved.
|
||
| default: | ||
| graphEdge.type = GraphEdgeType.Default; | ||
| } | ||
| } | ||
| } | ||
|
handreyrc marked this conversation as resolved.
handreyrc marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return graph; | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -15,3 +15,5 @@ | |
| */ | ||
|
|
||
| export * from "./workflowSdk"; | ||
| export * from "./graph"; | ||
| export * from "./autoLayout"; | ||
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
96 changes: 96 additions & 0 deletions
96
...ess-workflow-diagram-editor/tests/core/__snapshots__/workflowSdk.integration.test.ts.snap
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
37 changes: 37 additions & 0 deletions
37
packages/serverless-workflow-diagram-editor/tests/core/autoLayout.integration.test.ts
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,37 @@ | ||
| /* | ||
| * 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 { describe, it, expect } from "vitest"; | ||
| import { applyAutoLayout, buildGraph, parseWorkflow } from "../../src/core"; | ||
| import { BASIC_VALID_WORKFLOW_JSON_TASKS } from "../fixtures/workflows"; | ||
|
|
||
| describe("applyAutoLayout", () => { | ||
| it("apply auto-layout calculated layout to graph elements", () => { | ||
| const result = parseWorkflow(BASIC_VALID_WORKFLOW_JSON_TASKS); | ||
|
|
||
| const graph = applyAutoLayout(buildGraph(result.model!)); | ||
|
|
||
| expect(graph!.nodes).toHaveLength(7); | ||
| expect(graph!.edges).toHaveLength(6); | ||
|
|
||
| let y = 0; | ||
| graph!.nodes.forEach((node) => { | ||
| // TODO coordinates are fixed (y = y + 100) for now | ||
| expect(node.position!.y).toBe(y); | ||
| y += 100; | ||
| }); | ||
| }); | ||
| }); |
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.