mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-28 07:03:17 +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 { assignWithDepth } from './utils';
|
||||||
import { logger } from './logger';
|
import { logger } from './logger';
|
||||||
|
import theme from './themes';
|
||||||
|
|
||||||
|
// import { unflatten } from 'flat';
|
||||||
|
// import flatten from 'flat';
|
||||||
|
|
||||||
// import themeVariables from './theme-default';
|
// import themeVariables from './theme-default';
|
||||||
// import themeForestVariables from './theme-forest';
|
// import themeForestVariables from './theme-forest';
|
||||||
// import themeNeutralVariables from './theme-neutral';
|
// 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)].**
|
* **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>
|
* </pre>
|
||||||
*/
|
*/
|
||||||
theme: 'default',
|
theme: 'default',
|
||||||
themeVariables: themes['default'].getThemeVariables(),
|
themeVariables: theme['default'].getThemeVariables(),
|
||||||
themeCSS: undefined,
|
themeCSS: undefined,
|
||||||
/* **maxTextSize** - The maximum allowed size of the users text diamgram */
|
/* **maxTextSize** - The maximum allowed size of the users text diamgram */
|
||||||
maxTextSize: 50000,
|
maxTextSize: 50000,
|
||||||
@ -869,6 +877,11 @@ const currentConfig = assignWithDepth({}, defaultConfig);
|
|||||||
* @returns {*} - the siteConfig
|
* @returns {*} - the siteConfig
|
||||||
*/
|
*/
|
||||||
export const setSiteConfig = conf => {
|
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 });
|
assignWithDepth(currentConfig, conf, { clobber: true });
|
||||||
// Set theme variables if user has set the theme option
|
// Set theme variables if user has set the theme option
|
||||||
assignWithDepth(siteConfig, conf);
|
assignWithDepth(siteConfig, conf);
|
||||||
@ -901,6 +914,10 @@ export const getSiteConfig = () => {
|
|||||||
*/
|
*/
|
||||||
export const setConfig = conf => {
|
export const setConfig = conf => {
|
||||||
sanitize(conf);
|
sanitize(conf);
|
||||||
|
Object.keys(conf).forEach(key => {
|
||||||
|
const manipulator = manipulators[key];
|
||||||
|
conf[key] = manipulator ? manipulator(conf[key]) : conf[key];
|
||||||
|
});
|
||||||
|
|
||||||
assignWithDepth(currentConfig, conf);
|
assignWithDepth(currentConfig, conf);
|
||||||
return getConfig();
|
return getConfig();
|
||||||
|
@ -52,11 +52,7 @@ import journeyDb from './diagrams/user-journey/journeyDb';
|
|||||||
import journeyRenderer from './diagrams/user-journey/journeyRenderer';
|
import journeyRenderer from './diagrams/user-journey/journeyRenderer';
|
||||||
import configApi from './config';
|
import configApi from './config';
|
||||||
import getStyles from './styles';
|
import getStyles from './styles';
|
||||||
const themes = {};
|
import theme from './themes';
|
||||||
|
|
||||||
for (const themeName of ['default', 'forest', 'dark', 'neutral', 'base']) {
|
|
||||||
themes[themeName] = require(`./themes/theme-${themeName}.js`);
|
|
||||||
}
|
|
||||||
|
|
||||||
function parse(text) {
|
function parse(text) {
|
||||||
const graphInit = utils.detectInit(text);
|
const graphInit = utils.detectInit(text);
|
||||||
@ -534,9 +530,9 @@ function updateRendererConfigs(conf) {
|
|||||||
|
|
||||||
function reinitialize(options) {
|
function reinitialize(options) {
|
||||||
// console.warn(`mermaidAPI.reinitialize: v${pkg.version}`, 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
|
// Todo merge with user options
|
||||||
options.themeVariables = themes[options.theme].getThemeVariables(options.themeVariables);
|
options.themeVariables = options.theme;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set default options
|
// Set default options
|
||||||
@ -550,11 +546,11 @@ function initialize(options) {
|
|||||||
// console.log(`mermaidAPI.initialize: v${pkg.version} ${options}`);
|
// console.log(`mermaidAPI.initialize: v${pkg.version} ${options}`);
|
||||||
// Set default options
|
// Set default options
|
||||||
|
|
||||||
if (options && options.theme && themes[options.theme]) {
|
if (options && options.theme && theme[options.theme]) {
|
||||||
// Todo merge with user options
|
// Todo merge with user options
|
||||||
options.themeVariables = themes[options.theme].getThemeVariables(options.themeVariables);
|
options.themeVariables = theme[options.theme].getThemeVariables(options.themeVariables);
|
||||||
} else {
|
} else {
|
||||||
if (options) options.themeVariables = themes.default;
|
if (options) options.themeVariables = theme.default.getThemeVariables();
|
||||||
}
|
}
|
||||||
|
|
||||||
const config = typeof options === 'object' ? setSiteConfig(options) : getSiteConfig();
|
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