@atlaskit/editor-plugin-expand
Version:
Expand plugin for @atlaskit/editor-core
75 lines • 3.08 kB
JavaScript
import { Selection } from '@atlaskit/editor-prosemirror/state';
import { getExpandTitleInput } from '../utils';
/**
* The empty paragraph holding the caret, when its previous sibling is a collapsed expand or nested
* expand and it is not the last child of its container. The last child is kept so there is always a
* line to type in after the expand.
*/
var getEmptyLineAfterCollapsedExpand = function getEmptyLineAfterCollapsedExpand(state, isCollapsed) {
var $from = state.selection.$from;
var _state$schema$nodes = state.schema.nodes,
paragraph = _state$schema$nodes.paragraph,
expand = _state$schema$nodes.expand,
nestedExpand = _state$schema$nodes.nestedExpand;
var textblock = $from.parent;
if (textblock.type !== paragraph || textblock.content.size > 0) {
return undefined;
}
var container = $from.node(-1);
var index = $from.index(-1);
var isLastChild = index === container.childCount - 1;
if (index === 0 || isLastChild) {
return undefined;
}
var previousSibling = container.child(index - 1);
var isExpand = previousSibling.type === expand || previousSibling.type === nestedExpand;
if (!isExpand || !isCollapsed(previousSibling)) {
return undefined;
}
var lineStart = $from.before();
return {
expandContentStart: lineStart - previousSibling.nodeSize + 1,
lineStart: lineStart,
lineEnd: $from.after()
};
};
/**
* Backspace in an empty line directly after a collapsed expand: deletes the line and moves focus to
* the end of the expand title, in one transaction. Returns false, without changing anything, when
* the caret is anywhere else or the title input cannot be found, so other Backspace handling runs.
*/
export var deleteEmptyLineAfterCollapsedExpand = function deleteEmptyLineAfterCollapsedExpand(_ref) {
var focusTitle = _ref.focusTitle,
isCollapsed = _ref.isCollapsed;
return function (state, dispatch, editorView) {
if (!editorView || !state.selection.empty) {
return false;
}
var emptyLine = getEmptyLineAfterCollapsedExpand(state, isCollapsed);
if (!emptyLine) {
return false;
}
var titleInput = getExpandTitleInput(editorView, emptyLine.expandContentStart);
if (!titleInput) {
return false;
}
if (!dispatch) {
return true;
}
var tr = state.tr.delete(emptyLine.lineStart, emptyLine.lineEnd);
var selectionInsideExpand = Selection.findFrom(tr.doc.resolve(emptyLine.expandContentStart), 1, true);
if (selectionInsideExpand) {
tr.setSelection(selectionInsideExpand);
}
dispatch(tr);
var titleInputAfterDelete = getExpandTitleInput(editorView, emptyLine.expandContentStart);
if (!(titleInputAfterDelete !== null && titleInputAfterDelete !== void 0 && titleInputAfterDelete.isConnected)) {
focusTitle(emptyLine.expandContentStart)(editorView.state, dispatch, editorView);
return true;
}
titleInputAfterDelete.focus();
var titleEnd = titleInputAfterDelete.value.length;
titleInputAfterDelete.setSelectionRange(titleEnd, titleEnd);
return true;
};
};