<commit_before>_        = require('lodash')
DOM      = require('../dom')
Line     = require('../line')


class Keyboard
  @hotkeys:
    BOLD:       { key: 'B',          meta: true }
    INDENT:     { key: DOM.KEYS.TAB, shift: false }
    ITALIC:     { key: 'I',          meta: true }
    OUTDENT:    { key: DOM.KEYS.TAB, shift: true }
    UNDERLINE:  { key: 'U',          meta: true }
    UNDO:       { key: 'Z',          meta: true, shift: false }
    REDO:       { key: 'Z',          meta: true, shift: true }

  @NAVIGATION: [DOM.KEYS.UP, DOM.KEYS.DOWN, DOM.KEYS.LEFT, DOM.KEYS.RIGHT]

  constructor: (@quill, @editorContainer, options) ->
    @hotkeys = {}
    this._initListeners()
    this._initHotkeys()
    this._initDeletes()

  addHotkey: (hotkey, callback) ->
    hotkey = if _.isObject(hotkey) then _.clone(hotkey) else { key: hotkey }
    hotkey.key = hotkey.key.toUpperCase().charCodeAt(0) if _.isString(hotkey.key)
    hotkey.callback = callback
    @hotkeys[hotkey.key] ?= []
    @hotkeys[hotkey.key].push(hotkey)

  indent: (selection, increment) ->
    lines = selection.getLines()
    applyIndent = (line, format) =>
      if increment
        indent = if _.isNumber(line.formats[format]) then line.formats[format] else (if line.formats[format] then 1 else 0)
        indent += increment
        indent = Math.min(Math.max(indent, Line.MIN_INDENT), Line.MAX_INDENT)
      else
        indent = false
      index = Position.getIndex(line.node, 0)
      @quill.formatText(index, 0, format, indent)

    _.each(lines, (line) =>
      if line.formats.bullet?
        applyIndent(line, 'bullet')
      else if line.formats.list?
        applyIndent(line, 'list')
      else
        applyIndent(line, 'indent')
    )

  toggleFormat: (range, format) ->
    formats = range.getFormats()
    value = !formats[format]
    @quill.formatText(range, format, value, { source: 'user' })

  _initDeletes: ->
    _.each([DOM.KEYS.DELETE, DOM.KEYS.BACKSPACE], (key) =>
      this.addHotkey(key, =>
        # Prevent deleting if editor is already blank
        return @quill.getLength() > 0<commit_msg>forced trailing newline is back<commit_after>_        = require('lodash')
DOM      = require('../dom')
Line     = require('../line')


class Keyboard
  @hotkeys:
    BOLD:       { key: 'B',          meta: true }
    INDENT:     { key: DOM.KEYS.TAB, shift: false }
    ITALIC:     { key: 'I',          meta: true }
    OUTDENT:    { key: DOM.KEYS.TAB, shift: true }
    UNDERLINE:  { key: 'U',          meta: true }
    UNDO:       { key: 'Z',          meta: true, shift: false }
    REDO:       { key: 'Z',          meta: true, shift: true }

  @NAVIGATION: [DOM.KEYS.UP, DOM.KEYS.DOWN, DOM.KEYS.LEFT, DOM.KEYS.RIGHT]

  constructor: (@quill, @editorContainer, options) ->
    @hotkeys = {}
    this._initListeners()
    this._initHotkeys()
    this._initDeletes()

  addHotkey: (hotkey, callback) ->
    hotkey = if _.isObject(hotkey) then _.clone(hotkey) else { key: hotkey }
    hotkey.key = hotkey.key.toUpperCase().charCodeAt(0) if _.isString(hotkey.key)
    hotkey.callback = callback
    @hotkeys[hotkey.key] ?= []
    @hotkeys[hotkey.key].push(hotkey)

  indent: (selection, increment) ->
    lines = selection.getLines()
    applyIndent = (line, format) =>
      if increment
        indent = if _.isNumber(line.formats[format]) then line.formats[format] else (if line.formats[format] then 1 else 0)
        indent += increment
        indent = Math.min(Math.max(indent, Line.MIN_INDENT), Line.MAX_INDENT)
      else
        indent = false
      index = Position.getIndex(line.node, 0)
      @quill.formatText(index, 0, format, indent)

    _.each(lines, (line) =>
      if line.formats.bullet?
        applyIndent(line, 'bullet')
      else if line.formats.list?
        applyIndent(line, 'list')
      else
        applyIndent(line, 'indent')
    )

  toggleFormat: (range, format) ->
    formats = range.getFormats()
    value = !formats[format]
    @quill.formatText(range, format, value, { source: 'user' })

  _initDeletes: ->
    _.each([DOM.KEYS.DELETE, DOM.KEYS.BACKSPACE], (key) =>
      this.addHotkey(key, =>
        # Prevent deleting if editor is already blank
        return @quill.getLength() > 1
{'fraction_non_alphanumeric': 0.14678030303030304, 'fraction_numerical': 0.002840909090909091, 'mean_word_length': 2.500414250207125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '3487873', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1427, 'n_words': 321}