123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- /**
- * Copies text to clipboard by creating an almost invisible textarea,
- * adding text there, then running execCommand('copy').
- * Falls back to prompt() when the easy way fails (hello, Safari!)
- * From http://stackoverflow.com/a/30810322
- *
- * @param {String} textToCopy
- * @param {String} fallbackString
- * @return {Promise}
- */
- module.exports = function copyToClipboard (textToCopy, fallbackString) {
- fallbackString = fallbackString || 'Copy the URL below'
- return new Promise((resolve) => {
- const textArea = document.createElement('textarea')
- textArea.setAttribute('style', {
- position: 'fixed',
- top: 0,
- left: 0,
- width: '2em',
- height: '2em',
- padding: 0,
- border: 'none',
- outline: 'none',
- boxShadow: 'none',
- background: 'transparent'
- })
- textArea.value = textToCopy
- document.body.appendChild(textArea)
- textArea.select()
- const magicCopyFailed = () => {
- document.body.removeChild(textArea)
- window.prompt(fallbackString, textToCopy)
- resolve()
- }
- try {
- const successful = document.execCommand('copy')
- if (!successful) {
- return magicCopyFailed('copy command unavailable')
- }
- document.body.removeChild(textArea)
- return resolve()
- } catch (err) {
- document.body.removeChild(textArea)
- return magicCopyFailed(err)
- }
- })
- }
|