From f29cfc7590da5ef3f2a1d728dc3bb202bbed356a Mon Sep 17 00:00:00 2001 From: kagol Date: Sun, 19 Sep 2021 19:36:06 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=BB=E9=A2=98?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- devui/theme/core/theme.ts | 43 ++++++++++++++++++ devui/theme/index.ts | 10 ----- devui/theme/theme.ts | 45 +++---------------- .../devui-theme/components/NavBar.vue | 2 +- 5 files changed, 50 insertions(+), 52 deletions(-) create mode 100644 devui/theme/core/theme.ts delete mode 100644 devui/theme/index.ts diff --git a/README.md b/README.md index 344ed901..7032a70e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@

- DevUI Logo + DevUI Logo

diff --git a/devui/theme/core/theme.ts b/devui/theme/core/theme.ts new file mode 100644 index 00000000..2b37e404 --- /dev/null +++ b/devui/theme/core/theme.ts @@ -0,0 +1,43 @@ +class Theme { + static imports: any = {} + + static import(name: string): any { + return this.imports[name] + } + + static register(name: string, target: any): void { + this.imports[name] = target + } + + constructor(theme: string) { + this.applyTheme(theme) + } + + applyTheme(name: string): void { + const theme = Theme.imports[name] + if (!theme) { + console.error(`主题 ${theme} 未注册!`) + return + } + + const id = 'devui-theme-variable' + const themeVariable = `:root { ${stringify(theme)} }` + let styleElement = document.getElementById(id) + if (styleElement) { + styleElement.innerText = themeVariable + } else { + styleElement = document.createElement('style') + styleElement.id = id + styleElement.innerText = themeVariable + document.head.appendChild(styleElement) + } + } +} + +function stringify(theme: any) { + return Object.entries(theme) + .map(([key, value]) => `--${key}:${value}`) + .join(';') +} + +export default Theme diff --git a/devui/theme/index.ts b/devui/theme/index.ts deleted file mode 100644 index 045f7781..00000000 --- a/devui/theme/index.ts +++ /dev/null @@ -1,10 +0,0 @@ -import Theme from './theme' -import dark from './themes/dark' -import light from './themes/light' - -Theme.register('dark', dark) -Theme.register('light', light) - -export { dark, light } - -export default Theme diff --git a/devui/theme/theme.ts b/devui/theme/theme.ts index 2b37e404..95be1ea3 100644 --- a/devui/theme/theme.ts +++ b/devui/theme/theme.ts @@ -1,43 +1,8 @@ -class Theme { - static imports: any = {} +import Theme from './core/theme' +import dark from './themes/dark' +import light from './themes/light' - static import(name: string): any { - return this.imports[name] - } - - static register(name: string, target: any): void { - this.imports[name] = target - } - - constructor(theme: string) { - this.applyTheme(theme) - } - - applyTheme(name: string): void { - const theme = Theme.imports[name] - if (!theme) { - console.error(`主题 ${theme} 未注册!`) - return - } - - const id = 'devui-theme-variable' - const themeVariable = `:root { ${stringify(theme)} }` - let styleElement = document.getElementById(id) - if (styleElement) { - styleElement.innerText = themeVariable - } else { - styleElement = document.createElement('style') - styleElement.id = id - styleElement.innerText = themeVariable - document.head.appendChild(styleElement) - } - } -} - -function stringify(theme: any) { - return Object.entries(theme) - .map(([key, value]) => `--${key}:${value}`) - .join(';') -} +Theme.register('dark', dark) +Theme.register('light', light) export default Theme diff --git a/sites/.vitepress/devui-theme/components/NavBar.vue b/sites/.vitepress/devui-theme/components/NavBar.vue index 95bf76f3..9cf3acec 100644 --- a/sites/.vitepress/devui-theme/components/NavBar.vue +++ b/sites/.vitepress/devui-theme/components/NavBar.vue @@ -1,6 +1,6 @@