Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

57 строки
1.7 KiB

  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. const Immutable = require("immutable");
  13. const insertIntoList = require("./insertIntoList");
  14. const invariant = require("fbjs/lib/invariant");
  15. const {
  16. Repeat
  17. } = Immutable;
  18. import type CharacterMetadata from "./CharacterMetadata";
  19. import type ContentState from "./ContentState";
  20. import type SelectionState from "./SelectionState";
  21. function insertTextIntoContentState(contentState: ContentState, selectionState: SelectionState, text: string, characterMetadata: CharacterMetadata): ContentState {
  22. invariant(selectionState.isCollapsed(), '`insertText` should only be called with a collapsed range.');
  23. let len: ?number = null;
  24. if (text != null) {
  25. len = text.length;
  26. }
  27. if (len == null || len === 0) {
  28. return contentState;
  29. }
  30. const blockMap = contentState.getBlockMap();
  31. const key = selectionState.getStartKey();
  32. const offset = selectionState.getStartOffset();
  33. const block = blockMap.get(key);
  34. const blockText = block.getText();
  35. const newBlock = block.merge({
  36. text: blockText.slice(0, offset) + text + blockText.slice(offset, block.getLength()),
  37. characterList: insertIntoList(block.getCharacterList(), Repeat(characterMetadata, len).toList(), offset)
  38. });
  39. const newOffset = offset + len;
  40. return contentState.merge({
  41. blockMap: blockMap.set(key, newBlock),
  42. selectionAfter: selectionState.merge({
  43. anchorOffset: newOffset,
  44. focusOffset: newOffset
  45. })
  46. });
  47. }
  48. module.exports = insertTextIntoContentState;