UNPKG

@wix/design-system

Version:

@wix/design-system

72 lines (63 loc) 2.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _draftJs = require("draft-js"); var _getSelectedBlocks = _interopRequireDefault(require("./getSelectedBlocks")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } /** * Calls a provided `modifier` function with a selection for each * selected block in the current editor selection. Passes through additional * arguments to the modifier. * * Note: At the moment it will retain the original selection and override * possible selection changes from modifiers * * @param {object} editorState The current draft.js editor state object * * @param {function} modifier A modifier function to be executed. * Must have the signature (editorState, selection, ...) * * @param {mixed} ...args Additional arguments to be passed through to the modifier * * @return {object} The new editor state */ var _default = function _default(editorState, modifier) { for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { args[_key - 2] = arguments[_key]; } var contentState = editorState.getCurrentContent(); var currentSelection = editorState.getSelection(); var startKey = currentSelection.getStartKey(); var endKey = currentSelection.getEndKey(); var startOffset = currentSelection.getStartOffset(); var endOffset = currentSelection.getEndOffset(); var isSameBlock = startKey === endKey; var selectedBlocks = (0, _getSelectedBlocks["default"])(contentState, startKey, endKey); var finalEditorState = editorState; selectedBlocks.forEach(function (block) { var currentBlockKey = block.getKey(); var selectionStart = startOffset; var selectionEnd = endOffset; if (currentBlockKey === startKey) { selectionStart = startOffset; selectionEnd = isSameBlock ? endOffset : block.getText().length; } else if (currentBlockKey === endKey) { selectionStart = isSameBlock ? startOffset : 0; selectionEnd = endOffset; } else { selectionStart = 0; selectionEnd = block.getText().length; } var selection = new _draftJs.SelectionState({ anchorKey: currentBlockKey, anchorOffset: selectionStart, focusKey: currentBlockKey, focusOffset: selectionEnd }); finalEditorState = modifier.apply(void 0, [finalEditorState, selection].concat(args)); }); return _draftJs.EditorState.forceSelection(finalEditorState, currentSelection); }; exports["default"] = _default;