-
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
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,52 @@ | ||
| /* | ||
| * 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); | ||
| } | ||
|
|
||
| export function copyMermaidToClipboard(mermaidCode: 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(mermaidCode); | ||
| } | ||
|
|
||
| export function downloadMermaidFile(mermaidCode: string, filename: string = "mermaid.mmd"): void { | ||
|
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. With this the filename is always Updated code in previous comment |
||
| if (typeof document === "undefined") { | ||
| throw new Error("Document API is not available in this environment"); | ||
| } | ||
|
|
||
| const blob = new Blob([mermaidCode], { type: "text/plain" }); | ||
| 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); | ||
|
cheryl7114 marked this conversation as resolved.
|
||
| } | ||
|
cheryl7114 marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,10 @@ export const en = { | |
| "sidebar.sectionSource": "Source", | ||
| "sidebar.viewSource": "View source", | ||
| "sidebar.noDetails": "No additional details for this node", | ||
| "sidebar.exportMermaid": "Export to Mermaid", | ||
|
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. I dont see this used anywhere? |
||
| "sidebar.exportMermaid.copy": "Copy Mermaid Code", | ||
| "sidebar.exportMermaid.download": "Download as Mermaid File", | ||
| "sidebar.exportMermaid.copied": "Copied!", | ||
| } as const; | ||
|
|
||
| export type TranslationKeys = keyof typeof en; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,19 +17,28 @@ | |
| import * as React from "react"; | ||
| import type * as RF from "@xyflow/react"; | ||
| import { useI18n } from "@serverlessworkflow/i18n"; | ||
| import { Workflow, Info, Box } from "lucide-react"; | ||
| import { Sidebar, SidebarContent, SidebarHeader, useSidebar } from "@/components/ui/sidebar"; | ||
| import { Workflow, Info, Box, Copy, Download } from "lucide-react"; | ||
| import { | ||
| Sidebar, | ||
| SidebarContent, | ||
| SidebarHeader, | ||
| useSidebar, | ||
| SidebarFooter, | ||
| } from "@/components/ui/sidebar"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { useDiagramEditorContext } from "@/store/DiagramEditorContext"; | ||
| import { WorkflowInfoView } from "@/side-panel/WorkflowInfoView"; | ||
| import { NodeDetailsView } from "@/side-panel/NodeDetailsView"; | ||
| import { taskNodeConfigMap, type LeafNodeType } from "@/react-flow/nodes/taskNodeConfig"; | ||
| import type { BaseNodeData } from "@/react-flow/nodes/Nodes"; | ||
| import "./SidePanel.css"; | ||
| import { exportToMermaid, copyMermaidToClipboard, downloadMermaidFile } from "@/core"; | ||
|
|
||
| export function SidePanel() { | ||
| const { model, nodes, selectedNodeId } = useDiagramEditorContext(); | ||
| const { setOpen } = useSidebar(); | ||
| const { t } = useI18n(); | ||
| const [isCopied, setIsCopied] = React.useState(false); | ||
|
|
||
| const selectedNode = React.useMemo( | ||
| () => | ||
|
|
@@ -55,6 +64,45 @@ export function SidePanel() { | |
| setOpen(selectedNodeId !== null); | ||
| }, [selectedNodeId, setOpen]); | ||
|
cheryl7114 marked this conversation as resolved.
|
||
|
|
||
| const copyTimeoutRef = React.useRef<ReturnType<typeof setTimeout> | null>(null); | ||
|
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. I would move all mermaid logic into its own file like |
||
| React.useEffect(() => { | ||
| return () => { | ||
| if (copyTimeoutRef.current) { | ||
| clearTimeout(copyTimeoutRef.current); | ||
| } | ||
| }; | ||
| }, []); | ||
|
|
||
| const handleCopyMermaid = async () => { | ||
| if (!model) return; | ||
| try { | ||
| const mermaidCode = exportToMermaid(model); | ||
| await copyMermaidToClipboard(mermaidCode); | ||
| setIsCopied(true); | ||
|
|
||
| if (copyTimeoutRef.current) { | ||
| clearTimeout(copyTimeoutRef.current); | ||
| } | ||
|
|
||
| copyTimeoutRef.current = setTimeout(() => { | ||
| setIsCopied(false); | ||
| copyTimeoutRef.current = null; | ||
| }, 2000); | ||
| } catch (error) { | ||
| console.error("Failed to copy mermaid code:", error); | ||
| } | ||
| }; | ||
|
cheryl7114 marked this conversation as resolved.
cheryl7114 marked this conversation as resolved.
cheryl7114 marked this conversation as resolved.
|
||
|
|
||
| const handleDownloadMermaid = () => { | ||
| if (!model) return; | ||
| try { | ||
| const mermaidCode = exportToMermaid(model); | ||
| downloadMermaidFile(mermaidCode); | ||
| } catch (error) { | ||
| console.error("Failed to download mermaid file:", error); | ||
| } | ||
| }; | ||
|
cheryl7114 marked this conversation as resolved.
|
||
|
|
||
| return ( | ||
| <Sidebar side="right"> | ||
| <SidebarHeader> | ||
|
|
@@ -92,6 +140,20 @@ export function SidePanel() { | |
| </> | ||
| )} | ||
| </SidebarContent> | ||
| <SidebarFooter> | ||
| {model !== null && selectedNodeId === null && ( | ||
| <> | ||
|
cheryl7114 marked this conversation as resolved.
|
||
| <Button onClick={handleCopyMermaid} variant="outline" size="sm"> | ||
| <Copy /> | ||
| {isCopied ? t("sidebar.exportMermaid.copied") : t("sidebar.exportMermaid.copy")} | ||
|
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. Would be nice to change the icon as well when it switches to Copied, maybe a check mark or something? |
||
| </Button> | ||
|
cheryl7114 marked this conversation as resolved.
cheryl7114 marked this conversation as resolved.
|
||
| <Button onClick={handleDownloadMermaid} variant="outline" size="sm"> | ||
| <Download /> | ||
| {t("sidebar.exportMermaid.download")} | ||
| </Button> | ||
|
cheryl7114 marked this conversation as resolved.
cheryl7114 marked this conversation as resolved.
|
||
| </> | ||
| )} | ||
| </SidebarFooter> | ||
| </Sidebar> | ||
| ); | ||
| } | ||
| 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", () => { | ||
|
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. Should add more tests for other functions in file |
||
| 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); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copyMermaidToClipboardanddownloadMermaidFileshould probably be moved intolibfolder and not in core which deals with sdk mainlyWe can also make them more generic so they can be reused in the future, so maybe 2 files
lib/clipboard.tsandlib/download.tsDont name anything mermaid as it doesnt need to be specific to mermaid ... you could change the download a bit to something generic like
downloadFile(content: string, filename:string, mimeType = "text/plain")and call it like :