mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-14 06:43:25 +08:00
Refactor
Moved theme variables to the config module
This commit is contained in:
parent
17478491e9
commit
a54f3c8c7f
@ -1,14 +1,22 @@
|
||||
import { assignWithDepth } from './utils';
|
||||
import { logger } from './logger';
|
||||
import theme from './themes';
|
||||
|
||||
// import { unflatten } from 'flat';
|
||||
// import flatten from 'flat';
|
||||
|
||||
// import themeVariables from './theme-default';
|
||||
// import themeForestVariables from './theme-forest';
|
||||
// import themeNeutralVariables from './theme-neutral';
|
||||
|
||||
const themes = {};
|
||||
const handleThemeVariables = value => {
|
||||
return theme[value] ? theme[value].getThemeVariables() : theme.default.getThemeVariables();
|
||||
};
|
||||
|
||||
const manipulators = {
|
||||
themeVariables: handleThemeVariables
|
||||
};
|
||||
|
||||
for (const themeName of ['default', 'forest', 'dark', 'neutral', 'base']) {
|
||||
themes[themeName] = require(`./themes/theme-${themeName}.js`);
|
||||
}
|
||||
/**
|
||||
* **Configuration methods in Mermaid version 8.6.0 have been updated, to learn more[[click here](8.6.0_docs.md)].**
|
||||
*
|
||||
@ -59,7 +67,7 @@ const config = {
|
||||
* </pre>
|
||||
*/
|
||||
theme: 'default',
|
||||
themeVariables: themes['default'].getThemeVariables(),
|
||||
themeVariables: theme['default'].getThemeVariables(),
|
||||
themeCSS: undefined,
|
||||
/* **maxTextSize** - The maximum allowed size of the users text diamgram */
|
||||
maxTextSize: 50000,
|
||||
@ -869,6 +877,11 @@ const currentConfig = assignWithDepth({}, defaultConfig);
|
||||
* @returns {*} - the siteConfig
|
||||
*/
|
||||
export const setSiteConfig = conf => {
|
||||
Object.keys(conf).forEach(key => {
|
||||
const manipulator = manipulators[key];
|
||||
conf[key] = manipulator ? manipulator(conf[key]) : conf[key];
|
||||
});
|
||||
|
||||
assignWithDepth(currentConfig, conf, { clobber: true });
|
||||
// Set theme variables if user has set the theme option
|
||||
assignWithDepth(siteConfig, conf);
|
||||
@ -901,6 +914,10 @@ export const getSiteConfig = () => {
|
||||
*/
|
||||
export const setConfig = conf => {
|
||||
sanitize(conf);
|
||||
Object.keys(conf).forEach(key => {
|
||||
const manipulator = manipulators[key];
|
||||
conf[key] = manipulator ? manipulator(conf[key]) : conf[key];
|
||||
});
|
||||
|
||||
assignWithDepth(currentConfig, conf);
|
||||
return getConfig();
|
||||
|
@ -52,11 +52,7 @@ import journeyDb from './diagrams/user-journey/journeyDb';
|
||||
import journeyRenderer from './diagrams/user-journey/journeyRenderer';
|
||||
import configApi from './config';
|
||||
import getStyles from './styles';
|
||||
const themes = {};
|
||||
|
||||
for (const themeName of ['default', 'forest', 'dark', 'neutral', 'base']) {
|
||||
themes[themeName] = require(`./themes/theme-${themeName}.js`);
|
||||
}
|
||||
import theme from './themes';
|
||||
|
||||
function parse(text) {
|
||||
const graphInit = utils.detectInit(text);
|
||||
@ -534,9 +530,9 @@ function updateRendererConfigs(conf) {
|
||||
|
||||
function reinitialize(options) {
|
||||
// console.warn(`mermaidAPI.reinitialize: v${pkg.version}`, options);
|
||||
if (options.theme && themes[options.theme]) {
|
||||
if (options.theme && theme[options.theme]) {
|
||||
// Todo merge with user options
|
||||
options.themeVariables = themes[options.theme].getThemeVariables(options.themeVariables);
|
||||
options.themeVariables = options.theme;
|
||||
}
|
||||
|
||||
// Set default options
|
||||
@ -550,11 +546,11 @@ function initialize(options) {
|
||||
// console.log(`mermaidAPI.initialize: v${pkg.version} ${options}`);
|
||||
// Set default options
|
||||
|
||||
if (options && options.theme && themes[options.theme]) {
|
||||
if (options && options.theme && theme[options.theme]) {
|
||||
// Todo merge with user options
|
||||
options.themeVariables = themes[options.theme].getThemeVariables(options.themeVariables);
|
||||
options.themeVariables = theme[options.theme].getThemeVariables(options.themeVariables);
|
||||
} else {
|
||||
if (options) options.themeVariables = themes.default;
|
||||
if (options) options.themeVariables = theme.default.getThemeVariables();
|
||||
}
|
||||
|
||||
const config = typeof options === 'object' ? setSiteConfig(options) : getSiteConfig();
|
||||
|
23
src/themes/index.js
Normal file
23
src/themes/index.js
Normal file
@ -0,0 +1,23 @@
|
||||
import { getThemeVariables as baseThemeVariables } from './theme-base';
|
||||
import { getThemeVariables as darkThemeVariables } from './theme-dark';
|
||||
import { getThemeVariables as defaultThemeVariables } from './theme-default';
|
||||
import { getThemeVariables as forestThemeVariables } from './theme-forest';
|
||||
import { getThemeVariables as neutralThemeVariables } from './theme-neutral';
|
||||
|
||||
export default {
|
||||
base: {
|
||||
getThemeVariables: baseThemeVariables
|
||||
},
|
||||
dark: {
|
||||
getThemeVariables: darkThemeVariables
|
||||
},
|
||||
default: {
|
||||
getThemeVariables: defaultThemeVariables
|
||||
},
|
||||
forest: {
|
||||
getThemeVariables: forestThemeVariables
|
||||
},
|
||||
neutral: {
|
||||
getThemeVariables: neutralThemeVariables
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user