From 14586efa2abd9aaef68eaef9d5464e2176fcc95f Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Sun, 23 Oct 2022 00:21:06 +0100 Subject: [PATCH] implement copyToClipBoard functionality (main.js). This feature copies the selected file in /storage/media and builds it full URL. The intention is to make it easier for users to copy the URL when the need to paster it into one of the 'edit' pages (I.E. Pages and Archive sections) text areas. At the minute the code assumes its only copying and building URL for the files listed on the 'Storage Index' page (/storage/manage defroute). If the website's features expand in the future in this area, this function/feature will need to be refactored. --- static/js/main.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/static/js/main.js b/static/js/main.js index ed52553..c663643 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -1,4 +1,3 @@ -console.log("Site Side Menu script loaded."); function toggleSiteSideMenu() { let displayStyle = document.getElementById("be-site-side-menu").style.display; if (displayStyle === "flex") { @@ -30,3 +29,21 @@ function closeAlert() { } }; }); + + +function copyToClipBoard (slug) { + let URL = location.protocol + "//" + location.host + "/storage/view/media/" + slug; + let popup = document.getElementById(slug + "-popup"); + navigator.clipboard.writeText(URL).then(() => { + /* Resolved - text copied to clipboard */ + console.log("Copied: " + URL + " to clipboard."); + popup.style.visibility = "visible"; + setTimeout(function () { + popup.style.visibility = "hidden"; + }, 2000); + + },() => { + /* Rejected - clipboard failed */ + console.log("Couldn't copy " + URL + " to clipboard"); + }); +}