diff --git a/src/BootstrapBlazor/Components/Anchor/AnchorLink.js b/src/BootstrapBlazor/Components/Anchor/AnchorLink.js index b81efeec1986fab1875664feea53dd11d8035964..042d28d522bcb428c1e727ebc63c9c4edf437261 100644 --- a/src/BootstrapBlazor/Components/Anchor/AnchorLink.js +++ b/src/BootstrapBlazor/Components/Anchor/AnchorLink.js @@ -1,22 +1,4 @@ (function ($) { - $.extend({ - bb_copyText: function (ele) { - if (navigator.clipboard) { - navigator.clipboard.writeText(ele); - } - else { - if (typeof ele !== "string") return false; - var input = document.createElement('input'); - input.setAttribute('type', 'text'); - input.setAttribute('value', ele); - document.body.appendChild(input); - input.select(); - document.execCommand('copy'); - document.body.removeChild(input); - } - } - }); - $(function () { $(document) .on('click', '.anchor-link', function (e) { diff --git a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor index be6251b6bc22df36450fde1f0e425eaebd783971..2d2c2e33fa8e40d77c899fb73131f5e724c18125 100644 --- a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor +++ b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor @@ -21,6 +21,7 @@ else + @code { diff --git a/src/BootstrapBlazor/Components/Clipboard/Clipboard.cs b/src/BootstrapBlazor/Components/Clipboard/Clipboard.cs new file mode 100644 index 0000000000000000000000000000000000000000..07ab16560eb9d54ea25f1e23a7df96b13c49eab3 --- /dev/null +++ b/src/BootstrapBlazor/Components/Clipboard/Clipboard.cs @@ -0,0 +1,57 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using Microsoft.AspNetCore.Components; + +namespace BootstrapBlazor.Components; + +/// <summary> +/// FullScreen 组件部分类 +/// </summary> +public class Clipboard : BootstrapComponentBase, IDisposable +{ + /// <summary> + /// DialogServices 服务实例 + /// </summary> + [Inject] + [NotNull] + private ClipboardService? ClipboardService { get; set; } + + /// <summary> + /// OnInitialized 方法 + /// </summary> + protected override void OnInitialized() + { + base.OnInitialized(); + + // 注册 ClipboardService 弹窗事件 + ClipboardService.Register(this, Copy); + } + + private async Task Copy(ClipboardOption option) + { + await JSRuntime.InvokeVoidAsync(null, "bb_copyText", option.Text ?? string.Empty); + } + + /// <summary> + /// Dispose 方法 + /// </summary> + /// <param name="disposing"></param> + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + ClipboardService.UnRegister(this); + } + } + + /// <summary> + /// Dispose 方法 + /// </summary> + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } +} diff --git a/src/BootstrapBlazor/Components/Clipboard/Clipboard.js b/src/BootstrapBlazor/Components/Clipboard/Clipboard.js new file mode 100644 index 0000000000000000000000000000000000000000..48d8f509f5e85f293d089348edc5db85f14bd2c2 --- /dev/null +++ b/src/BootstrapBlazor/Components/Clipboard/Clipboard.js @@ -0,0 +1,19 @@ +(function ($) { + $.extend({ + bb_copyText: function (text) { + if (navigator.clipboard) { + navigator.clipboard.writeText(text); + } + else { + if (typeof ele !== "string") return false; + var input = document.createElement('input'); + input.setAttribute('type', 'text'); + input.setAttribute('value', text); + document.body.appendChild(input); + input.select(); + document.execCommand('copy'); + document.body.removeChild(input); + } + } + }); +})(jQuery); diff --git a/src/BootstrapBlazor/Components/Clipboard/ClipboardOption.cs b/src/BootstrapBlazor/Components/Clipboard/ClipboardOption.cs new file mode 100644 index 0000000000000000000000000000000000000000..b9675824e2d20c8e69ab84a97e18710b77503a47 --- /dev/null +++ b/src/BootstrapBlazor/Components/Clipboard/ClipboardOption.cs @@ -0,0 +1,16 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +namespace BootstrapBlazor.Components; + +/// <summary> +/// Clipboard 配置类 +/// </summary> +public class ClipboardOption +{ + /// <summary> + /// 获得/设置 要拷贝的文字 + /// </summary> + public string? Text { get; set; } +} diff --git a/src/BootstrapBlazor/Components/Clipboard/ClipboardService.cs b/src/BootstrapBlazor/Components/Clipboard/ClipboardService.cs new file mode 100644 index 0000000000000000000000000000000000000000..e4d9f9ead5151720457b45725db5b99839d88556 --- /dev/null +++ b/src/BootstrapBlazor/Components/Clipboard/ClipboardService.cs @@ -0,0 +1,18 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +namespace BootstrapBlazor.Components; + +/// <summary> +/// 粘贴板服务 +/// </summary> +public class ClipboardService : BootstrapServiceBase<ClipboardOption> +{ + /// <summary> + /// 拷贝方法 + /// </summary> + /// <param name="text"></param> + /// <returns></returns> + public Task Copy(string? text) => Invoke(new ClipboardOption() { Text = text }); +} diff --git a/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs b/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs index 0a97c6066a8d6c0dbbe2091acecf3eb19dd63b35..e14bb764c0afa09f7053866e87d4ad89b55b5847 100644 --- a/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs +++ b/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs @@ -51,6 +51,7 @@ public static class BootstrapBlazorServiceCollectionExtensions services.TryAddScoped<WebClientService>(); services.TryAddScoped<AjaxService>(); services.TryAddScoped(typeof(DragDropService<>)); + services.TryAddScoped<ClipboardService>(); services.TryAddSingleton<IConfigureOptions<BootstrapBlazorOptions>, ConfigureOptions<BootstrapBlazorOptions>>(); services.ConfigureBootstrapBlazorOption(configureOptions);