UNPKG

d2-ui

Version:
37 lines (32 loc) 1.14 kB
/** * 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 setBlockTypeForContentState * @typechecks * */ 'use strict'; var Immutable = require('immutable'); function setBlockTypeForContentState(contentState, selectionState, blockType) { var startKey = selectionState.getStartKey(); var endKey = selectionState.getEndKey(); var blockMap = contentState.getBlockMap(); var newBlocks = blockMap.toSeq().skipUntil(function (_, k) { return k === startKey; }).takeUntil(function (_, k) { return k === endKey; }).concat(Immutable.Map([[endKey, blockMap.get(endKey)]])).map(function (block) { return block.merge({ type: blockType, depth: 0 }); }); return contentState.merge({ blockMap: blockMap.merge(newBlocks), selectionBefore: selectionState, selectionAfter: selectionState }); } module.exports = setBlockTypeForContentState;