You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

adjustBlockDepthForContentState.js 1.0 KiB

пре 3 година
12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. *
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. function adjustBlockDepthForContentState(contentState, selectionState, adjustment, maxDepth) {
  13. var startKey = selectionState.getStartKey();
  14. var endKey = selectionState.getEndKey();
  15. var blockMap = contentState.getBlockMap();
  16. var blocks = blockMap.toSeq().skipUntil(function (_, k) {
  17. return k === startKey;
  18. }).takeUntil(function (_, k) {
  19. return k === endKey;
  20. }).concat([[endKey, blockMap.get(endKey)]]).map(function (block) {
  21. var depth = block.getDepth() + adjustment;
  22. depth = Math.max(0, Math.min(depth, maxDepth));
  23. return block.set('depth', depth);
  24. });
  25. blockMap = blockMap.merge(blocks);
  26. return contentState.merge({
  27. blockMap: blockMap,
  28. selectionBefore: selectionState,
  29. selectionAfter: selectionState
  30. });
  31. }
  32. module.exports = adjustBlockDepthForContentState;