Skip to content

Commit

Permalink
feat(Clipboard): Support Flush, Clear, ContentChanged on WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed May 19, 2020
1 parent 2e6932b commit 920304e
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 51 deletions.
28 changes: 26 additions & 2 deletions src/Uno.UI.Wasm/ts/Clipboard.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
namespace Uno.Utils {
export class Clipboard {
private static dispatchContentChanged: () => number;

public static startContentChanged() {
['cut', 'copy', 'paste'].forEach(function (event) {
document.addEventListener(event, Clipboard.onClipboardChanged);
});
}

public static stopContentChanged() {
['cut', 'copy', 'paste'].forEach(function (event) {
document.removeEventListener(event, Clipboard.onClipboardChanged);
});
}

public static setText(text: string): string {
const nav = navigator as any;
if (nav.clipboard) {
// Use clipboard object when available
nav.clipboard.setText(text);
nav.clipboard.writeText(text);
// Trigger change notification, as clipboard API does
// not execute "copy".
Clipboard.onClipboardChanged();
} else {
// Hack when the clipboard is not available
const textarea = document.createElement("textarea");
Expand All @@ -17,5 +34,12 @@

return "ok";
}

private static onClipboardChanged() {
if (!Clipboard.dispatchContentChanged) {
Clipboard.dispatchContentChanged = (<any>Module).mono_bind_static_method("[Uno] Windows.ApplicationModel.DataTransfer.Clipboard:DispatchContentChanged");
}
Clipboard.dispatchContentChanged();
}
}
}
}

This file was deleted.

3 changes: 3 additions & 0 deletions src/Uno.UI/LinkerDefinition.Wasm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
</assembly>

<assembly fullname="Uno">
<type fullname="Windows.ApplicationModel.DataTransfer.Clipboard">
<method name="DispatchContentChanged" />
</type>
<type fullname="Windows.UI.Core.CoreDispatcher" />
<type fullname="Windows.UI.Core.SystemNavigationManager" />
<type fullname="Windows.Devices.Sensors.Accelerometer">
Expand Down
35 changes: 31 additions & 4 deletions src/Uno.UWP/ApplicationModel/DataTransfer/Clipboard.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,42 @@ namespace Windows.ApplicationModel.DataTransfer
{
public static partial class Clipboard
{
private const string JsType = "Uno.Utils.Clipboard";

public static void Clear() => SetClipboardText(string.Empty);

public static void SetContent(DataPackage content)
{
if (content?.Text != null)
{
var text = WebAssemblyRuntime.EscapeJs(content.Text);
var command = $"Uno.Utils.Clipboard.setText(\"{text}\");";

WebAssemblyRuntime.InvokeJS(command);
SetClipboardText(content.Text);
}
}

private static void SetClipboardText(string text)
{
var escapedText = WebAssemblyRuntime.EscapeJs(text);
var command = $"{JsType}.setText(\"{escapedText}\");";

WebAssemblyRuntime.InvokeJS(command);
}

private static void StartContentChanged()
{
var command = $"{JsType}.startContentChanged()";
WebAssemblyRuntime.InvokeJS(command);
}

private static void StopContentChanged()
{
var command = $"{JsType}.stopContentChanged()";
WebAssemblyRuntime.InvokeJS(command);
}

public static int DispatchContentChanged()
{
OnContentChanged();
return 0;
}
}
}
34 changes: 17 additions & 17 deletions src/Uno.UWP/ApplicationModel/DataTransfer/DataPackageView.macOS.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#if __MACOS__
#if __MACOS__
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Windows.ApplicationModel.DataTransfer
{
namespace Windows.ApplicationModel.DataTransfer
{
public partial class DataPackageView
{
private List<string> _availableFormats = new List<string>();
private string _text = null;

public IReadOnlyList<string> AvailableFormats => _availableFormats.AsReadOnly();

{
private List<string> _availableFormats = new List<string>();
private string _text = null;

public IReadOnlyList<string> AvailableFormats => _availableFormats.AsReadOnly();

internal void SetText(string text)
{
_text = text;
Expand All @@ -23,16 +23,16 @@ internal void SetText(string text)
{
_availableFormats.Remove(StandardDataFormats.Text);
}
}

public Foundation.IAsyncOperation<string> GetTextAsync()
{
}

public Foundation.IAsyncOperation<string> GetTextAsync()
{
if (!_availableFormats.Contains(StandardDataFormats.Text))
{
throw new InvalidOperationException("DataPackage does not contain Text data.");
}
return Task.FromResult(_text).AsAsyncOperation();
}
}
}
return Task.FromResult(_text).AsAsyncOperation();
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ public static void SetContent( global::Windows.ApplicationModel.DataTransfer.Dat
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.ApplicationModel.DataTransfer.Clipboard", "void Clipboard.SetContent(DataPackage content)");
}
#endif
#if __ANDROID__ || false || NET461 || __WASM__ || __MACOS__
#if false || false || NET461 || false || false
[global::Uno.NotImplemented]
public static void Flush()
{
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.ApplicationModel.DataTransfer.Clipboard", "void Clipboard.Flush()");
}
#endif
#if __ANDROID__ || false || NET461 || __WASM__ || __MACOS__
#if __ANDROID__ || false || NET461 || false || false
[global::Uno.NotImplemented]
public static void Clear()
{
Expand Down Expand Up @@ -140,7 +140,7 @@ public static void Clear()
}
}
#endif
#if __ANDROID__ || false || NET461 || __WASM__ || __MACOS__
#if __ANDROID__ || false || NET461 || false || __MACOS__
[global::Uno.NotImplemented]
public static event global::System.EventHandler<object> ContentChanged
{
Expand Down

0 comments on commit 920304e

Please sign in to comment.