1473 Updated config handling

This commit is contained in:
Knut Sveidqvist 2020-06-15 01:11:56 +02:00
parent 5677523800
commit bd0210cdce
6 changed files with 56 additions and 52 deletions

View File

@ -21,6 +21,14 @@
<body> <body>
<h1>info below</h1> <h1>info below</h1>
<div class="mermaid" style="width: 50%; height: 20%;"> <div class="mermaid" style="width: 50%; height: 20%;">
graph TD
A[Christmas] -->|Get money| B(Go shopping)
B --> C{Let me think}
C -->|One| D[Laptop]
C -->|Two| E[iPhone]
C -->|Three| F[fa:fa-car Car]
</div>
<div class="mermaid2" style="width: 50%; height: 20%;">
flowchart TB flowchart TB
subgraph 1 subgraph 1
A --> B; A --> B;
@ -64,13 +72,14 @@ flowchart TB
// arrowMarkerAbsolute: true, // arrowMarkerAbsolute: true,
// themeCSS: '.edgePath .path {stroke: red;} .arrowheadPath {fill: red;}', // themeCSS: '.edgePath .path {stroke: red;} .arrowheadPath {fill: red;}',
logLevel: 0, logLevel: 0,
flowchart: { curve: 'linear', "htmlLabels": false }, flowchart: { curve: 'linear',htmlLabels: false },
// gantt: { axisFormat: '%m/%d/%Y' }, // gantt: { axisFormat: '%m/%d/%Y' },
sequence: { actorMargin: 50, showSequenceNumbers: true }, sequence: { actorMargin: 50, showSequenceNumbers: true },
// sequenceDiagram: { actorMargin: 300 } // deprecated // sequenceDiagram: { actorMargin: 300 } // deprecated
fontFamily: '"arial", sans-serif', fontFamily: '"arial", sans-serif',
curve: 'linear', curve: 'linear',
securityLevel: 'loose' securityLevel: 'loose',
htmlLabels: false
}); });
function callback(){alert('It worked');} function callback(){alert('It worked');}
</script> </script>

View File

@ -1,3 +1,4 @@
import { logger } from './logger';
let config = {}; let config = {};
const setConf = function(cnf) { const setConf = function(cnf) {
@ -13,10 +14,18 @@ const setConf = function(cnf) {
if (typeof config[lvl1Keys[i]] === 'undefined') { if (typeof config[lvl1Keys[i]] === 'undefined') {
config[lvl1Keys[i]] = {}; config[lvl1Keys[i]] = {};
} }
// logger.debug('Setting config: ' + lvl1Keys[i] + ' ' + lvl2Keys[j] + ' to ' + cnf[lvl1Keys[i]][lvl2Keys[j]]) // logger.debug(
// 'Setting config: ' +
// lvl1Keys[i] +
// ' ' +
// lvl2Keys[j] +
// ' to ' +
// cnf[lvl1Keys[i]][lvl2Keys[j]]
// );
config[lvl1Keys[i]][lvl2Keys[j]] = cnf[lvl1Keys[i]][lvl2Keys[j]]; config[lvl1Keys[i]][lvl2Keys[j]] = cnf[lvl1Keys[i]][lvl2Keys[j]];
} }
} else { } else {
// logger.debug('Setting config: ' + lvl1Keys[i] + ' to ' + cnf[lvl1Keys[i]]);
config[lvl1Keys[i]] = cnf[lvl1Keys[i]]; config[lvl1Keys[i]] = cnf[lvl1Keys[i]];
} }
} }

View File

@ -1351,7 +1351,7 @@ describe('when rendering a sequenceDiagram with directives', function() {
renderer.setConf(conf); renderer.setConf(conf);
}); });
it('it should handle one actor, when theme is dark and logLevel is 1', function() { it('it should handle one actor, when theme is dark and logLevel is 1 DX1', function() {
renderer.bounds.init(); renderer.bounds.init();
const str = ` const str = `
%%{init: { "theme": "dark", "logLevel": 1 } }%% %%{init: { "theme": "dark", "logLevel": 1 } }%%

View File

@ -122,6 +122,9 @@ const init = function() {
}; };
const initialize = function(config) { const initialize = function(config) {
mermaidAPI.reset();
// logger.debug('Initializing mermaid 1', config);
if (typeof config.mermaid !== 'undefined') { if (typeof config.mermaid !== 'undefined') {
if (typeof config.mermaid.startOnLoad !== 'undefined') { if (typeof config.mermaid.startOnLoad !== 'undefined') {
mermaid.startOnLoad = config.mermaid.startOnLoad; mermaid.startOnLoad = config.mermaid.startOnLoad;
@ -131,7 +134,7 @@ const initialize = function(config) {
} }
} }
mermaidAPI.initialize(config); mermaidAPI.initialize(config);
logger.debug('Initializing mermaid '); logger.debug('Initializing mermaid ', config);
}; };
/** /**

View File

@ -699,6 +699,7 @@ export const decodeEntities = function(text) {
* completed. * completed.
*/ */
const render = function(id, _txt, cb, container) { const render = function(id, _txt, cb, container) {
const config = getConfig();
// Check the maximum allowed text size // Check the maximum allowed text size
let txt = _txt; let txt = _txt;
if (_txt.length > config.maxTextSize) { if (_txt.length > config.maxTextSize) {
@ -925,61 +926,35 @@ const render = function(id, _txt, cb, container) {
return svgCode; return svgCode;
}; };
const setConf = function(cnf) {
// console.log('set conf ', cnf);
// Top level initially mermaid, gflow, sequenceDiagram and gantt
const lvl1Keys = Object.keys(cnf);
for (let i = 0; i < lvl1Keys.length; i++) {
if (typeof cnf[lvl1Keys[i]] === 'object' && cnf[lvl1Keys[i]] != null) {
const lvl2Keys = Object.keys(cnf[lvl1Keys[i]]);
for (let j = 0; j < lvl2Keys.length; j++) {
logger.debug('Setting conf ', lvl1Keys[i], '-', lvl2Keys[j]);
if (typeof config[lvl1Keys[i]] === 'undefined') {
config[lvl1Keys[i]] = {};
}
logger.debug(
'Setting config: ' +
lvl1Keys[i] +
' ' +
lvl2Keys[j] +
' to ' +
cnf[lvl1Keys[i]][lvl2Keys[j]]
);
config[lvl1Keys[i]][lvl2Keys[j]] = cnf[lvl1Keys[i]][lvl2Keys[j]];
}
} else {
config[lvl1Keys[i]] = cnf[lvl1Keys[i]];
}
}
// console.log('set conf done', config);
};
function reinitialize(options) { function reinitialize(options) {
// console.log('re-initialize ', options.logLevel, config.logLevel, getConfig().logLevel);
if (typeof options === 'object') { if (typeof options === 'object') {
setConf(options); // setConf(options);
setConfig(options);
} }
setConfig(config); // setConfig(config);
setLogLevel(config.logLevel); setLogLevel(getConfig().logLevel);
logger.debug('RE-Initializing mermaidAPI ', { version: pkg.version, options, config }); logger.debug('RE-Initializing mermaidAPI ', { version: pkg.version, options, config });
} }
let firstInit = true;
function initialize(options) { function initialize(options) {
// console.log('initialize ', options, config, getConfig());
let _config = getConfig();
// Set default options // Set default options
if (typeof options === 'object') { if (typeof options === 'object') {
setConf(_config); if (firstInit) {
setConfig(_config); firstInit = false;
setConfig(config);
} }
logger.debug('Initializing mermaidAPI ', { version: pkg.version, options, _config }); setConfig(options);
// Update default config with options supplied at initialization
if (typeof options === 'object') {
_config = Object.assign(_config, options);
setConf(_config);
} }
setConfig(_config); logger.warn('Initializing mermaidAPI theme', {
setLogLevel(_config.logLevel); version: pkg.version,
options,
config,
current: getConfig().theme
});
setLogLevel(getConfig().logLevel);
} }
// function getConfig () { // function getConfig () {
@ -992,7 +967,10 @@ const mermaidAPI = {
parse, parse,
initialize, initialize,
reinitialize, reinitialize,
getConfig getConfig,
reset: () => {
firstInit = true;
}
}; };
export default mermaidAPI; export default mermaidAPI;

View File

@ -26,8 +26,13 @@ describe('when using mermaidAPI and ', function() {
}; };
mermaidAPI.initialize({ testObject: object }); mermaidAPI.initialize({ testObject: object });
let config = mermaidAPI.getConfig();
console.log('1:', config);
expect(config.testObject.test1).toBe(1);
mermaidAPI.initialize({ testObject: { test3: true } }); mermaidAPI.initialize({ testObject: { test3: true } });
const config = mermaidAPI.getConfig(); config = mermaidAPI.getConfig();
console.log(config);
expect(config.testObject.test1).toBe(1); expect(config.testObject.test1).toBe(1);
expect(config.testObject.test2).toBe(false); expect(config.testObject.test2).toBe(false);