From 1ba847eadf8bb324608418774567f663427e4567 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 25 Mar 2025 09:25:44 +0800 Subject: [PATCH 1/4] =?UTF-8?q?refactor:=20=E4=B8=BB=E9=A2=98=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E6=94=B9=E4=B8=BA=20JS=20=E6=A1=86=E6=9E=B6=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Components/Pre.razor.cs | 22 ------------------- .../Components/Components/Pre.razor.js | 22 +++++++++++++++---- .../wwwroot/modules/utility.js | 6 ++++- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/BootstrapBlazor.Server/Components/Components/Pre.razor.cs b/src/BootstrapBlazor.Server/Components/Components/Pre.razor.cs index 90133402d80..7077a5f9adc 100644 --- a/src/BootstrapBlazor.Server/Components/Components/Pre.razor.cs +++ b/src/BootstrapBlazor.Server/Components/Components/Pre.razor.cs @@ -29,10 +29,6 @@ public partial class Pre [NotNull] private CodeSnippetService? CodeSnippetService { get; set; } - [Inject] - [NotNull] - private IThemeProvider? ThemeProviderService { get; set; } - /// /// 获得/设置 子组件 CodeFile 为空时生效 /// @@ -71,16 +67,6 @@ public partial class Pre private string? CopiedText { get; set; } - /// - /// - /// - protected override void OnInitialized() - { - base.OnInitialized(); - - ThemeProviderService.ThemeChangedAsync += OnThemeChanged; - } - /// /// /// @@ -203,12 +189,4 @@ private string FindCodeSnippetByName(string code) [GeneratedRegex(@"[\s\S]*?")] private static partial Regex TipsRegex(); - - private async Task OnThemeChanged(string themeName) - { - if (themeName == "light" || themeName == "dark") - { - await InvokeVoidAsync("switchTheme", themeName); - } - } } diff --git a/src/BootstrapBlazor.Server/Components/Components/Pre.razor.js b/src/BootstrapBlazor.Server/Components/Components/Pre.razor.js index 54d82b2be68..8c412668f0b 100644 --- a/src/BootstrapBlazor.Server/Components/Components/Pre.razor.js +++ b/src/BootstrapBlazor.Server/Components/Components/Pre.razor.js @@ -1,4 +1,4 @@ -import { copy, getDescribedElement, addLink, removeLink, addScript, getHeight, getPreferredTheme } from "../../_content/BootstrapBlazor/modules/utility.js" +import { copy, getDescribedElement, addLink, removeLink, addScript, getHeight, getPreferredTheme, registerBootstrapBlazorModule } from "../../_content/BootstrapBlazor/modules/utility.js" import EventHandler from "../../_content/BootstrapBlazor/modules/event-handler.js" export async function init(id, title, assetRoot) { @@ -48,6 +48,15 @@ export async function init(id, title, assetRoot) { preElement.style.maxHeight = `${preHeight}px` }) } + + registerBootstrapBlazorModule('Pre', id, () => { + EventHandler.on(document, 'changed.bb.theme', updateTheme); + }); +} + +const updateTheme = e => { + const theme = e.theme; + switchTheme(theme); } export async function highlight(id) { @@ -96,7 +105,12 @@ export function dispose(id) { return } - EventHandler.off(el, 'click', '.btn-copy') - EventHandler.off(el, 'click', '.btn-plus') - EventHandler.off(el, 'click', '.btn-minus') + EventHandler.off(el, 'click', '.btn-copy'); + EventHandler.off(el, 'click', '.btn-plus'); + EventHandler.off(el, 'click', '.btn-minus'); + + const { Pre } = window.BootstrapBlazor; + Pre.dispose(id, () => { + EventHandler.off(document, 'changed.bb.theme', updateTheme); + }); } diff --git a/src/BootstrapBlazor/wwwroot/modules/utility.js b/src/BootstrapBlazor/wwwroot/modules/utility.js index a8cf06a0271..5930a97baae 100644 --- a/src/BootstrapBlazor/wwwroot/modules/utility.js +++ b/src/BootstrapBlazor/wwwroot/modules/utility.js @@ -1,4 +1,6 @@ -const vibrate = () => { +import EventHandler from "./event-handler.js" + +const vibrate = () => { if ('vibrate' in window.navigator) { window.navigator.vibrate([200, 100, 200]) const handler = window.setTimeout(function () { @@ -788,6 +790,8 @@ export function switchTheme(theme, x = 0, y = 0, sync = true) { else { setTheme(theme, sync); } + + EventHandler.trigger(document, 'changed.bb.theme', { theme: theme }); } const deepMerge = (obj1, obj2, skipNull = true) => { From 19da4435b6521c03edd64915921ebecaf7f69e4b Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 25 Mar 2025 10:01:26 +0800 Subject: [PATCH 2/4] =?UTF-8?q?refactor:=20=E5=BC=82=E6=AD=A5=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/wwwroot/modules/utility.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BootstrapBlazor/wwwroot/modules/utility.js b/src/BootstrapBlazor/wwwroot/modules/utility.js index 5930a97baae..2b539fb5264 100644 --- a/src/BootstrapBlazor/wwwroot/modules/utility.js +++ b/src/BootstrapBlazor/wwwroot/modules/utility.js @@ -785,13 +785,13 @@ export function switchTheme(theme, x = 0, y = 0, sync = true) { document.documentElement.style.setProperty('--bb-theme-y', `${y}px`); document.startViewTransition(() => { setTheme(theme, sync); + EventHandler.trigger(document, 'changed.bb.theme', { theme: theme }); }); } else { setTheme(theme, sync); + EventHandler.trigger(document, 'changed.bb.theme', { theme: theme }); } - - EventHandler.trigger(document, 'changed.bb.theme', { theme: theme }); } const deepMerge = (obj1, obj2, skipNull = true) => { From ccffd230af1e1378fd3e0d64200e589339741924 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 25 Mar 2025 10:08:56 +0800 Subject: [PATCH 3/4] =?UTF-8?q?refactor:=20=E7=B2=BE=E7=AE=80=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=8F=90=E9=AB=98=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/wwwroot/modules/utility.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/BootstrapBlazor/wwwroot/modules/utility.js b/src/BootstrapBlazor/wwwroot/modules/utility.js index 2b539fb5264..f8e6f86aeab 100644 --- a/src/BootstrapBlazor/wwwroot/modules/utility.js +++ b/src/BootstrapBlazor/wwwroot/modules/utility.js @@ -756,6 +756,7 @@ export function setTheme(theme, sync) { }) saveTheme(theme); } + EventHandler.trigger(document, 'changed.bb.theme', { theme: theme }); } export function setActiveTheme(el, activeItem) { @@ -785,12 +786,10 @@ export function switchTheme(theme, x = 0, y = 0, sync = true) { document.documentElement.style.setProperty('--bb-theme-y', `${y}px`); document.startViewTransition(() => { setTheme(theme, sync); - EventHandler.trigger(document, 'changed.bb.theme', { theme: theme }); }); } else { setTheme(theme, sync); - EventHandler.trigger(document, 'changed.bb.theme', { theme: theme }); } } From 275312487b6fda7a645b4af8e91879e57cb0a793 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 25 Mar 2025 11:53:45 +0800 Subject: [PATCH 4/4] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E9=99=8D=E4=BD=8E=E7=89=88=E6=9C=AC=E8=A6=81=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/wwwroot/modules/base-popover.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/wwwroot/modules/base-popover.js b/src/BootstrapBlazor/wwwroot/modules/base-popover.js index eb56a9ef373..d062b18140c 100644 --- a/src/BootstrapBlazor/wwwroot/modules/base-popover.js +++ b/src/BootstrapBlazor/wwwroot/modules/base-popover.js @@ -17,7 +17,7 @@ const Popover = { initCallback: null, hideCallback: null }, - ...(config ?? {}) + ...(config || {}) } const createPopover = () => { if (!popover.isDisabled()) {