@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
61 lines (56 loc) • 2.32 kB
JavaScript
import { NodeSelection, TextSelection } from '@atlaskit/editor-prosemirror/state';
import { hasParentNodeOfType } from '@atlaskit/editor-prosemirror/utils';
export var insertBlock = function insertBlock(state, nodeType, start, end, attrs
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/max-params
) {
// To ensure that match is done after HardBreak.
var _state$schema$nodes = state.schema.nodes,
hardBreak = _state$schema$nodes.hardBreak,
codeBlock = _state$schema$nodes.codeBlock,
listItem = _state$schema$nodes.listItem;
var $pos = state.doc.resolve(start);
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if ($pos.nodeAfter.type !== hardBreak) {
return null;
}
// To ensure no nesting is done. (unless we're inserting a codeBlock inside lists)
if ($pos.depth > 1 && !(nodeType === codeBlock && hasParentNodeOfType(listItem)(state.selection))) {
return null;
}
// Split at the start of autoformatting and delete formatting characters.
var tr = state.tr.delete(start, end).split(start);
var currentNode = tr.doc.nodeAt(start + 1);
// If node has more content split at the end of autoformatting.
var nodeHasMoreContent = false;
tr.doc.nodesBetween(start, start + currentNode.nodeSize, function (node, pos) {
if (!nodeHasMoreContent && node.type === hardBreak) {
nodeHasMoreContent = true;
tr = tr.split(pos + 1).delete(pos, pos + 1);
}
});
if (nodeHasMoreContent) {
currentNode = tr.doc.nodeAt(start + 1);
}
// Create new node and fill with content of current node.
var _state$schema$nodes2 = state.schema.nodes,
blockquote = _state$schema$nodes2.blockquote,
paragraph = _state$schema$nodes2.paragraph;
var content;
var depth;
if (nodeType === blockquote) {
depth = 3;
content = [paragraph.create({}, currentNode.content)];
} else {
depth = 2;
content = currentNode.content;
}
var newNode = nodeType.create(attrs, content);
// Add new node.
tr = tr.setSelection(new NodeSelection(tr.doc.resolve(start + 1)))
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
.replaceSelectionWith(newNode).setSelection(new TextSelection(tr.doc.resolve(start + depth)));
return tr;
};