<commit_before>"use strict";

var selectionRange = require('selection-range');

var _ = require('./lodash');
var utils = require('./utils');
var config = require('./config');

var EventBus = require('./event-bus');
var Blocks = require('./blocks');

var Dom = require("./packages/dom");

const BLOCK_OPTION_KEYS =
  ['convertToMarkdown', 'convertFromMarkdown', 'formatBar'];

var BlockManager = function(SirTrevor) {
  this.options = SirTrevor.options;
  this.blockOptions = BLOCK_OPTION_KEYS.reduce(function(acc, key) {
    acc[key] = SirTrevor.options[key];
    return acc;
  }, {});
  this.instance_scope = SirTrevor.ID;
  this.mediator = SirTrevor.mediator;

  // REFACTOR: this is a hack until I can focus on reworking the blockmanager
  this.wrapper = SirTrevor.wrapper;

  this.blocks = [];
  this.blockCounts = {};
  this.blockTypes = [];

  this._setBlocksTypes();

  this._setRequired();
  this._bindMediatedEvents();

  this.initialize();
};

Object.assign(BlockManager.prototype, require('./function-bind'), require('./mediated-events'), require('./events'), {

  eventNamespace: 'block',

  mediatedEvents: {
    'create': 'createBlock',
    'createBefore': 'createBlockBefore',
    'remove': 'removeBlock',
    'rerender': 'rerenderBlock',
    'replace': 'replaceBlock',
    'focusPrevious': 'focusPreviousBlock',
    'focusNext': 'focusNextBlock'
  },

  initialize: function() {},

  createBlock: function(type, data, previousSibling, options) {
    type = utils.classify(type);

    // Run validations
    if (!this.canCreateBlock(type)) { return; }

    var block = new Blocks[type](data, this.instance_scope, this.mediator,
                                 this.blockOptions);
    this.blocks.push(block);

    this._incrementBlockTypeCount(type);
    this.renderBlock(block, previousSibling);

    if (options && options.autoFocus) {
      block.focus();
    }

    this.triggerBlockCountUpdate();
    this.mediator.trigger('block:limitReached', this.blockLimitReached());

    EventBus.trigger(data ? "block:create:existing" : "block:create:new", block);
    utils.log("Block created of type " + type);
  },

  createBlockBefore: function(type, data, nextBlock, options) {
    type = utils.classify(type);

    // Run validations
    if (!this.canCreateBlock(type)) { return; }

    var block = new Blocks[type](data, this.instance_scope, this.mediator,
                                 this.blockOptions);
    this.blocks.push(block);

    this._incrementBlockTypeCount(type);

    const previousBlock = this.getPreviousBlock(nextBlock);
    if (previousBlock) {
      this.renderBlock(block, previousBlock.el);
    } else {
      this.renderBlock(block, this.wrapper.querySelector(".st-top-controls"));
    }

    if (options && options.autoFocus) {
      block.focus();
    }

    this.triggerBlockCountUpdate();
    this.mediator.trigger('block:limitReached', this.blockLimitReached());

    EventBus.trigger(data ? "block:create:existing" : "block:create:new", block);
    utils.log("Block created of type " + type);
  },<commit_msg>Add block:created event<commit_after>"use strict";

var selectionRange = require('selection-range');

var _ = require('./lodash');
var utils = require('./utils');
var config = require('./config');

var EventBus = require('./event-bus');
var Blocks = require('./blocks');

var Dom = require("./packages/dom");

const BLOCK_OPTION_KEYS =
  ['convertToMarkdown', 'convertFromMarkdown', 'formatBar'];

var BlockManager = function(SirTrevor) {
  this.options = SirTrevor.options;
  this.blockOptions = BLOCK_OPTION_KEYS.reduce(function(acc, key) {
    acc[key] = SirTrevor.options[key];
    return acc;
  }, {});
  this.instance_scope = SirTrevor.ID;
  this.mediator = SirTrevor.mediator;

  // REFACTOR: this is a hack until I can focus on reworking the blockmanager
  this.wrapper = SirTrevor.wrapper;

  this.blocks = [];
  this.blockCounts = {};
  this.blockTypes = [];

  this._setBlocksTypes();

  this._setRequired();
  this._bindMediatedEvents();

  this.initialize();
};

Object.assign(BlockManager.prototype, require('./function-bind'), require('./mediated-events'), require('./events'), {

  eventNamespace: 'block',

  mediatedEvents: {
    'create': 'createBlock',
    'createBefore': 'createBlockBefore',
    'remove': 'removeBlock',
    'rerender': 'rerenderBlock',
    'replace': 'replaceBlock',
    'focusPrevious': 'focusPreviousBlock',
    'focusNext': 'focusNextBlock'
  },

  initialize: function() {},

  createBlock: function(type, data, previousSibling, options) {
    type = utils.classify(type);

    // Run validations
    if (!this.canCreateBlock(type)) { return; }

    var block = new Blocks[type](data, this.instance_scope, this.mediator,
                                 this.blockOptions);
    this.blocks.push(block);

    this._incrementBlockTypeCount(type);
    this.renderBlock(block, previousSibling);

    if (options && options.autoFocus) {
      block.focus();
    }

    this.triggerBlockCountUpdate();
    this.mediator.trigger('block:limitReached', this.blockLimitReached());
    this.mediator.trigger('block:created', block);

    EventBus.trigger(data ? "block:create:existing" : "block:create:new", block);
    utils.log("Block created of type " + type);
  },

  createBlockBefore: function(type, data, nextBlock, options) {
    type = utils.classify(type);

    // Run validations
    if (!this.canCreateBlock(type)) { return; }

    var block = new Blocks[type](data, this.instance_scope, this.mediator,
                                 this.blockOptions);
    this.blocks.push(block);

    this._incrementBlockTypeCount(type);

    const previousBlock = this.getPreviousBlock(nextBlock);
    if (previousBlock) {
      this.renderBlock(block, previousBlock.el);
    } else {
      this.renderBlock(block, this.wrapper.querySelector(".st-top-controls"));
    }

    if (options && options.autoFocus) {
      block.focus();
    }

    this.triggerBlockCountUpdate();
    this.mediator.trigger('block:limitReached', this.blockLimitReached());
    this.mediator.trigger('block:created', block);

    EventBus.trigger(data ? "block:create:existing" : "block:create:new", block);
    utils.log("Block created of type " + type);
  },
{'fraction_non_alphanumeric': 0.15136436597110756, 'fraction_numerical': 0.0, 'mean_word_length': 3.7456207159177457, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7180489', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1864, 'n_words': 354}