mirror of https://github.com/BTCPrivate/copay.git
Add clipboard plugin
This commit is contained in:
parent
fe53f1b87c
commit
66482cd9e2
|
@ -20,6 +20,13 @@ module.exports = [
|
|||
"clobbers": [
|
||||
"navigator.splashscreen"
|
||||
]
|
||||
},
|
||||
{
|
||||
"file": "plugins/com.verso.cordova.clipboard/www/clipboard.js",
|
||||
"id": "com.verso.cordova.clipboard.Clipboard",
|
||||
"clobbers": [
|
||||
"cordova.plugins.clipboard"
|
||||
]
|
||||
}
|
||||
];
|
||||
module.exports.metadata =
|
||||
|
@ -27,7 +34,8 @@ module.exports.metadata =
|
|||
{
|
||||
"de.appplant.cordova.plugin.email-composer": "0.8.2dev",
|
||||
"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
|
||||
});
|
|
@ -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;
|
||||
|
||||
});
|
|
@ -28,4 +28,7 @@
|
|||
<feature name="SplashScreen">
|
||||
<param name="android-package" value="org.apache.cordova.splashscreen.SplashScreen" />
|
||||
</feature>
|
||||
<feature name="Clipboard">
|
||||
<param name="android-package" value="com.verso.cordova.clipboard.Clipboard" />
|
||||
</feature>
|
||||
</widget>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue