ClipLazor 2.1.2

dotnet add package ClipLazor --version 2.1.2                
NuGet\Install-Package ClipLazor -Version 2.1.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ClipLazor" Version="2.1.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ClipLazor --version 2.1.2                
#r "nuget: ClipLazor, 2.1.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ClipLazor as a Cake Addin
#addin nuget:?package=ClipLazor&version=2.1.2

// Install ClipLazor as a Cake Tool
#tool nuget:?package=ClipLazor&version=2.1.2                

ClipLazor: Clipboard API Interop for Blazor

ClipLazor is a library that provides interop for the Clipboard API in Blazor applications, allowing you to easily interact with the clipboard.

alt text

NuGet

Installation

  1. Install ClipLazor with dotnet cli in your Blazor app.
dotnet add package ClipLazor 
  1. Register the service to the IoC container using AddClipboard method:
using ClipLazor.Extention;

// Inside your ConfigureServices method
builder.Service.AddClipboard();

3.Add this script tag in your root file _Host.cshtml for Blazor Server Apps or index.html for Blazor WebAssembly Apps:


<script src="_framework/blazor.server.js"></script>
<script src="_content/ClipLazor/clipboard.min.js"></script>

Usage

After ClipLazor being installed, you can inject it in your razor file using IClipLazor interface:

@using ClipLazor.Components;
@inject IClipLazor Clipboard
Checking Browser Support

You can check if the browser supports the Clipboard API with the IsClipboardSupported() asynchronous method:

bool isSupported = await Clipbaord.IsClipboardSupported();

For more information about clipboard api support for browsers check this link

Checking Permissions

The Clipboard API requires user permission to read and write to the clipboard. Use the IsPermitted() asynchronous method with the PermissionCommand enum to check read or write permission:

bool isWritePermitted = await Clipboard.IsPermitted(PermissionCommand.Write);
bool isReadPermitted = await Clipboard.IsPermitted(PermissionCommand.Read);
Read And Write Text

1.To copy a text to the clipboard use WriteTextAsync() async method and pass the text(:warning: pass the argument by ReadOnlyMemory). The method return true if the copy operation is successful; otherwise, false:

string txt = string.Empty;
if(isSupported && txt.Length > 0)
{
    if (isWritePermitted)
    {
        var isCopied = await Clipboard.WriteTextAsync(txt.AsMemory());
        if (isCopied)
        {
            msg = "Text Copied";
        }
        else
        {
            msg = "Couldn't copy the text!.";
        }
    }
}
  1. To paste a text from the clipboard use 'ReadTextAsync()' async method. if the paste operation was successed the method ruturns the text string; otherwise null:
string pastedTxt = string.Empty;
if(isSupported && isReadPermitted)
{
    var pastedText = await Clipboard.ReadTextAsync();
    if (pastedText is not null)
    {
        msg = "Text Pasted";
        pastedTxt = pastedText;
    }
    else
    {
        msg = "Couldn't paste the text!.";
    }
}
Read And Write Data

❗ When you work with data to copy to the clipboard or paste from it, the MIME Type has to be specified. Note that not all MIME types supported by the clipboard api.

Common Supported Mime Types:
  • "text/plain"
  • "text/html"
  • "text/uri-list"
  • "image/png"
  1. To copy data to the clipboard use WriteDataAsync async method. pass the array buffer of the data and it's associated MIME Type. This method will return true if the copy operation is successful; otherwise, false:
 byte[] imgArray = Convert.FromBase64String(imageToCopy);

 if(imgArray.Length > 0 && isWritePermitted)
 {
     var isDataCopied = await Clipboard.WriteDataAsync(imgArray, "image/png");

     if (isDataCopied)
     {
         dmsg = "Image Copied!";
     }
     else
     {
         dmsg = "Failed to copy the image!.";
     }
 }
  1. To paste the data from the clipboard use ReadDataAsync async method and pass the MIME Type argument, the method will return Memory<byte> of the data; or an empty one:
if (isReadPermitted)
{
    var pastedData = await Clipboard.ReadDataAsync("image/png");
    if (!pastedData.IsEmpty)
    {
        pastedImg = Convert.ToBase64String(pastedData.ToArray());
        dmsg = "Image Pasted";

    }
    else
    {
        dmsg = "Couldn't paste the image";
    }
}

📄 See the full example code.

License

MIT License

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.2 88 11/15/2024
2.1.1 3,475 2/9/2024
2.1.0 872 11/20/2023
2.0.0 491 9/23/2023
1.2.0 1,080 10/8/2022