Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

applyEntityToContentState.js.flow 1.4 KiB

vor 3 Jahren
1234567891011121314151617181920212223242526272829303132333435363738
  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 ContentState from "./ContentState";
  13. import type SelectionState from "./SelectionState";
  14. const applyEntityToContentBlock = require("./applyEntityToContentBlock");
  15. const Immutable = require("immutable");
  16. function applyEntityToContentState(contentState: ContentState, selectionState: SelectionState, entityKey: ?string): ContentState {
  17. const blockMap = contentState.getBlockMap();
  18. const startKey = selectionState.getStartKey();
  19. const startOffset = selectionState.getStartOffset();
  20. const endKey = selectionState.getEndKey();
  21. const endOffset = selectionState.getEndOffset();
  22. const newBlocks = blockMap.skipUntil((_, k) => k === startKey).takeUntil((_, k) => k === endKey).toOrderedMap().merge(Immutable.OrderedMap([[endKey, blockMap.get(endKey)]])).map((block, blockKey) => {
  23. const sliceStart = blockKey === startKey ? startOffset : 0;
  24. const sliceEnd = blockKey === endKey ? endOffset : block.getLength();
  25. return applyEntityToContentBlock(block, sliceStart, sliceEnd, entityKey);
  26. });
  27. return contentState.merge({
  28. blockMap: blockMap.merge(newBlocks),
  29. selectionBefore: selectionState,
  30. selectionAfter: selectionState
  31. });
  32. }
  33. module.exports = applyEntityToContentState;