From: Jakub Pawlowicz Date: Mon, 31 Oct 2016 17:03:18 +0000 (+0100) Subject: See #260 - adds 'copy to clipboard' in UI. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=8d37edcbea521d8a471150b58d07012b557252a1;p=clean-css.git See #260 - adds 'copy to clipboard' in UI. Why: * So user can copy optimized content without downloading a file. --- diff --git a/docs/css/home.css b/docs/css/home.css index cf5de0d6..cba6ed6e 100644 --- a/docs/css/home.css +++ b/docs/css/home.css @@ -53,6 +53,15 @@ a, article, body, div, fieldset, form, h1, h2, h3, h4, h5, h6, input, label, li, display: none; } +.clipboard-copy { + border: none; + height: 2rem; + position: fixed; + right: -2rem; + top: 0; + width: 2rem; +} + .footer { text-align: center; margin-bottom: 2rem; diff --git a/docs/index.html b/docs/index.html index e8214f78..e2a69318 100644 --- a/docs/index.html +++ b/docs/index.html @@ -21,10 +21,12 @@ + diff --git a/docs/js/drag-drop.js b/docs/js/drag-drop.js index 24f9d423..a1990f39 100644 --- a/docs/js/drag-drop.js +++ b/docs/js/drag-drop.js @@ -1,4 +1,6 @@ (function () { + var COPY_TO_CLIPBOARD_RESET_DELAY = 2500 + var uniqueID = (function () { var UID = new Date().getTime() @@ -57,10 +59,31 @@ function optimizationCompleted(optimizationId, output) { var fileNode = document.querySelector('.dropped-files__file--' + optimizationId) var downloadNode = fileNode.querySelector('.dropped-files__file__save') + var copyToClipboardNode = fileNode.querySelector('.dropped-files__file__copy') var stylesBlob = new Blob([output.styles]) fileNode.classList.add('dropped-files__file--optimized') downloadNode.href = URL.createObjectURL(stylesBlob) + + copyToClipboardNode.addEventListener('click', function (event) { + var clipboardCopyNode = document.querySelector('.clipboard-copy') + + event.preventDefault() + clipboardCopyNode.value = output.styles + clipboardCopyNode.select() + + try { + document.execCommand('copy') + copyToClipboardNode.innerText = copyToClipboardNode.dataset.successLabel + } catch (e) { + console.error(e) + copyToClipboardNode.innerText = copyToClipboardNode.dataset.errorLabel + } + + setTimeout(function () { + copyToClipboardNode.innerText = copyToClipboardNode.dataset.originalLabel + }, COPY_TO_CLIPBOARD_RESET_DELAY) + }) } window.addEventListener('DOMContentLoaded', function () {