<commit_before>coordsForTouchEvent = (el, e) ->
  tx = e.changedTouches[0].clientX
  ty = e.changedTouches[0].clientY
  p = el.getBoundingClientRect()
  return [tx - p.left, ty - p.top]


position = (el, e) ->
  p = el.getBoundingClientRect()
  {
    left: e.clientX - p.left,
    top: e.clientY - p.top,
  }


buttonIsDown = (e) ->
  if e.buttons?
    return e.buttons == 1
  else
    return e.which > 0


module.exports = bindEvents = (lc, canvas, panWithKeyboard = false) ->
  mouseMoveListener = (e) =>
    e.preventDefault()
    p = position(canvas, e)
    lc.pointerMove(p.left, p.top)

  mouseUpListener = (e) =>
    e.preventDefault()
    canvas.onselectstart = -> true # enable selection while dragging
    p = position(canvas, e)
    lc.pointerUp(p.left, p.top)
    document.removeEventListener 'mousemove', mouseMoveListener
    document.removeEventListener 'mouseup', mouseUpListener

  canvas.addEventListener 'mousedown', (e) =>
    down = true
    e.preventDefault()
    canvas.onselectstart = -> false # disable selection while dragging
    p = position(canvas, e)
    lc.pointerDown(p.left, p.top)

    document.addEventListener 'mousemove', mouseMoveListener
    document.addEventListener 'mouseup', mouseUpListener


  touchMoveListener = (e) ->
    e.preventDefault()
    lc.pointerMove(coordsForTouchEvent(canvas, e)...)

  touchEndListener = (e) ->
    e.preventDefault()
    lc.pointerUp(coordsForTouchEvent(canvas, e)...)
    document.removeEventListener 'touchmove', touchMoveListener
    document.removeEventListener 'touchend', touchEndListener
    document.removeEventListener 'touchcancel', touchEndListener

  canvas.addEventListener 'touchstart', (e) ->
    e.preventDefault()
    if e.touches.length == 1
      lc.pointerDown(coordsForTouchEvent(canvas, e)...)
      document.addEventListener 'touchmove', touchMoveListener
      document.addEventListener 'touchend', touchEndListener
      document.addEventListener 'touchcancel', touchEndListener
    else
      lc.pointerMove(coordsForTouchEvent(canvas, e)...)

  if panWithKeyboard
    document.addEventListener 'keydown', (e) ->
      switch e.keyCode
        when 37 then lc.pan -10, 0
        when 38 then lc.pan 0, -10
        when 39 then lc.pan 10, 0
        when 40 then lc.pan 0, 10

      lc.repaintAllLayers()
<commit_msg>Actually get mousemove events over canvas<commit_after>coordsForTouchEvent = (el, e) ->
  tx = e.changedTouches[0].clientX
  ty = e.changedTouches[0].clientY
  p = el.getBoundingClientRect()
  return [tx - p.left, ty - p.top]


position = (el, e) ->
  p = el.getBoundingClientRect()
  {
    left: e.clientX - p.left,
    top: e.clientY - p.top,
  }


buttonIsDown = (e) ->
  if e.buttons?
    return e.buttons == 1
  else
    return e.which > 0


module.exports = bindEvents = (lc, canvas, panWithKeyboard = false) ->
  mouseMoveListener = (e) =>
    e.preventDefault()
    p = position(canvas, e)
    lc.pointerMove(p.left, p.top)

  mouseUpListener = (e) =>
    e.preventDefault()
    canvas.onselectstart = -> true # enable selection while dragging
    p = position(canvas, e)
    lc.pointerUp(p.left, p.top)
    document.removeEventListener 'mousemove', mouseMoveListener
    document.removeEventListener 'mouseup', mouseUpListener

    canvas.addEventListener 'mousemove', mouseMoveListener

  canvas.addEventListener 'mousedown', (e) =>
    down = true
    e.preventDefault()
    canvas.onselectstart = -> false # disable selection while dragging
    p = position(canvas, e)
    lc.pointerDown(p.left, p.top)

    canvas.removeEventListener 'mousemove', mouseMoveListener
    document.addEventListener 'mousemove', mouseMoveListener
    document.addEventListener 'mouseup', mouseUpListener


  touchMoveListener = (e) ->
    e.preventDefault()
    lc.pointerMove(coordsForTouchEvent(canvas, e)...)

  touchEndListener = (e) ->
    e.preventDefault()
    lc.pointerUp(coordsForTouchEvent(canvas, e)...)
    document.removeEventListener 'touchmove', touchMoveListener
    document.removeEventListener 'touchend', touchEndListener
    document.removeEventListener 'touchcancel', touchEndListener

  canvas.addEventListener 'touchstart', (e) ->
    e.preventDefault()
    if e.touches.length == 1
      lc.pointerDown(coordsForTouchEvent(canvas, e)...)
      document.addEventListener 'touchmove', touchMoveListener
      document.addEventListener 'touchend', touchEndListener
      document.addEventListener 'touchcancel', touchEndListener
    else
      lc.pointerMove(coordsForTouchEvent(canvas, e)...)

  if panWithKeyboard
    document.addEventListener 'keydown', (e) ->
      switch e.keyCode
        when 37 then lc.pan -10, 0
        when 38 then lc.pan 0, -10
        when 39 then lc.pan 10, 0
        when 40 then lc.pan 0, 10

      lc.repaintAllLayers()

{'fraction_non_alphanumeric': 0.11491851232762224, 'fraction_numerical': 0.010447137484329294, 'mean_word_length': 3.339981867633726, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1330872', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1457, 'n_words': 341}