Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 76 lines (55 sloc) 1.91 kb
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
Serialize = require('app/models/serialize')

class Clipboard
  constructor: (@stage) ->
    $(window).bind 'beforecopy', @cancel
    $(window).bind 'copy', @copy

    # Remove pasting for the moment, until
    # it's better supported in browsers
    #
    # $(window).bind 'beforepaste', @cancel
    # $(window).bind 'paste', @paste

  cancel: (e) =>
    return if 'value' of e.target
    return if $(e.target).attr('contenteditable')

    # We need to cancel the default to get
    # the 'copy' event to trigger
    e.preventDefault()

  copy: (e) =>
    return unless @stage.selection.isAny()

    e.preventDefault()
    e = e.originalEvent

    json = JSON.stringify(@stage.selection.elements)
    e.clipboardData.setData('json/x-stylo', json)

    styles = (el.outerCSS() for el in @stage.selection.elements)
    e.clipboardData.setData('text/plain', styles.join("\n\n"))

  paste: (e) =>
    return if 'value' of e.target

    e.preventDefault()
    e = e.originalEvent

    # Some browsers restrict the clipboard data types,
    # so we need to revert back to text/html
    json = e.clipboardData.getData('json/x-stylo')
    return unless json

    elements = Serialize.fromJSON(json)

    @stage.history.record()
    @stage.add(el) for el in elements
    @stage.selection.refresh(elements)
    @stage.selection.moveBy(left: 10, top: 10)

  data: null

  copyInternal: ->
    @data = (el.clone() for el in @stage.selection.elements)

  pasteInternal: (e) ->
    return unless @data

    e?.preventDefault()

    @stage.history.record()
    @stage.add(el) for el in @data
    @stage.selection.refresh(@data)
    @stage.selection.moveBy(left: 10, top: 10)

    # Re-clone the elements
    @copyInternal()

  release: ->
    $(window).unbind 'beforecopy', @cancel
    $(window).unbind 'copy', @copy
    $(window).unbind 'beforepaste', @cancel
    $(window).unbind 'paste', @paste

module.exports = Clipboard
Something went wrong with that request. Please try again.