UNPKG

@atlaskit/editor-plugin-list

Version:

List plugin for @atlaskit/editor-core

57 lines (55 loc) 2.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sanitiseMarksInSelection = void 0; var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals"); var listContainerTypes = new Set(['bulletList', 'orderedList']); /** * When wrapping in a list, the paragraph's direct parent will be listItem, * not the list container itself. For fontSize marks, resolve to listItem * so mark compatibility is checked against the actual parent. */ var resolveEffectiveParentType = function resolveEffectiveParentType(newParentType) { if (newParentType && listContainerTypes.has(newParentType.name) && (0, _expValEquals.expValEquals)('platform_editor_small_font_size', 'isEnabled', true)) { return newParentType.schema.nodes.listItem; } return newParentType; }; var isMarkDisallowed = function isMarkDisallowed(mark, parent, effectiveParentType) { return !(parent !== null && parent !== void 0 && parent.type.allowsMarkType(mark.type)) || effectiveParentType && !effectiveParentType.allowsMarkType(mark.type); }; var sanitiseMarksInSelection = exports.sanitiseMarksInSelection = function sanitiseMarksInSelection(tr, newParentType) { var _tr$selection = tr.selection, from = _tr$selection.from, to = _tr$selection.to; var nodesSanitized = []; tr.doc.nodesBetween(from, to, function (node, pos, parent) { if (node.isText) { return false; } // Skip expands and layouts if they are outside selection // but continue to iterate over their children. if (['expand', 'layoutSection'].includes(node.type.name) && (pos < from || pos > to)) { return true; } node.marks.forEach(function (mark) { var effectiveParentType = resolveEffectiveParentType(newParentType); if (isMarkDisallowed(mark, parent, effectiveParentType)) { var filteredMarks = node.marks.filter(function (m) { return m.type !== mark.type; }); var position = pos > 0 ? pos : 0; var marksRemoved = node.marks.filter(function (m) { return m.type === mark.type; }); nodesSanitized.push({ node: node, marksRemoved: marksRemoved }); tr.setNodeMarkup(position, undefined, node.attrs, filteredMarks); } }); }); return nodesSanitized; };