Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For windows uwp, no callback will be executed when the stuff inside the clipboard is not a text. #39

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ Clipboard management plugin for Cordova/PhoneGap that supports iOS, Android, and

## Usage

Install the plugin using the CLI, for instance with PhoneGap:
```
cordova plugin add cordova-clipboard
```

phonegap local plugin add https://github.com/VersoSolutions/CordovaClipboard

The plugin creates the object `cordova.plugins.clipboard` with the methods `copy(text, onSuccess, onError)` and `paste(onSuccess, onError)`.
The plugin creates the object `cordova.plugins.clipboard` with the methods `copy(text, onSuccess, onError)`, `paste(onSuccess, onError)` and `clear(onSuccess, onError)`

Example:

Expand All @@ -19,6 +19,8 @@ Example:

cordova.plugins.clipboard.paste(function (text) { alert(text); });

cordova.plugins.clipboard.clear();

## Notes

### All platforms
Expand Down
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "cordova-clipboard",
"version": "1.3.0",
"description": "Clipboard management plugin for Cordova/PhoneGap that supports iOS, Android, and WP8",
"cordova": {
"id": "cordova-clipboard",
"platforms": [
"android",
"ios",
"wp8"
]
},
"repository": {
"type": "git",
"url": "git+https://github.com/ihadeed/cordova-clipboard.git"
},
"keywords": [
"cordova",
"plugin",
"clipboard",
"phonegap",
"ionic"
],
"author": "Ibrahim Hadeed",
"license": "MIT",
"bugs": {
"url": "https://github.com/ihadeed/cordova-clipboard/issues"
},
"homepage": "https://github.com/ihadeed/cordova-clipboard#readme"
}
26 changes: 22 additions & 4 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.verso.cordova.clipboard"
version="0.1.0">
id="cordova-clipboard"
version="1.3.0">

<engines>
<engine name="cordova" version=">=3.0.0" />
<engine name="cordova" version=">=4.0.0" />
</engines>

<name>Clipboard</name>
Expand All @@ -14,7 +14,7 @@

<author>Verso Solutions LLC</author>

<keywords>clipboard,copy,paste</keywords>
<keywords>clipboard,copy,paste,clear</keywords>

<license>MIT</license>

Expand All @@ -33,7 +33,19 @@
<header-file src="src/ios/CDVClipboard.h" />
<source-file src="src/ios/CDVClipboard.m" />
</platform>

<!-- OS X -->
<platform name="osx">
<config-file target="config.xml" parent="/*">
<feature name="Clipboard">
<param name="ios-package" value="CDVClipboard" />
</feature>
</config-file>

<header-file src="src/osx/CDVClipboard.h" />
<source-file src="src/osx/CDVClipboard.m" />
</platform>

<!-- Android -->
<platform name="android">
<source-file src="src/android/Clipboard.java" target-dir="src/com/verso/cordova/clipboard" />
Expand All @@ -56,4 +68,10 @@
<source-file src="src/wp8/Clipboard.cs" />
</platform>

<!-- Windows uwp -->
<platform name="windows">
<js-module src="src/windows/ClipboardProxy.js" name="ClipboardProxy">
<merges target="" />
</js-module>
</platform>
</plugin>
26 changes: 18 additions & 8 deletions src/android/Clipboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Clipboard extends CordovaPlugin {

private static final String actionCopy = "copy";
private static final String actionPaste = "paste";
private static final String actionClear = "clear";

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
Expand All @@ -37,17 +38,26 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, e.toString()));
}
} else if (action.equals(actionPaste)) {
if (!clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.NO_RESULT));
}

try {
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
String text = item.getText().toString();
String text = "";

ClipData clip = clipboard.getPrimaryClip();
if (clip != null) {
ClipData.Item item = clip.getItemAt(0);
text = item.getText().toString();
}
callbackContext.success(text);

if (text == null) text = "";
return true;
} catch (Exception e) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, e.toString()));
}
} else if (action.equals(actionClear)) {
try {
ClipData clip = ClipData.newPlainText("", "");
clipboard.setPrimaryClip(clip);

callbackContext.success(text);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));

return true;
} catch (Exception e) {
Expand Down
1 change: 1 addition & 0 deletions src/ios/CDVClipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

- (void)copy:(CDVInvokedUrlCommand*)command;
- (void)paste:(CDVInvokedUrlCommand*)command;
- (void)clear:(CDVInvokedUrlCommand*)command;

@end
23 changes: 18 additions & 5 deletions src/ios/CDVClipboard.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ - (void)copy:(CDVInvokedUrlCommand*)command {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString *text = [command.arguments objectAtIndex:0];

[pasteboard setValue:text forPasteboardType:@"public.text"];
pasteboard.string = text;

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:text];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
Expand All @@ -21,10 +21,23 @@ - (void)paste:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString *text = [pasteboard valueForPasteboardType:@"public.text"];

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:text];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
if (text == nil) {
text = @"";
}

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:text];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}

- (void)clear:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setValue:@"" forPasteboardType:UIPasteboardNameGeneral];

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:true];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}

@end
@end
9 changes: 9 additions & 0 deletions src/osx/CDVClipboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#import <Foundation/Foundation.h>
#import <Cordova/CDVPlugin.h>

@interface CDVClipboard : CDVPlugin {}

- (void)copy:(CDVInvokedUrlCommand*)command;
- (void)paste:(CDVInvokedUrlCommand*)command;

@end
34 changes: 34 additions & 0 deletions src/osx/CDVClipboard.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#import <Foundation/Foundation.h>
#import <Cordova/CDVPlugin.h>
#import <Cordova/CDVPluginResult.h>
#import "CDVClipboard.h"

@implementation CDVClipboard

- (void)copy:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSString *text = [command.arguments objectAtIndex:0];

[pasteboard clearContents];
[pasteboard setString:text forType:NSStringPboardType];

CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:text];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}

- (void)paste:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSString *text = [pasteboard stringForType:NSPasteboardTypeString];
if (text == nil) {
text = @"";
}

CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:text];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}

@end
67 changes: 67 additions & 0 deletions src/windows/ClipboardProxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

/*jslint sloppy:true */
/*global Windows:true, require, document, window, module */

var cordova = require('cordova');

module.exports = {

copy: function (successCallback, errorCallback, args) {
try {
var text = args[0];

var dataPackage = new Windows.ApplicationModel.DataTransfer.DataPackage();
dataPackage.setText(text);
Windows.ApplicationModel.DataTransfer.Clipboard.setContent(dataPackage);
successCallback(text);
} catch (e) {
errorCallback(e);;
}
},
paste: function (successCallback, errorCallback, args) {
try {
var text = "";

var dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.getContent();
if (dataPackageView.contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.text)) {
dataPackageView.getTextAsync().then(function (value) {
text = value;
successCallback(text);
});
}
} catch (e) {
errorCallback(e);;
}
},
clear: function (successCallback, errorCallback, args) {
try {
if(Windows.ApplicationModel.DataTransfer.Clipboard.getContent()){
successCallback(true);
}
} catch (e) {
errorCallback(e);;
}
}
}; // exports

require("cordova/exec/proxy").add("Clipboard", module.exports);
18 changes: 17 additions & 1 deletion src/wp8/Clipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void copy(string options)

}

public void paste(string options)
public void paste()
{
string text = "";

Expand All @@ -54,5 +54,21 @@ public void paste(string options)
}
});
}

public void clear()
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
try
{
System.Windows.Clipboard.Clear();
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, true));
}
catch
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR));
}
});
}
}
}
10 changes: 10 additions & 0 deletions www/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ Clipboard.prototype.paste = function (onSuccess, onFail) {
cordova.exec(onSuccess, onFail, "Clipboard", "paste", []);
};

/**
* Clear the clipboard content
*
* @param {Function} onSuccess The function to call in case of success
* @param {Function} onFail The function to call in case of error
*/
Clipboard.prototype.clear = function (onSuccess, onFail) {
cordova.exec(onSuccess, onFail, "Clipboard", "clear", []);
};

// Register the plugin
var clipboard = new Clipboard();
module.exports = clipboard;