Skip to content

Commit 97c4e67

Browse files
authored
feat(UniverSheet): add auto switch darkmode function (#859)
* feat(UniverSheet): add auto switch darkmode function * chore: bump version 10.0.5
1 parent f8a72da commit 97c4e67

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

src/components/BootstrapBlazor.UniverSheet/BootstrapBlazor.UniverSheet.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>10.0.4</Version>
4+
<Version>10.0.5</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>

src/components/BootstrapBlazor.UniverSheet/Components/UniverSheet.razor.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import Data from '../../BootstrapBlazor/modules/data.js'
2-
import { isFunction } from '../../BootstrapBlazor/modules/utility.js'
1+
import { isFunction, registerBootstrapBlazorModule } from '../../BootstrapBlazor/modules/utility.js'
32
import { createUniverSheetAsync } from '../univer.js'
3+
import Data from '../../BootstrapBlazor/modules/data.js'
4+
import EventHandler from "../../BootstrapBlazor/modules/event-handler.js"
45

56
export async function init(id, invoke, options) {
67
const el = document.getElementById(id);
@@ -24,6 +25,10 @@ export async function init(id, invoke, options) {
2425
Data.set(id, univerSheet);
2526

2627
invoke.invokeMethodAsync('TriggerReadyAsync');
28+
29+
registerBootstrapBlazorModule('UniverSheet', id, () => {
30+
EventHandler.on(document, 'changed.bb.theme', updateTheme);
31+
});
2732
}
2833

2934
export function execute(id, data) {
@@ -39,4 +44,28 @@ export function dispose(id) {
3944
if (isFunction(univerSheet.dispose)) {
4045
univerSheet.dispose();
4146
}
47+
48+
const { UniverSheet } = window.BootstrapBlazor;
49+
if (UniverSheet) {
50+
UniverSheet.dispose(id, () => {
51+
EventHandler.off(document, 'changed.bb.theme', updateTheme);
52+
});
53+
}
54+
}
55+
56+
const updateTheme = e => {
57+
const theme = e.theme;
58+
59+
[...document.querySelectorAll('.bb-univer-sheet')].forEach(s => {
60+
const id = s.getAttribute('id');
61+
if (id) {
62+
const univerSheet = Data.get(id);
63+
if (univerSheet && univerSheet.darkMode === null) {
64+
const { univerAPI } = univerSheet;
65+
if (univerAPI) {
66+
univerAPI.toggleDarkMode(theme === 'dark');
67+
}
68+
}
69+
}
70+
});
4271
}

0 commit comments

Comments
 (0)