UNPKG

@atlaskit/editor-plugin-base

Version:

Base plugin for @atlaskit/editor-core

46 lines 2.04 kB
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { filterCommand as filter, isSelectionEndOfParagraph } from '@atlaskit/editor-common/utils'; import { keydownHandler } from '@atlaskit/editor-prosemirror/keymap'; import { PluginKey } from '@atlaskit/editor-prosemirror/state'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; export var newlinePreserveMarksKey = new PluginKey('newlinePreserveMarksPlugin'); var isSelectionAligned = function isSelectionAligned(state) { return !!state.selection.$to.parent.marks.find(function (m) { return m.type === state.schema.marks.alignment; }); }; var isSelectionFontSized = function isSelectionFontSized(state) { var _state$schema$nodes = state.schema.nodes, blockTaskItem = _state$schema$nodes.blockTaskItem, listItem = _state$schema$nodes.listItem; var fontSize = state.schema.marks.fontSize; var $to = state.selection.$to; var grandParent = $to.node($to.depth - 1); if (!fontSize || // don't intercept Enter inside blockTaskItem or listItem grandParent && (grandParent.type === blockTaskItem || grandParent.type === listItem)) { return false; } return !!$to.parent.marks.find(function (m) { return m.type === fontSize; }); }; var hasBlockMarksToPreserve = function hasBlockMarksToPreserve(state) { return isSelectionAligned(state) || isSelectionFontSized(state); }; var splitBlockPreservingMarks = function splitBlockPreservingMarks(state, dispatch) { if (dispatch) { dispatch(state.tr.split(state.tr.mapping.map(state.selection.$from.pos), 1)); } return true; }; export default (function () { return new SafePlugin({ key: newlinePreserveMarksKey, props: { handleKeyDown: keydownHandler({ Enter: expValEquals('platform_editor_small_font_size', 'isEnabled', true) ? filter([isSelectionEndOfParagraph, hasBlockMarksToPreserve], splitBlockPreservingMarks) : filter([isSelectionEndOfParagraph, isSelectionAligned], splitBlockPreservingMarks) }) } }); });