From eb4046cc6e7a42fc4e6449c15901e224f50b2b63 Mon Sep 17 00:00:00 2001 From: Garrett Robinson Date: Fri, 9 Jun 2017 00:41:58 -0700 Subject: [PATCH 1/3] Copy shares with Electron's clipboard API --- src/components/CopyButton.js | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/src/components/CopyButton.js b/src/components/CopyButton.js index 3d3eff5..a9bd664 100644 --- a/src/components/CopyButton.js +++ b/src/components/CopyButton.js @@ -1,4 +1,5 @@ import React, { Component } from 'react'; +const {clipboard} = require('electron'); import PropTypes from 'prop-types'; import Button from './Button'; @@ -16,23 +17,11 @@ export default class CopyButton extends Component { } handleClicked() { - let successful; - this.copyEl.select(); - this.copyEl.setSelectionRange(0, this.copyEl.value.length); + clipboard.writeText(this.props.targetText); - try { - successful = document.execCommand('copy'); - } catch (e) { - successful = false; - } - - if (successful) { - this.setState({ copied: 'successful' }); - if (this.props.onCopied) { - this.props.onCopied(); - } - } else { - this.setState({ copied: 'error' }); + this.setState({ copied: 'successful' }); + if (this.props.onCopied) { + this.props.onCopied(); } window.setTimeout(() => this.setState({ copied: null }), 5000); @@ -45,8 +34,6 @@ export default class CopyButton extends Component { if (copied === 'successful') { copyText = 'Copied'; - } else if (copied === 'error') { - copyText = 'Copy failed'; } else { copyText = this.props.buttonText || 'Copy'; } @@ -57,14 +44,7 @@ export default class CopyButton extends Component { onClick={this.handleClicked.bind(this)} {...this.props}> {copyText} - {/* hidden input for copy to clipboard functionality */} - (this.copyEl = el)} - value={this.props.targetText} /> - ); } } From 96a993c86f900487cdf5be2255414367bb3b4ca1 Mon Sep 17 00:00:00 2001 From: Garrett Robinson Date: Wed, 14 Jun 2017 16:17:59 -0700 Subject: [PATCH 2/3] Fix e2e test Now that we've integrated Spectron, we can access Electron API's directly from within the e2e tests, which makes testing clipboard-related functionality straightforward. I added the className to CopyButton and unique id's to each ShareRow to make it easier to construct an explicit WebdriverIO selector. I think explicit selectors (and correspondingly, careful design of classes and ID's) are good because they make tests easier to read and reason about, and reduce the likelihood of test failures due to imprecise selectors. --- src/components/CopyButton.js | 1 + src/components/ShareRow.js | 2 +- test/e2e.js | 12 ++++++++---- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/components/CopyButton.js b/src/components/CopyButton.js index a9bd664..6cc364a 100644 --- a/src/components/CopyButton.js +++ b/src/components/CopyButton.js @@ -40,6 +40,7 @@ export default class CopyButton extends Component { return (