UNPKG

@atlaskit/editor-plugin-list

Version:

List plugin for @atlaskit/editor-core

87 lines (85 loc) 3.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.selectionContainsList = exports.isWrappingPossible = exports.isPosInsideParagraph = exports.isPosInsideList = exports.isInsideTableCell = exports.isInsideListItem = exports.createListNodeRange = exports.canJoinToPreviousListItem = void 0; var _selection = require("@atlaskit/editor-common/selection"); var _utils = require("@atlaskit/editor-common/utils"); var _transform = require("@atlaskit/editor-prosemirror/transform"); var _utils2 = require("@atlaskit/editor-prosemirror/utils"); var isPosInsideParagraph = exports.isPosInsideParagraph = function isPosInsideParagraph($pos) { return $pos.parent.type.name === 'paragraph'; }; var isPosInsideList = exports.isPosInsideList = function isPosInsideList($pos) { var posGrandParent = $pos.node(-1); return (0, _utils.isListItemNode)($pos.parent) || (0, _utils.isListNode)($pos.parent) || (0, _utils.isListItemNode)(posGrandParent); }; var isWrappingPossible = exports.isWrappingPossible = function isWrappingPossible(nodeType, selection) { var $from = selection.$from, $to = selection.$to; var range; if (selection instanceof _selection.GapCursorSelection && $from.nodeAfter) { var nodeSize = $from.nodeAfter.nodeSize || 1; range = $from.blockRange($from.doc.resolve($from.pos + nodeSize)); } else { range = $from.blockRange($to); } if (!range) { return false; } var wrap = (0, _transform.findWrapping)(range, nodeType); if (!wrap) { return false; } return true; }; // canOutdent var isInsideListItem = exports.isInsideListItem = function isInsideListItem(tr) { var parent = tr.selection.$from.parent; var listItem = tr.doc.type.schema.nodes.listItem; if (tr.selection instanceof _selection.GapCursorSelection) { return (0, _utils.isListItemNode)(parent); } return (0, _utils2.hasParentNodeOfType)(listItem)(tr.selection) && (0, _utils.isParagraphNode)(parent); }; var isInsideTableCell = exports.isInsideTableCell = function isInsideTableCell(tr) { var _tr$doc$type$schema$n = tr.doc.type.schema.nodes, tableCell = _tr$doc$type$schema$n.tableCell, tableHeader = _tr$doc$type$schema$n.tableHeader; return !!(0, _utils2.findParentNodeOfType)([tableCell, tableHeader])(tr.selection); }; var canJoinToPreviousListItem = exports.canJoinToPreviousListItem = function canJoinToPreviousListItem(tr) { var $from = tr.selection.$from; var $before = tr.doc.resolve($from.pos - 1); var nodeBefore = $before ? $before.nodeBefore : null; if (tr.selection instanceof _selection.GapCursorSelection) { nodeBefore = $from.nodeBefore; } return (0, _utils.isListNode)(nodeBefore); }; var selectionContainsList = exports.selectionContainsList = function selectionContainsList(tr) { var _tr$selection = tr.selection, from = _tr$selection.from, to = _tr$selection.to; var foundListNode = null; tr.doc.nodesBetween(from, to, function (node) { if ((0, _utils.isListNode)(node)) { foundListNode = node; } if (foundListNode) { return false; } return true; }); return foundListNode; }; var createListNodeRange = exports.createListNodeRange = function createListNodeRange(_ref) { var selection = _ref.selection; var $from = selection.$from, $to = selection.$to; var range = $from.blockRange($to, _utils.isListNode); if (!range) { return null; } return range; };