Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
76 lines (55 sloc) 1.87 KB
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
return if $('contenteditable')
# We need to cancel the default to get
# the 'copy' event to trigger
copy: (e) =>
return unless @stage.selection.isAny()
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 = 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.add(el) for el in 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
@stage.add(el) for el in @data
@stage.selection.moveBy(left: 10, top: 10)
# Re-clone the elements
release: ->
$(window).unbind 'beforecopy', @cancel
$(window).unbind 'copy', @copy
$(window).unbind 'beforepaste', @cancel
$(window).unbind 'paste', @paste
module.exports = Clipboard