Skip to content

Commit 696292e

Browse files
authored
feat(Clipboard): remove Clipboard component (#4541)
* refactor: 移动剪切板服务 * refactor: 移除 ClipBoard 组件 * test: 更新单元测试 * test: 更新单元测试
1 parent b9a5bbd commit 696292e

File tree

8 files changed

+105
-282
lines changed

8 files changed

+105
-282
lines changed

src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
@RenderBody()
55

66
<EyeDropper></EyeDropper>
7-
<Clipboard></Clipboard>
87
<Title></Title>
98

109
<Message @ref="MessageContainer"></Message>

src/BootstrapBlazor/Components/Clipboard/Clipboard.cs

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/BootstrapBlazor/Components/Clipboard/ClipboardOption.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/BootstrapBlazor/Components/Clipboard/ClipboardService.cs

Lines changed: 0 additions & 96 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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([email protected]) Website: https://www.blazor.zone
5+
6+
namespace BootstrapBlazor.Components;
7+
8+
/// <summary>
9+
/// 粘贴板服务
10+
/// </summary>
11+
public class ClipboardService(IJSRuntime jSRuntime)
12+
{
13+
[NotNull]
14+
private JSModule? _module = null;
15+
16+
private Task<JSModule> LoadModule() => jSRuntime.LoadModule("./_content/BootstrapBlazor/modules/utility.js");
17+
18+
/// <summary>
19+
/// 获取剪切板数据方法
20+
/// </summary>
21+
public async Task<List<ClipboardItem>> Get(CancellationToken token = default)
22+
{
23+
_module ??= await LoadModule();
24+
return await _module.InvokeAsync<List<ClipboardItem>?>("getAllClipboardContents", token) ?? [];
25+
}
26+
27+
/// <summary>
28+
/// 获得剪切板拷贝文字方法
29+
/// </summary>
30+
/// <returns></returns>
31+
public async Task<string?> GetText(CancellationToken token = default)
32+
{
33+
_module ??= await LoadModule();
34+
return await _module.InvokeAsync<string?>("getTextFromClipboard", token);
35+
}
36+
37+
/// <summary>
38+
/// 将指定文本设置到剪切板方法
39+
/// </summary>
40+
/// <param name="text">要拷贝的文字</param>
41+
/// <param name="callback">拷贝后回调方法</param>
42+
/// <param name="token"></param>
43+
/// <returns></returns>
44+
public async Task Copy(string? text, Func<Task>? callback = null, CancellationToken token = default)
45+
{
46+
_module ??= await LoadModule();
47+
await _module.InvokeAsync<string?>("copy", token, text);
48+
if (callback != null)
49+
{
50+
await callback();
51+
}
52+
}
53+
}

test/UnitTest/Components/ClipboardServiceTest.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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([email protected]) Website: https://www.blazor.zone
5+
6+
using UnitTest.Pages;
7+
8+
namespace UnitTest.Components;
9+
10+
public class ClipboardServiceTest : BootstrapBlazorTestBase
11+
{
12+
[Fact]
13+
public async Task Clipboard_Ok()
14+
{
15+
var service = Context.Services.GetRequiredService<ClipboardService>();
16+
await service.Copy(null, () => Task.CompletedTask);
17+
await service.Copy("Test", () => Task.CompletedTask);
18+
}
19+
20+
[Fact]
21+
public async Task GetText()
22+
{
23+
Context.JSInterop.Setup<string?>("getTextFromClipboard").SetResult("test123");
24+
var service = Context.Services.GetRequiredService<ClipboardService>();
25+
26+
var text = await service.GetText();
27+
Assert.Equal("test123", text);
28+
await service.GetText();
29+
}
30+
31+
[Fact]
32+
public async Task Get()
33+
{
34+
Context.JSInterop.Setup<List<ClipboardItem>?>("getAllClipboardContents").SetResult([new() { Data = [0x31, 0x32, 0x33], MimeType = "text/text" }]);
35+
var service = Context.Services.GetRequiredService<ClipboardService>();
36+
var items = await service.Get();
37+
var item = items[0];
38+
39+
Assert.Equal("123", item.Text);
40+
Assert.Equal("text/text", item.MimeType);
41+
42+
Context.JSInterop.Setup<List<ClipboardItem>?>("getAllClipboardContents").SetResult([new() { Data = [0x31, 0x32, 0x33] }]);
43+
items = await service.Get();
44+
item = items[0];
45+
Assert.Empty(item.Text);
46+
47+
Context.JSInterop.Setup<List<ClipboardItem>?>("getAllClipboardContents").SetResult([new()]);
48+
items = await service.Get();
49+
item = items[0];
50+
Assert.Empty(item.Text);
51+
}
52+
}

0 commit comments

Comments
 (0)