-
Notifications
You must be signed in to change notification settings - Fork 8
Export diagram from model to mermaid code #174
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
ricardozanini
merged 12 commits into
serverlessworkflow:main
from
cheryl7114:mermaid-export-150
Jun 18, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a056412
feat: add mermaid export functionality with copy and download options
19194a1
test: add mermaid export tests
07bff75
add changeset
f1fa049
feat: add copied confirmation and fix copilot complaints
ff5109d
fix copilot complaints
449b14a
fix copilot complaints
eef1707
fix according to suggestions
9f90245
Merge remote-tracking branch 'upstream/main' into mermaid-export-150
2e759d6
fix copilot complaints
b2c417f
fix copilot complaints
497d2dd
fix: add TODO for alert component
c10abc6
Merge branch 'main' into mermaid-export-150
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
Some comments aren't visible on the classic Files Changed page.
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 @@ | ||
| --- | ||
| "@serverlessworkflow/diagram-editor": minor | ||
| --- | ||
|
|
||
| Add mermaid export functionality |
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
27 changes: 27 additions & 0 deletions
27
packages/serverless-workflow-diagram-editor/src/core/mermaidExport.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,27 @@ | ||
| /* | ||
| * 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 { convertToMermaidCode } from "@serverlessworkflow/sdk"; | ||
| import type { Specification } from "@serverlessworkflow/sdk"; | ||
|
|
||
| /** | ||
| * Converts a workflow model to Mermaid diagram code | ||
| * @param workflow - The workflow object (parsed from JSON/YAML) | ||
| * @returns Mermaid diagram code as a string | ||
| */ | ||
| export function exportToMermaid(workflow: Specification.Workflow): string { | ||
| return convertToMermaidCode(workflow); | ||
| } |
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
22 changes: 22 additions & 0 deletions
22
packages/serverless-workflow-diagram-editor/src/lib/clipboard.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,22 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| export function copyToClipboard(text: string): Promise<void> { | ||
| if (typeof navigator === "undefined" || !navigator.clipboard) { | ||
| return Promise.reject(new Error("Clipboard API is not available in this environment")); | ||
| } | ||
| return navigator.clipboard.writeText(text); | ||
| } |
33 changes: 33 additions & 0 deletions
33
packages/serverless-workflow-diagram-editor/src/lib/download.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,33 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| export function downloadFile(content: string, filename: string, mimeType = "text/plain"): void { | ||
| if (typeof document === "undefined") { | ||
| throw new Error("Document API is not available in this environment"); | ||
| } | ||
|
|
||
| const blob = new Blob([content], { type: mimeType }); | ||
| const url = URL.createObjectURL(blob); | ||
| const link = document.createElement("a"); | ||
| link.href = url; | ||
| link.download = filename; | ||
| document.body.appendChild(link); | ||
| link.click(); | ||
| document.body.removeChild(link); | ||
| setTimeout(() => { | ||
| URL.revokeObjectURL(url); | ||
| }, 100); | ||
| } |
87 changes: 87 additions & 0 deletions
87
packages/serverless-workflow-diagram-editor/src/side-panel/MermaidActions.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 @@ | ||
| /* | ||
| * 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 { useI18n } from "@serverlessworkflow/i18n"; | ||
| import { ClipboardPen, Download, ClipboardCheck } from "lucide-react"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { exportToMermaid } from "@/core"; | ||
| import { copyToClipboard } from "@/lib/clipboard"; | ||
| import { downloadFile } from "@/lib/download"; | ||
| import type { Specification } from "@serverlessworkflow/sdk"; | ||
|
|
||
| export function MermaidActions({ model }: { model: Specification.Workflow }): React.JSX.Element { | ||
| const { t } = useI18n(); | ||
| const [isCopied, setIsCopied] = React.useState(false); | ||
| const copyTimeoutRef = React.useRef<ReturnType<typeof setTimeout> | null>(null); | ||
|
cheryl7114 marked this conversation as resolved.
|
||
|
|
||
| React.useEffect(() => { | ||
| return () => { | ||
| if (copyTimeoutRef.current) { | ||
| clearTimeout(copyTimeoutRef.current); | ||
| } | ||
| }; | ||
| }, []); | ||
|
|
||
| const handleCopyMermaid = async () => { | ||
| try { | ||
| const mermaidCode = exportToMermaid(model); | ||
| await copyToClipboard(mermaidCode); | ||
| setIsCopied(true); | ||
|
|
||
| if (copyTimeoutRef.current) { | ||
| clearTimeout(copyTimeoutRef.current); | ||
| } | ||
|
|
||
| copyTimeoutRef.current = setTimeout(() => { | ||
| setIsCopied(false); | ||
| copyTimeoutRef.current = null; | ||
| }, 2000); | ||
|
cheryl7114 marked this conversation as resolved.
cheryl7114 marked this conversation as resolved.
|
||
| } catch (error) { | ||
| console.error("Failed to copy mermaid code:", error); | ||
| // TODO: Create component to show errors to users | ||
| } | ||
|
cheryl7114 marked this conversation as resolved.
cheryl7114 marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| const handleDownloadMermaid = () => { | ||
| try { | ||
| const mermaidCode = exportToMermaid(model); | ||
| const sanitizedName = (model.document?.name || "workflow") | ||
| .replace(/[/\\:*?"<>|]/g, "_") | ||
| .replace(/\s+/g, "_") | ||
| .trim() | ||
| .substring(0, 200); | ||
| const filename = `${sanitizedName}.mmd`; | ||
| downloadFile(mermaidCode, filename); | ||
|
cheryl7114 marked this conversation as resolved.
cheryl7114 marked this conversation as resolved.
|
||
| } catch (error) { | ||
| console.error("Failed to download mermaid file:", error); | ||
| // TODO: Create component to show errors to users | ||
| } | ||
|
cheryl7114 marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <Button onClick={handleCopyMermaid} variant="outline" size="sm"> | ||
| {isCopied ? <ClipboardCheck /> : <ClipboardPen />} | ||
| {isCopied ? t("sidebar.exportMermaid.copied") : t("sidebar.exportMermaid.copy")} | ||
| </Button> | ||
| <Button onClick={handleDownloadMermaid} variant="outline" size="sm"> | ||
| <Download /> | ||
| {t("sidebar.exportMermaid.download")} | ||
| </Button> | ||
| </> | ||
| ); | ||
| } | ||
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
33 changes: 33 additions & 0 deletions
33
packages/serverless-workflow-diagram-editor/tests/core/mermaidExport.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,33 @@ | ||
| /* | ||
| * 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 { exportToMermaid } from "../../src/core/mermaidExport"; | ||
| import { parseWorkflow } from "../../src/core/workflowSdk"; | ||
| import { BASIC_VALID_WORKFLOW_YAML } from "../fixtures/workflows"; | ||
|
|
||
| describe("exportToMermaid", () => { | ||
|
cheryl7114 marked this conversation as resolved.
|
||
| it("converts a valid workflow to Mermaid code", () => { | ||
| const { model } = parseWorkflow(BASIC_VALID_WORKFLOW_YAML); | ||
| expect(model).not.toBeNull(); | ||
|
|
||
| const mermaidCode = exportToMermaid(model!); | ||
| expect(mermaidCode).toBeTruthy(); | ||
| expect(typeof mermaidCode).toBe("string"); | ||
| // Mermaid diagrams start with flowchart | ||
| expect(mermaidCode).toMatch(/flowchart/i); | ||
| }); | ||
| }); | ||
55 changes: 55 additions & 0 deletions
55
packages/serverless-workflow-diagram-editor/tests/lib/clipboard.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,55 @@ | ||
| /* | ||
| * 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 { copyToClipboard } from "../../src/lib/clipboard"; | ||
| import { describe, it, expect, vi, afterEach } from "vitest"; | ||
|
|
||
| describe("copyToClipboard", () => { | ||
| const originalClipboard = Object.getOwnPropertyDescriptor(navigator, "clipboard"); | ||
|
|
||
| afterEach(() => { | ||
| if (originalClipboard) { | ||
| Object.defineProperty(navigator, "clipboard", originalClipboard); | ||
| } else { | ||
| delete (navigator as unknown as { clipboard?: Clipboard }).clipboard; | ||
| } | ||
| }); | ||
|
|
||
| it("copies text to clipboard successfully", async () => { | ||
| const mockWriteText = vi.fn().mockResolvedValue(undefined); | ||
| Object.defineProperty(navigator, "clipboard", { | ||
| value: { writeText: mockWriteText }, | ||
| writable: true, | ||
| configurable: true, | ||
| }); | ||
|
cheryl7114 marked this conversation as resolved.
|
||
|
|
||
| const testCode = "flowchart TD\n A --> B"; | ||
| await copyToClipboard(testCode); | ||
| expect(mockWriteText).toHaveBeenCalledWith(testCode); | ||
| }); | ||
|
|
||
| it("rejects if clipboard API is not available", async () => { | ||
| Object.defineProperty(navigator, "clipboard", { | ||
| value: undefined, | ||
| writable: true, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| await expect(copyToClipboard("test")).rejects.toThrow( | ||
| "Clipboard API is not available in this environment", | ||
| ); | ||
| }); | ||
| }); | ||
51 changes: 51 additions & 0 deletions
51
packages/serverless-workflow-diagram-editor/tests/lib/download.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,51 @@ | ||
| /* | ||
| * 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 { downloadFile } from "../../src/lib/download"; | ||
| import { describe, it, expect, vi, afterEach } from "vitest"; | ||
|
|
||
| describe("downloadFile", () => { | ||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it("creates and triggers file download", () => { | ||
| const mockClick = vi.fn(); | ||
| const mockAppendChild = vi.fn(); | ||
| const mockRemoveChild = vi.fn(); | ||
| const mockElement = { | ||
| click: mockClick, | ||
| href: "", | ||
| download: "", | ||
| }; | ||
|
|
||
| vi.spyOn(document, "createElement").mockReturnValue( | ||
| mockElement as unknown as HTMLAnchorElement, | ||
| ); | ||
| vi.spyOn(document.body, "appendChild").mockImplementation(mockAppendChild); | ||
| vi.spyOn(document.body, "removeChild").mockImplementation(mockRemoveChild); | ||
| vi.spyOn(URL, "createObjectURL").mockReturnValue("blob:mock-url"); | ||
| vi.spyOn(URL, "revokeObjectURL").mockImplementation(vi.fn()); | ||
|
|
||
| const testCode = "flowchart TD\n A --> B"; | ||
| downloadFile(testCode, "test.mmd"); | ||
|
|
||
| expect(document.createElement).toHaveBeenCalledWith("a"); | ||
| expect(mockClick).toHaveBeenCalled(); | ||
| expect(mockAppendChild).toHaveBeenCalled(); | ||
| expect(mockRemoveChild).toHaveBeenCalled(); | ||
| }); | ||
| }); |
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.