import { Fragment } from '@atlaskit/editor-prosemirror/model';
import { TextSelection } from '@atlaskit/editor-prosemirror/state';
export function insertNewLine() {
  return function (state, dispatch) {
    var $from = state.selection.$from;
    var parent = $from.parent;
    var hardBreak = state.schema.nodes.hardBreak;
    if (hardBreak) {
      var hardBreakNode = hardBreak.createChecked();
      if (parent && parent.type.validContent(Fragment.from(hardBreakNode))) {
        if (dispatch) {
          dispatch(state.tr.replaceSelectionWith(hardBreakNode, false));
        }
        return true;
      }
    }
    if (state.selection instanceof TextSelection) {
      if (dispatch) {
        dispatch(state.tr.insertText('\n'));
      }
      return true;
    }
    return false;
  };
}