1 |
|
2 |
|
3 |
|
4 |
|
5 | import he from 'he'
|
6 |
|
7 | import mermaidAPI from './mermaidAPI'
|
8 | import { logger } from './logger'
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | const init = function () {
|
32 | const conf = mermaidAPI.getConfig()
|
33 | logger.debug('Starting rendering diagrams')
|
34 | let nodes
|
35 | if (arguments.length >= 2) {
|
36 |
|
37 | if (typeof arguments[0] !== 'undefined') {
|
38 | mermaid.sequenceConfig = arguments[0]
|
39 | }
|
40 |
|
41 | nodes = arguments[1]
|
42 | } else {
|
43 | nodes = arguments[0]
|
44 | }
|
45 |
|
46 |
|
47 | let callback
|
48 | if (typeof arguments[arguments.length - 1] === 'function') {
|
49 | callback = arguments[arguments.length - 1]
|
50 | logger.debug('Callback function found')
|
51 | } else {
|
52 | if (typeof conf.mermaid !== 'undefined') {
|
53 | if (typeof conf.mermaid.callback === 'function') {
|
54 | callback = conf.mermaid.callback
|
55 | logger.debug('Callback function found')
|
56 | } else {
|
57 | logger.debug('No Callback function found')
|
58 | }
|
59 | }
|
60 | }
|
61 | nodes = nodes === undefined ? document.querySelectorAll('.mermaid')
|
62 | : typeof nodes === 'string' ? document.querySelectorAll(nodes)
|
63 | : nodes instanceof window.Node ? [nodes]
|
64 | : nodes
|
65 |
|
66 | logger.debug('Start On Load before: ' + mermaid.startOnLoad)
|
67 | if (typeof mermaid.startOnLoad !== 'undefined') {
|
68 | logger.debug('Start On Load inner: ' + mermaid.startOnLoad)
|
69 | mermaidAPI.initialize({ startOnLoad: mermaid.startOnLoad })
|
70 | }
|
71 |
|
72 | if (typeof mermaid.ganttConfig !== 'undefined') {
|
73 | mermaidAPI.initialize({ gantt: mermaid.ganttConfig })
|
74 | }
|
75 |
|
76 | let txt
|
77 |
|
78 | for (let i = 0; i < nodes.length; i++) {
|
79 | const element = nodes[i]
|
80 |
|
81 |
|
82 | if (!element.getAttribute('data-processed')) {
|
83 | element.setAttribute('data-processed', true)
|
84 | } else {
|
85 | continue
|
86 | }
|
87 |
|
88 | const id = `mermaid-${Date.now()}`
|
89 |
|
90 |
|
91 | txt = element.innerHTML
|
92 |
|
93 |
|
94 | txt = he.decode(txt).trim().replace(/<br>/ig, '<br/>')
|
95 |
|
96 | mermaidAPI.render(id, txt, (svgCode, bindFunctions) => {
|
97 | element.innerHTML = svgCode
|
98 | if (typeof callback !== 'undefined') {
|
99 | callback(id)
|
100 | }
|
101 | bindFunctions(element)
|
102 | }, element)
|
103 | }
|
104 | }
|
105 |
|
106 | const initialize = function (config) {
|
107 | logger.debug('Initializing mermaid')
|
108 | if (typeof config.mermaid !== 'undefined') {
|
109 | if (typeof config.mermaid.startOnLoad !== 'undefined') {
|
110 | mermaid.startOnLoad = config.mermaid.startOnLoad
|
111 | }
|
112 | if (typeof config.mermaid.htmlLabels !== 'undefined') {
|
113 | mermaid.htmlLabels = config.mermaid.htmlLabels
|
114 | }
|
115 | }
|
116 | mermaidAPI.initialize(config)
|
117 | }
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | const contentLoaded = function () {
|
125 | let config
|
126 |
|
127 | if (mermaid.startOnLoad) {
|
128 |
|
129 | config = mermaidAPI.getConfig()
|
130 | if (config.startOnLoad) {
|
131 | mermaid.init()
|
132 | }
|
133 | } else {
|
134 | if (typeof mermaid.startOnLoad === 'undefined') {
|
135 | logger.debug('In start, no config')
|
136 | config = mermaidAPI.getConfig()
|
137 | if (config.startOnLoad) {
|
138 | mermaid.init()
|
139 | }
|
140 | }
|
141 | }
|
142 | }
|
143 |
|
144 | if (typeof document !== 'undefined') {
|
145 | |
146 |
|
147 |
|
148 | window.addEventListener('load', function () {
|
149 | contentLoaded()
|
150 | }, false)
|
151 | }
|
152 |
|
153 | const mermaid = {
|
154 | startOnLoad: true,
|
155 | htmlLabels: true,
|
156 |
|
157 | mermaidAPI,
|
158 | parse: mermaidAPI.parse,
|
159 | render: mermaidAPI.render,
|
160 |
|
161 | init,
|
162 | initialize,
|
163 |
|
164 | contentLoaded
|
165 | }
|
166 |
|
167 | export default mermaid
|