UNPKG

@atlaskit/editor-plugin-base

Version:

Base plugin for @atlaskit/editor-core

52 lines (51 loc) 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.newlinePreserveMarksKey = exports.default = void 0; var _safePlugin = require("@atlaskit/editor-common/safe-plugin"); var _utils = require("@atlaskit/editor-common/utils"); var _keymap = require("@atlaskit/editor-prosemirror/keymap"); var _state = require("@atlaskit/editor-prosemirror/state"); var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals"); var newlinePreserveMarksKey = exports.newlinePreserveMarksKey = new _state.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; }; var _default = exports.default = function _default() { return new _safePlugin.SafePlugin({ key: newlinePreserveMarksKey, props: { handleKeyDown: (0, _keymap.keydownHandler)({ Enter: (0, _expValEquals.expValEquals)('platform_editor_small_font_size', 'isEnabled', true) ? (0, _utils.filterCommand)([_utils.isSelectionEndOfParagraph, hasBlockMarksToPreserve], splitBlockPreservingMarks) : (0, _utils.filterCommand)([_utils.isSelectionEndOfParagraph, isSelectionAligned], splitBlockPreservingMarks) }) } }); };