From 70a1a1d58416ba0d0e64cd65c5d263b79182115e Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Fri, 8 Sep 2023 15:17:16 +0200 Subject: [PATCH] Move copyToClipboard to L.Util --- umap/static/umap/js/umap.core.js | 28 ++++++++++++++++++++++++++++ umap/static/umap/js/umap.js | 30 +----------------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/umap/static/umap/js/umap.core.js b/umap/static/umap/js/umap.core.js index b03979f8..a9398945 100644 --- a/umap/static/umap/js/umap.core.js +++ b/umap/static/umap/js/umap.core.js @@ -257,6 +257,34 @@ L.Util.hasVar = (value) => { 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) => { const el = L.DomUtil.create(tagName, className, container) if (content) { diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index 27414e33..b1d8a42e 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -1174,34 +1174,6 @@ 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, @@ -1233,7 +1205,7 @@ L.U.Map.include({ { label: L._('Copy link'), callback: () => { - copyToClipboard(data.permissions.anonymous_edit_url) + L.Util.copyToClipboard(data.permissions.anonymous_edit_url) this.ui.alert({ content: L._('Secret edit link copied to clipboard!'), level: 'info',