UNPKG

@atlaskit/editor-plugin-text-formatting

Version:

Text-formatting plugin for @atlaskit/editor-core

75 lines (74 loc) 2.58 kB
import { anyMarkActive } from '@atlaskit/editor-common/mark'; import { FORMATTING_MARK_TYPES, FORMATTING_NODE_TYPES } from './clear-formatting'; export var hasCode = function hasCode(state, pos) { var code = state.schema.marks.code; var node = pos >= 0 && state.doc.nodeAt(pos); if (node) { return !!node.marks.filter(function (mark) { return mark.type === code; }).length; } return false; }; /** * Determine if a mark (with specific attribute values) exists anywhere in the selection. */ export var markActive = function markActive(state, mark) { var _state$selection = state.selection, from = _state$selection.from, to = _state$selection.to, empty = _state$selection.empty; // When the selection is empty, only the active marks apply. if (empty) { return !!mark.isInSet(state.tr.storedMarks || state.selection.$from.marks()); } // For a non-collapsed selection, the marks on the nodes matter. var found = false; state.doc.nodesBetween(from, to, function (node) { found = found || mark.isInSet(node.marks); }); return found; }; var blockStylingIsPresent = function blockStylingIsPresent(state) { var _state$selection2 = state.selection, from = _state$selection2.from, to = _state$selection2.to; var isBlockStyling = false; state.doc.nodesBetween(from, to, function (node) { if (FORMATTING_NODE_TYPES.indexOf(node.type.name) !== -1) { isBlockStyling = true; return false; } return true; }); return isBlockStyling; }; var marksArePresent = function marksArePresent(state) { var activeMarkTypes = FORMATTING_MARK_TYPES.filter(function (mark) { if (!!state.schema.marks[mark]) { var _state$selection3 = state.selection, $from = _state$selection3.$from, empty = _state$selection3.empty; var marks = state.schema.marks; if (empty) { return !!marks[mark].isInSet(state.storedMarks || $from.marks()); } return anyMarkActive(state, marks[mark]); } return false; }); return activeMarkTypes.length > 0; }; export var checkFormattingIsPresent = function checkFormattingIsPresent(state) { return marksArePresent(state) || blockStylingIsPresent(state); }; export var compareItemsArrays = function compareItemsArrays(items, prevItems) { return items && items.filter(function (item) { return !prevItems.includes(item); }); }; export var isArrayContainsContent = function isArrayContainsContent(items, content) { return items.filter(function (item) { return item.content === content; }).length > 0; };