Debian Codegraph @

docs/src/js/uml.js

raw
1 /* Notes (as of Mermaid 8.7.0):
2 * - Gantt: width is always relative to the parent, if you have a small parent, the chart will be squashed.
3 * Can't help it.
4 * - Journey: Suffers from the same issues that Gantt does.
5 * - Pie: These charts have no default height or width. Good luck pinning them down to a reasonable size.
6 * - Git: The render portion is agnostic to the size of the parent element. But padding of the SVG is relative
7 * to the parent element. You will never find a happy size.
8 */
9
10 /**
11 * Targets special code or div blocks and converts them to UML.
12 * @param {string} className is the name of the class to target.
13 * @return {void}
14 */
15 export default async className => {
16
17 // Custom element to encapsulate Mermaid content.
18 class MermaidDiv extends HTMLElement {
19
20 /**
21 * Creates a special Mermaid div shadow DOM.
22 * Works around issues of shared IDs.
23 * @return {void}
24 */
25 constructor() {
26 super()
27
28 // Create the Shadow DOM and attach style
29 const shadow = this.attachShadow({mode: "open"})
30 const style = document.createElement("style")
31 style.textContent = `
32 :host {
33 display: block;
34 line-height: initial;
35 font-size: 16px;
36 }
37 div.diagram {
38 margin: 0;
39 overflow: visible;
40 }`
41 shadow.appendChild(style)
42 }
43 }
44
45 if (typeof customElements.get("diagram-div") === "undefined") {
46 customElements.define("diagram-div", MermaidDiv)
47 }
48
49 const getFromCode = parent => {
50 // Handles <pre><code> text extraction.
51 let text = ""
52 for (let j = 0; j < parent.childNodes.length; j++) {
53 const subEl = parent.childNodes[j]
54 if (subEl.tagName.toLowerCase() === "code") {
55 for (let k = 0; k < subEl.childNodes.length; k++) {
56 const child = subEl.childNodes[k]
57 const whitespace = /^\s*$/
58 if (child.nodeName === "#text" && !(whitespace.test(child.nodeValue))) {
59 text = child.nodeValue
60 break
61 }
62 }
63 }
64 }
65 return text
66 }
67
68 // We use this to determine if we want the dark or light theme.
69 // This is specific for our MkDocs Material environment.
70 // You should load your configs based on your own environment's needs.
71 const defaultConfig = {
72 startOnLoad: false,
73 theme: "default",
74 flowchart: {
75 useMaxWidth: false
76 },
77 er: {
78 useMaxWidth: false
79 },
80 sequence: {
81 useMaxWidth: false,
82 noteFontWeight: "14px",
83 actorFontSize: "14px",
84 messageFontSize: "16px"
85 }
86 }
87 mermaid.mermaidAPI.globalReset()
88 // Non Material themes should just use "default"
89 let scheme = null
90 try {
91 scheme = document.querySelector("[data-md-color-scheme]").getAttribute("data-md-color-scheme")
92 } catch (err) {
93 scheme = "slate"
94 }
95 const config = (typeof mermaidConfig === "undefined") ?
96 defaultConfig :
97 mermaidConfig[scheme] || (mermaidConfig.default || defaultConfig)
98 mermaid.initialize(config)
99
100 // Find all of our Mermaid sources and render them.
101 const blocks = document.querySelectorAll(`pre.${className}, diagram-div`)
102 const surrogate = document.querySelector("html body")
103 for (let i = 0; i < blocks.length; i++) {
104 const block = blocks[i]
105 const parentEl = (block.tagName.toLowerCase() === "diagram-div") ?
106 block.shadowRoot.querySelector(`pre.${className}`) :
107 block
108
109 // Create a temporary element with the typeset and size we desire.
110 // Insert it at the end of our parent to render the SVG.
111 const temp = document.createElement("div")
112 temp.style.visibility = "hidden"
113 temp.style.display = "display"
114 temp.style.padding = "0"
115 temp.style.margin = "0"
116 temp.style.lineHeight = "initial"
117 temp.style.fontSize = "16px"
118 surrogate.appendChild(temp)
119
120 try {
121 const res = await mermaid.render(`_diagram_${i}`, getFromCode(parentEl), temp)
122 const content = res.svg
123 const fn = res.bindFunctions
124 const el = document.createElement("div")
125 el.className = className
126 el.innerHTML = content
127 if (fn) {
128 fn(el)
129 }
130
131 // Insert the render where we want it and remove the original text source.
132 // Mermaid will clean up the temporary element.
133 const shadow = document.createElement("diagram-div")
134 shadow.shadowRoot.appendChild(el)
135 block.parentNode.insertBefore(shadow, block)
136 parentEl.style.display = "none"
137 shadow.shadowRoot.appendChild(parentEl)
138 if (parentEl !== block) {
139 block.parentNode.removeChild(block)
140 }
141 } catch (err) {} // eslint-disable-line no-empty
142
143 if (surrogate.contains(temp)) {
144 surrogate.removeChild(temp)
145 }
146 }
147 }