@area17/a17-helpers
Advanced tools
Comparing version 0.6.5 to 0.6.6
{ | ||
"name": "@area17/a17-helpers", | ||
"version": "0.6.5", | ||
"version": "0.6.6", | ||
"description": "A js helper package", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -53,2 +53,5 @@ # A17 JS Helpers | ||
**0.6.6** | ||
* Updated `copyTextToClipboard` as recent browser updates stopped it working | ||
**0.6.5** | ||
@@ -55,0 +58,0 @@ * Updated `lazyLoad` to v2.1.1 inline with updates to that lib |
@@ -6,36 +6,51 @@ var copyTextToClipboard = function(textToCopy,successMsg) { | ||
var textArea = document.createElement('textarea'); | ||
// and then | ||
// https://stackoverflow.com/questions/47879184/document-execcommandcopy-not-working-on-chrome?rq=1&utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa | ||
// https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa | ||
textArea.style.position = 'fixed'; | ||
textArea.style.top = 0; | ||
textArea.style.left = 0; | ||
textArea.style.width = '2em'; | ||
textArea.style.height = '2em'; | ||
textArea.style.padding = 0; | ||
textArea.style.border = 'none'; | ||
textArea.style.outline = 'none'; | ||
textArea.style.boxShadow = 'none'; | ||
textArea.style.background = 'transparent'; | ||
if (navigator.clipboard && ('Promise' in window) && window.location.protocol == 'https:') { | ||
navigator.clipboard.writeText(text).textToCopy(function() { | ||
console.log(successMsg); | ||
}, function(err) { | ||
console.error('Could not copy text: ', err); | ||
}); | ||
} else { | ||
var textArea = document.createElement('textarea'); | ||
textArea.value = textToCopy; | ||
textArea.style.position = 'fixed'; | ||
textArea.style.top = 0; | ||
textArea.style.left = 0; | ||
textArea.style.width = '2em'; | ||
textArea.style.height = '2em'; | ||
textArea.style.padding = 0; | ||
textArea.style.border = 'none'; | ||
textArea.style.outline = 'none'; | ||
textArea.style.boxShadow = 'none'; | ||
textArea.style.background = 'transparent'; | ||
document.body.appendChild(textArea); | ||
//textArea.value = textToCopy; | ||
textArea.textContent = textToCopy; | ||
document.body.appendChild(textArea); | ||
textArea.select(); | ||
var selection = document.getSelection(); | ||
var range = document.createRange(); | ||
range.selectNode(textArea); | ||
selection.removeAllRanges(); | ||
selection.addRange(range); | ||
try { | ||
var successful = document.execCommand('copy'); | ||
if (successful) { | ||
window.alert(successMsg || 'Copied to clipboard'); | ||
} else { | ||
console.log('Oops, unable to copy'); | ||
try { | ||
var successful = document.execCommand('copy'); | ||
if (successful) { | ||
window.alert(successMsg || 'Copied to clipboard'); | ||
} else { | ||
console.log('Could not copy text'); | ||
} | ||
} catch (err) { | ||
console.log('Could not copy text'); | ||
} | ||
} catch (err) { | ||
console.log('Oops, unable to copy'); | ||
document.body.removeChild(textArea); | ||
} | ||
document.body.removeChild(textArea); | ||
}; | ||
export default copyTextToClipboard; |
45494
1126
126