<commit_before>Scribe = require('./scribe')
Tandem = require('tandem-core')


class Scribe.PasteManager
  constructor: (@editor) ->
    @container = @editor.root.ownerDocument.createElement('div')
    @container.classList.add('paste-container')
    @container.setAttribute('contenteditable', true)
    @editor.renderer.addStyles(
      '.paste-container':
        'left': '-10000px'
        'position': 'fixed'
        'top': '50%'
    )
    @editor.renderer.addContainer(@container)
    this.initListeners()

  initListeners: ->
    @editor.root.addEventListener('paste', =>
      oldDocLength = @editor.getLength()
      range = @editor.getSelection()
      return unless range?
      @container.innerHTML = ""
      @container.focus()
      _.defer( =>
        doc = new Scribe.Document(@container)
        # Need to remove trailing newline so paste is inline
        lastLine = doc.lines.last
        lastLine.deleteText(lastLine.length - 1, 1)
        delta = doc.toDelta()
        lengthAdded = delta.endLength
        delta.ops.unshift(new Tandem.RetainOp(0, range.start.index)) if range.start.index > 0
        delta.ops.push(new Tandem.RetainOp(range.end.index, oldDocLength)) if range.end.index < oldDocLength
        delta.endLength += (@editor.getLength() - (range.end.index - range.start.index))
        delta.startLength = oldDocLength
        @editor.applyDelta(delta, false)
        @editor.setSelection(new Scribe.Range(@editor, range.start.index + lengthAdded, range.start.index + lengthAdded))
      )
    )


module.exports = Scribe<commit_msg>use new applyDelta api<commit_after>Scribe = require('./scribe')
Tandem = require('tandem-core')


class Scribe.PasteManager
  constructor: (@editor) ->
    @container = @editor.root.ownerDocument.createElement('div')
    @container.classList.add('paste-container')
    @container.setAttribute('contenteditable', true)
    @editor.renderer.addStyles(
      '.paste-container':
        'left': '-10000px'
        'position': 'fixed'
        'top': '50%'
    )
    @editor.renderer.addContainer(@container)
    this.initListeners()

  initListeners: ->
    @editor.root.addEventListener('paste', =>
      oldDocLength = @editor.getLength()
      range = @editor.getSelection()
      return unless range?
      @container.innerHTML = ""
      @container.focus()
      _.defer( =>
        doc = new Scribe.Document(@container)
        # Need to remove trailing newline so paste is inline
        lastLine = doc.lines.last
        lastLine.deleteText(lastLine.length - 1, 1)
        delta = doc.toDelta()
        lengthAdded = delta.endLength
        delta.ops.unshift(new Tandem.RetainOp(0, range.start.index)) if range.start.index > 0
        delta.ops.push(new Tandem.RetainOp(range.end.index, oldDocLength)) if range.end.index < oldDocLength
        delta.endLength += (@editor.getLength() - (range.end.index - range.start.index))
        delta.startLength = oldDocLength
        @editor.applyDelta(delta, { source: 'user' })
        @editor.setSelection(new Scribe.Range(@editor, range.start.index + lengthAdded, range.start.index + lengthAdded))
      )
    )


module.exports = Scribe
{'fraction_non_alphanumeric': 0.13555555555555557, 'fraction_numerical': 0.006984126984126984, 'mean_word_length': 2.983565107458913, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '6478263', 'n_tokens_mistral': 954, 'n_tokens_neox': 931, 'n_words': 171}