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.

feat(core): expose theme controls through facade (#7465)

白熱dae6d8f40454
diff --git a/packages/core/src/facade/__tests__/f-univer.spec.ts b/packages/core/src/facade/__tests__/f-univer.spec.ts
index 21b3b68c54..30a3d7f044 100644
--- a/packages/core/src/facade/__tests__/f-univer.spec.ts
+++ b/packages/core/src/facade/__tests__/f-univer.spec.ts
@@ -81,7 +81,6 @@ describe('FUniver integration', () => {
const injector = univer.__getInjector();
const commandService = injector.get(ICommandService);
const localeService = injector.get(LocaleService);
- const themeService = injector.get(ThemeService);
const logs: string[] = [];
const disposables: IDisposable[] = [];
@@ -103,11 +102,9 @@ describe('FUniver integration', () => {
facade: { hello: 'Hola {0}' },
} as never);
univerAPI.setLocale('esES');
- univerAPI.toggleDarkMode(true);
expect(localeService.getCurrentLocale()).toBe('esES');
expect(localeService.t('facade.hello', 'Univer')).toBe('Hola Univer');
- expect(themeService.darkMode).toBe(true);
expect(await univerAPI.executeCommand(TEST_COMMAND_ID, { value: 'ok' })).toBe(true);
expect(univerAPI.syncExecuteCommand(TEST_COMMAND_ID, { value: 'ok' })).toBe(true);
@@ -160,6 +157,26 @@ describe('FUniver integration', () => {
disposables.forEach((disposable) => disposable.dispose());
});
+ it('should configure and read the theme through the facade api', () => {
+ const themeService = univer.__getInjector().get(ThemeService);
+ const currentTheme = univerAPI.getCurrentTheme();
+ const theme = {
+ ...currentTheme,
+ primary: {
+ ...currentTheme.primary,
+ 600: '#123456',
+ },
+ };
+
+ univerAPI.setTheme(theme);
+ univerAPI.toggleDarkMode(true);
+
+ expect(univerAPI.getCurrentTheme().primary[600]).toBe('#123456');
+ expect(univerAPI.isDarkMode()).toBe(true);
+ expect(themeService.getCurrentTheme().primary[600]).toBe('#123456');
+ expect(themeService.darkMode).toBe(true);
+ });
+
it('should support cancelable commands and undo-redo facade events', async () => {
const injector = univer.__getInjector();
const commandService = injector.get(ICommandService);
diff --git a/packages/core/src/facade/f-univer.ts b/packages/core/src/facade/f-univer.ts
index 02f0ba9243..88d4388330 100644
--- a/packages/core/src/facade/f-univer.ts
+++ b/packages/core/src/facade/f-univer.ts
@@ -26,6 +26,7 @@ import type {
LifecycleStages,
LocaleType,
} from '@univerjs/core';
+import type { Theme } from '@univerjs/themes';
import type { Subscription } from 'rxjs';
import type { ICommandEvent, IEventParamConfig } from './f-event';
import {
@@ -370,6 +371,54 @@ export class FUniver extends Disposable {
return this._commandService.executeCommand(RedoCommand.id);
}
+ /**
+ * Set the theme used by Univer.
+ * @param {Theme} theme - The complete theme to use.
+ * @example
+ * ```ts
+ * import { defaultTheme } from '@univerjs/themes';
+ *
+ * univerAPI.setTheme({
+ * ...defaultTheme,
+ * primary: {
+ * ...defaultTheme.primary,
+ * 600: '#274fee',
+ * },
+ * });
+ * ```
+ */
+ setTheme(theme: Theme): void {
+ const themeService = this._injector.get(ThemeService);
+ themeService.setTheme(theme);
+ }
+
+ /**
+ * Get the theme currently used by Univer.
+ * @returns {Theme} The current theme.
+ * @example
+ * ```ts
+ * const theme = univerAPI.getCurrentTheme();
+ * console.log(theme.primary[600]);
+ * ```
+ */
+ getCurrentTheme(): Theme {
+ const themeService = this._injector.get(ThemeService);
+ return themeService.getCurrentTheme();
+ }
+
+ /**
+ * Whether Univer is currently using dark mode.
+ * @returns {boolean} Whether dark mode is enabled.
+ * @example
+ * ```ts
+ * const darkMode = univerAPI.isDarkMode();
+ * ```
+ */
+ isDarkMode(): boolean {
+ const themeService = this._injector.get(ThemeService);
+ return themeService.darkMode;
+ }
+
/**
* Toggle dark mode on or off.
* @param {boolean} isDarkMode - Whether the dark mode is enabled.