Fallback to copy to clipboard without HTTPS

This commit is contained in:
David Larlet 2023-06-02 11:50:03 -04:00
parent ca97a4d745
commit 725feb8d01
No known key found for this signature in database
GPG key ID: 3E2953A359E7E7BD

View file

@ -1332,6 +1332,34 @@ L.U.Map.include({
formData.append('name', this.options.name)
formData.append('center', JSON.stringify(this.geometry()))
formData.append('settings', JSON.stringify(geojson))
function copyToClipboard(textToCopy) {
// https://stackoverflow.com/a/65996386
// Navigator clipboard api needs a secure context (https)
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText(textToCopy)
} else {
// Use the 'out of viewport hidden text area' trick
const textArea = document.createElement('textarea')
textArea.value = textToCopy
// Move textarea out of the viewport so it's not visible
textArea.style.position = 'absolute'
textArea.style.left = '-999999px'
document.body.prepend(textArea)
textArea.select()
try {
document.execCommand('copy')
} catch (error) {
console.error(error)
} finally {
textArea.remove()
}
}
}
this.post(this.getSaveUrl(), {
data: formData,
context: this,
@ -1363,8 +1391,11 @@ L.U.Map.include({
{
label: L._('Copy link'),
callback: () => {
navigator.clipboard.writeText(data.permissions.anonymous_edit_url)
this.ui.alert({content: L._('Copied!'), level: 'info'})
copyToClipboard(data.permissions.anonymous_edit_url)
this.ui.alert({
content: L._('Secret edit link copied to clipboard!'),
level: 'info',
})
},
callbackContext: this,
},