44 lines
1.5 KiB
JavaScript
44 lines
1.5 KiB
JavaScript
/**
|
|
* 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
|
|
*
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
var Immutable = require('immutable');
|
|
|
|
var applyEntityToContentBlock = require('./applyEntityToContentBlock');
|
|
|
|
function applyEntityToContentState(contentState, selectionState, entityKey) {
|
|
var blockMap = contentState.getBlockMap();
|
|
var startKey = selectionState.getStartKey();
|
|
var startOffset = selectionState.getStartOffset();
|
|
var endKey = selectionState.getEndKey();
|
|
var endOffset = selectionState.getEndOffset();
|
|
|
|
var newBlocks = blockMap.skipUntil(function (_, k) {
|
|
return k === startKey;
|
|
}).takeUntil(function (_, k) {
|
|
return k === endKey;
|
|
}).toOrderedMap().merge(Immutable.OrderedMap([[endKey, blockMap.get(endKey)]])).map(function (block, blockKey) {
|
|
var sliceStart = blockKey === startKey ? startOffset : 0;
|
|
var 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; |