Skip to content

Commit

Permalink
feat(Clipboard): remove Clipboard component (#4541)
Browse files Browse the repository at this point in the history
* refactor: 移动剪切板服务

* refactor: 移除 ClipBoard 组件

* test: 更新单元测试

* test: 更新单元测试
  • Loading branch information
ArgoZhang authored Oct 25, 2024
1 parent b9a5bbd commit 696292e
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
@RenderBody()

<EyeDropper></EyeDropper>
<Clipboard></Clipboard>
<Title></Title>

<Message @ref="MessageContainer"></Message>
Expand Down
76 changes: 0 additions & 76 deletions src/BootstrapBlazor/Components/Clipboard/Clipboard.cs

This file was deleted.

22 changes: 0 additions & 22 deletions src/BootstrapBlazor/Components/Clipboard/ClipboardOption.cs

This file was deleted.

96 changes: 0 additions & 96 deletions src/BootstrapBlazor/Components/Clipboard/ClipboardService.cs

This file was deleted.

53 changes: 53 additions & 0 deletions src/BootstrapBlazor/Services/ClipboardService.cs
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();
}
}
}
37 changes: 0 additions & 37 deletions test/UnitTest/Components/ClipboardServiceTest.cs

This file was deleted.

52 changes: 52 additions & 0 deletions test/UnitTest/Services/ClipboardServiceTest.cs
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);
}
}
Loading

0 comments on commit 696292e

Please sign in to comment.