Encode code into url to make sharing snippets easy

resolves #253
This commit is contained in:
Kurt Hutten
2021-03-16 20:26:58 +11:00
parent 03ce3530c1
commit 01bc76e09a
3 changed files with 73 additions and 5 deletions

View File

@@ -0,0 +1,37 @@
function fallbackCopyTextToClipboard(text) {
var textArea = document.createElement('textarea')
textArea.value = text
// Avoid scrolling to bottom
textArea.style.top = '0'
textArea.style.left = '0'
textArea.style.position = 'fixed'
document.body.appendChild(textArea)
textArea.focus()
textArea.select()
try {
var successful = document.execCommand('copy')
var msg = successful ? 'successful' : 'unsuccessful'
console.log('Fallback: Copying text command was ' + msg)
} catch (err) {
console.error('Fallback: Oops, unable to copy', err)
}
document.body.removeChild(textArea)
}
export function copyTextToClipboard(text) {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text)
return
}
navigator.clipboard.writeText(text).then(
function () {
console.log('Async: Copying to clipboard was successful!')
},
function (err) {
console.error('Async: Could not copy text: ', err)
}
)
}