Skip to content

Commit

Permalink
Allow parameterizing title of mermaid graph/download
Browse files Browse the repository at this point in the history
  • Loading branch information
christhekeele committed Oct 13, 2024
1 parent 1221c26 commit 5e5b292
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
9 changes: 5 additions & 4 deletions assets/packs/mermaid/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ import "./main.css";

mermaid.initialize({ startOnLoad: false });

export function init(ctx, content) {
export function init(ctx, {content, title}) {
ctx.importCSS("main.css")

function render() {
mermaid.render("graph1", content).then(({ svg, bindFunctions }) => {
ctx.root.innerHTML = `
<div id="mermaid">
${svg}
<button id="download" title="Download">⇩</button>
<button id="download" title="Download ${title}">⇩</button>
</div>
`
`;

ctx.root.querySelector("#download").addEventListener("click", (event) => {
var binaryData = [];
binaryData.push(svg);
const downloadBlob = URL.createObjectURL(new Blob(binaryData, {type: "image/svg+xml"}));

const downloadLink = document.createElement("a");
downloadLink.href = downloadBlob;
downloadLink.download = "mermaid.svg";
downloadLink.download = `${title}.svg`;
document.body.appendChild(downloadLink);

downloadLink.dispatchEvent(
Expand Down
8 changes: 5 additions & 3 deletions lib/kino/mermaid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ defmodule Kino.Mermaid do
@doc """
Creates a new kino displaying the given Mermaid graph.
"""
@spec new(binary()) :: t()
def new(content) do
Kino.JS.new(__MODULE__, content, export: fn content -> {"mermaid", content} end)
@spec new(binary(), Keyword.t()) :: t()
def new(content, options \\ []) do
options = Keyword.validate!(options, [:title])
title = Keyword.get(options, :title, "diagram")
Kino.JS.new(__MODULE__, %{content: content, title: title}, export: fn content -> {"mermaid", content} end)
end
end

0 comments on commit 5e5b292

Please sign in to comment.