<commit_before>window.LC = window.LC ? {}


coordsForEvent = ($el, e) ->
  t = e.originalEvent.changedTouches[0]
  p = $el.position()
  return [t.clientX - p.left, t.clientY - p.top]


IS_IOS = /iphone|ipad/i.test(navigator.userAgent)
$.fn.nodoubletapzoom = ->
  if (IS_IOS)
    $(this).bind 'touchstart', (e) ->
      t2 = e.timeStamp
      t1 = $(this).data('lastTouch') || t2
      dt = t2 - t1
      fingers = e.originalEvent.touches.length;
      $(this).data('lastTouch', t2);
      if !dt || dt > 500 || fingers > 1 then return; # not double-tap

      e.preventDefault() # double tap - prevent the zoom
      # also synthesize click events we just swallowed up
      $(this).trigger('click').trigger('click')


$.fn.literallycanvas = ->
  @nodoubletapzoom()

  $c = @find('canvas')
  c = $c.get(0)

  lc = new LC.LiterallyCanvas(c)
  tb = new LC.Toolbar(lc, @find('.toolbar'))

  $c.mousedown (e) =>
    document.onselectstart = -> false # disable selection while dragging
    lc.begin(e.offsetX, e.offsetY)

  $c.mousemove (e) =>
    lc.continue(e.offsetX, e.offsetY)

  $c.mouseup (e) =>
    document.onselectstart = -> true # disable selection while dragging
    lc.end(e.offsetX, e.offsetY)

  $c.mouseout (e) =>
    lc.end(e.offsetX, e.offsetY)

  $c.bind 'touchstart', (e) ->
    e.preventDefault()
    coords = coordsForEvent($c, e)
    if e.originalEvent.touches.length == 1
      lc.begin(coords[0], coords[1])
    else
      lc.continue(coords[0], coords[1])

  $c.bind 'touchmove', (e) ->
    e.preventDefault()
    coords = coordsForEvent($c, e)
    lc.continue(coords[0], coords[1])

  $c.bind 'touchend', (e) ->
    e.preventDefault()
    return unless e.originalEvent.touches.length == 0
    coords = coordsForEvent($c, e)
    lc.end(coords[0], coords[1])

  $c.bind 'touchcancel', (e) ->
    e.preventDefault()
    return unless e.originalEvent.touches.length == 0
    coords = coordsForEvent($c, e)
    lc.end(coords[0], coords[1])

  $(document).keydown (e) ->
    switch e.which
      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.repaint()


class LC.Tool

  begin: (x, y, lc) ->
  continue: (x, y, lc) ->
  end: (x, y, lc) ->


class LC.Pencil extends LC.Tool

  constructor: ->
    @isDrawing = false
    @strokeWidth = 5

  begin: (x, y, lc) ->
    if @isDrawing
      @saveShape()

    @color = lc.primaryColor
    
    x = x - lc.position.x
    y = y - lc.position.y
    @isDrawing = true
    @currentShape = new LC.LinePathShape(this)
    @currentShape.addPoint(x, y)

  continue: (x, y, lc) ->
    return unless @isDrawing
    x = x - lc.position.x
    y = y - lc.position.y
    @currentShape.addPoint(x, y)
    lc.repaint(@currentShape)

  end: (x, y, lc) ->
    return unless @isDrawing
    x = x - lc.position.x
    y = y - lc.position.y
    @isDrawing = false
    @currentShape.addPoint(x, y)
    lc.saveShape(@currentShape)
    @currentShape = undefined

  makePoint: (x, y, lc) -> new LC.Point(x, y, @strokeWidth, @color)


class LC.LiterallyCanvas

  constructor: (@canvas) ->
    @$canvas = $(@canvas)
    @ctx = @canvas.getContext('2d')
    $(@canvas).css('background-color', '#eee')
    @shapes = []
    @isDrawing = false
    @position = {x: 0, y: 0}
    @tool = new LC.Pencil
    @primaryColor = '#000'
    @secondaryColor = '#fff'
    @repaint()

  begin: (x, y) ->
    @tool.begin x, y, this

  continue: (x, y) ->
    @tool.continue x, y, this

  end: (x, y) ->
    @tool.end x, y, this

  saveShape: (shape) ->
    @shapes.push(shape)
    @repaint()

  pan: (x, y) ->
    # Subtract because we are moving the viewport
    @position.x = @position.x - x
    @position.y = @position.y - y

  repaint: (currentShape) ->
    @ctx.clearRect(0, 0, @canvas.width, @canvas.height)
    @ctx.save()
    @ctx.translate @position.x, @position.y
    _.each @shapes, (s) =>
      s.draw(@ctx)
    currentShape.draw(@ctx) if currentShape
    @ctx.restore()

  clear: ->
    @undoStack.push(@shapes)
    @shapes = []
    @repaint()

  undo: -><commit_msg>Don't call code that doesn't exist<commit_after>window.LC = window.LC ? {}


coordsForEvent = ($el, e) ->
  t = e.originalEvent.changedTouches[0]
  p = $el.position()
  return [t.clientX - p.left, t.clientY - p.top]


IS_IOS = /iphone|ipad/i.test(navigator.userAgent)
$.fn.nodoubletapzoom = ->
  if (IS_IOS)
    $(this).bind 'touchstart', (e) ->
      t2 = e.timeStamp
      t1 = $(this).data('lastTouch') || t2
      dt = t2 - t1
      fingers = e.originalEvent.touches.length;
      $(this).data('lastTouch', t2);
      if !dt || dt > 500 || fingers > 1 then return; # not double-tap

      e.preventDefault() # double tap - prevent the zoom
      # also synthesize click events we just swallowed up
      $(this).trigger('click').trigger('click')


$.fn.literallycanvas = ->
  @nodoubletapzoom()

  $c = @find('canvas')
  c = $c.get(0)

  lc = new LC.LiterallyCanvas(c)
  tb = new LC.Toolbar(lc, @find('.toolbar'))

  $c.mousedown (e) =>
    document.onselectstart = -> false # disable selection while dragging
    lc.begin(e.offsetX, e.offsetY)

  $c.mousemove (e) =>
    lc.continue(e.offsetX, e.offsetY)

  $c.mouseup (e) =>
    document.onselectstart = -> true # disable selection while dragging
    lc.end(e.offsetX, e.offsetY)

  $c.mouseout (e) =>
    lc.end(e.offsetX, e.offsetY)

  $c.bind 'touchstart', (e) ->
    e.preventDefault()
    coords = coordsForEvent($c, e)
    if e.originalEvent.touches.length == 1
      lc.begin(coords[0], coords[1])
    else
      lc.continue(coords[0], coords[1])

  $c.bind 'touchmove', (e) ->
    e.preventDefault()
    coords = coordsForEvent($c, e)
    lc.continue(coords[0], coords[1])

  $c.bind 'touchend', (e) ->
    e.preventDefault()
    return unless e.originalEvent.touches.length == 0
    coords = coordsForEvent($c, e)
    lc.end(coords[0], coords[1])

  $c.bind 'touchcancel', (e) ->
    e.preventDefault()
    return unless e.originalEvent.touches.length == 0
    coords = coordsForEvent($c, e)
    lc.end(coords[0], coords[1])

  $(document).keydown (e) ->
    switch e.which
      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.repaint()


class LC.Tool

  begin: (x, y, lc) ->
  continue: (x, y, lc) ->
  end: (x, y, lc) ->


class LC.Pencil extends LC.Tool

  constructor: ->
    @isDrawing = false
    @strokeWidth = 5

  begin: (x, y, lc) ->
    if @isDrawing
      @saveShape()

    @color = lc.primaryColor
    
    x = x - lc.position.x
    y = y - lc.position.y
    @isDrawing = true
    @currentShape = new LC.LinePathShape(this)
    @currentShape.addPoint(x, y)

  continue: (x, y, lc) ->
    return unless @isDrawing
    x = x - lc.position.x
    y = y - lc.position.y
    @currentShape.addPoint(x, y)
    lc.repaint(@currentShape)

  end: (x, y, lc) ->
    return unless @isDrawing
    x = x - lc.position.x
    y = y - lc.position.y
    @isDrawing = false
    @currentShape.addPoint(x, y)
    lc.saveShape(@currentShape)
    @currentShape = undefined

  makePoint: (x, y, lc) -> new LC.Point(x, y, @strokeWidth, @color)


class LC.LiterallyCanvas

  constructor: (@canvas) ->
    @$canvas = $(@canvas)
    @ctx = @canvas.getContext('2d')
    $(@canvas).css('background-color', '#eee')
    @shapes = []
    @isDrawing = false
    @position = {x: 0, y: 0}
    @tool = new LC.Pencil
    @primaryColor = '#000'
    @secondaryColor = '#fff'
    @repaint()

  begin: (x, y) ->
    @tool.begin x, y, this

  continue: (x, y) ->
    @tool.continue x, y, this

  end: (x, y) ->
    @tool.end x, y, this

  saveShape: (shape) ->
    @shapes.push(shape)
    @repaint()

  pan: (x, y) ->
    # Subtract because we are moving the viewport
    @position.x = @position.x - x
    @position.y = @position.y - y

  repaint: (currentShape) ->
    @ctx.clearRect(0, 0, @canvas.width, @canvas.height)
    @ctx.save()
    @ctx.translate @position.x, @position.y
    _.each @shapes, (s) =>
      s.draw(@ctx)
    currentShape.draw(@ctx) if currentShape
    @ctx.restore()

  clear: ->
    @shapes = []
    @repaint()

  undo: ->
{'fraction_non_alphanumeric': 0.16764922623434045, 'fraction_numerical': 0.013264554163596167, 'mean_word_length': 2.5683610867659947, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 73, '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': '2179007', 'n_tokens_mistral': 3267, 'n_tokens_neox': 3078, 'n_words': 722}