aboutsummaryrefslogtreecommitdiffstats
path: root/lib/clipboard.coffee
blob: 11542b1190b974038a2dc0f0162fba8349d0f231 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Clipboard =
  _createTextArea: (tagName = "textarea") ->
    textArea = document.createElement tagName
    textArea.style.position = "absolute"
    textArea.style.left = "-100%"
    textArea.contentEditable = "true"
    textArea

  # http://groups.google.com/group/chromium-extensions/browse_thread/thread/49027e7f3b04f68/f6ab2457dee5bf55
  copy: ({data}) ->
    textArea = @_createTextArea()
    textArea.value = data.replace /\xa0/g, " "

    document.body.appendChild(textArea)
    textArea.select()
    document.execCommand("Copy")
    document.body.removeChild(textArea)

  paste: ->
    textArea = @_createTextArea "div" # Use a <div> so Firefox pastes rich text.
    document.body.appendChild(textArea)
    textArea.focus()
    document.execCommand("Paste")
    value = textArea.innerText
    document.body.removeChild(textArea)
    value.replace /\xa0/g, " "


root = exports ? (window.root ?= {})
root.Clipboard = Clipboard
extend window, root unless exports?