Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Clipboard): remove Clipboard component #4541

Merged
merged 5 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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]
ArgoZhang marked this conversation as resolved.
Show resolved Hide resolved
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);
}
ArgoZhang marked this conversation as resolved.
Show resolved Hide resolved

[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