forked from thinktecture/Thinktecture.Blazor
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAsyncClipboard.razor.cs
54 lines (47 loc) · 1.9 KB
/
AsyncClipboard.razor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using PatrickJahr.Blazor.AsyncClipboard;
using PatrickJahr.Blazor.AsyncClipboard.Models;
namespace PatrickJahr.Blazor.Sample.Pages
{
public partial class AsyncClipboard
{
[Inject] private IJSRuntime _jsRuntime { get; set; } = default!;
[Inject] private AsyncClipboardService _asyncClipboardService { get; set; } = default!;
private IJSObjectReference? _module;
private bool _isAsyncClipboardSupported;
protected override async void OnInitialized()
{
_isAsyncClipboardSupported = await _asyncClipboardService.IsSupportedAsync();
_module = await _jsRuntime.InvokeAsync<IJSObjectReference>("import", "./Pages/AsyncClipboard.razor.js");
await InvokeAsync(StateHasChanged);
await base.OnInitializedAsync();
}
private async Task CopyText()
{
await _asyncClipboardService.WriteTextAsync("Foo");
}
private async Task CopyItems()
{
var blobPromise = _asyncClipboardService.GetObjectReference(_module!, "getBlazorLogoBlobPromise");
await _asyncClipboardService.WriteAsync(new []
{
new ClipboardItem(new Dictionary<string, IJSObjectReference>
{
{ "image/png", blobPromise }
}, new ClipboardItemOptions { PresentationStyle = PresentationStyle.Attachment })
});
}
private async Task PasteText()
{
var data = await _asyncClipboardService.ReadTextAsync();
Console.WriteLine(data);
}
private async Task PasteItems()
{
var data = await _asyncClipboardService.ReadAsync();
var blob = await data.First().GetTypeAsync("image/png");
await _module!.InvokeVoidAsync("showBlob", blob);
}
}
}