@wix/design-system
Version:
@wix/design-system
35 lines (30 loc) • 1.17 kB
Flow
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
* @emails oncall+draft_js
*/
;
import type { BlockNodeRecord } from "./BlockNodeRecord";
import type ContentState from "./ContentState";
import type SelectionState from "./SelectionState";
const Immutable = require("immutable");
const {
Map
} = Immutable;
function modifyBlockForContentState(contentState: ContentState, selectionState: SelectionState, operation: (block: BlockNodeRecord) => BlockNodeRecord): ContentState {
const startKey = selectionState.getStartKey();
const endKey = selectionState.getEndKey();
const blockMap = contentState.getBlockMap();
const newBlocks = blockMap.toSeq().skipUntil((_, k) => k === startKey).takeUntil((_, k) => k === endKey).concat(Map([[endKey, blockMap.get(endKey)]])).map(operation);
return contentState.merge({
blockMap: blockMap.merge(newBlocks),
selectionBefore: selectionState,
selectionAfter: selectionState
});
}
module.exports = modifyBlockForContentState;