2019-10-05 00:39:07 +02:00
|
|
|
import { Base64 } from 'js-base64';
|
|
|
|
import mermaid2 from '../../src/mermaid';
|
2019-09-11 18:53:05 +02:00
|
|
|
|
|
|
|
/**
|
2021-11-18 19:17:00 +01:00
|
|
|
* ##contentLoaded Callback function that is called when page is loaded. This functions fetches
|
2022-09-20 22:13:49 +05:30
|
|
|
* configuration for mermaid rendering and calls init for rendering the mermaid diagrams on the
|
|
|
|
* page.
|
2019-09-11 18:53:05 +02:00
|
|
|
*/
|
2021-11-18 19:17:00 +01:00
|
|
|
const contentLoaded = function () {
|
2019-10-05 00:39:07 +02:00
|
|
|
let pos = document.location.href.indexOf('?graph=');
|
2019-09-11 18:53:05 +02:00
|
|
|
if (pos > 0) {
|
2019-10-05 00:39:07 +02:00
|
|
|
pos = pos + 7;
|
|
|
|
const graphBase64 = document.location.href.substr(pos);
|
|
|
|
const graphObj = JSON.parse(Base64.decode(graphBase64));
|
2020-07-15 14:02:19 +02:00
|
|
|
if (graphObj.mermaid && graphObj.mermaid.theme === 'dark') {
|
|
|
|
document.body.style.background = '#3f3f3f';
|
|
|
|
}
|
2019-10-05 00:39:07 +02:00
|
|
|
console.log(graphObj);
|
2019-11-05 17:24:07 +09:00
|
|
|
if (Array.isArray(graphObj.code)) {
|
|
|
|
const numCodes = graphObj.code.length;
|
|
|
|
for (let i = 0; i < numCodes; i++) {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
div.id = 'block' + i;
|
|
|
|
div.className = 'mermaid';
|
|
|
|
div.innerHTML = graphObj.code[i];
|
|
|
|
document.getElementsByTagName('body')[0].appendChild(div);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
div.id = 'block';
|
|
|
|
div.className = 'mermaid';
|
|
|
|
div.innerHTML = graphObj.code;
|
|
|
|
document.getElementsByTagName('body')[0].appendChild(div);
|
|
|
|
}
|
2020-07-15 14:02:19 +02:00
|
|
|
|
2022-09-20 22:13:49 +05:30
|
|
|
mermaid2.initialize(graphObj.mermaid);
|
|
|
|
mermaid2.init();
|
2019-09-11 18:53:05 +02:00
|
|
|
}
|
2019-10-05 00:39:07 +02:00
|
|
|
};
|
2020-08-16 13:01:45 +02:00
|
|
|
|
2021-11-18 19:17:00 +01:00
|
|
|
/**
|
|
|
|
* @param current
|
|
|
|
* @param update
|
|
|
|
*/
|
2020-08-16 13:01:45 +02:00
|
|
|
function merge(current, update) {
|
2021-11-18 19:17:00 +01:00
|
|
|
Object.keys(update).forEach(function (key) {
|
2020-08-16 13:01:45 +02:00
|
|
|
// if update[key] exist, and it's not a string or array,
|
|
|
|
// we go in one level deeper
|
|
|
|
if (
|
2022-09-05 01:00:47 +05:30
|
|
|
current.hasOwnProperty(key) &&
|
2020-08-16 13:01:45 +02:00
|
|
|
typeof current[key] === 'object' &&
|
|
|
|
!(current[key] instanceof Array)
|
|
|
|
) {
|
|
|
|
merge(current[key], update[key]);
|
|
|
|
|
|
|
|
// if update[key] doesn't exist in current, or it's a string
|
|
|
|
// or array, then assign/overwrite current[key] to update[key]
|
|
|
|
} else {
|
|
|
|
current[key] = update[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2021-11-18 19:17:00 +01:00
|
|
|
const contentLoadedApi = function () {
|
2019-10-05 00:39:07 +02:00
|
|
|
let pos = document.location.href.indexOf('?graph=');
|
2019-09-11 18:53:05 +02:00
|
|
|
if (pos > 0) {
|
2019-10-05 00:39:07 +02:00
|
|
|
pos = pos + 7;
|
|
|
|
const graphBase64 = document.location.href.substr(pos);
|
|
|
|
const graphObj = JSON.parse(Base64.decode(graphBase64));
|
2019-09-11 18:53:05 +02:00
|
|
|
// const graph = 'hello'
|
2019-11-05 17:24:07 +09:00
|
|
|
if (Array.isArray(graphObj.code)) {
|
|
|
|
const numCodes = graphObj.code.length;
|
|
|
|
const divs = [];
|
2019-11-08 11:42:26 +01:00
|
|
|
let div;
|
2019-11-05 17:24:07 +09:00
|
|
|
for (let i = 0; i < numCodes; i++) {
|
2019-11-08 11:42:26 +01:00
|
|
|
div = document.createElement('div');
|
2019-11-05 17:24:07 +09:00
|
|
|
div.id = 'block' + i;
|
|
|
|
div.className = 'mermaid';
|
|
|
|
// div.innerHTML = graphObj.code
|
|
|
|
document.getElementsByTagName('body')[0].appendChild(div);
|
|
|
|
divs[i] = div;
|
|
|
|
}
|
|
|
|
|
2020-08-16 13:01:45 +02:00
|
|
|
const defaultE2eCnf = { theme: 'forest' };
|
|
|
|
|
|
|
|
const cnf = merge(defaultE2eCnf, graphObj.mermaid);
|
|
|
|
|
|
|
|
mermaid2.initialize(cnf);
|
2019-11-05 17:24:07 +09:00
|
|
|
|
|
|
|
for (let i = 0; i < numCodes; i++) {
|
|
|
|
mermaid2.render(
|
|
|
|
'newid' + i,
|
|
|
|
graphObj.code[i],
|
|
|
|
(svgCode, bindFunctions) => {
|
|
|
|
div.innerHTML = svgCode;
|
|
|
|
|
|
|
|
bindFunctions(div);
|
|
|
|
},
|
|
|
|
divs[i]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
div.id = 'block';
|
|
|
|
div.className = 'mermaid';
|
|
|
|
// div.innerHTML = graphObj.code
|
2019-12-07 12:19:45 +01:00
|
|
|
console.warn('graphObj.mermaid', graphObj.mermaid);
|
2019-11-05 17:24:07 +09:00
|
|
|
document.getElementsByTagName('body')[0].appendChild(div);
|
2019-12-07 12:19:45 +01:00
|
|
|
mermaid2.initialize(graphObj.mermaid);
|
2019-09-11 18:53:05 +02:00
|
|
|
|
2019-11-05 17:24:07 +09:00
|
|
|
mermaid2.render(
|
|
|
|
'newid',
|
|
|
|
graphObj.code,
|
|
|
|
(svgCode, bindFunctions) => {
|
|
|
|
div.innerHTML = svgCode;
|
2019-09-11 18:53:05 +02:00
|
|
|
|
2019-11-08 11:42:26 +01:00
|
|
|
if (bindFunctions) bindFunctions(div);
|
|
|
|
},
|
|
|
|
div
|
|
|
|
);
|
|
|
|
}
|
2019-09-11 18:53:05 +02:00
|
|
|
}
|
2019-10-05 00:39:07 +02:00
|
|
|
};
|
|
|
|
|
2019-09-11 18:53:05 +02:00
|
|
|
if (typeof document !== 'undefined') {
|
|
|
|
/*!
|
|
|
|
* Wait for document loaded before starting the execution
|
|
|
|
*/
|
|
|
|
window.addEventListener(
|
|
|
|
'load',
|
2021-11-18 19:17:00 +01:00
|
|
|
function () {
|
2019-09-11 18:53:05 +02:00
|
|
|
if (this.location.href.match('xss.html')) {
|
2019-10-05 00:39:07 +02:00
|
|
|
this.console.log('Using api');
|
|
|
|
contentLoadedApi();
|
2019-09-11 18:53:05 +02:00
|
|
|
} else {
|
2019-10-05 00:39:07 +02:00
|
|
|
this.console.log('Not using api');
|
|
|
|
contentLoaded();
|
2019-09-11 18:53:05 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
false
|
2019-10-05 00:39:07 +02:00
|
|
|
);
|
2019-09-11 18:53:05 +02:00
|
|
|
}
|