2015-06-30 14:23:32 +02:00
|
|
|
/**
|
2021-11-10 08:41:52 +01:00
|
|
|
* Web page integration module for the mermaid framework. It uses the mermaidAPI for mermaid
|
|
|
|
* functionality and to render the diagrams to svg code.
|
2015-06-30 14:23:32 +02:00
|
|
|
*/
|
2021-02-06 15:56:05 +05:30
|
|
|
import { log } from './logger';
|
2020-11-23 23:05:41 +01:00
|
|
|
import mermaidAPI from './mermaidAPI';
|
2020-06-17 05:54:24 -04:00
|
|
|
import utils from './utils';
|
|
|
|
|
2015-05-26 20:59:23 +02:00
|
|
|
/**
|
2015-06-30 14:23:32 +02:00
|
|
|
* ## init
|
2021-11-10 08:41:52 +01:00
|
|
|
*
|
2015-05-26 20:59:23 +02:00
|
|
|
* Function that goes through the document to find the chart definitions in there and render them.
|
|
|
|
*
|
2021-11-10 08:41:52 +01:00
|
|
|
* The function tags the processed attributes with the attribute data-processed and ignores found
|
|
|
|
* elements with the attribute already set. This way the init function can be triggered several times.
|
2015-09-26 12:09:47 +02:00
|
|
|
*
|
2015-05-26 20:59:23 +02:00
|
|
|
* Optionally, `init` can accept in the second argument one of the following:
|
2021-11-10 08:41:52 +01:00
|
|
|
*
|
|
|
|
* - A DOM Node
|
|
|
|
* - An array of DOM nodes (as would come from a jQuery selector)
|
|
|
|
* - A W3C selector, a la `.mermaid`
|
2015-09-26 12:09:47 +02:00
|
|
|
*
|
2015-06-30 14:23:32 +02:00
|
|
|
* ```mermaid
|
2015-05-26 20:59:23 +02:00
|
|
|
* graph LR;
|
2015-06-30 14:23:32 +02:00
|
|
|
* a(Find elements)-->b{Processed}
|
|
|
|
* b-->|Yes|c(Leave element)
|
|
|
|
* b-->|No |d(Transform)
|
2015-05-26 20:59:23 +02:00
|
|
|
* ```
|
2021-11-08 22:06:24 +01:00
|
|
|
*
|
2021-11-10 08:41:52 +01:00
|
|
|
* Renders the mermaid diagrams
|
2015-05-26 20:59:23 +02:00
|
|
|
*/
|
2021-07-15 11:35:12 +02:00
|
|
|
const init = function () {
|
2022-04-19 17:21:10 +02:00
|
|
|
try {
|
2022-05-11 14:04:50 +02:00
|
|
|
initThrowsErrors(...arguments);
|
2022-04-19 17:21:10 +02:00
|
|
|
} catch (e) {
|
|
|
|
log.warn('Syntax Error rendering');
|
|
|
|
log.warn(e);
|
|
|
|
if (this.parseError) {
|
|
|
|
this.parseError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const initThrowsErrors = function () {
|
2019-09-12 12:59:13 -07:00
|
|
|
const conf = mermaidAPI.getConfig();
|
2020-07-30 10:16:29 +02:00
|
|
|
// console.log('Starting rendering diagrams (init) - mermaid.init', conf);
|
2019-09-12 12:59:13 -07:00
|
|
|
let nodes;
|
2017-04-11 22:14:25 +08:00
|
|
|
if (arguments.length >= 2) {
|
2017-04-16 23:08:37 +08:00
|
|
|
/*! sequence config was passed as #1 */
|
2017-04-11 22:14:25 +08:00
|
|
|
if (typeof arguments[0] !== 'undefined') {
|
2019-09-12 12:59:13 -07:00
|
|
|
mermaid.sequenceConfig = arguments[0];
|
2015-05-26 20:59:23 +02:00
|
|
|
}
|
2015-07-15 11:39:46 +02:00
|
|
|
|
2019-09-12 12:59:13 -07:00
|
|
|
nodes = arguments[1];
|
2017-04-11 22:14:25 +08:00
|
|
|
} else {
|
2019-09-12 12:59:13 -07:00
|
|
|
nodes = arguments[0];
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
|
|
|
|
2017-04-16 23:08:37 +08:00
|
|
|
// if last argument is a function this is the callback function
|
2019-09-12 12:59:13 -07:00
|
|
|
let callback;
|
2017-04-11 22:14:25 +08:00
|
|
|
if (typeof arguments[arguments.length - 1] === 'function') {
|
2019-09-12 12:59:13 -07:00
|
|
|
callback = arguments[arguments.length - 1];
|
2021-02-06 15:56:05 +05:30
|
|
|
log.debug('Callback function found');
|
2017-04-11 22:14:25 +08:00
|
|
|
} else {
|
|
|
|
if (typeof conf.mermaid !== 'undefined') {
|
|
|
|
if (typeof conf.mermaid.callback === 'function') {
|
2019-09-12 12:59:13 -07:00
|
|
|
callback = conf.mermaid.callback;
|
2021-02-06 15:56:05 +05:30
|
|
|
log.debug('Callback function found');
|
2017-04-11 22:14:25 +08:00
|
|
|
} else {
|
2021-02-06 15:56:05 +05:30
|
|
|
log.debug('No Callback function found');
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2015-07-15 11:39:46 +02:00
|
|
|
}
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2019-09-12 12:59:13 -07:00
|
|
|
nodes =
|
|
|
|
nodes === undefined
|
|
|
|
? document.querySelectorAll('.mermaid')
|
|
|
|
: typeof nodes === 'string'
|
|
|
|
? document.querySelectorAll(nodes)
|
|
|
|
: nodes instanceof window.Node
|
|
|
|
? [nodes]
|
|
|
|
: nodes; // Last case - sequence config was passed pick next
|
|
|
|
|
2021-02-06 15:56:05 +05:30
|
|
|
log.debug('Start On Load before: ' + mermaid.startOnLoad);
|
2017-09-10 10:24:48 +08:00
|
|
|
if (typeof mermaid.startOnLoad !== 'undefined') {
|
2021-02-06 15:56:05 +05:30
|
|
|
log.debug('Start On Load inner: ' + mermaid.startOnLoad);
|
2020-07-29 18:38:59 +02:00
|
|
|
mermaidAPI.updateSiteConfig({ startOnLoad: mermaid.startOnLoad });
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
|
|
|
|
2017-09-10 10:24:48 +08:00
|
|
|
if (typeof mermaid.ganttConfig !== 'undefined') {
|
2020-07-29 18:38:59 +02:00
|
|
|
mermaidAPI.updateSiteConfig({ gantt: mermaid.ganttConfig });
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
|
|
|
|
2022-06-27 12:34:28 +09:00
|
|
|
const idGenerator = new utils.initIdGenerator(conf.deterministicIds, conf.deterministicIDSeed);
|
2020-11-23 23:05:41 +01:00
|
|
|
|
2019-09-12 12:59:13 -07:00
|
|
|
let txt;
|
2015-06-21 17:25:58 +02:00
|
|
|
|
2017-09-14 21:12:35 +08:00
|
|
|
for (let i = 0; i < nodes.length; i++) {
|
2022-01-20 19:37:52 +01:00
|
|
|
// element is the current div with mermaid class
|
2019-09-12 12:59:13 -07:00
|
|
|
const element = nodes[i];
|
2015-05-26 20:59:23 +02:00
|
|
|
|
2017-04-16 23:08:37 +08:00
|
|
|
/*! Check if previously processed */
|
2017-04-11 22:14:25 +08:00
|
|
|
if (!element.getAttribute('data-processed')) {
|
2019-09-12 12:59:13 -07:00
|
|
|
element.setAttribute('data-processed', true);
|
2017-04-11 22:14:25 +08:00
|
|
|
} else {
|
2019-09-12 12:59:13 -07:00
|
|
|
continue;
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2015-05-26 20:59:23 +02:00
|
|
|
|
2022-06-27 12:34:28 +09:00
|
|
|
const id = `mermaid-${idGenerator.next()}`;
|
2015-05-26 20:59:23 +02:00
|
|
|
|
2017-04-16 23:08:37 +08:00
|
|
|
// Fetch the graph definition including tags
|
2019-09-12 12:59:13 -07:00
|
|
|
txt = element.innerHTML;
|
2015-05-26 20:59:23 +02:00
|
|
|
|
2017-04-16 23:08:37 +08:00
|
|
|
// transforms the html to pure text
|
2021-07-07 02:00:44 +05:30
|
|
|
txt = utils
|
|
|
|
.entityDecode(txt)
|
2019-09-12 12:59:13 -07:00
|
|
|
.trim()
|
2020-01-13 22:04:47 +01:00
|
|
|
.replace(/<br\s*\/?>/gi, '<br/>');
|
2019-09-12 12:59:13 -07:00
|
|
|
|
2020-06-17 05:54:24 -04:00
|
|
|
const init = utils.detectInit(txt);
|
|
|
|
if (init) {
|
2021-02-06 15:56:05 +05:30
|
|
|
log.debug('Detected early reinit: ', init);
|
2020-06-17 05:54:24 -04:00
|
|
|
}
|
|
|
|
|
2022-04-19 17:21:10 +02:00
|
|
|
mermaidAPI.render(
|
|
|
|
id,
|
|
|
|
txt,
|
|
|
|
(svgCode, bindFunctions) => {
|
|
|
|
element.innerHTML = svgCode;
|
|
|
|
if (typeof callback !== 'undefined') {
|
|
|
|
callback(id);
|
|
|
|
}
|
|
|
|
if (bindFunctions) bindFunctions(element);
|
|
|
|
},
|
|
|
|
element
|
|
|
|
);
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2019-09-12 12:59:13 -07:00
|
|
|
};
|
2015-10-03 21:50:32 +02:00
|
|
|
|
2021-07-15 11:35:12 +02:00
|
|
|
const initialize = function (config) {
|
2020-07-30 19:18:18 +02:00
|
|
|
// mermaidAPI.reset();
|
2017-04-11 22:14:25 +08:00
|
|
|
if (typeof config.mermaid !== 'undefined') {
|
|
|
|
if (typeof config.mermaid.startOnLoad !== 'undefined') {
|
2019-09-12 12:59:13 -07:00
|
|
|
mermaid.startOnLoad = config.mermaid.startOnLoad;
|
2015-07-15 11:39:46 +02:00
|
|
|
}
|
2017-04-11 22:14:25 +08:00
|
|
|
if (typeof config.mermaid.htmlLabels !== 'undefined') {
|
2021-06-03 20:47:24 +02:00
|
|
|
mermaid.htmlLabels =
|
|
|
|
config.mermaid.htmlLabels === 'false' || config.mermaid.htmlLabels === false ? false : true;
|
2015-05-26 20:59:23 +02:00
|
|
|
}
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2019-09-12 12:59:13 -07:00
|
|
|
mermaidAPI.initialize(config);
|
2020-07-17 09:16:39 +02:00
|
|
|
// mermaidAPI.reset();
|
2019-09-12 12:59:13 -07:00
|
|
|
};
|
2017-04-11 22:14:25 +08:00
|
|
|
|
2015-06-30 14:23:32 +02:00
|
|
|
/**
|
2021-11-10 08:41:52 +01:00
|
|
|
* ##contentLoaded Callback function that is called when page is loaded. This functions fetches
|
|
|
|
* configuration for mermaid rendering and calls init for rendering the mermaid diagrams on the page.
|
2015-06-30 14:23:32 +02:00
|
|
|
*/
|
2021-07-15 11:35:12 +02:00
|
|
|
const contentLoaded = function () {
|
2019-09-12 12:59:13 -07:00
|
|
|
let config;
|
2015-05-26 20:59:23 +02:00
|
|
|
|
2017-09-10 10:24:48 +08:00
|
|
|
if (mermaid.startOnLoad) {
|
2018-03-09 15:13:05 +08:00
|
|
|
// No config found, do check API config
|
2019-09-12 12:59:13 -07:00
|
|
|
config = mermaidAPI.getConfig();
|
2018-03-09 15:13:05 +08:00
|
|
|
if (config.startOnLoad) {
|
2019-09-12 12:59:13 -07:00
|
|
|
mermaid.init();
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
|
|
|
} else {
|
2017-09-10 10:24:48 +08:00
|
|
|
if (typeof mermaid.startOnLoad === 'undefined') {
|
2021-02-06 15:56:05 +05:30
|
|
|
log.debug('In start, no config');
|
2019-09-12 12:59:13 -07:00
|
|
|
config = mermaidAPI.getConfig();
|
2017-04-11 22:14:25 +08:00
|
|
|
if (config.startOnLoad) {
|
2019-09-12 12:59:13 -07:00
|
|
|
mermaid.init();
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2015-05-26 20:59:23 +02:00
|
|
|
}
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2019-09-12 12:59:13 -07:00
|
|
|
};
|
2015-05-26 20:59:23 +02:00
|
|
|
|
2017-04-11 22:14:25 +08:00
|
|
|
if (typeof document !== 'undefined') {
|
2017-04-16 23:08:37 +08:00
|
|
|
/*!
|
|
|
|
* Wait for document loaded before starting the execution
|
|
|
|
*/
|
2019-09-12 12:59:13 -07:00
|
|
|
window.addEventListener(
|
|
|
|
'load',
|
2021-07-15 11:35:12 +02:00
|
|
|
function () {
|
2019-09-12 12:59:13 -07:00
|
|
|
contentLoaded();
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
2015-05-26 20:59:23 +02:00
|
|
|
}
|
2017-09-09 21:47:21 +08:00
|
|
|
|
2022-05-05 11:47:46 -07:00
|
|
|
/**
|
2022-06-27 12:34:28 +09:00
|
|
|
* ## setParseErrorHandler Alternative to directly setting parseError using:
|
2022-05-05 11:47:46 -07:00
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* mermaid.parseError = function(err,hash){=
|
|
|
|
* forExampleDisplayErrorInGui(err); // do something with the error
|
|
|
|
* };
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* This is provided for environments where the mermaid object can't directly have a new member added
|
|
|
|
* to it (eg. dart interop wrapper). (Initially there is no parseError member of mermaid).
|
|
|
|
*
|
|
|
|
* @param {function (err, hash)} newParseErrorHandler New parseError() callback.
|
|
|
|
*/
|
|
|
|
const setParseErrorHandler = function (newParseErrorHandler) {
|
|
|
|
mermaid.parseError = newParseErrorHandler;
|
|
|
|
};
|
|
|
|
|
2017-09-09 21:47:21 +08:00
|
|
|
const mermaid = {
|
|
|
|
startOnLoad: true,
|
|
|
|
htmlLabels: true,
|
2022-07-18 16:00:03 +02:00
|
|
|
diagrams: {},
|
2017-09-10 10:19:15 +08:00
|
|
|
mermaidAPI,
|
2022-05-05 11:47:46 -07:00
|
|
|
parse: mermaidAPI != undefined ? mermaidAPI.parse : null,
|
|
|
|
render: mermaidAPI != undefined ? mermaidAPI.render : null,
|
2017-09-10 10:19:15 +08:00
|
|
|
|
2017-09-09 21:47:21 +08:00
|
|
|
init,
|
2022-05-18 08:51:19 +02:00
|
|
|
initThrowsErrors,
|
2017-09-09 21:47:21 +08:00
|
|
|
initialize,
|
|
|
|
|
2021-07-15 11:35:12 +02:00
|
|
|
contentLoaded,
|
2022-05-05 11:47:46 -07:00
|
|
|
|
|
|
|
setParseErrorHandler,
|
2019-09-12 12:59:13 -07:00
|
|
|
};
|
2017-09-09 21:47:21 +08:00
|
|
|
|
2019-09-12 12:59:13 -07:00
|
|
|
export default mermaid;
|