-
Notifications
You must be signed in to change notification settings - Fork 7
Add Mermaid diagrams to plans #136
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
150 changes: 150 additions & 0 deletions
150
engine/app/javascript/controllers/coplan/mermaid_controller.js
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,150 @@ | ||
| import { Controller } from "@hotwired/stimulus" | ||
|
|
||
| let diagramId = 0 | ||
| let mermaidPromise | ||
| let configuredTheme | ||
|
|
||
| export default class extends Controller { | ||
| connect() { | ||
| this.renderGeneration ||= 0 | ||
| this.boundThemeChange = () => this.renderDiagrams() | ||
| this.colorSchemeQuery = window.matchMedia("(prefers-color-scheme: dark)") | ||
| window.addEventListener("coplan:theme-changed", this.boundThemeChange) | ||
| this.colorSchemeQuery.addEventListener("change", this.boundThemeChange) | ||
| this.renderDiagrams() | ||
| } | ||
|
|
||
| disconnect() { | ||
| this.renderGeneration += 1 | ||
| window.removeEventListener("coplan:theme-changed", this.boundThemeChange) | ||
| this.colorSchemeQuery.removeEventListener("change", this.boundThemeChange) | ||
| } | ||
|
|
||
| async renderDiagrams() { | ||
| const sources = [ | ||
| ...Array.from(this.element.querySelectorAll('pre[lang="mermaid"] > code'), block => ({ | ||
| container: block.parentElement, | ||
| source: block.textContent | ||
| })), | ||
| ...Array.from(this.element.querySelectorAll(".mermaid-diagram[data-mermaid-source]"), diagram => ({ | ||
| container: diagram, | ||
| source: diagram.dataset.mermaidSource | ||
| })) | ||
| ] | ||
| if (sources.length === 0) return | ||
|
|
||
| let mermaid | ||
| try { | ||
| mermaid = await loadMermaid() | ||
| } catch { | ||
| sources.forEach(({ container }) => this.showError(container)) | ||
| return | ||
| } | ||
|
|
||
| const theme = configureMermaid(mermaid) | ||
| const generation = ++this.renderGeneration | ||
|
|
||
| for (const { container, source } of sources) { | ||
| await this.renderDiagram(mermaid, container, source, theme, generation) | ||
| } | ||
| } | ||
|
|
||
| async renderDiagram(mermaid, sourceContainer, source, theme, generation) { | ||
| const id = `coplan-mermaid-${++diagramId}` | ||
|
|
||
| try { | ||
| const { svg, bindFunctions } = await mermaid.render(id, source) | ||
| if (!this.element.isConnected || generation !== this.renderGeneration) return | ||
|
|
||
| const diagram = document.createElement("div") | ||
| diagram.className = "mermaid-diagram" | ||
| diagram.setAttribute("role", "img") | ||
| diagram.setAttribute("aria-label", "Mermaid diagram") | ||
| diagram.dataset.mermaidSource = source | ||
| diagram.dataset.mermaidTheme = theme | ||
| diagram.innerHTML = svg | ||
| sourceContainer.replaceWith(diagram) | ||
| bindFunctions?.(diagram) | ||
| } catch { | ||
| document.getElementById(id)?.remove() | ||
| document.getElementById(`d${id}`)?.remove() | ||
| this.showError(sourceContainer) | ||
| } | ||
| } | ||
|
|
||
| showError(sourceContainer) { | ||
| if (!sourceContainer.matches('pre[lang="mermaid"]')) return | ||
|
|
||
| sourceContainer.classList.add("mermaid-diagram--error") | ||
| sourceContainer.removeAttribute("lang") | ||
|
|
||
| const message = document.createElement("span") | ||
| message.className = "mermaid-diagram__error-message" | ||
| message.textContent = "Diagram could not be rendered." | ||
| sourceContainer.prepend(message) | ||
| } | ||
| } | ||
|
|
||
| function loadMermaid() { | ||
| if (mermaidPromise) return mermaidPromise | ||
|
|
||
| mermaidPromise = import("mermaid").then(({ default: mermaid }) => mermaid) | ||
| return mermaidPromise | ||
| } | ||
|
|
||
| function configureMermaid(mermaid) { | ||
| const explicitTheme = document.documentElement.dataset.theme | ||
| const dark = explicitTheme === "dark" || | ||
| (!explicitTheme && window.matchMedia("(prefers-color-scheme: dark)").matches) | ||
| const theme = dark ? "dark" : "light" | ||
| if (theme === configuredTheme) return theme | ||
|
|
||
| mermaid.initialize({ | ||
| startOnLoad: false, | ||
| securityLevel: "strict", | ||
| suppressErrorRendering: true, | ||
| theme: dark ? "base" : "default", | ||
| ...(dark && { themeVariables: darkThemeVariables() }) | ||
| }) | ||
| configuredTheme = theme | ||
| return theme | ||
| } | ||
|
|
||
| function darkThemeVariables() { | ||
| return { | ||
| darkMode: true, | ||
| background: "#0f172a", | ||
| primaryColor: "#1e3a5f", | ||
| primaryBorderColor: "#60a5fa", | ||
| primaryTextColor: "#eff6ff", | ||
| secondaryColor: "#3b255f", | ||
| secondaryBorderColor: "#a78bfa", | ||
| secondaryTextColor: "#f5f3ff", | ||
| tertiaryColor: "#123f3a", | ||
| tertiaryBorderColor: "#34d399", | ||
| tertiaryTextColor: "#ecfdf5", | ||
| lineColor: "#93c5fd", | ||
| textColor: "#e5eefc", | ||
| mainBkg: "#1e3a5f", | ||
| nodeBorder: "#60a5fa", | ||
| clusterBkg: "#172554", | ||
| clusterBorder: "#818cf8", | ||
| edgeLabelBackground: "#152033", | ||
| actorBkg: "#312e81", | ||
| actorBorder: "#a5b4fc", | ||
| actorTextColor: "#eef2ff", | ||
| actorLineColor: "#64748b", | ||
| signalColor: "#7dd3fc", | ||
| signalTextColor: "#e0f2fe", | ||
| labelBoxBkgColor: "#123f3a", | ||
| labelBoxBorderColor: "#34d399", | ||
| labelTextColor: "#ecfdf5", | ||
| activationBkgColor: "#164e63", | ||
| activationBorderColor: "#22d3ee", | ||
| noteBkgColor: "#713f12", | ||
| noteBorderColor: "#fbbf24", | ||
| noteTextColor: "#fef3c7", | ||
| labelColor: "#eff6ff", | ||
| altBackground: "#3b255f" | ||
| } | ||
| } | ||
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
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| pin "@rails/actioncable", to: "actioncable.esm.js" | ||
| pin "coplan/web_push", to: "coplan/web_push.js" | ||
| pin "mermaid", to: "https://cdn.jsdelivr.net/npm/mermaid@11.16.0/dist/mermaid.esm.min.mjs", preload: false | ||
| pin_all_from CoPlan::Engine.root.join("app/javascript/controllers/coplan"), under: "controllers/coplan", preload: true |
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
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.
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.
When a thread is anchored to text inside a Mermaid fence, this replacement removes the only DOM text that
coplan--text-selectioncan wrap with an anchor highlight, so existing/API-created feedback on diagram source loses its visible mark and cannot be opened from the plan document. This can also affect duplicate anchor text whose occurrence was calculated after the diagram was rendered and the source was hidden; preserve the source in the DOM or map the thread to a stable diagram/source view instead of replacing it outright.Useful? React with 👍 / 👎.