UNPKG

@atlaskit/editor-plugin-base

Version:

Base plugin for @atlaskit/editor-core

39 lines 1.74 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 const newlinePreserveMarksKey = new PluginKey('newlinePreserveMarksPlugin'); const isSelectionAligned = state => !!state.selection.$to.parent.marks.find(m => m.type === state.schema.marks.alignment); const isSelectionFontSized = state => { const { blockTaskItem, listItem } = state.schema.nodes; const { fontSize } = state.schema.marks; const $to = state.selection.$to; const 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(m => m.type === fontSize); }; const hasBlockMarksToPreserve = state => isSelectionAligned(state) || isSelectionFontSized(state); const splitBlockPreservingMarks = (state, dispatch) => { if (dispatch) { dispatch(state.tr.split(state.tr.mapping.map(state.selection.$from.pos), 1)); } return true; }; export default (() => new SafePlugin({ key: newlinePreserveMarksKey, props: { handleKeyDown: keydownHandler({ Enter: expValEquals('platform_editor_small_font_size', 'isEnabled', true) ? filter([isSelectionEndOfParagraph, hasBlockMarksToPreserve], splitBlockPreservingMarks) : filter([isSelectionEndOfParagraph, isSelectionAligned], splitBlockPreservingMarks) }) } }));