@atlaskit/editor-plugin-list
Version:
List plugin for @atlaskit/editor-core
51 lines (50 loc) • 2.15 kB
JavaScript
import { expValEquals } from '@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) && 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);
};
export var 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;
};