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 113 lines (93 sloc) 2.363 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
class KeyBindings extends Spine.Module
  @include Spine.Log

  mapping:
    8: 'backspace'
    37: 'leftArrow'
    38: 'upArrow'
    39: 'rightArrow'
    40: 'downArrow'
    46: 'backspace'
    65: 'aKey'
    67: 'cKey'
    68: 'dKey'
    79: 'oKey'
    83: 'sKey'
    86: 'vKey'
    90: 'zKey'
    187: 'plusKey'
    189: 'minusKey'

  constructor: (@stage) ->
    $(document).bind('keydown', @keypress)

  keypress: (e) =>
    # Disable keyboard shortcuts in inputs
    return if 'value' of e.target
    return if $(e.target).attr('contenteditable')
    @[@mapping[e.which]]?(e)

  backspace: (e) ->
    e.preventDefault()
    @stage.history.record()
    @stage.removeSelected()

  leftArrow: (e) ->
    e.preventDefault()
    amount = -1
    amount *= 10 if e.shiftKey
    @stage.history.record('leftArrow')
    @stage.selection.moveBy(left: amount, top: 0)

  upArrow: (e) ->
    e.preventDefault()
    amount = -1
    amount *= 10 if e.shiftKey
    @stage.history.record('upArrow')
    @stage.selection.moveBy(left: 0, top: amount)

  rightArrow: (e) ->
    e.preventDefault()
    amount = 1
    amount *= 10 if e.shiftKey
    @stage.history.record('rightArrow')
    @stage.selection.moveBy(left: amount, top: 0)

  downArrow: (e) ->
    e.preventDefault()
    amount = 1
    amount *= 10 if e.shiftKey
    @stage.history.record('downArrow')
    @stage.selection.moveBy(left: 0, top: amount)

  aKey: (e) ->
    return unless e.metaKey
    e.preventDefault()
    @stage.selectAll()

  dKey: (e) ->
    return unless e.metaKey
    e.preventDefault()
    @stage.selection.clear() if e.metaKey

  oKey: (e) ->
    return unless e.metaKey
    e.preventDefault()
    @stage.load()

  sKey: (e) ->
    return unless e.metaKey
    e.preventDefault()
    @stage.save()

  plusKey: (e) ->
    return unless e.metaKey
    e.preventDefault()
    @log('zoomIn')

  minusKey: (e) ->
    return unless e.metaKey
    e.preventDefault()
    @log('zoomOut')

  cKey: (e) ->
    return unless e.metaKey
    @stage.clipboard.copyInternal(e)

  vKey: (e) ->
    return unless e.metaKey
    @stage.clipboard.pasteInternal(e)

  zKey: (e) ->
    return unless e.metaKey
    e.preventDefault()

    if e.shiftKey
      @stage.history.redo()
    else
      @stage.history.undo()

  release: ->
    $(document).unbind('keydown', @keypress)

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