Skip to content

Commit b3f199e

Browse files
authored
doc(Vote): add gitee vote toast (#7009)
* refactor: 增加 doTask 客户端脚本 * feat: 增加投票弹窗 * refactor: 增加点击逻辑 * refactor: 增加延时
1 parent 09610c4 commit b3f199e

File tree

3 files changed

+94
-10
lines changed

3 files changed

+94
-10
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,14 @@
1515
<a href="" class="reload">Reload</a>
1616
<a class="dismiss">🗙</a>
1717
</div>
18+
19+
@code {
20+
RenderFragment RenderVote =>
21+
@<div>
22+
<p style="font-weight: bold;">我正在参加 Gitee 2025 最受欢迎的开源软件投票活动,快来给我投票吧!</p>
23+
<div style="display: flex; justify-content: space-around;" id="bb-gitee-vote">
24+
<a href="https://gitee.com/activity/2025opensource?ident=I6MYBB" target="_blank" style="font-weight: bold; padding: 3px 12px; border: 1px solid var(--bs-primary); border-radius: var(--bs-border-radius);">必须投一票</a>
25+
<a href="https://gitee.com/activity/2025opensource?ident=I6MYBB" target="_blank" class="text-muted" style="padding: 3px 12px;" title="老六你居然不投票">我知道了</a>
26+
</div>
27+
</div>;
28+
}

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

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

66
using Microsoft.Extensions.Options;
77
using Microsoft.JSInterop;
8+
using System.Globalization;
89

910
namespace BootstrapBlazor.Server.Components.Layout;
1011

1112
/// <summary>
1213
/// 母版页基类
1314
/// </summary>
14-
public partial class BaseLayout : IDisposable
15+
public partial class BaseLayout : IAsyncDisposable
1516
{
1617
[Inject]
1718
[NotNull]
@@ -50,6 +51,8 @@ public partial class BaseLayout : IDisposable
5051
private string? CancelText { get; set; }
5152

5253
private bool _init = false;
54+
private JSModule? _module;
55+
private DotNetObjectReference<BaseLayout>? _interop;
5356

5457
/// <summary>
5558
/// <inheritdoc/>
@@ -71,13 +74,18 @@ protected override void OnInitialized()
7174
/// <inheritdoc/>
7275
/// </summary>
7376
/// <returns></returns>
74-
protected override async Task OnInitializedAsync()
77+
protected override async Task OnAfterRenderAsync(bool firstRender)
7578
{
76-
await base.OnInitializedAsync();
79+
await base.OnAfterRenderAsync(firstRender);
7780

78-
var module = await JSRuntime.LoadModule($"{WebsiteOption.Value.JSModuleRootPath}Layout/BaseLayout.razor.js");
79-
await module.InvokeVoidAsync("initTheme");
80-
_init = true;
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);
86+
_init = true;
87+
StateHasChanged();
88+
}
8189
}
8290

8391
private async Task NotifyCommit(DispatchEntry<GiteePostBody> payload)
@@ -121,25 +129,55 @@ private async Task NotifyReboot(DispatchEntry<bool> payload)
121129
}
122130
}
123131

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 = "Gitee 评选活动",
149+
IsAutoHide = false,
150+
ChildContent = RenderVote,
151+
PreventDuplicates = true
152+
};
153+
await Toast.Show(_option);
154+
}
155+
124156
/// <summary>
125157
/// 释放资源
126158
/// </summary>
127159
/// <param name="disposing"></param>
128-
private void Dispose(bool disposing)
160+
private async ValueTask DisposeAsync(bool disposing)
129161
{
130162
if (disposing)
131163
{
132164
CommitDispatchService.UnSubscribe(NotifyCommit);
133165
RebootDispatchService.UnSubscribe(NotifyReboot);
166+
167+
if (_module != null)
168+
{
169+
await _module.InvokeVoidAsync("dispose");
170+
await _module.DisposeAsync();
171+
}
134172
}
135173
}
136174

137175
/// <summary>
138176
/// 释放资源
139177
/// </summary>
140-
public void Dispose()
178+
public async ValueTask DisposeAsync()
141179
{
142-
Dispose(true);
180+
await DisposeAsync(true);
143181
GC.SuppressFinalize(this);
144182
}
145183
}
Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
11
import { getTheme, setTheme } from "../../_content/BootstrapBlazor/modules/utility.js"
2+
import EventHandler from "../../_content/BootstrapBlazor/modules/event-handler.js"
23

3-
export function initTheme() {
4+
function initTheme() {
45
const currentTheme = getTheme();
56
setTheme(currentTheme, false);
67
}
8+
9+
export function doTask(invoke) {
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+
const handler = setTimeout(async () => {
25+
clearTimeout(handler);
26+
await invoke.invokeMethodAsync("ShowVoteToast");
27+
}, 10000);
28+
29+
EventHandler.on(document, 'click', '#bb-gitee-vote', e => {
30+
const toast = e.delegateTarget.closest('.toast');
31+
if (toast) {
32+
toast.classList.remove('show');
33+
34+
localStorage.setItem('bb-gitee-vote', new Date().getTime());
35+
}
36+
});
37+
}
38+
39+
export function dispose() {
40+
EventHandler.off(document, 'click', '#bb-gitee-vote');
41+
}

0 commit comments

Comments
 (0)