Add clipboard plugin

This commit is contained in:
Yemel Jardi 2014-08-13 10:18:36 -03:00
parent fe53f1b87c
commit 66482cd9e2
4 changed files with 110 additions and 1 deletions

View File

@ -20,6 +20,13 @@ module.exports = [
"clobbers": [ "clobbers": [
"navigator.splashscreen" "navigator.splashscreen"
] ]
},
{
"file": "plugins/com.verso.cordova.clipboard/www/clipboard.js",
"id": "com.verso.cordova.clipboard.Clipboard",
"clobbers": [
"cordova.plugins.clipboard"
]
} }
]; ];
module.exports.metadata = module.exports.metadata =
@ -27,7 +34,8 @@ module.exports.metadata =
{ {
"de.appplant.cordova.plugin.email-composer": "0.8.2dev", "de.appplant.cordova.plugin.email-composer": "0.8.2dev",
"com.phonegap.plugins.barcodescanner": "1.0.1", "com.phonegap.plugins.barcodescanner": "1.0.1",
"org.apache.cordova.splashscreen": "0.3.0" "org.apache.cordova.splashscreen": "0.3.0",
"com.verso.cordova.clipboard": "0.1.0"
} }
// BOTTOM OF METADATA // BOTTOM OF METADATA
}); });

View File

@ -0,0 +1,36 @@
cordova.define("com.verso.cordova.clipboard.Clipboard", function(require, exports, module) { var cordova = require('cordova');
/**
* Clipboard plugin for Cordova
*
* @constructor
*/
function Clipboard () {}
/**
* Sets the clipboard content
*
* @param {String} text The content to copy to the clipboard
* @param {Function} onSuccess The function to call in case of success (takes the copied text as argument)
* @param {Function} onFail The function to call in case of error
*/
Clipboard.prototype.copy = function (text, onSuccess, onFail) {
if (typeof text === "undefined" || text === null) text = "";
cordova.exec(onSuccess, onFail, "Clipboard", "copy", [text]);
};
/**
* Gets 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.paste = function (onSuccess, onFail) {
cordova.exec(onSuccess, onFail, "Clipboard", "paste", []);
};
// Register the plugin
var clipboard = new Clipboard();
module.exports = clipboard;
});

View File

@ -28,4 +28,7 @@
<feature name="SplashScreen"> <feature name="SplashScreen">
<param name="android-package" value="org.apache.cordova.splashscreen.SplashScreen" /> <param name="android-package" value="org.apache.cordova.splashscreen.SplashScreen" />
</feature> </feature>
<feature name="Clipboard">
<param name="android-package" value="com.verso.cordova.clipboard.Clipboard" />
</feature>
</widget> </widget>

View File

@ -0,0 +1,62 @@
package com.verso.cordova.clipboard;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Context;
import android.content.ClipboardManager;
import android.content.ClipData;
import android.content.ClipDescription;
public class Clipboard extends CordovaPlugin {
private static final String actionCopy = "copy";
private static final String actionPaste = "paste";
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
ClipboardManager clipboard = (ClipboardManager) cordova.getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
if (action.equals(actionCopy)) {
try {
String text = args.getString(0);
ClipData clip = ClipData.newPlainText("Text", text);
clipboard.setPrimaryClip(clip);
callbackContext.success(text);
return true;
} catch (JSONException e) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
} catch (Exception e) {
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();
if (text == null) text = "";
callbackContext.success(text);
return true;
} catch (Exception e) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, e.toString()));
}
}
return false;
}
}