Sha256: 2f143245f831cf35dd8bf0a331d1dd6b8d2a20c3ccf4df2b072f89ecc5a1cc05

Contents?: true

Size: 1.74 KB

Versions: 22

Compression:

Stored size: 1.74 KB

Contents

function testCopyComand() {
  try {
    document.execCommand("copy")
    return true
  }
  catch (error) {
    return false
  }
}

function addClickToCopyButton(codeBlocks) {
  codeBlocks.map(function(codeBlock, index){
    var clickToCopyButton = createButton()
    codeBlock.appendChild(clickToCopyButton)
    clickToCopyButton.addEventListener('click', function(e) {
      selectElementRange(this.parentNode)
    })
  })
}

function selectElementRange(copyElementSelect) {
  var range = document.createRange()
  range.selectNode(copyElementSelect)
  window.getSelection().removeAllRanges()
  window.getSelection().addRange(range)
  document.execCommand("copy")
  window.getSelection().removeAllRanges()
}

function createButton() {
  var copyButton = document.createElement('button')
  copyButton.className = 'ae-btn ae-btn-primary click-to-copy'
  copyButton.data = 'Click to copy'
  var clipboard = document.createElement('i')
  clipboard.className = 'fa fa-clipboard'
  copyButton.appendChild(clipboard)
  return copyButton
}

function addClickToCopyText() {
  var codeBlockButtons = Array.prototype.slice.call(document.querySelectorAll('.ae-code-block .ae-btn'))

  codeBlockButtons.map(function(button, index) {
    var pre = button.closest('pre')
    button.setAttribute('data-tooltip', 'Click to copy')
    button.addEventListener('click', function(e){
      button.setAttribute('data-tooltip', 'Copied!')
      setTimeout(function(){
        button.setAttribute('data-tooltip', 'Click to copy')},
      500)
    })
  })
}

document.addEventListener("DOMContentLoaded", function() {
  if (testCopyComand()) {
    var codeBlocks = Array.prototype.slice.call(document.querySelectorAll(".ae-code-block"))

    addClickToCopyButton(codeBlocks)
    addClickToCopyText()
  }
})

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
express_ui-0.5.2 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.5.1 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.5.0 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.4.2 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.4.1 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.4.0 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.3.0 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.2.1 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.2.0 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.1.7 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.1.6 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.1.5 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.1.4 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.1.3 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.1.2 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.1.1 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.1.0 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.1.0.rc7 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.1.0.rc5 app/assets/javascripts/express_ui/copy_code.js
express_ui-0.1.0.rc4 app/assets/javascripts/express_ui/copy_code.js