Skip to content

Commit 2f7011f

Browse files
authored
feat(Utility): add custom changed.bb.theme event (#5709)
* refactor: 主题切换改为 JS 框架完成 * refactor: 异步支持 * refactor: 精简代码提高可读性 * refactor: 重构代码降低版本要求
1 parent 2713111 commit 2f7011f

File tree

4 files changed

+23
-28
lines changed

4 files changed

+23
-28
lines changed

src/BootstrapBlazor.Server/Components/Components/Pre.razor.cs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ public partial class Pre
2929
[NotNull]
3030
private CodeSnippetService? CodeSnippetService { get; set; }
3131

32-
[Inject]
33-
[NotNull]
34-
private IThemeProvider? ThemeProviderService { get; set; }
35-
3632
/// <summary>
3733
/// 获得/设置 子组件 CodeFile 为空时生效
3834
/// </summary>
@@ -71,16 +67,6 @@ public partial class Pre
7167

7268
private string? CopiedText { get; set; }
7369

74-
/// <summary>
75-
/// <inheritdoc/>
76-
/// </summary>
77-
protected override void OnInitialized()
78-
{
79-
base.OnInitialized();
80-
81-
ThemeProviderService.ThemeChangedAsync += OnThemeChanged;
82-
}
83-
8470
/// <summary>
8571
/// <inheritdoc/>
8672
/// </summary>
@@ -203,12 +189,4 @@ private string FindCodeSnippetByName(string code)
203189

204190
[GeneratedRegex(@"<Tips[\s\S]*>[\s\S]*?</Tips>")]
205191
private static partial Regex TipsRegex();
206-
207-
private async Task OnThemeChanged(string themeName)
208-
{
209-
if (themeName == "light" || themeName == "dark")
210-
{
211-
await InvokeVoidAsync("switchTheme", themeName);
212-
}
213-
}
214192
}

src/BootstrapBlazor.Server/Components/Components/Pre.razor.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { copy, getDescribedElement, addLink, removeLink, addScript, getHeight, getPreferredTheme } from "../../_content/BootstrapBlazor/modules/utility.js"
1+
import { copy, getDescribedElement, addLink, removeLink, addScript, getHeight, getPreferredTheme, registerBootstrapBlazorModule } from "../../_content/BootstrapBlazor/modules/utility.js"
22
import EventHandler from "../../_content/BootstrapBlazor/modules/event-handler.js"
33

44
export async function init(id, title, assetRoot) {
@@ -48,6 +48,15 @@ export async function init(id, title, assetRoot) {
4848
preElement.style.maxHeight = `${preHeight}px`
4949
})
5050
}
51+
52+
registerBootstrapBlazorModule('Pre', id, () => {
53+
EventHandler.on(document, 'changed.bb.theme', updateTheme);
54+
});
55+
}
56+
57+
const updateTheme = e => {
58+
const theme = e.theme;
59+
switchTheme(theme);
5160
}
5261

5362
export async function highlight(id) {
@@ -96,7 +105,12 @@ export function dispose(id) {
96105
return
97106
}
98107

99-
EventHandler.off(el, 'click', '.btn-copy')
100-
EventHandler.off(el, 'click', '.btn-plus')
101-
EventHandler.off(el, 'click', '.btn-minus')
108+
EventHandler.off(el, 'click', '.btn-copy');
109+
EventHandler.off(el, 'click', '.btn-plus');
110+
EventHandler.off(el, 'click', '.btn-minus');
111+
112+
const { Pre } = window.BootstrapBlazor;
113+
Pre.dispose(id, () => {
114+
EventHandler.off(document, 'changed.bb.theme', updateTheme);
115+
});
102116
}

src/BootstrapBlazor/wwwroot/modules/base-popover.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const Popover = {
1717
initCallback: null,
1818
hideCallback: null
1919
},
20-
...(config ?? {})
20+
...(config || {})
2121
}
2222
const createPopover = () => {
2323
if (!popover.isDisabled()) {

src/BootstrapBlazor/wwwroot/modules/utility.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const vibrate = () => {
1+
import EventHandler from "./event-handler.js"
2+
3+
const vibrate = () => {
24
if ('vibrate' in window.navigator) {
35
window.navigator.vibrate([200, 100, 200])
46
const handler = window.setTimeout(function () {
@@ -754,6 +756,7 @@ export function setTheme(theme, sync) {
754756
})
755757
saveTheme(theme);
756758
}
759+
EventHandler.trigger(document, 'changed.bb.theme', { theme: theme });
757760
}
758761

759762
export function setActiveTheme(el, activeItem) {

0 commit comments

Comments
 (0)