-
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
base: main
Are you sure you want to change the base?
Changes from all commits
a056412
19194a1
07bff75
f1fa049
ff5109d
449b14a
eef1707
9f90245
2e759d6
b2c417f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@serverlessworkflow/diagram-editor": minor | ||
| --- | ||
|
|
||
| Add mermaid export functionality |
| 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); | ||
| } |
| 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); | ||
| } |
| 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); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * 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 () => { | ||
| if (!model) return; | ||
| 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); | ||
| alert(t("sidebar.exportMermaid.copyError")); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldnt do this, our editor will be embedded in a host application so we cannot have a modal blocking action which this would be. For now the console error is probably enough
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets discuss this in stand up tomorrow since people have conflicting opinions!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lornakelly |
||
| } | ||
|
cheryl7114 marked this conversation as resolved.
cheryl7114 marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| const handleDownloadMermaid = () => { | ||
| if (!model) return; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The sidepanel only renders if there is a model so no need for this check |
||
| 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); | ||
| alert(t("sidebar.exportMermaid.downloadError")); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as before, this should be removed |
||
| } | ||
|
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> | ||
| </> | ||
| ); | ||
| } | ||
| 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); | ||
| }); | ||
| }); | ||
| 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", | ||
| ); | ||
| }); | ||
| }); | ||
| 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(); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.