UNPKG

@atlaskit/editor-plugin-expand

Version:

Expand plugin for @atlaskit/editor-core

81 lines (80 loc) • 3.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteEmptyLineAfterCollapsedExpand = void 0; var _state = require("@atlaskit/editor-prosemirror/state"); var _utils = require("../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. */ var deleteEmptyLineAfterCollapsedExpand = exports.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 = (0, _utils.getExpandTitleInput)(editorView, emptyLine.expandContentStart); if (!titleInput) { return false; } if (!dispatch) { return true; } var tr = state.tr.delete(emptyLine.lineStart, emptyLine.lineEnd); var selectionInsideExpand = _state.Selection.findFrom(tr.doc.resolve(emptyLine.expandContentStart), 1, true); if (selectionInsideExpand) { tr.setSelection(selectionInsideExpand); } dispatch(tr); var titleInputAfterDelete = (0, _utils.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; }; };