diff --git a/Examples/UIExplorer/ClipboardExample.js b/Examples/UIExplorer/ClipboardExample.js index bcc53dfbb..0adfdd6f5 100644 --- a/Examples/UIExplorer/ClipboardExample.js +++ b/Examples/UIExplorer/ClipboardExample.js @@ -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 ( - + Tap to put "Hello World" in the clipboard @@ -55,6 +57,8 @@ exports.description = 'Show Clipboard contents.'; exports.examples = [ { title: 'Clipboard.setString() and getString()', - render(): ReactElement { return ; } + render() { + return ; + } } ]; diff --git a/Libraries/Components/Clipboard/Clipboard.js b/Libraries/Components/Clipboard/Clipboard.js index fd8461ba4..0abc57010 100644 --- a/Libraries/Components/Clipboard/Clipboard.js +++ b/Libraries/Components/Clipboard/Clipboard.js @@ -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); + } +}; diff --git a/React/Modules/RCTClipboard.m b/React/Modules/RCTClipboard.m index 0a62f3f88..0a0bf1481 100644 --- a/React/Modules/RCTClipboard.m +++ b/React/Modules/RCTClipboard.m @@ -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 diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/clipboard/ClipboardModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/clipboard/ClipboardModule.java index b3689f980..0ea6c9052 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/clipboard/ClipboardModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/clipboard/ClipboardModule.java @@ -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); } }