<commit_before>'use strict';

var paragraphs = require('./paragraphs'),
    sections = require('./sections')

function viewPlugin (Compose) {
  var setImmediate = Compose.require('setImmediate'),
      getChildren = Compose.require('getChildren'),
      Converter = Compose.require('converter'),
      Delta = Compose.require('delta'),
      ParagraphOperations,
      SectionOperations

  // Hackish. Get the paragraph and section operations without polluting
  // the module system.
  Compose.use(paragraphs)
  ParagraphOperations = Compose.require(paragraphs.provided)
  Compose.disable(paragraphs.provided)

  Compose.use(sections)
  SectionOperations = Compose.require(sections.provided)
  Compose.disable(sections.provided)

  function View () {
    this._modified = {}
    this._queue = []
    this._rendering = false

    this.paragraphs = []
    this.sections = []
  }

  /**
   * isSectionStart(index) determines if a sections starts at the given
   * index.
   *
   * @param {Int >= 0} index
   * @return {Boolean}
   */
  View.prototype.isSectionStart = function (index) {
    var i

    for (i = 0; i < this.sections.length; i += 1) {
      if (this.sections[i].start === index)
        return true
    }

    return false
  }

  View.prototype.markModified = function (index) {
    this._modified[index] = 1
  }

  View.prototype.sync = function () {
    var children = getChildren(),
        child

    Object.keys(this._modified).forEach(function (index) {
      var state

      child = children[index]

      if (!child) return

      state = Converter.toParagraph(child)

      if (!state.equals(this.paragraphs[index]))
        this.paragraphs[index] = state
    }, this)

    this._modified = {}
  }

  View.prototype.render = function (deltas) {
    if (!Array.isArray(deltas))
      this._queue.push(deltas)
    else
      this._queue.push.apply(this._queue, deltas)

    if (!this._rendering)
      this._rendering = setImmediate(function () {
        this._render()
        this._rendering = false
      }.bind(this))
  }

  View.prototype._render = function () {
    var action,
        delta,
        i

    Delta.reduce(this._queue)

    // TODO: cache result of getChildren() somewhere?<commit_msg>richMode/view: trim trailing <br>s when syncing.<commit_after>'use strict';

var paragraphs = require('./paragraphs'),
    sections = require('./sections')

function viewPlugin (Compose) {
  var setImmediate = Compose.require('setImmediate'),
      getChildren = Compose.require('getChildren'),
      Converter = Compose.require('converter'),
      Delta = Compose.require('delta'),
      ParagraphOperations,
      SectionOperations

  // Hackish. Get the paragraph and section operations without polluting
  // the module system.
  Compose.use(paragraphs)
  ParagraphOperations = Compose.require(paragraphs.provided)
  Compose.disable(paragraphs.provided)

  Compose.use(sections)
  SectionOperations = Compose.require(sections.provided)
  Compose.disable(sections.provided)

  function View () {
    this._modified = {}
    this._queue = []
    this._rendering = false

    this.paragraphs = []
    this.sections = []
  }

  /**
   * isSectionStart(index) determines if a sections starts at the given
   * index.
   *
   * @param {Int >= 0} index
   * @return {Boolean}
   */
  View.prototype.isSectionStart = function (index) {
    var i

    for (i = 0; i < this.sections.length; i += 1) {
      if (this.sections[i].start === index)
        return true
    }

    return false
  }

  View.prototype.markModified = function (index) {
    this._modified[index] = 1
  }

  View.prototype.sync = function () {
    var children = getChildren(),
        child

    Object.keys(this._modified).forEach(function (index) {
      var state

      child = children[index]

      if (!child) return

      state = Converter.toParagraph(child)
      state = state.replace(/([^^\n])\n$/, function (match, $0) {
        return $0
      })

      if (!state.equals(this.paragraphs[index]))
        this.paragraphs[index] = state
    }, this)

    this._modified = {}
  }

  View.prototype.render = function (deltas) {
    if (!Array.isArray(deltas))
      this._queue.push(deltas)
    else
      this._queue.push.apply(this._queue, deltas)

    if (!this._rendering)
      this._rendering = setImmediate(function () {
        this._render()
        this._rendering = false
      }.bind(this))
  }

  View.prototype._render = function () {
    var action,
        delta,
        i

    Delta.reduce(this._queue)

    // TODO: cache result of getChildren() somewhere?
{'fraction_non_alphanumeric': 0.12701876909646442, 'fraction_numerical': 0.002182453077258839, 'mean_word_length': 2.7412244897959184, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '3353482', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1358, 'n_words': 295}