Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

refactor(design): refactor theme class (#615)

白熱983f2fc27d38
diff --git a/examples/univer-sheet-ts/public/index.html b/examples/univer-sheet-ts/public/index.html
index 8d7a44503e..58ef50b847 100644
--- a/examples/univer-sheet-ts/public/index.html
+++ b/examples/univer-sheet-ts/public/index.html
@@ -23,9 +23,7 @@
</head>
<body style="overflow: hidden">
- <div id="app" style="z-index: 1; position: fixed; font-size: 20px; color: green"></div>
- <div id="univer-container" style="height: 100%"></div>
-
+ <div id="app" style="height: 100%;"></div>
<script src="./main.js"></script>
</body>
</html>
diff --git a/examples/univer-sheet-ts/src/main.ts b/examples/univer-sheet-ts/src/main.ts
index 9101fe4619..35736a1d05 100644
--- a/examples/univer-sheet-ts/src/main.ts
+++ b/examples/univer-sheet-ts/src/main.ts
@@ -32,7 +32,7 @@ univer.registerPlugin(DocPlugin, {
// univer.registerPlugin(DocUIPlugin);
univer.registerPlugin(RenderEngine);
univer.registerPlugin(UIPlugin, {
- container: 'univer-container',
+ container: 'app',
header: true,
toolbar: true,
footer: true,
diff --git a/packages/base-ui/src/views/app.module.less b/packages/base-ui/src/views/app.module.less
index 5d65a983e6..58dba43ff5 100644
--- a/packages/base-ui/src/views/app.module.less
+++ b/packages/base-ui/src/views/app.module.less
@@ -2,15 +2,8 @@
&-layout {
display: flex;
flex-direction: column;
-
height: 100%;
-
- // TODO: @jikkai portal div doesn't have the same font-family as the rest of the layout
- font-family: Inter, -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "noto sans", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif;
-
background-color: rgb(var(--bg-color));
-
- -webkit-font-smoothing: antialiased;
}
&-header {
diff --git a/packages/core/src/services/theme/theme.service.ts b/packages/core/src/services/theme/theme.service.ts
index 7ca785bb4a..bb4d6c71c9 100644
--- a/packages/core/src/services/theme/theme.service.ts
+++ b/packages/core/src/services/theme/theme.service.ts
@@ -1,6 +1,7 @@
-import { BehaviorSubject, Observable } from 'rxjs';
+import type { Observable } from 'rxjs';
+import { BehaviorSubject } from 'rxjs';
-import { Nullable } from '../../common/type-utils';
+import type { Nullable } from '../../common/type-utils';
import { Disposable, toDisposable } from '../../shared/lifecycle';
export interface IStyleSheet {
@@ -8,10 +9,7 @@ export interface IStyleSheet {
}
export class ThemeService extends Disposable {
- private currentTheme: Nullable<IStyleSheet>;
-
- // TODO: dark mode
- private darkMode: boolean = false;
+ private _currentTheme: Nullable<IStyleSheet>;
private readonly _currentTheme$ = new BehaviorSubject<IStyleSheet>({});
readonly currentTheme$: Observable<IStyleSheet> = this._currentTheme$.asObservable();
@@ -22,21 +20,15 @@ export class ThemeService extends Disposable {
this.disposeWithMe(toDisposable(() => this._currentTheme$.complete()));
}
- getDarkMode() {
- return this.darkMode;
- }
-
getCurrentTheme() {
- if (!this.currentTheme) {
+ if (!this._currentTheme) {
throw new Error('[ThemeService]: current theme is not set!');
}
- return this.currentTheme;
+ return this._currentTheme;
}
- setDarkMode(flag: boolean) {}
-
setTheme(theme: IStyleSheet) {
- this.currentTheme = theme;
+ this._currentTheme = theme;
this._currentTheme$.next(theme);
}
}
diff --git a/packages/design/src/components/menu/index.module.less b/packages/design/src/components/menu/index.module.less
index 99ac071c9f..9a654e0fc4 100644
--- a/packages/design/src/components/menu/index.module.less
+++ b/packages/design/src/components/menu/index.module.less
@@ -82,6 +82,7 @@
position: relative;
box-sizing: border-box;
+ min-height: 28px;
margin: 0;
padding: var(--padding-xs);
@@ -89,7 +90,6 @@
list-style: none;
border-radius: var(--border-radius-base);
-
// Disabled state sets text to gray and nukes hover/tab effects
&.menu-item-disabled,
&.menu-submenu-disabled {
@@ -110,6 +110,7 @@
display: flex;
align-items: center;
justify-content: space-between;
+ height: 100%;
}
}
diff --git a/packages/design/src/themes/theme-root.module.less b/packages/design/src/themes/theme-root.module.less
new file mode 100644
index 0000000000..c3464b9baf
--- /dev/null
+++ b/packages/design/src/themes/theme-root.module.less
@@ -0,0 +1,5 @@
+.theme {
+ font-family: Inter, -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "noto sans", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif;
+
+ -webkit-font-smoothing: antialiased;
+}
diff --git a/packages/design/src/themes/theme.ts b/packages/design/src/themes/theme.ts
index 54a77d1b79..546a52242d 100644
--- a/packages/design/src/themes/theme.ts
+++ b/packages/design/src/themes/theme.ts
@@ -1,3 +1,5 @@
+import styles from './theme-root.module.less';
+
function convertToDashCase(input: string): string {
const dashCase = input.replace(/([A-Z])/g, (match) => `-${match.toLowerCase()}`).replace(/(\d+)/g, '-$1');
@@ -18,43 +20,32 @@ function convertHexToRgb(input: string): string {
}
class Theme {
- private styleSheet;
+ private _styleSheet: HTMLStyleElement | null = null;
+
+ private _themeRootName = styles.theme;
constructor() {
const $style = document.createElement('style');
+ $style.id = this._themeRootName;
document.head.appendChild($style);
- const index = document.styleSheets.length - 1;
- this.styleSheet = document.styleSheets[index];
+ this._styleSheet = $style;
}
setTheme(root: HTMLElement, theme: Record<string, string>) {
- // 1. remove old theme
- if (this.styleSheet) {
- let index = 0;
- for (let i = 0; i < this.styleSheet.cssRules.length; i++) {
- const rule = this.styleSheet.cssRules[i];
-
- if (rule instanceof CSSStyleRule) {
- index = i;
- this.styleSheet.deleteRule(index);
- break;
- }
- }
- }
-
- // 2. convert new theme to css style
- /**
- * covert object to style, remove " and replace , to ;
- * Example:
- * before: {--primary-color:"#0188fb",--primary-color-hover:"#5391ff"}
- * after: {--primary-color:#0188fb;--primary-color-hover:#5391ff;}
- */
- Object.keys(theme).forEach((key) => {
- const property = convertToDashCase(key);
- const value = convertHexToRgb(theme[key]);
- root.style.setProperty(property, value);
- });
+ if (!this._styleSheet) return;
+
+ // 1. set theme root class
+ root.classList.remove(this._themeRootName);
+ root.classList.add(this._themeRootName);
+
+ // 2. remove old theme
+ this._styleSheet.innerHTML = '';
+
+ // 3. set new theme
+ this._styleSheet.innerHTML = `.${this._themeRootName} {${Object.keys(theme)
+ .map((key) => `${convertToDashCase(key)}: ${convertHexToRgb(theme[key])};`)
+ .join('')}}`;
}
}