<commit_before>Scribe = require('../scribe')


initButtonFormats = ->
  _.each(Scribe.Toolbar.BUTTON_FORMATS, (format) =>
    button = @container.querySelector(".#{format}")
    return unless button?
    button.addEventListener('click', =>
      value = !button.classList.contains('active')
      @editor.selection.format(format, value, false)
      this.emit(Scribe.Toolbar.events.FORMAT, format, value)
    )
  )

initLinkFormat = ->
  linkButton = @container.querySelector('.link')
  return unless linkButton?
  linkButton.addEventListener('click', =>
    value = false
    if !linkButton.classList.contains('active')
      range = @editor.selection.getRange()
      value = range.getText()
    @editor.selection.format('link', value, false)
    this.emit(Scribe.Toolbar.events.FORMAT, 'link', value)
  )

initSelectFormats = ->
  _.each(Scribe.Toolbar.SELECT_FORMATS, (format) =>
    select = @container.querySelector(".#{format}")
    return unless select?
    select.addEventListener('change', =>
      value = select.options[select.selectedIndex].value
      @editor.selection.format(format, value, false)
      this.emit(Scribe.Toolbar.events.FORMAT, format, value)
    )
  )

initSelectionListener = ->
  @editor.on(Scribe.Editor.events.SELECTION_CHANGE, (selection) =>
    formats = selection.getFormats()
    _.each(@container.querySelectorAll('.active'), (button) =>
      button.classList.remove('active')
    )
    _.each(formats, (value, key) =>
      if value?
        elem = @container.querySelector(".#{key}")
        return unless elem?
        if elem.tagName == 'SELECT'
          value = '' if _.isArray(value)
          elem.value = value
        else
          elem.classList.add('active')
    )
  )


class Scribe.Toolbar extends EventEmitter2
  @BUTTON_FORMATS: ['bold', 'italic', 'strike', 'underline', 'bullet', 'list', 'indent', 'outdent']
  @SELECT_FORMATS: ['background', 'color', 'family', 'size']

  @events:
    FORMAT: 'format'

  constructor: (@container, @editor) ->
    @container = document.getElementById(@container) if _.isString(@container)
    initButtonFormats.call(this)
    initLinkFormat.call(this)
    initSelectFormats.call(this)
    initSelectionListener.call(this)


module.exports = Scribe<commit_msg>focus editor after selecting toolbar element<commit_after>Scribe = require('../scribe')


initButtonFormats = ->
  _.each(Scribe.Toolbar.BUTTON_FORMATS, (format) =>
    button = @container.querySelector(".#{format}")
    return unless button?
    button.addEventListener('click', =>
      value = !button.classList.contains('active')
      @editor.selection.format(format, value, false)
      this.emit(Scribe.Toolbar.events.FORMAT, format, value)
    )
  )

initLinkFormat = ->
  linkButton = @container.querySelector('.link')
  return unless linkButton?
  linkButton.addEventListener('click', =>
    value = false
    if !linkButton.classList.contains('active')
      range = @editor.selection.getRange()
      value = range.getText()
    @editor.selection.format('link', value, false)
    this.emit(Scribe.Toolbar.events.FORMAT, 'link', value)
  )

initSelectFormats = ->
  _.each(Scribe.Toolbar.SELECT_FORMATS, (format) =>
    select = @container.querySelector(".#{format}")
    return unless select?
    select.addEventListener('change', =>
      value = select.options[select.selectedIndex].value
      @editor.selection.format(format, value, false)
      this.emit(Scribe.Toolbar.events.FORMAT, format, value)
    )
  )

initSelectionListener = ->
  @editor.on(Scribe.Editor.events.SELECTION_CHANGE, (selection) =>
    formats = selection.getFormats()
    _.each(@container.querySelectorAll('.active'), (button) =>
      button.classList.remove('active')
    )
    _.each(formats, (value, key) =>
      if value?
        elem = @container.querySelector(".#{key}")
        return unless elem?
        if elem.tagName == 'SELECT'
          value = '' if _.isArray(value)
          elem.value = value
        else
          elem.classList.add('active')
    )
  )


class Scribe.Toolbar extends EventEmitter2
  @BUTTON_FORMATS: ['bold', 'italic', 'strike', 'underline', 'bullet', 'list', 'indent', 'outdent']
  @SELECT_FORMATS: ['background', 'color', 'family', 'size']

  @events:
    FORMAT: 'format'

  constructor: (@container, @editor) ->
    @container = document.getElementById(@container) if _.isString(@container)
    initButtonFormats.call(this)
    initLinkFormat.call(this)
    initSelectFormats.call(this)
    initSelectionListener.call(this)
    this.on(Scribe.Toolbar.events.FORMAT, =>
      @editor.root.focus()
    )

module.exports = Scribe
{'fraction_non_alphanumeric': 0.14997832683138274, 'fraction_numerical': 0.00043346337234503684, 'mean_word_length': 3.5738354806739348, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 30, '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': '2236953', 'n_tokens_mistral': 1358, 'n_tokens_neox': 1398, 'n_words': 244}