Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/BootstrapBlazor.Server/Components/Components/Header.razor
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
<li class="nav-item">
<a class="nav-link" href="tutorials">@TutorialsText</a>
</li>
@if (CultureInfo.CurrentUICulture.Name == "zh-CN")
@* @if (CultureInfo.CurrentUICulture.Name == "zh-CN")
{
<li class="nav-item">
<a class="nav-link" href="https://theme.blazor.zone">主题</a>
</li>
}
} *@
</ul>
</div>
<div class="d-flex flex-fill"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ protected override async Task OnInitializedAsync()
{
Text = Localizer["OnlineSheet"],
Url = "tutorials/online-sheet",
},
new()
{
Text = Localizer["MemorialMode"],
Url = "tutorials/memorial",
}
]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@page "/tutorials/memorial"

<Button Text="切换哀悼模式" OnClick="OnToggle" class="mb-3"></Button>

<p class="code-label">1. 加载 <code>Utlity</code> 工具</p>
<Pre>var module = await JSRuntime.LoadUtility();</Pre>

<p class="code-label">2. 设置哀悼模式</p>
<Pre>await module.InvokeVoidAsync("SetMemorial", true);</Pre>

<p class="code-label">3. 全站默认设置追悼模式方法</p>
<p>更新 <code>App.razor</code> 文档内容如下</p>
<Pre>&lt;html lang="en" data-bs-theme='dark' data-bb-theme="memorial"&gt;</Pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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([email protected]) Website: https://www.blazor.zone

using Microsoft.JSInterop;

namespace BootstrapBlazor.Server.Components.Samples.Tutorials;

/// <summary>
/// 追悼模式
/// </summary>
public partial class Memorial
{
[Inject, NotNull]
private IJSRuntime? JSRuntime { get; set; }

private bool _isMemorial = false;

private async Task OnToggle()
{
var module = await JSRuntime.LoadUtility();

_isMemorial = !_isMemorial;
await module.SetMemorialModeAsync(_isMemorial);
}
}
3 changes: 2 additions & 1 deletion src/BootstrapBlazor.Server/Locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"TranslateSummary": "Translate",
"DrawingSummary": "Drawing",
"AdminSummary": "Admin",
"OnlineSheet": "UniverSheet"
"OnlineSheet": "UniverSheet",
"MemorialMode": "Memorial"
},
"BootstrapBlazor.Server.Components.Components.Pre": {
"LoadingText": "Loading ...",
Expand Down
3 changes: 2 additions & 1 deletion src/BootstrapBlazor.Server/Locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"TranslateSummary": "翻译工具 Translate",
"DrawingSummary": "画图 Drawing",
"AdminSummary": "中台 Admin",
"OnlineSheet": "在线表格 UniverSheet"
"OnlineSheet": "在线表格 UniverSheet",
"MemorialMode": "追悼模式"
},
"BootstrapBlazor.Server.Components.Components.Pre": {
"LoadingText": "正在加载 ...",
Expand Down
100 changes: 54 additions & 46 deletions src/BootstrapBlazor/Extensions/JSModuleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,33 @@
public static class JSModuleExtensions
{
/// <summary>
/// 导入 utility js 模块
/// Load utility js module
/// </summary>
/// <param name="jsRuntime"></param>
/// <param name="version"></param>
/// <returns>A <see cref="Task"/><![CDATA[<]]><see cref="JSModule"/><![CDATA[>]]> 模块加载器</returns>
/// <param name="jsRuntime">The <see cref="IJSRuntime"/> instance</param>
/// <param name="version">The version of the module</param>
/// <returns>A <see cref="Task"/><![CDATA[<]]><see cref="JSModule"/><![CDATA[>]]> module loader</returns>
public static Task<JSModule> LoadUtility(this IJSRuntime jsRuntime, string? version = null) => LoadModuleByName(jsRuntime, "utility", version);

/// <summary>
/// 通过名称导入内置脚本模块
/// Load built-in script module by name
/// </summary>
/// <param name="jsRuntime"></param>
/// <param name="moduleName"></param>
/// <param name="version"></param>
/// <returns>A <see cref="Task"/><![CDATA[<]]><see cref="JSModule"/><![CDATA[>]]> 模块加载器</returns>
/// <param name="jsRuntime">The <see cref="IJSRuntime"/> instance</param>
/// <param name="moduleName">The name of the module</param>
/// <param name="version">The version of the module</param>
/// <returns>A <see cref="Task"/><![CDATA[<]]><see cref="JSModule"/><![CDATA[>]]> module loader</returns>
public static Task<JSModule> LoadModuleByName(this IJSRuntime jsRuntime, string moduleName, string? version = null)
{
var fileName = $"./_content/BootstrapBlazor/modules/{moduleName}.js";
return LoadModule(jsRuntime, fileName, version);
}

/// <summary>
/// IJSRuntime 扩展方法 动态加载脚本
/// IJSRuntime extension method to dynamically load scripts
/// </summary>
/// <param name="jsRuntime"></param>
/// <param name="fileName"></param>
/// <param name="version"></param>
/// <returns>A <see cref="Task"/><![CDATA[<]]><see cref="JSModule"/><![CDATA[>]]> 模块加载器</returns>
/// <param name="jsRuntime">The <see cref="IJSRuntime"/> instance</param>
/// <param name="fileName">The file name of the script</param>
/// <param name="version">The version of the script</param>
/// <returns>A <see cref="Task"/><![CDATA[<]]><see cref="JSModule"/><![CDATA[>]]> module loader</returns>
public static async Task<JSModule> LoadModule(this IJSRuntime jsRuntime, string fileName, string? version = null)
{
if (!string.IsNullOrEmpty(version))
Expand All @@ -64,10 +64,10 @@
}

/// <summary>
/// 获得指定类型的加载 Module 名称
/// Get the module name of the specified type
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
/// <param name="type">The type</param>
/// <returns>The module name</returns>
public static string GetTypeModuleName(this Type type)
{
var name = type.Name;
Expand All @@ -80,47 +80,47 @@
}

/// <summary>
/// 在新标签页打开指定网址
/// Open the specified URL in a new tab
/// </summary>
/// <param name="module"><see cref="JSModule"/> 实例</param>
/// <param name="url">打开网页地址</param>
/// <param name="target">默认 _blank</param>
/// <param name="features">默认 null</param>
/// <param name="module"><see cref="JSModule"/> instance</param>
/// <param name="url">The URL to open</param>
/// <param name="target">The target window, default is _blank</param>
/// <param name="features">The features of the new window, default is null</param>
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous invocation operation.</returns>
public static ValueTask OpenUrl(this JSModule module, string url, string? target = "_blank", string? features = null) => module.InvokeVoidAsync("openUrl", url, target, features);

/// <summary>
/// 动态运行js代码
/// Dynamically run js code
/// </summary>
/// <param name="module"><see cref="JSModule"/> 实例</param>
/// <param name="script"></param>
/// <param name="module"><see cref="JSModule"/> instance</param>
/// <param name="script">The script to run</param>
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous invocation operation.</returns>
public static async ValueTask Eval(this JSModule module, string script) => await module.InvokeVoidAsync("runEval", script);

/// <summary>
/// 通过 Eval 动态运行 JavaScript 代码
/// Dynamically run JavaScript code via Eval
/// </summary>
/// <param name="module"><see cref="JSModule"/> 实例</param>
/// <param name="script"></param>
/// <param name="module"><see cref="JSModule"/> instance</param>
/// <param name="script">The script to run</param>
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous invocation operation.</returns>
public static ValueTask<TValue?> Eval<TValue>(this JSModule module, string script) => module.InvokeAsync<TValue?>("runEval", script);

/// <summary>
/// 通过 Function 动态运行 JavaScript 代码
/// Dynamically run JavaScript code via Function
/// </summary>
/// <param name="module"><see cref="JSModule"/> 实例</param>
/// <param name="script"></param>
/// <param name="args"></param>
/// <param name="module"><see cref="JSModule"/> instance</param>
/// <param name="script">The script to run</param>
/// <param name="args">The arguments to pass to the script</param>
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous invocation operation.</returns>
public static ValueTask Function(this JSModule module, string script, params object?[]? args) => module.InvokeVoidAsync("runFunction", script, args);

/// <summary>
/// 动态运行js代码
/// Dynamically run js code
/// </summary>
/// <typeparam name="TValue"></typeparam>
/// <param name="module"><see cref="JSModule"/> 实例</param>
/// <param name="script"></param>
/// <param name="args"></param>
/// <typeparam name="TValue">The return type</typeparam>
/// <param name="module"><see cref="JSModule"/> instance</param>
/// <param name="script">The script to run</param>
/// <param name="args">The arguments to pass to the script</param>
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous invocation operation.</returns>
public static async ValueTask<TValue?> Function<TValue>(this JSModule module, string script, params object?[]? args)
{
Expand All @@ -133,41 +133,49 @@
}

/// <summary>
/// 获取当前终端是否为移动设备
/// Check if the current terminal is a mobile device
/// </summary>
/// <param name="module"><see cref="JSModule"/> 实例</param>
/// <param name="module"><see cref="JSModule"/> instance</param>
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous invocation operation.</returns>
public static ValueTask<bool> IsMobile(this JSModule module) => module.InvokeAsync<bool>("isMobile");

/// <summary>
/// 获取一个页面上不重复的元素ID
/// Get a unique element ID on a page
/// </summary>
/// <param name="module">An instance of <see cref="JSModule"/></param>
/// <param name="prefix">A prefix of type <see cref="string"/></param>
/// <returns>Returns a <see cref="string"/> formatted element ID</returns>
public static ValueTask<string?> GenerateId(this JSModule module, string? prefix = null) => module.InvokeAsync<string?>("getUID", prefix);

/// <summary>
/// 获取一个页面内指定元素 Html 字符串
/// Get the HTML string of a specified element on a page
/// </summary>
/// <param name="module">An instance of <see cref="JSModule"/></param>
/// <param name="id"></param>
/// <param name="selector"></param>
/// <param name="id">The ID of the element</param>
/// <param name="selector">The selector of the element</param>
/// <returns>Returns a <see cref="string"/> formatted element ID</returns>
public static ValueTask<string?> GetHtml(this JSModule module, string? id = null, string? selector = null) => module.InvokeAsync<string?>("getHtml", new { id, selector });

/// <summary>
/// 设置主题方法
/// Set the theme method
/// </summary>
/// <param name="module">An instance of <see cref="JSModule"/></param>
/// <param name="themeName">theme name</param>
/// <param name="themeName">The name of the theme</param>
/// <returns></returns>
public static ValueTask SetThemeAsync(this JSModule module, string themeName) => module.InvokeVoidAsync("setTheme", themeName, true);

/// <summary>
/// 设置主题方法
/// Get the theme method
/// </summary>
/// <param name="module">An instance of <see cref="JSModule"/></param>
/// <returns></returns>
public static ValueTask<string?> GetThemeAsync(this JSModule module) => module.InvokeAsync<string?>("getTheme");

/// <summary>
/// Set memorial mode
/// </summary>
/// <param name="module">An instance of <see cref="JSModule"/></param>
/// <param name="isMemorial">Whether it is memorial mode</param>
/// <returns></returns>
public static ValueTask SetMemorialModeAsync(this JSModule module, bool isMemorial) => module.InvokeVoidAsync("setMemorialMode", isMemorial);

Check warning on line 180 in src/BootstrapBlazor/Extensions/JSModuleExtensions.cs

View check run for this annotation

Codecov / codecov/patch

src/BootstrapBlazor/Extensions/JSModuleExtensions.cs#L180

Added line #L180 was not covered by tests
}
Loading