Added button that copies code to clipboard

Signed-off-by: Oliver Hunt <oliver@hunt.bz>
This commit is contained in:
Oliver Hunt 2017-06-06 00:29:14 +01:00
parent b8b6a87423
commit a640e943f7
2 changed files with 40 additions and 0 deletions

View file

@ -62,6 +62,19 @@ module.exports = React.createClass({
};
},
copyToClipboard: function(text) {
const textArea = document.createElement("textarea");
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
try {
const successful = document.execCommand('copy');
} catch (err) {
console.log('Unable to copy');
}
document.body.removeChild(textArea);
},
componentDidMount: function() {
this._unmounted = false;
@ -80,6 +93,20 @@ module.exports = React.createClass({
}
}, 10);
}
const buttons = ReactDOM.findDOMNode(this).getElementsByClassName("mx_EventTile_copyButton");
if (buttons.length > 0) {
// Do this asynchronously: parsing code takes time and we don't
// need to block the DOM update on it.
setTimeout(() => {
if (this._unmounted) return;
for (let i = 0; i < buttons.length; i++) {
buttons[i].onclick = (e) => {
const copyCode = buttons[i].parentNode.getElementsByTagName("code")[0];
this.copyToClipboard(copyCode.textContent);
};
}
}, 10);
}
}
},