UNPKG

@atlaskit/editor-plugin-text-color

Version:

Text color plugin for @atlaskit/editor-core

78 lines (75 loc) 2.33 kB
import { isMarkAllowedInRange, isMarkExcluded } from '@atlaskit/editor-common/mark'; import { overrideMarks } from './constants'; const hasLinkMark = $pos => { const { doc: { type: { schema: { marks: { link: linkMarkType } } } }, pos } = $pos; if (!linkMarkType) { return false; } return $pos.doc.rangeHasMark(pos, Math.min(pos + 1, $pos.doc.content.size), linkMarkType); }; /** * Use getDisabledStateNew instead and pass in `tr` */ export const getDisabledState = state => { const { textColor } = state.schema.marks; if (textColor) { var _state$storedMarks; const { empty, ranges, $cursor } = state.selection; if (empty && !$cursor || $cursor && hasLinkMark($cursor) || isMarkAllowedInRange(state.doc, ranges, textColor) === false) { return true; } // Allow "excluded" marks that can be overridden // These marks are explicitly removed before applying the new mark (see toggleColor command) const omitOverrides = mark => { return !overrideMarks.includes(mark.type.name); }; if (isMarkExcluded(textColor, ((_state$storedMarks = state.storedMarks) === null || _state$storedMarks === void 0 ? void 0 : _state$storedMarks.filter(omitOverrides)) || $cursor && $cursor.marks().filter(omitOverrides))) { return true; } return false; } return true; }; export const getDisabledStateNew = tr => { const { textColor } = tr.doc.type.schema.marks; if (textColor) { var _tr$storedMarks; const { empty, ranges, $cursor } = tr.selection; if (empty && !$cursor || $cursor && hasLinkMark($cursor) || isMarkAllowedInRange(tr.doc, ranges, textColor) === false) { return true; } // Allow "excluded" marks that can be overridden // These marks are explicitly removed before applying the new mark (see toggleColor command) const omitOverrides = mark => { return !overrideMarks.includes(mark.type.name); }; if (isMarkExcluded(textColor, ((_tr$storedMarks = tr.storedMarks) === null || _tr$storedMarks === void 0 ? void 0 : _tr$storedMarks.filter(omitOverrides)) || $cursor && $cursor.marks().filter(omitOverrides))) { return true; } return false; } return true; };