-
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
9 changed files
with
219 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Threading.Tasks; | ||
using Microsoft.JSInterop; | ||
using static System.Net.Mime.MediaTypeNames; | ||
|
||
namespace Bit.Butil; | ||
|
||
public class Clipboard(IJSRuntime js) | ||
{ | ||
/// <summary> | ||
/// Requests text from the system clipboard, returning a Promise that | ||
/// is fulfilled with a string containing the clipboard's text once it's available. | ||
/// <br/> | ||
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/readText">https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/readText</see> | ||
/// </summary> | ||
public async ValueTask<string> ReadText() | ||
=> await js.InvokeAsync<string>("BitButil.clipboard.readText"); | ||
|
||
/// <summary> | ||
/// Writes text to the system clipboard, returning a Promise that is | ||
/// resolved once the text is fully copied into the clipboard. | ||
/// <br/> | ||
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText">https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText</see> | ||
/// </summary> | ||
public async ValueTask WriteText(string text) | ||
{ | ||
if (text is not null) | ||
{ | ||
await js.InvokeVoidAsync("BitButil.clipboard.writeText", text); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Requests arbitrary data (such as images) from the clipboard, returning a Promise that | ||
/// resolves with an array of ClipboardItem objects containing the clipboard's contents. | ||
/// <br/> | ||
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/read">https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/read</see> | ||
/// </summary> | ||
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ClipboardItem))] | ||
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ClipboardFormats))] | ||
public async ValueTask<ClipboardItem[]> Read(ClipboardFormats? formats = null) | ||
=> await (formats is null ? js.InvokeAsync<ClipboardItem[]>("BitButil.clipboard.read") | ||
: js.InvokeAsync<ClipboardItem[]>("BitButil.clipboard.read", formats)); | ||
|
||
/// <summary> | ||
/// Writes arbitrary data to the system clipboard, returning a Promise | ||
/// that resolves when the operation completes. | ||
/// <br/> | ||
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/write">https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/write</see> | ||
/// </summary> | ||
public async ValueTask Write(ClipboardItem[] items) | ||
{ | ||
if (items is not null) | ||
{ | ||
await js.InvokeVoidAsync("BitButil.clipboard.write", (object)items); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Bit.Butil; | ||
|
||
public class ClipboardFormats | ||
{ | ||
public string[] Unsanitized { get; set; } = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Bit.Butil; | ||
|
||
public class ClipboardItem | ||
{ | ||
public string MimeType { get; set; } = default!; | ||
|
||
public byte[] Data { get; set; } = default!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
var BitButil = BitButil || {}; | ||
|
||
(function (butil: any) { | ||
butil.clipboard = { | ||
readText, | ||
writeText, | ||
read, | ||
write | ||
}; | ||
|
||
async function readText() { | ||
return await window.navigator.clipboard.readText(); | ||
} | ||
|
||
async function writeText(text: string) { | ||
return await window.navigator.clipboard.writeText(text); | ||
} | ||
|
||
async function read(formats) { | ||
const clipboardItems = await (navigator.clipboard as any).read(formats); | ||
const result = []; | ||
for (const item of clipboardItems) { | ||
for (const mimeType of item.types) { | ||
const blob = await item.getType(mimeType); | ||
const buffer = await blob.arrayBuffer(); | ||
result.push({ mimeType: mimeType, data: new Uint8Array(buffer) }); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
async function write(items) { | ||
const clipboardItems = []; | ||
for (const item of items) { | ||
const type = item.mimeType; | ||
const blob = new Blob([butil.utils.arrayToBuffer(item.data)], { type }); | ||
clipboardItems.push(new ClipboardItem({ [type]: blob })); | ||
} | ||
await navigator.clipboard.write(clipboardItems); | ||
} | ||
}(BitButil)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
var BitButil = BitButil || {}; | ||
|
||
(function (butil: any) { | ||
butil.utils = { | ||
arrayToBuffer | ||
}; | ||
|
||
function arrayToBuffer(array: Uint8Array) { | ||
return array?.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset) | ||
} | ||
}(BitButil)); |
85 changes: 85 additions & 0 deletions
85
src/Butil/Demo/Bit.Butil.Demo.Core/Pages/ClipboardPage.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
@page "/clipboard" | ||
@inject Bit.Butil.Console console | ||
@inject Bit.Butil.Clipboard clipboard | ||
|
||
<PageTitle>Clipboard Samples</PageTitle> | ||
|
||
<h1>Clipboard</h1> | ||
|
||
<pre style="font-family:Consolas"> | ||
@@inject Bit.Butil.Clipboard clipboard | ||
|
||
@@code { | ||
... | ||
await clipboard.WriteText("new clipboard text"); | ||
... | ||
} | ||
</pre> | ||
|
||
<br /> | ||
<hr /> | ||
|
||
<h3>Open the DevTools console and start clicking on buttons</h3> | ||
|
||
<hr /> | ||
<br /> | ||
|
||
<button @onclick="ReadText">Read text</button> | ||
|
||
<br /> | ||
<hr /> | ||
<br /> | ||
|
||
<input @bind=@newClipText /> | ||
<br /> | ||
<br /> | ||
<button @onclick="WriteText">Write text</button> | ||
|
||
<br /> | ||
<hr /> | ||
<br /> | ||
|
||
<button @onclick="Read">Read</button> | ||
|
||
<br /> | ||
<hr /> | ||
<br /> | ||
|
||
<input @bind=@newText /> | ||
<br /> | ||
<br /> | ||
<button @onclick="Write">Write</button> | ||
|
||
@code { | ||
private string newClipText; | ||
private string newText; | ||
|
||
private async Task ReadText() | ||
{ | ||
var text = await clipboard.ReadText(); | ||
await console.Log("Clipboard.ReadText =", $"\"{text}\""); | ||
} | ||
|
||
private async Task WriteText() | ||
{ | ||
await clipboard.WriteText(newClipText ?? string.Empty); | ||
await console.Log("Clipboard.WriteText =", $"\"{newClipText}\""); | ||
} | ||
|
||
private async Task Read() | ||
{ | ||
var items = await clipboard.Read(); | ||
foreach (var item in items) | ||
{ | ||
await console.Log("Clipboard.Read=", $"\"{item.MimeType}\",", System.Text.Encoding.UTF8.GetString(item.Data)); | ||
} | ||
} | ||
|
||
public async Task Write() | ||
{ | ||
var data = System.Text.Encoding.UTF8.GetBytes(newText); | ||
var item = new ClipboardItem() { MimeType = "text/plain", Data = data }; | ||
await clipboard.Write([item]); | ||
await console.Log("Clipboard.Write=", $"\"{item.MimeType}\",", System.Text.Encoding.UTF8.GetString(item.Data)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters