themeUtil.js 3.26 KB
Newer Older
wb-ct393452's avatar
wb-ct393452 committed
1 2 3 4 5 6 7
import client from 'webpack-theme-color-replacer/client';
import { getMenuColors, getAntdColors, getThemeToggleColors, getFunctionalColors } from '../utils/colorUtil';
import { globalConfig, settingConfig } from '../config';

const theme = settingConfig.theme;

function getThemeColors(color, $theme) {
8 9 10 11 12 13 14 15 16 17 18 19 20 21
    const _color = color || theme.color;
    const mode = $theme || theme.mode;
    const replaceColors = getThemeToggleColors(_color, mode);
    const themeColors = [
        ...replaceColors.mainColors,
        ...replaceColors.subColors,
        ...replaceColors.menuColors,
        ...replaceColors.contentColors,
        ...replaceColors.rgbColors,
        ...replaceColors.functionalColors.success,
        ...replaceColors.functionalColors.warning,
        ...replaceColors.functionalColors.error,
    ];
    return themeColors;
wb-ct393452's avatar
wb-ct393452 committed
22 23 24
}

function changeThemeColor(newColor, $theme) {
25 26
    let promise = client.changer.changeColor({ newColors: getThemeColors(newColor, $theme) });
    return promise;
wb-ct393452's avatar
wb-ct393452 committed
27 28 29
}

function modifyVars(color) {
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    let _color = color || theme.color;
    const palettes = getAntdColors(_color, theme.mode);
    const menuColors = getMenuColors(_color, theme.mode);
    const { success, warning, error } = getFunctionalColors(theme.mode);
    const primary = palettes[5];
    return {
        'primary-color': primary,
        'primary-1': palettes[0],
        'primary-2': palettes[1],
        'primary-3': palettes[2],
        'primary-4': palettes[3],
        'primary-5': palettes[4],
        'primary-6': palettes[5],
        'primary-7': palettes[6],
        'primary-8': palettes[7],
        'primary-9': palettes[8],
        'primary-10': palettes[9],
        'info-color': primary,
        'success-color': success[5],
        'warning-color': warning[5],
        'error-color': error[5],
        'alert-info-bg-color': palettes[0],
        'alert-info-border-color': palettes[2],
        'alert-success-bg-color': success[0],
        'alert-success-border-color': success[2],
        'alert-warning-bg-color': warning[0],
        'alert-warning-border-color': warning[2],
        'alert-error-bg-color': error[0],
        'alert-error-border-color': error[2],
        'processing-color': primary,
        'menu-dark-submenu-bg': menuColors[0],
        'layout-header-background': menuColors[1],
        'layout-trigger-background': menuColors[2],
        'btn-danger-bg': error[4],
        'btn-danger-border': error[4],
        ...globalConfig.theme[theme.mode],
    };
wb-ct393452's avatar
wb-ct393452 committed
67 68 69
}

function loadLocalTheme(localSetting) {
70 71 72 73 74 75
    if (localSetting && localSetting.theme) {
        let { color, mode } = localSetting.theme;
        color = color || theme.color;
        mode = mode || theme.mode;
        changeThemeColor(color, mode);
    }
wb-ct393452's avatar
wb-ct393452 committed
76 77 78 79 80 81 82 83
}

/**
 * 获取本地保存的配置
 * @param load {boolean} 是否加载配置中的主题
 * @returns {Object}
 */
function getLocalSetting(loadTheme) {
84 85 86 87 88 89 90 91 92 93 94
    let localSetting = {};
    try {
        const localSettingStr = localStorage.getItem(process.env.VUE_APP_SETTING_KEY) || '{}';
        localSetting = JSON.parse(localSettingStr);
    } catch (e) {
        console.error(e);
    }
    if (loadTheme) {
        loadLocalTheme(localSetting);
    }
    return localSetting;
wb-ct393452's avatar
wb-ct393452 committed
95 96
}

97
export { getThemeColors, changeThemeColor, modifyVars, loadLocalTheme, getLocalSetting };