Move copyToClipboard to L.Util

This commit is contained in:
Yohan Boniface 2023-09-08 15:17:16 +02:00
parent 76239ef55c
commit 70a1a1d584
2 changed files with 29 additions and 29 deletions

View file

@ -257,6 +257,34 @@ L.Util.hasVar = (value) => {
return typeof value === 'string' && value.indexOf('{') != -1 return typeof value === 'string' && value.indexOf('{') != -1
} }
L.Util.copyToClipboard = function (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()
}
}
}
L.DomUtil.add = (tagName, className, container, content) => { L.DomUtil.add = (tagName, className, container, content) => {
const el = L.DomUtil.create(tagName, className, container) const el = L.DomUtil.create(tagName, className, container)
if (content) { if (content) {

View file

@ -1174,34 +1174,6 @@ L.U.Map.include({
formData.append('name', this.options.name) formData.append('name', this.options.name)
formData.append('center', JSON.stringify(this.geometry())) formData.append('center', JSON.stringify(this.geometry()))
formData.append('settings', JSON.stringify(geojson)) 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(), { this.post(this.getSaveUrl(), {
data: formData, data: formData,
context: this, context: this,
@ -1233,7 +1205,7 @@ L.U.Map.include({
{ {
label: L._('Copy link'), label: L._('Copy link'),
callback: () => { callback: () => {
copyToClipboard(data.permissions.anonymous_edit_url) L.Util.copyToClipboard(data.permissions.anonymous_edit_url)
this.ui.alert({ this.ui.alert({
content: L._('Secret edit link copied to clipboard!'), content: L._('Secret edit link copied to clipboard!'),
level: 'info', level: 'info',