<commit_before>var dom = require('@nymag/dom'),
  _ = require('lodash'),
  kilnServices = window.kiln.services,
  references = kilnServices.references,
  render = kilnServices.render,
  focus = kilnServices.focus,
  select = kilnServices.select,
  progress = kilnServices.progress,
  select = kilnServices.select,
  edit = kilnServices.edit,
  spaceName = 'clay-space';


function newComponentInLogic(componentName) {
  return edit.createComponent(componentName)
    .then(function(component) {
      return edit.createComponent('space-logic', {
        embededComponent: {
          ref: component._ref,
          path: null,
          data: {
            _ref: component._ref
          }
        }
      });
    });
}

/**
 * [wrapInLogic description]
 * @param  {[type]} clickedComponent [description]
 * @param  {[type]} options          [description]
 * @param  {[type]} parent           [description]
 * @return {[type]}                  [description]
 */
function wrapInLogic(clickedComponent, options, parent) {
  return Promise.all([
    edit.createComponent('space-logic', { embededComponent: options }),
    edit.removeFromParentList({ el: clickedComponent, ref: options.ref, parentField: parent.path, parentRef: parent.ref })
  ]).then(function(resp) {
    return resp[0];
  });
}

/**
 * [addInSpace description]
 * @param {[type]} options        [description]
 * @param {[type]} parent         [description]
 * @param {[type]} logicComponent [description]
 */
function addInSpace(options, parent, logicComponent) {
  return Promise.all([edit.createComponent('clay-space', { content: [{ _ref: logicComponent[references.referenceProperty] }] }), edit.getData(parent.ref)])
    .then(function(promises) {
      var res = promises[0],
        parentRes = promises[1],
        newRef = res._ref,
        args = {
          ref: newRef,
          parentField: parent.path,
          parentRef: parent.ref,
          prevRef: 'localhost/daily/intelligencer/components/top-stories-desktop/instances/di'
        };

      return edit.addToParentList(args)
        .then(function(newEl) {
          return render.addComponentsHandlers(newEl).then(function() {
            // focus.unfocus();
            // select.unselect();
            return select.select(newEl);
          });
        });
    });
}

function createSpace(options, parent, e) {
  if (!confirmMakeSpace()) {
    return null;
  }

  var parentEl = dom.closest(e.target, '[data-uri]');

  return wrapInLogic(parentEl, options, parent)
    .then(addInSpace.bind(null, options, parent))
}

function confirmMakeSpace() {
  return window.confirm('Do you really want to make a new Space?');
}

module.exports.createSpace = createSpace;
module.exports.newComponentInLogic = newComponentInLogic;
<commit_msg>Adding focus and selecting back in<commit_after>var dom = require('@nymag/dom'),
  _ = require('lodash'),
  kilnServices = window.kiln.services,
  references = kilnServices.references,
  render = kilnServices.render,
  focus = kilnServices.focus,
  select = kilnServices.select,
  progress = kilnServices.progress,
  select = kilnServices.select,
  edit = kilnServices.edit,
  spaceName = 'clay-space';


function newComponentInLogic(componentName) {
  return edit.createComponent(componentName)
    .then(function(component) {
      return edit.createComponent('space-logic', {
        embededComponent: {
          ref: component._ref,
          path: null,
          data: {
            _ref: component._ref
          }
        }
      });
    });
}

/**
 * [wrapInLogic description]
 * @param  {[type]} clickedComponent [description]
 * @param  {[type]} options          [description]
 * @param  {[type]} parent           [description]
 * @return {[type]}                  [description]
 */
function wrapInLogic(clickedComponent, options, parent) {
  return Promise.all([
    edit.createComponent('space-logic', { embededComponent: options }),
    edit.removeFromParentList({ el: clickedComponent, ref: options.ref, parentField: parent.path, parentRef: parent.ref })
  ]).then(function(resp) {
    return resp[0];
  });
}

/**
 * [addInSpace description]
 * @param {[type]} options        [description]
 * @param {[type]} parent         [description]
 * @param {[type]} logicComponent [description]
 */
function addInSpace(options, parent, logicComponent) {
  return Promise.all([edit.createComponent('clay-space', { content: [{ _ref: logicComponent[references.referenceProperty] }] }), edit.getData(parent.ref)])
    .then(function(promises) {
      var res = promises[0],
        parentRes = promises[1],
        newRef = res._ref,
        args = {
          ref: newRef,
          parentField: parent.path,
          parentRef: parent.ref,
          prevRef: 'localhost/daily/intelligencer/components/top-stories-desktop/instances/di'
        };

      return edit.addToParentList(args)
        .then(function(newEl) {
          return render.addComponentsHandlers(newEl).then(function() {
            focus.unfocus();
            select.unselect();
            return select.select(newEl);
          });
        });
    });
}

function createSpace(options, parent, e) {
  if (!confirmMakeSpace()) {
    return null;
  }

  var parentEl = dom.closest(e.target, '[data-uri]');

  return wrapInLogic(parentEl, options, parent)
    .then(addInSpace.bind(null, options, parent))
}

function confirmMakeSpace() {
  return window.confirm('Do you really want to make a new Space?');
}

module.exports.createSpace = createSpace;
module.exports.newComponentInLogic = newComponentInLogic;

{'fraction_non_alphanumeric': 0.13460500269929818, 'fraction_numerical': 0.001079719272989023, 'mean_word_length': 3.0510204081632653, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '328753', 'n_tokens_mistral': 1655, 'n_tokens_neox': 1637, 'n_words': 325}