Skip to content

Commit

Permalink
Return a Promise for Clipboard.getString()
Browse files Browse the repository at this point in the history
Summary:
For clipboard, add error callback in Android. Code like
```javascript
Clipboard.getString((content)=>{
    //do something
},(error)=>{
   //do something for error
})
```
Closes #4792

Reviewed By: svcscm

Differential Revision: D2844937

Pulled By: nicklockwood

fb-gh-sync-id: 19953807ff07238e6a6ef5aedf1a3fcbca7e62a1
  • Loading branch information
tantan authored and facebook-github-bot-5 committed Jan 20, 2016
1 parent 1dffd05 commit 15f8069
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
20 changes: 12 additions & 8 deletions Examples/UIExplorer/ClipboardExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}}>
Expand All @@ -55,6 +57,8 @@ exports.description = 'Show Clipboard contents.';
exports.examples = [
{
title: 'Clipboard.setString() and getString()',
render(): ReactElement { return <ClipboardExample />; }
render() {
return <ClipboardExample/>;
}
}
];
37 changes: 36 additions & 1 deletion Libraries/Components/Clipboard/Clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

This comment has been minimized.

Copy link
@dmmiller

dmmiller Jan 20, 2016

Just saw this. I think we should remove callback and check from arguments. I'm putting up a PR today for CameraRoll that also moves to promises tomorrow. I'll cc you on this so you can see it.

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);
}
};
10 changes: 6 additions & 4 deletions React/Modules/RCTClipboard.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ - (dispatch_queue_t)methodQueue
return dispatch_get_main_queue();
}

RCT_EXPORT_METHOD(getString:(RCTResponseSenderBlock)callback)

RCT_EXPORT_METHOD(setString:(NSString *)content)
{
UIPasteboard *clipboard = [UIPasteboard generalPasteboard];
callback(@[RCTNullIfNil(clipboard.string)]);
clipboard.string = content;
}

RCT_EXPORT_METHOD(setString:(NSString *)content)
RCT_EXPORT_METHOD(getString:(RCTPromiseResolveBlock)resolve
rejecter:(__unused RCTPromiseRejectBlock)reject)
{
UIPasteboard *clipboard = [UIPasteboard generalPasteboard];
clipboard.string = content;
resolve(@[RCTNullIfNil(clipboard.string)]);
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand All @@ -43,28 +44,25 @@ public String getName() {
}

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);
}
}

Expand Down

0 comments on commit 15f8069

Please sign in to comment.