/** * Copyright (c) 2013-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule applyEntityToContentState * @format * @flow */ 'use strict'; import type ContentState from './ContentState'; import type SelectionState from './SelectionState'; var Immutable = require('immutable'); var applyEntityToContentBlock = require('./applyEntityToContentBlock'); function applyEntityToContentState(contentState: ContentState, selectionState: SelectionState, entityKey: ?string): ContentState { const blockMap = contentState.getBlockMap(); const startKey = selectionState.getStartKey(); const startOffset = selectionState.getStartOffset(); const endKey = selectionState.getEndKey(); const endOffset = selectionState.getEndOffset(); const newBlocks = blockMap.skipUntil((_, k) => k === startKey).takeUntil((_, k) => k === endKey).toOrderedMap().merge(Immutable.OrderedMap([[endKey, blockMap.get(endKey)]])).map((block, blockKey) => { const sliceStart = blockKey === startKey ? startOffset : 0; const sliceEnd = blockKey === endKey ? endOffset : block.getLength(); return applyEntityToContentBlock(block, sliceStart, sliceEnd, entityKey); }); return contentState.merge({ blockMap: blockMap.merge(newBlocks), selectionBefore: selectionState, selectionAfter: selectionState }); } module.exports = applyEntityToContentState;