Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mermaid-export.md
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
Expand Up @@ -19,3 +19,4 @@ export * from "./graph";
export * from "./taskDetails";
export * from "./taskSubType";
export * from "./elkjs";
export * from "./mermaidExport";
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 {
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);
Comment thread
cheryl7114 marked this conversation as resolved.
}
Comment thread
cheryl7114 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"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
Expand Up @@ -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(
() =>
Expand All @@ -55,6 +64,45 @@ export function SidePanel() {
setOpen(selectedNodeId !== null);
}, [selectedNodeId, setOpen]);
Comment thread
cheryl7114 marked this conversation as resolved.

const copyTimeoutRef = React.useRef<ReturnType<typeof setTimeout> | null>(null);
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);
}
};
Comment thread
cheryl7114 marked this conversation as resolved.
Comment thread
cheryl7114 marked this conversation as resolved.
Comment thread
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);
}
};
Comment thread
cheryl7114 marked this conversation as resolved.

return (
<Sidebar side="right">
<SidebarHeader>
Expand Down Expand Up @@ -92,6 +140,20 @@ export function SidePanel() {
</>
)}
</SidebarContent>
<SidebarFooter>
{model !== null && selectedNodeId === null && (
<>
Comment thread
cheryl7114 marked this conversation as resolved.
<Button onClick={handleCopyMermaid} variant="outline" size="sm">
<Copy />
{isCopied ? t("sidebar.exportMermaid.copied") : t("sidebar.exportMermaid.copy")}
</Button>
Comment thread
cheryl7114 marked this conversation as resolved.
Comment thread
cheryl7114 marked this conversation as resolved.
<Button onClick={handleDownloadMermaid} variant="outline" size="sm">
<Download />
{t("sidebar.exportMermaid.download")}
</Button>
Comment thread
cheryl7114 marked this conversation as resolved.
Comment thread
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", () => {
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
Expand Up @@ -14,12 +14,14 @@
* limitations under the License.
*/

import { describe, it, expect } from "vitest";
import { describe, it, expect, vi, afterEach } from "vitest";
import { screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { SidePanel } from "../../src/side-panel/SidePanel";
import { parseWorkflow } from "../../src/core/workflowSdk";
import { renderWithProviders } from "../test-utils/render-helpers";
import { WORKFLOW_WITH_METADATA_JSON } from "../fixtures/workflows";
import * as mermaidExport from "../../src/core/mermaidExport";

describe("SidePanel", () => {
it("renders sidebar with workflow info when model is present", () => {
Expand All @@ -39,4 +41,66 @@ describe("SidePanel", () => {

expect(screen.queryByTestId("workflow-info")).not.toBeInTheDocument();
});

it("renders export buttons when model is present and no node is selected", () => {
const { model } = parseWorkflow(WORKFLOW_WITH_METADATA_JSON);
renderWithProviders(<SidePanel />, { model, selectedNodeId: null });
expect(screen.getByText(/Copy Mermaid Code/i)).toBeInTheDocument();
expect(screen.getByText(/Download as Mermaid File/i)).toBeInTheDocument();
});

it("does not render export buttons when a node is selected", () => {
const { model } = parseWorkflow(WORKFLOW_WITH_METADATA_JSON);
const mockNode = {
id: "some-node-id",
type: "set",
position: { x: 0, y: 0 },
data: { label: "Test Node" },
};

renderWithProviders(<SidePanel />, {
model,
selectedNodeId: "some-node-id",
nodes: [mockNode],
});
expect(screen.queryByText(/Copy Mermaid Code/i)).not.toBeInTheDocument();
expect(screen.queryByText(/Download as Mermaid File/i)).not.toBeInTheDocument();
});

it("does not render export buttons when model is null", () => {
renderWithProviders(<SidePanel />, { model: null });

expect(screen.queryByText(/Copy Mermaid Code/i)).not.toBeInTheDocument();
expect(screen.queryByText(/Download as Mermaid File/i)).not.toBeInTheDocument();
});

it("should call copyMermaidToClipboard when copy button is clicked", async () => {
const user = userEvent.setup();
const { model } = parseWorkflow(WORKFLOW_WITH_METADATA_JSON);
const copySpy = vi.spyOn(mermaidExport, "copyMermaidToClipboard").mockResolvedValue(undefined);
vi.spyOn(mermaidExport, "exportToMermaid").mockReturnValue("mermaid code");
Comment thread
cheryl7114 marked this conversation as resolved.

renderWithProviders(<SidePanel />, { model });
const copyButton = screen.getByText(/Copy Mermaid Code/i);
await user.click(copyButton);
Comment thread
cheryl7114 marked this conversation as resolved.

expect(copySpy).toHaveBeenCalledWith("mermaid code");
});

it("should call downloadMermaidFile when download button is clicked", async () => {
const user = userEvent.setup();
const { model } = parseWorkflow(WORKFLOW_WITH_METADATA_JSON);
const downloadSpy = vi.spyOn(mermaidExport, "downloadMermaidFile").mockImplementation(() => {});
vi.spyOn(mermaidExport, "exportToMermaid").mockReturnValue("mermaid code");

renderWithProviders(<SidePanel />, { model });
const downloadButton = screen.getByText(/Download as Mermaid File/i);
await user.click(downloadButton);

expect(downloadSpy).toHaveBeenCalledWith("mermaid code");
});

afterEach(() => {
vi.restoreAllMocks();
});
});
Loading