See #260 - adds 'copy to clipboard' in UI.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 31 Oct 2016 17:03:18 +0000 (18:03 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Thu, 3 Nov 2016 19:06:19 +0000 (20:06 +0100)
Why:

* So user can copy optimized content without downloading a file.

docs/css/home.css
docs/index.html
docs/js/drag-drop.js

index cf5de0d..cba6ed6 100644 (file)
@@ -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;
index e8214f7..e2a6931 100644 (file)
   <footer class="footer">
     Hosted with <span class="footer__heart">❤️</span> on <a href="//pages.github.com/">GitHub Pages.</a>
   </footer>
+  <textarea class="clipboard-copy"></textarea>
   <template class="dropped-file-template">
     <li class="dropped-files__file">
       <span class="dropped-files__file__name"></span>
       <a class="dropped-files__file__save" href="#" download="test.css">save</a>
+      <a class="dropped-files__file__copy" href="#" data-success-label="done!" data-error-label="oops! see console for details" data-original-label="copy to clipboard">copy to clipboard</a>
     </li>
   </template>
 </body>
index 24f9d42..a1990f3 100644 (file)
@@ -1,4 +1,6 @@
 (function () {
+  var COPY_TO_CLIPBOARD_RESET_DELAY = 2500
+
   var uniqueID = (function () {
     var UID = new Date().getTime()
 
   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 () {