Return a Promise for `Clipboard.getString()`
Summary: For clipboard, add error callback in Android. Code like ```javascript Clipboard.getString((content)=>{ //do something },(error)=>{ //do something for error }) ``` Closes https://github.com/facebook/react-native/pull/4792 Reviewed By: svcscm Differential Revision: D2844937 Pulled By: nicklockwood fb-gh-sync-id: 19953807ff07238e6a6ef5aedf1a3fcbca7e62a1
This commit is contained in:
parent
1dffd056bf
commit
15f806957f
|
@ -23,23 +23,25 @@ var {
|
|||
} = React;
|
||||
|
||||
var ClipboardExample = React.createClass({
|
||||
getInitialState: function() {
|
||||
getInitialState() {
|
||||
return {
|
||||
content: 'Content will appear here'
|
||||
};
|
||||
},
|
||||
|
||||
_setContentToClipboard:function(){
|
||||
async _setClipboardContent(){
|
||||
Clipboard.setString('Hello World');
|
||||
Clipboard.getString(content => {
|
||||
try {
|
||||
var content = await Clipboard.getString();
|
||||
this.setState({content});
|
||||
});
|
||||
} catch (e) {
|
||||
this.setState({content:e.message});
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
render() {
|
||||
return (
|
||||
<View>
|
||||
<Text onPress={this._setContentToClipboard} style={{color: 'blue'}}>
|
||||
<Text onPress={this._setClipboardContent} style={{color: 'blue'}}>
|
||||
Tap to put "Hello World" in the clipboard
|
||||
</Text>
|
||||
<Text style={{color: 'red', marginTop: 20}}>
|
||||
|
@ -55,6 +57,8 @@ exports.description = 'Show Clipboard contents.';
|
|||
exports.examples = [
|
||||
{
|
||||
title: 'Clipboard.setString() and getString()',
|
||||
render(): ReactElement { return <ClipboardExample />; }
|
||||
render() {
|
||||
return <ClipboardExample/>;
|
||||
}
|
||||
}
|
||||
];
|
||||
|
|
|
@ -10,4 +10,39 @@
|
|||
*/
|
||||
'use strict';
|
||||
|
||||
module.exports = require('NativeModules').Clipboard;
|
||||
var Clipboard = require('NativeModules').Clipboard;
|
||||
|
||||
/**
|
||||
* `Clipboard` gives you an interface for setting and getting content from Clipboard on both iOS and Android
|
||||
*/
|
||||
module.exports = {
|
||||
/**
|
||||
* Get content of string type, this method returns a `Promise`, so you can use following code to get clipboard content
|
||||
* ```javascript
|
||||
* async _getContent() {
|
||||
* var content = await Clipboard.getString();
|
||||
* }
|
||||
* ```
|
||||
* @param this parameter is deprecated. callback is function with one argument of string type
|
||||
*/
|
||||
getString(callback) {
|
||||
if (callback) {
|
||||
console.warn('Clipboard.getString(callback) is deprecated. Use the returned Promise instead');
|
||||
Clipboard.getString().then(callback);
|
||||
return;
|
||||
}
|
||||
return Clipboard.getString();
|
||||
},
|
||||
/**
|
||||
* Set content of string type. You can use following code to set clipboard content
|
||||
* ```javascript
|
||||
* _setContent() {
|
||||
* Clipboard.setString('hello world');
|
||||
* }
|
||||
* ```
|
||||
* @param this parameter is content that will be set into clipboard.
|
||||
*/
|
||||
setString(content) {
|
||||
Clipboard.setString(content);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -22,11 +22,6 @@ RCT_EXPORT_MODULE()
|
|||
return dispatch_get_main_queue();
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(getString:(RCTResponseSenderBlock)callback)
|
||||
{
|
||||
UIPasteboard *clipboard = [UIPasteboard generalPasteboard];
|
||||
callback(@[RCTNullIfNil(clipboard.string)]);
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(setString:(NSString *)content)
|
||||
{
|
||||
|
@ -34,4 +29,11 @@ RCT_EXPORT_METHOD(setString:(NSString *)content)
|
|||
clipboard.string = content;
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(getString:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(__unused RCTPromiseRejectBlock)reject)
|
||||
{
|
||||
UIPasteboard *clipboard = [UIPasteboard generalPasteboard];
|
||||
resolve(@[RCTNullIfNil(clipboard.string)]);
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
@ -21,6 +21,7 @@ import com.facebook.react.bridge.ReactApplicationContext;
|
|||
import com.facebook.react.bridge.ReactContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
import com.facebook.react.bridge.Promise;
|
||||
import com.facebook.react.common.ReactConstants;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -43,28 +44,25 @@ public class ClipboardModule extends ReactContextBaseJavaModule {
|
|||
}
|
||||
|
||||
private ClipboardManager getClipboardService() {
|
||||
ReactApplicationContext reactContext = getReactApplicationContext();
|
||||
return (ClipboardManager) reactContext.getSystemService(reactContext.CLIPBOARD_SERVICE);
|
||||
return (ClipboardManager) getReactApplicationContext().getSystemService(getReactApplicationContext().CLIPBOARD_SERVICE);
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void getString(Callback cb) {
|
||||
public void getString(Promise promise){
|
||||
try {
|
||||
ClipboardManager clipboard = getClipboardService();
|
||||
ClipData clipData = clipboard.getPrimaryClip();
|
||||
if (clipData == null) {
|
||||
cb.invoke("");
|
||||
return;
|
||||
promise.resolve("");
|
||||
}
|
||||
if (clipData.getItemCount() >= 1) {
|
||||
ClipData.Item firstItem = clipboard.getPrimaryClip().getItemAt(0);
|
||||
String text = "" + firstItem.getText();
|
||||
cb.invoke(text);
|
||||
promise.resolve("" + firstItem.getText());
|
||||
} else {
|
||||
cb.invoke("");
|
||||
promise.resolve("");
|
||||
}
|
||||
} catch(Exception e) {
|
||||
FLog.w(ReactConstants.TAG, "Cannot get clipboard contents: " + e.getMessage());
|
||||
promise.reject(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue