diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index 5b9d1176..036a7abf 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -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, },