Skip to content

Commit b346242

Browse files
authored
chore(Vote): remove gitee vote (#7489)
* refactor: 撤销对 BaseLayout 的更改 * feat: 增加 GlobalToast 组件 * refactor: 移除 RenderVote 代码片段 * doc: 完善弹窗逻辑
1 parent 629ea42 commit b346242

6 files changed

Lines changed: 111 additions & 100 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@inherits WebSiteModuleComponentBase
2+
3+
@code {
4+
RenderFragment RenderVote =>
5+
@<div>
6+
<div style="font-size: 48px; text-align: center;">🏆</div>
7+
<p class="text-center mt-2">正在参加 <b>Gitee 2025 最受欢迎的开源软件</b> 投票活动,快来给我投票吧!</p>
8+
<div class="my-3 text-center">您的每一票都是对开源社区的支持,感谢您的参与!</div>
9+
<div style="display: flex; justify-content: space-around;" id="bb-g-toast">
10+
<a href="https://gitee.com/activity/2025opensource?ident=I6MYBB" target="_blank" style="font-weight: bold; padding: 6px 12px; border-radius: var(--bs-border-radius); background: linear-gradient(135deg, #667eea, #764ba2); color: #fff;">🚀 必须投一票</a>
11+
<a href="https://gitee.com/activity/2025opensource?ident=I6MYBB" target="_blank" class="text-muted" style="padding: 6px 12px;" title="老六你居然不投票">我知道了</a>
12+
</div>
13+
</div>;
14+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
5+
6+
using Microsoft.JSInterop;
7+
using System.Globalization;
8+
9+
namespace BootstrapBlazor.Server.Components.Components;
10+
11+
/// <summary>
12+
/// 正站通知组件
13+
/// </summary>
14+
[JSModuleAutoLoader("Components/GlobalToast.razor.js", JSObjectReference = true)]
15+
public partial class GlobalToast
16+
{
17+
[Inject]
18+
[NotNull]
19+
private ToastService? Toast { get; set; }
20+
21+
/// <summary>
22+
/// <inheritdoc/>
23+
/// </summary>
24+
/// <returns></returns>
25+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Interop, nameof(ShowToast));
26+
27+
/// <summary>
28+
/// 显示通知窗口
29+
/// </summary>
30+
/// <returns></returns>
31+
[JSInvokable]
32+
public async Task ShowToast()
33+
{
34+
// 英文环境不通知
35+
if (CultureInfo.CurrentUICulture.Name == "en-US")
36+
{
37+
return;
38+
}
39+
40+
var option = new ToastOption()
41+
{
42+
Category = ToastCategory.Information,
43+
Title = "Gitee 评选活动",
44+
IsAutoHide = false,
45+
ChildContent = RenderVote,
46+
PreventDuplicates = true
47+
};
48+
await Toast.Show(option);
49+
}
50+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import EventHandler from "../../_content/BootstrapBlazor/modules/event-handler.js"
2+
3+
export function init(invoke, method, debug) {
4+
const localstorageKey = 'bb-g-toast'
5+
const v = localStorage.getItem(localstorageKey);
6+
if (v) {
7+
try {
8+
const differ = new Date().getTime() - v;
9+
if (differ < 86400000) {
10+
return;
11+
}
12+
}
13+
catch {
14+
localStorage.removeItem(localstorageKey);
15+
}
16+
}
17+
18+
if (debug !== true) {
19+
const handler = setTimeout(async () => {
20+
clearTimeout(handler);
21+
await invoke.invokeMethodAsync(method);
22+
}, 10000);
23+
}
24+
25+
EventHandler.on(document, 'click', '#bb-g-toast', e => {
26+
const toast = e.delegateTarget.closest('.toast');
27+
if (toast) {
28+
toast.classList.remove('show');
29+
30+
localStorage.setItem(localstorageKey, new Date().getTime());
31+
}
32+
});
33+
}
34+
35+
export function dispose() {
36+
EventHandler.off(document, 'click', '#bb-g-toast');
37+
}

src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,3 @@
1515
<a href="" class="reload">Reload</a>
1616
<a class="dismiss">🗙</a>
1717
</div>
18-
19-
@code {
20-
RenderFragment RenderVote =>
21-
@<div>
22-
<div style="font-size: 48px; text-align: center;">🏆</div>
23-
<p class="text-center mt-2">正在参加 <b>Gitee 2025 最受欢迎的开源软件</b> 投票活动,快来给我投票吧!</p>
24-
<div class="my-3 text-center">您的每一票都是对开源社区的支持,感谢您的参与!</div>
25-
<div style="display: flex; justify-content: space-around;" id="bb-gitee-vote">
26-
<a href="https://gitee.com/activity/2025opensource?ident=I6MYBB" target="_blank" style="font-weight: bold; padding: 6px 12px; border-radius: var(--bs-border-radius); background: linear-gradient(135deg, #667eea, #764ba2); color: #fff;">🚀 必须投一票</a>
27-
<a href="https://gitee.com/activity/2025opensource?ident=I6MYBB" target="_blank" class="text-muted" style="padding: 6px 12px;" title="老六你居然不投票">我知道了</a>
28-
</div>
29-
</div>;
30-
}

src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor.cs

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55

66
using Microsoft.Extensions.Options;
77
using Microsoft.JSInterop;
8-
using System.Globalization;
98

109
namespace BootstrapBlazor.Server.Components.Layout;
1110

1211
/// <summary>
1312
/// 母版页基类
1413
/// </summary>
15-
public partial class BaseLayout : IAsyncDisposable
14+
public partial class BaseLayout : IDisposable
1615
{
1716
[Inject]
1817
[NotNull]
@@ -51,8 +50,6 @@ public partial class BaseLayout : IAsyncDisposable
5150
private string? CancelText { get; set; }
5251

5352
private bool _init = false;
54-
private JSModule? _module;
55-
private DotNetObjectReference<BaseLayout>? _interop;
5653

5754
/// <summary>
5855
/// <inheritdoc/>
@@ -74,18 +71,13 @@ protected override void OnInitialized()
7471
/// <inheritdoc/>
7572
/// </summary>
7673
/// <returns></returns>
77-
protected override async Task OnAfterRenderAsync(bool firstRender)
74+
protected override async Task OnInitializedAsync()
7875
{
79-
await base.OnAfterRenderAsync(firstRender);
76+
await base.OnInitializedAsync();
8077

81-
if (firstRender)
82-
{
83-
_module = await JSRuntime.LoadModule($"{WebsiteOption.Value.JSModuleRootPath}Layout/BaseLayout.razor.js");
84-
_interop = DotNetObjectReference.Create(this);
85-
await _module.InvokeVoidAsync("doTask", _interop, WebsiteOption.Value.IsDevelopment);
86-
_init = true;
87-
StateHasChanged();
88-
}
78+
var module = await JSRuntime.LoadModule($"{WebsiteOption.Value.JSModuleRootPath}Layout/BaseLayout.razor.js");
79+
await module.InvokeVoidAsync("initTheme");
80+
_init = true;
8981
}
9082

9183
private async Task NotifyCommit(DispatchEntry<GiteePostBody> payload)
@@ -129,56 +121,25 @@ private async Task NotifyReboot(DispatchEntry<bool> payload)
129121
}
130122
}
131123

132-
/// <summary>
133-
/// 显示投票弹窗
134-
/// </summary>
135-
/// <returns></returns>
136-
[JSInvokable]
137-
public async Task ShowVoteToast()
138-
{
139-
// 英文环境不投票
140-
if (CultureInfo.CurrentUICulture.Name == "en-US")
141-
{
142-
return;
143-
}
144-
145-
_option = new ToastOption()
146-
{
147-
Category = ToastCategory.Information,
148-
Title = "邀请您支持 BB 参与 Gitee 项目评选活动",
149-
IsAutoHide = false,
150-
ChildContent = RenderVote,
151-
PreventDuplicates = true,
152-
ClassString = "bb-vote-toast"
153-
};
154-
await Toast.Show(_option);
155-
}
156-
157124
/// <summary>
158125
/// 释放资源
159126
/// </summary>
160127
/// <param name="disposing"></param>
161-
private async ValueTask DisposeAsync(bool disposing)
128+
private void Dispose(bool disposing)
162129
{
163130
if (disposing)
164131
{
165132
CommitDispatchService.UnSubscribe(NotifyCommit);
166133
RebootDispatchService.UnSubscribe(NotifyReboot);
167-
168-
if (_module != null)
169-
{
170-
await _module.InvokeVoidAsync("dispose");
171-
await _module.DisposeAsync();
172-
}
173134
}
174135
}
175136

176137
/// <summary>
177138
/// 释放资源
178139
/// </summary>
179-
public async ValueTask DisposeAsync()
140+
public void Dispose()
180141
{
181-
await DisposeAsync(true);
142+
Dispose(true);
182143
GC.SuppressFinalize(this);
183144
}
184145
}
Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,6 @@
11
import { getTheme, setTheme } from "../../_content/BootstrapBlazor/modules/utility.js"
2-
import EventHandler from "../../_content/BootstrapBlazor/modules/event-handler.js"
32

4-
function initTheme() {
3+
export function initTheme() {
54
const currentTheme = getTheme();
65
setTheme(currentTheme, false);
76
}
8-
9-
export function doTask(invoke, debug) {
10-
initTheme();
11-
12-
const v = localStorage.getItem('bb-gitee-vote');
13-
if (v) {
14-
try {
15-
const differ = new Date().getTime() - v;
16-
if (differ < 86400000) {
17-
return;
18-
}
19-
}
20-
catch {
21-
localStorage.removeItem('bb-gitee-vote');
22-
}
23-
}
24-
25-
if (debug !== true) {
26-
const handler = setTimeout(async () => {
27-
clearTimeout(handler);
28-
await invoke.invokeMethodAsync("ShowVoteToast");
29-
}, 10000);
30-
}
31-
32-
EventHandler.on(document, 'click', '#bb-gitee-vote', e => {
33-
const toast = e.delegateTarget.closest('.toast');
34-
if (toast) {
35-
toast.classList.remove('show');
36-
37-
localStorage.setItem('bb-gitee-vote', new Date().getTime());
38-
}
39-
});
40-
}
41-
42-
export function dispose() {
43-
EventHandler.off(document, 'click', '#bb-gitee-vote');
44-
}

0 commit comments

Comments
 (0)