-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Clipboard): remove Clipboard component (#4541)
* refactor: 移动剪切板服务 * refactor: 移除 ClipBoard 组件 * test: 更新单元测试 * test: 更新单元测试
- Loading branch information
Showing
8 changed files
with
105 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
src/BootstrapBlazor/Components/Clipboard/ClipboardOption.cs
This file was deleted.
Oops, something went wrong.
96 changes: 0 additions & 96 deletions
96
src/BootstrapBlazor/Components/Clipboard/ClipboardService.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// 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 | ||
|
||
namespace BootstrapBlazor.Components; | ||
|
||
/// <summary> | ||
/// 粘贴板服务 | ||
/// </summary> | ||
public class ClipboardService(IJSRuntime jSRuntime) | ||
{ | ||
[NotNull] | ||
private JSModule? _module = null; | ||
|
||
private Task<JSModule> LoadModule() => jSRuntime.LoadModule("./_content/BootstrapBlazor/modules/utility.js"); | ||
|
||
/// <summary> | ||
/// 获取剪切板数据方法 | ||
/// </summary> | ||
public async Task<List<ClipboardItem>> Get(CancellationToken token = default) | ||
{ | ||
_module ??= await LoadModule(); | ||
return await _module.InvokeAsync<List<ClipboardItem>?>("getAllClipboardContents", token) ?? []; | ||
} | ||
|
||
/// <summary> | ||
/// 获得剪切板拷贝文字方法 | ||
/// </summary> | ||
/// <returns></returns> | ||
public async Task<string?> GetText(CancellationToken token = default) | ||
{ | ||
_module ??= await LoadModule(); | ||
return await _module.InvokeAsync<string?>("getTextFromClipboard", token); | ||
} | ||
|
||
/// <summary> | ||
/// 将指定文本设置到剪切板方法 | ||
/// </summary> | ||
/// <param name="text">要拷贝的文字</param> | ||
/// <param name="callback">拷贝后回调方法</param> | ||
/// <param name="token"></param> | ||
/// <returns></returns> | ||
public async Task Copy(string? text, Func<Task>? callback = null, CancellationToken token = default) | ||
{ | ||
_module ??= await LoadModule(); | ||
await _module.InvokeAsync<string?>("copy", token, text); | ||
if (callback != null) | ||
{ | ||
await callback(); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// 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 UnitTest.Pages; | ||
|
||
namespace UnitTest.Components; | ||
|
||
public class ClipboardServiceTest : BootstrapBlazorTestBase | ||
{ | ||
[Fact] | ||
public async Task Clipboard_Ok() | ||
{ | ||
var service = Context.Services.GetRequiredService<ClipboardService>(); | ||
await service.Copy(null, () => Task.CompletedTask); | ||
await service.Copy("Test", () => Task.CompletedTask); | ||
} | ||
|
||
[Fact] | ||
public async Task GetText() | ||
{ | ||
Context.JSInterop.Setup<string?>("getTextFromClipboard").SetResult("test123"); | ||
var service = Context.Services.GetRequiredService<ClipboardService>(); | ||
|
||
var text = await service.GetText(); | ||
Assert.Equal("test123", text); | ||
await service.GetText(); | ||
} | ||
|
||
[Fact] | ||
public async Task Get() | ||
{ | ||
Context.JSInterop.Setup<List<ClipboardItem>?>("getAllClipboardContents").SetResult([new() { Data = [0x31, 0x32, 0x33], MimeType = "text/text" }]); | ||
var service = Context.Services.GetRequiredService<ClipboardService>(); | ||
var items = await service.Get(); | ||
var item = items[0]; | ||
|
||
Assert.Equal("123", item.Text); | ||
Assert.Equal("text/text", item.MimeType); | ||
|
||
Context.JSInterop.Setup<List<ClipboardItem>?>("getAllClipboardContents").SetResult([new() { Data = [0x31, 0x32, 0x33] }]); | ||
items = await service.Get(); | ||
item = items[0]; | ||
Assert.Empty(item.Text); | ||
|
||
Context.JSInterop.Setup<List<ClipboardItem>?>("getAllClipboardContents").SetResult([new()]); | ||
items = await service.Get(); | ||
item = items[0]; | ||
Assert.Empty(item.Text); | ||
} | ||
} |
Oops, something went wrong.