draft-js
Version:
A React framework for building text editors.
39 lines (35 loc) • 1.34 kB
JavaScript
/**
* 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
*
* @emails oncall+draft_js
*/
;
var applyEntityToContentBlock = require("./applyEntityToContentBlock");
var Immutable = require("immutable");
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;