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

52 строки
1.5 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. *
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. var Immutable = require("immutable");
  13. var insertIntoList = require("./insertIntoList");
  14. var invariant = require("fbjs/lib/invariant");
  15. var Repeat = Immutable.Repeat;
  16. function insertTextIntoContentState(contentState, selectionState, text, characterMetadata) {
  17. !selectionState.isCollapsed() ? process.env.NODE_ENV !== "production" ? invariant(false, '`insertText` should only be called with a collapsed range.') : invariant(false) : void 0;
  18. var len = null;
  19. if (text != null) {
  20. len = text.length;
  21. }
  22. if (len == null || len === 0) {
  23. return contentState;
  24. }
  25. var blockMap = contentState.getBlockMap();
  26. var key = selectionState.getStartKey();
  27. var offset = selectionState.getStartOffset();
  28. var block = blockMap.get(key);
  29. var blockText = block.getText();
  30. var newBlock = block.merge({
  31. text: blockText.slice(0, offset) + text + blockText.slice(offset, block.getLength()),
  32. characterList: insertIntoList(block.getCharacterList(), Repeat(characterMetadata, len).toList(), offset)
  33. });
  34. var newOffset = offset + len;
  35. return contentState.merge({
  36. blockMap: blockMap.set(key, newBlock),
  37. selectionAfter: selectionState.merge({
  38. anchorOffset: newOffset,
  39. focusOffset: newOffset
  40. })
  41. });
  42. }
  43. module.exports = insertTextIntoContentState;