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.

modifyBlockForContentState.js.flow 1.1 KiB

3 years ago
1234567891011121314151617181920212223242526272829303132333435
  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. * @flow strict-local
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. import type { BlockNodeRecord } from "./BlockNodeRecord";
  13. import type ContentState from "./ContentState";
  14. import type SelectionState from "./SelectionState";
  15. const Immutable = require("immutable");
  16. const {
  17. Map
  18. } = Immutable;
  19. function modifyBlockForContentState(contentState: ContentState, selectionState: SelectionState, operation: (block: BlockNodeRecord) => BlockNodeRecord): ContentState {
  20. const startKey = selectionState.getStartKey();
  21. const endKey = selectionState.getEndKey();
  22. const blockMap = contentState.getBlockMap();
  23. const newBlocks = blockMap.toSeq().skipUntil((_, k) => k === startKey).takeUntil((_, k) => k === endKey).concat(Map([[endKey, blockMap.get(endKey)]])).map(operation);
  24. return contentState.merge({
  25. blockMap: blockMap.merge(newBlocks),
  26. selectionBefore: selectionState,
  27. selectionAfter: selectionState
  28. });
  29. }
  30. module.exports = modifyBlockForContentState;