UNPKG

@atlaskit/editor-plugin-tasks-and-decisions

Version:

Tasks and decisions plugin for @atlaskit/editor-core

99 lines (94 loc) 4.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.joinAtCut = void 0; var _commands = require("@atlaskit/editor-common/commands"); var _lists = require("@atlaskit/editor-common/lists"); var _transforms = require("@atlaskit/editor-common/transforms"); var _transform = require("@atlaskit/editor-prosemirror/transform"); var _helpers = require("./helpers"); var _paste = require("./utils/paste"); /** * Tries to move the paragraph content near the given position into the taskItem or decisionItem * before it. * * Looks backwards from the given position to find the "cut point" between the last taskItem and the * following paragraph. Then tries to move the content from that paragraph into the taskItem. * * @param $pos Position at the end of, or anywhere in paragraph following, the last taskItem * @see {joinToPreviousListItem} */ var joinAtCut = exports.joinAtCut = function joinAtCut($pos) { return function (state, dispatch) { var $cut = (0, _commands.findCutBefore)($pos); var blockTaskItem = state.schema.nodes.blockTaskItem; var fontSize = state.schema.marks.fontSize; if (!$cut) { return false; } var paragraph = $cut.doc.type.schema.nodes.paragraph; // find the boundary between the taskList and paragraph if ($cut.nodeBefore && (0, _helpers.isActionOrDecisionList)($cut.nodeBefore) && $cut.nodeAfter && $cut.nodeAfter.type === paragraph) { // we'll find the boundary of a taskList // so resolve -1 to find the inside end of the last taskItem var $lastNode = $cut.doc.resolve($cut.pos - 1); // might have deeply nested taskList, keep trying to find it while (!(0, _helpers.isActionOrDecisionItem)($lastNode.parent)) { $lastNode = state.doc.resolve($lastNode.pos - 1); } // grab the structure between the taskItem and the paragraph // note: structure = true in ReplaceAroundStep var slice = state.tr.doc.slice($lastNode.pos, $cut.pos); var from = $lastNode.pos; var to = $cut.pos + $cut.nodeAfter.nodeSize; var gapFrom = $cut.pos + 1; var gapTo = $cut.pos + $cut.nodeAfter.nodeSize - 1; var insert = 0; if (blockTaskItem) { if ($lastNode.parent.type === blockTaskItem) { var childOfLastNode = state.doc.resolve($lastNode.pos - 1); if (childOfLastNode.parent.type === paragraph) { // Recalculate the slice to include the full blockTaskItem structure slice = state.tr.doc.slice(childOfLastNode.pos, $cut.pos); // Need to move one pos in to get to the text node of the paragraph from = $lastNode.pos - 1; } else { // If the blockTaskItem last node is not a paragraph // Expand the gap to include the paragraph being merged gapFrom = $cut.pos; // To get the actual paragraph node gapTo = $cut.pos + $cut.nodeAfter.nodeSize - 1; } } } // collapse the range between end of last taskItem and after the paragraph // with the gap being the paragraph's content (i.e. take that content) // // we pass the structure we found earlier to join the p and taskItem nodes // // see https://prosemirror.net/docs/ref/#transform.ReplaceStep.constructor // see https://prosemirror.net/docs/ref/#transform.ReplaceAroundStep.constructor var tr = state.tr.step(new _transform.ReplaceAroundStep(from, to, gapFrom, gapTo, slice, insert, true)); if (fontSize) { var targetTaskListSmallTextAttrs = (0, _lists.getFirstParagraphBlockMarkAttrs)($cut.nodeBefore, fontSize); var followingListPos = $cut.pos + $cut.nodeAfter.nodeSize; var followingListNode = state.doc.resolve(followingListPos).nodeAfter; if (followingListNode && (0, _transforms.isTaskList)(followingListNode.type)) { var normalizedListNode = (0, _paste.normalizeNodeForTaskTextSize)(followingListNode, state.schema, targetTaskListSmallTextAttrs)[0]; if (normalizedListNode && normalizedListNode !== followingListNode) { var mappedFollowingListPos = tr.mapping.map(followingListPos); var currentFollowingListNode = tr.doc.nodeAt(mappedFollowingListPos); if (currentFollowingListNode) { tr = tr.replaceWith(mappedFollowingListPos, mappedFollowingListPos + currentFollowingListNode.nodeSize, normalizedListNode); } } } } if (dispatch) { dispatch(tr); } return true; } return false; }; };