-
Notifications
You must be signed in to change notification settings - Fork 5
/
ClipboardService.cs
90 lines (83 loc) · 3.56 KB
/
ClipboardService.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CurrieTechnologies.Razor.Clipboard
{
public class ClipboardService
{
static readonly IDictionary<Guid, TaskCompletionSource<string>> pendingReadRequests =
new Dictionary<Guid, TaskCompletionSource<string>>();
static readonly IDictionary<Guid, TaskCompletionSource<object>> pendingWriteRequests =
new Dictionary<Guid, TaskCompletionSource<object>>();
private readonly IJSRuntime jSRuntime;
public ClipboardService(IJSRuntime jSRuntime)
{
this.jSRuntime = jSRuntime;
}
/// <summary>
/// Requests text from the system clipboard.
/// </summary>
/// <returns>
/// A Task that resolves with a string containing the textual contents of the clipboard. A JSException is thrown if the caller does not have permission to write to the clipboard.
/// </returns>
public async Task<string> ReadTextAsync()
{
var tcs = new TaskCompletionSource<string>();
var requestId = Guid.NewGuid();
pendingReadRequests.Add(requestId, tcs);
await jSRuntime
.InvokeAsync<object>("CurrieTechnologies.Razor.Clipboard.ReadText", requestId)
.ConfigureAwait(false);
return await tcs.Task.ConfigureAwait(false);
}
/// <summary>
/// Writes text to the system clipboard.
/// </summary>
/// <param name="newClipText">The string to be written to the clipboard.</param>
/// <returns>
/// A Task which is resolved once the text is fully copied into the clipboard. Returns an empty string if the clipboard is empty, does not contain text, or does not include a textual representation among the DataTransfer objects representing the clipboard's contents.
/// </returns>
public async Task WriteTextAsync(string newClipText)
{
var tcs = new TaskCompletionSource<object>();
var requestId = Guid.NewGuid();
pendingWriteRequests.Add(requestId, tcs);
await jSRuntime
.InvokeAsync<object>("CurrieTechnologies.Razor.Clipboard.WriteText", requestId, newClipText)
.ConfigureAwait(false);
await tcs.Task.ConfigureAwait(false);
return;
}
/// <summary>
/// Detects if the browser supports the Clipboard API.
/// </summary>
/// <returns>
/// A Task which resolves to a boolean stating if the clipboard is supported or not.
/// </returns>
public Task<bool> IsSupportedAsync()
{
return jSRuntime.InvokeAsync<bool>("CurrieTechnologies.Razor.Clipboard.IsSupported")
.AsTask();
}
[JSInvokable]
public static void ReceiveReadResponse(string id, string text)
{
TaskCompletionSource<string> pendingTask;
var idVal = Guid.Parse(id);
pendingTask = pendingReadRequests.First(x => x.Key == idVal).Value;
pendingTask.SetResult(text);
pendingReadRequests.Remove(idVal);
}
[JSInvokable]
public static void ReceiveWriteResponse(string id)
{
TaskCompletionSource<object> pendingTask;
var idVal = Guid.Parse(id);
pendingTask = pendingWriteRequests.First(x => x.Key == idVal).Value;
pendingTask.SetResult(null);
pendingWriteRequests.Remove(idVal);
}
}
}