diff --git a/src/BootstrapBlazor.Server/Components/Components/GlobalToast.razor b/src/BootstrapBlazor.Server/Components/Components/GlobalToast.razor new file mode 100644 index 00000000000..ed7c68b60d1 --- /dev/null +++ b/src/BootstrapBlazor.Server/Components/Components/GlobalToast.razor @@ -0,0 +1,14 @@ +@inherits WebSiteModuleComponentBase + +@code { + RenderFragment RenderVote => + @
+
🏆
+

正在参加 Gitee 2025 最受欢迎的开源软件 投票活动,快来给我投票吧!

+
您的每一票都是对开源社区的支持,感谢您的参与!
+
+ 🚀 必须投一票 + 我知道了 +
+
; +} diff --git a/src/BootstrapBlazor.Server/Components/Components/GlobalToast.razor.cs b/src/BootstrapBlazor.Server/Components/Components/GlobalToast.razor.cs new file mode 100644 index 00000000000..121db43b0ec --- /dev/null +++ b/src/BootstrapBlazor.Server/Components/Components/GlobalToast.razor.cs @@ -0,0 +1,50 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the Apache 2.0 License +// See the LICENSE file in the project root for more information. +// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone + +using Microsoft.JSInterop; +using System.Globalization; + +namespace BootstrapBlazor.Server.Components.Components; + +/// +/// 正站通知组件 +/// +[JSModuleAutoLoader("Components/GlobalToast.razor.js", JSObjectReference = true)] +public partial class GlobalToast +{ + [Inject] + [NotNull] + private ToastService? Toast { get; set; } + + /// + /// + /// + /// + protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Interop, nameof(ShowToast)); + + /// + /// 显示通知窗口 + /// + /// + [JSInvokable] + public async Task ShowToast() + { + // 英文环境不通知 + if (CultureInfo.CurrentUICulture.Name == "en-US") + { + return; + } + + var option = new ToastOption() + { + Category = ToastCategory.Information, + Title = "Gitee 评选活动", + IsAutoHide = false, + ChildContent = RenderVote, + PreventDuplicates = true + }; + await Toast.Show(option); + } +} diff --git a/src/BootstrapBlazor.Server/Components/Components/GlobalToast.razor.js b/src/BootstrapBlazor.Server/Components/Components/GlobalToast.razor.js new file mode 100644 index 00000000000..bf8fabb8c86 --- /dev/null +++ b/src/BootstrapBlazor.Server/Components/Components/GlobalToast.razor.js @@ -0,0 +1,37 @@ +import EventHandler from "../../_content/BootstrapBlazor/modules/event-handler.js" + +export function init(invoke, method, debug) { + const localstorageKey = 'bb-g-toast' + const v = localStorage.getItem(localstorageKey); + if (v) { + try { + const differ = new Date().getTime() - v; + if (differ < 86400000) { + return; + } + } + catch { + localStorage.removeItem(localstorageKey); + } + } + + if (debug !== true) { + const handler = setTimeout(async () => { + clearTimeout(handler); + await invoke.invokeMethodAsync(method); + }, 10000); + } + + EventHandler.on(document, 'click', '#bb-g-toast', e => { + const toast = e.delegateTarget.closest('.toast'); + if (toast) { + toast.classList.remove('show'); + + localStorage.setItem(localstorageKey, new Date().getTime()); + } + }); +} + +export function dispose() { + EventHandler.off(document, 'click', '#bb-g-toast'); +} diff --git a/src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor b/src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor index 626831ce89e..86a69bf08c0 100644 --- a/src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor +++ b/src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor @@ -15,16 +15,3 @@ Reload 🗙 - -@code { - RenderFragment RenderVote => - @
-
🏆
-

正在参加 Gitee 2025 最受欢迎的开源软件 投票活动,快来给我投票吧!

-
您的每一票都是对开源社区的支持,感谢您的参与!
-
- 🚀 必须投一票 - 我知道了 -
-
; -} diff --git a/src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor.cs b/src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor.cs index c58ff812ff2..764276ff03b 100644 --- a/src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor.cs +++ b/src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor.cs @@ -5,14 +5,13 @@ using Microsoft.Extensions.Options; using Microsoft.JSInterop; -using System.Globalization; namespace BootstrapBlazor.Server.Components.Layout; /// /// 母版页基类 /// -public partial class BaseLayout : IAsyncDisposable +public partial class BaseLayout : IDisposable { [Inject] [NotNull] @@ -51,8 +50,6 @@ public partial class BaseLayout : IAsyncDisposable private string? CancelText { get; set; } private bool _init = false; - private JSModule? _module; - private DotNetObjectReference? _interop; /// /// @@ -74,18 +71,13 @@ protected override void OnInitialized() /// /// /// - protected override async Task OnAfterRenderAsync(bool firstRender) + protected override async Task OnInitializedAsync() { - await base.OnAfterRenderAsync(firstRender); + await base.OnInitializedAsync(); - if (firstRender) - { - _module = await JSRuntime.LoadModule($"{WebsiteOption.Value.JSModuleRootPath}Layout/BaseLayout.razor.js"); - _interop = DotNetObjectReference.Create(this); - await _module.InvokeVoidAsync("doTask", _interop, WebsiteOption.Value.IsDevelopment); - _init = true; - StateHasChanged(); - } + var module = await JSRuntime.LoadModule($"{WebsiteOption.Value.JSModuleRootPath}Layout/BaseLayout.razor.js"); + await module.InvokeVoidAsync("initTheme"); + _init = true; } private async Task NotifyCommit(DispatchEntry payload) @@ -129,56 +121,25 @@ private async Task NotifyReboot(DispatchEntry payload) } } - /// - /// 显示投票弹窗 - /// - /// - [JSInvokable] - public async Task ShowVoteToast() - { - // 英文环境不投票 - if (CultureInfo.CurrentUICulture.Name == "en-US") - { - return; - } - - _option = new ToastOption() - { - Category = ToastCategory.Information, - Title = "邀请您支持 BB 参与 Gitee 项目评选活动", - IsAutoHide = false, - ChildContent = RenderVote, - PreventDuplicates = true, - ClassString = "bb-vote-toast" - }; - await Toast.Show(_option); - } - /// /// 释放资源 /// /// - private async ValueTask DisposeAsync(bool disposing) + private void Dispose(bool disposing) { if (disposing) { CommitDispatchService.UnSubscribe(NotifyCommit); RebootDispatchService.UnSubscribe(NotifyReboot); - - if (_module != null) - { - await _module.InvokeVoidAsync("dispose"); - await _module.DisposeAsync(); - } } } /// /// 释放资源 /// - public async ValueTask DisposeAsync() + public void Dispose() { - await DisposeAsync(true); + Dispose(true); GC.SuppressFinalize(this); } } diff --git a/src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor.js b/src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor.js index 0489d590128..b143375ae13 100644 --- a/src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor.js +++ b/src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor.js @@ -1,44 +1,6 @@ import { getTheme, setTheme } from "../../_content/BootstrapBlazor/modules/utility.js" -import EventHandler from "../../_content/BootstrapBlazor/modules/event-handler.js" -function initTheme() { +export function initTheme() { const currentTheme = getTheme(); setTheme(currentTheme, false); } - -export function doTask(invoke, debug) { - initTheme(); - - const v = localStorage.getItem('bb-gitee-vote'); - if (v) { - try { - const differ = new Date().getTime() - v; - if (differ < 86400000) { - return; - } - } - catch { - localStorage.removeItem('bb-gitee-vote'); - } - } - - if (debug !== true) { - const handler = setTimeout(async () => { - clearTimeout(handler); - await invoke.invokeMethodAsync("ShowVoteToast"); - }, 10000); - } - - EventHandler.on(document, 'click', '#bb-gitee-vote', e => { - const toast = e.delegateTarget.closest('.toast'); - if (toast) { - toast.classList.remove('show'); - - localStorage.setItem('bb-gitee-vote', new Date().getTime()); - } - }); -} - -export function dispose() { - EventHandler.off(document, 'click', '#bb-gitee-vote'); -}