From 695eba45af1e4b35159877755e881c8cd41144a5 Mon Sep 17 00:00:00 2001 From: Nicolas Gallagher Date: Fri, 16 Dec 2016 11:39:28 +0000 Subject: [PATCH] [add] Clipboard API Close #125 Fix #122 --- README.md | 1 + docs/apis/Clipboard.md | 16 ++++++++++ examples/apis/Clipboard/ClipboardExample.js | 33 +++++++++++++++++++++ src/apis/Clipboard/index.js | 21 +++++++++++++ src/index.js | 2 ++ 5 files changed, 73 insertions(+) create mode 100644 docs/apis/Clipboard.md create mode 100644 examples/apis/Clipboard/ClipboardExample.js create mode 100644 src/apis/Clipboard/index.js diff --git a/README.md b/README.md index 84700a61..406a8380 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ Exported modules: * [`AppRegistry`](docs/apis/AppRegistry.md) * [`AppState`](docs/apis/AppState.md) * [`AsyncStorage`](docs/apis/AsyncStorage.md) + * [`Clipboard`](docs/apis/Clipboard.md) * [`Dimensions`](docs/apis/Dimensions.md) * [`I18nManager`](docs/apis/I18nManager.md) * [`NativeMethods`](docs/apis/NativeMethods.md) diff --git a/docs/apis/Clipboard.md b/docs/apis/Clipboard.md new file mode 100644 index 00000000..84de7d87 --- /dev/null +++ b/docs/apis/Clipboard.md @@ -0,0 +1,16 @@ +# Clipboard + +Clipboard gives you an interface for setting to the clipboard. (Getting +clipboard content is not supported on web.) + +## Methods + +static **getString**() + +Returns a `Promise` of an empty string. + +static **setString**(content: string): boolean + +Copies a string to the clipboard. On web, some browsers may not support copying +to the clipboard, therefore, this function returns a boolean to indicate if the +copy was successful. diff --git a/examples/apis/Clipboard/ClipboardExample.js b/examples/apis/Clipboard/ClipboardExample.js new file mode 100644 index 00000000..46a38aec --- /dev/null +++ b/examples/apis/Clipboard/ClipboardExample.js @@ -0,0 +1,33 @@ +import { Clipboard, Text, TextInput, View } from 'react-native' +import React, { Component } from 'react'; +import { action, storiesOf } from '@kadira/storybook'; + +class ClipboardExample extends Component { + render() { + return ( + + Copy to clipboard + + (Clipboard.getString returns a Promise that always resolves to an empty string on web) + + ) + } + + _handleGet() { + Clipboard.getString().then((value) => { console.log(`Clipboard value: ${value}`) }); + } + + _handleSet() { + const success = Clipboard.setString('This text was copied to the clipboard by React Native'); + console.log(`Clipboard.setString success? ${success}`); + } +} + +storiesOf('api: Clipboard', module) + .add('setString', () => ( + + )); diff --git a/src/apis/Clipboard/index.js b/src/apis/Clipboard/index.js new file mode 100644 index 00000000..70c7d6a2 --- /dev/null +++ b/src/apis/Clipboard/index.js @@ -0,0 +1,21 @@ +class Clipboard { + static getString() { + return Promise.resolve(''); + } + + static setString(text) { + let success = false; + const textField = document.createElement('textarea'); + textField.innerText = text; + document.body.appendChild(textField); + textField.select(); + try { + document.execCommand('copy'); + success = true; + } catch (e) {} + document.body.removeChild(textField); + return success; + } +} + +module.exports = Clipboard; diff --git a/src/index.js b/src/index.js index 94787f4b..613885bc 100644 --- a/src/index.js +++ b/src/index.js @@ -9,6 +9,7 @@ import Animated from './apis/Animated'; import AppRegistry from './apis/AppRegistry'; import AppState from './apis/AppState'; import AsyncStorage from './apis/AsyncStorage'; +import Clipboard from './apis/Clipboard'; import Dimensions from './apis/Dimensions'; import Easing from 'animated/lib/Easing'; import I18nManager from './apis/I18nManager'; @@ -58,6 +59,7 @@ const ReactNative = { AppRegistry, AppState, AsyncStorage, + Clipboard, Dimensions, Easing, I18nManager,