/* eslint-disable max-lines-per-function */ /* argus-disable dangerMethod */ import React from 'react'; import PropTypes from 'prop-types'; const darkmodeProcesser = () => { if (!window) { return; } function cache(key, value) { try { localStorage.setItem(key, JSON.stringify(value)); } catch (error) { console.log(error); } } function getCache(key) { try { return JSON.parse(localStorage.getItem(key)); } catch (error) { console.log(error); return undefined; } } const setMode = mode => { if (!window) { return; } const { body } = document; cache('semiMode', mode); const attr = 'theme-mode'; if (mode === 'dark') { body.setAttribute(attr, mode); } else { body.removeAttribute(attr); } window.themeStorage.darkModeListener.map(listener => listener(mode)); }; const getValue = () => { const mql = window.matchMedia('(prefers-color-scheme: dark)'); const match = mql.matches; return match ? 'dark' : 'light'; }; const cacheMode = getCache('semiMode'); const mode = cacheMode ? cacheMode : getValue(); document.addEventListener('readystatechange', () => { if (document.readyState === 'interactive') { setMode(mode); } }); if (!window.themeStorage) { window.themeStorage = { darkModeListener: [] }; } window.themeStorage.setMode = setMode; }; const semiThemeProcesser = () => { function cache(key, value) { try { localStorage.setItem(key, JSON.stringify(value)); } catch (error) { console.log(error); } } function getCache(key) { try { return JSON.parse(localStorage.getItem(key)); } catch (error) { console.log(error); return undefined; } } const defaultGlobal = { dark: { '--semi-color-primary': 'rgba(var(--semi-blue-5), 1)', '--semi-color-primary-hover': 'rgba(var(--semi-blue-6), 1)', '--semi-color-primary-active': 'rgba(var(--semi-blue-7), 1)', '--semi-color-primary-disabled': 'rgba(var(--semi-blue-2), 1)', '--semi-color-primary-light-default': 'rgba(var(--semi-blue-5), .2)', '--semi-color-primary-light-hover': 'rgba(var(--semi-blue-5), .3)', '--semi-color-primary-light-active': 'rgba(var(--semi-blue-5), .4)', '--semi-color-focus-border': 'rgba(var(--semi-blue-5), 1)' }, light: { '--semi-color-primary': 'rgba(var(--semi-blue-5), 1)', '--semi-color-primary-hover': 'rgba(var(--semi-blue-6), 1)', '--semi-color-primary-active': 'rgba(var(--semi-blue-7), 1)', '--semi-color-primary-disabled': 'rgba(var(--semi-blue-2), 1)', '--semi-color-primary-light-default': 'rgba(var(--semi-blue-0), 1)', '--semi-color-primary-light-hover': 'rgba(var(--semi-blue-1), 1)', '--semi-color-primary-light-active': 'rgba(var(--semi-blue-2), 1)', '--semi-color-focus-border': 'rgba(var(--semi-blue-5), 1)' } }; const _conventStr = obj => { let str = ''; Object.keys(obj).forEach(key => { const value = obj[key]; str = `${str}${key}: ${value};`; }); return str; }; const paletteObj = getCache('semiPaletteObj') ? getCache('semiPaletteObj') : { light: {}, dark: {} }; const globalObj = getCache('semiGlobalObj') ? getCache('semiGlobalObj') : { light: defaultGlobal.light, dark: defaultGlobal.dark, }; const writeStyle = ({ lightPaletteStr, darkPaletteStr, lightGlobalStr, darkGlobalStr }) => { let cssText = ` body{ ${lightPaletteStr} ${lightGlobalStr} }\n body[theme-mode='dark'] { ${darkPaletteStr} ${darkGlobalStr} } `; const styleSheet = document.querySelector('style[name="semi"]'); if (!styleSheet) { const style = document.createElement('style'); style.innerText = cssText; style.setAttribute('name', 'semi'); document.head.appendChild(style); } else { styleSheet.innerText = cssText; } }; const lightPaletteStr = _conventStr(paletteObj.light); const darkPaletteStr = _conventStr(paletteObj.dark); const lightGlobalStr = _conventStr(globalObj.light); const darkGlobalStr = _conventStr(globalObj.dark); writeStyle({ lightPaletteStr, darkPaletteStr, lightGlobalStr, darkGlobalStr }); }; export default function HTML(props) { if (process.env.NODE_ENV === 'production') { for (const component of props.headComponents) { if (component.type === 'style') { const index = props.headComponents.indexOf(component); const link = ; props.headComponents.splice(index, 1, link); } } } return (
{ THEME_SWITCHER_URL?