25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

135 satır
4.1 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 ContentBlockNode = require("./ContentBlockNode");
  13. var generateRandomKey = require("./generateRandomKey");
  14. var Immutable = require("immutable");
  15. var invariant = require("fbjs/lib/invariant");
  16. var modifyBlockForContentState = require("./modifyBlockForContentState");
  17. var List = Immutable.List,
  18. Map = Immutable.Map;
  19. var transformBlock = function transformBlock(key, blockMap, func) {
  20. if (!key) {
  21. return;
  22. }
  23. var block = blockMap.get(key);
  24. if (!block) {
  25. return;
  26. }
  27. blockMap.set(key, func(block));
  28. };
  29. var updateBlockMapLinks = function updateBlockMapLinks(blockMap, originalBlock, belowBlock) {
  30. return blockMap.withMutations(function (blocks) {
  31. var originalBlockKey = originalBlock.getKey();
  32. var belowBlockKey = belowBlock.getKey(); // update block parent
  33. transformBlock(originalBlock.getParentKey(), blocks, function (block) {
  34. var parentChildrenList = block.getChildKeys();
  35. var insertionIndex = parentChildrenList.indexOf(originalBlockKey) + 1;
  36. var newChildrenArray = parentChildrenList.toArray();
  37. newChildrenArray.splice(insertionIndex, 0, belowBlockKey);
  38. return block.merge({
  39. children: List(newChildrenArray)
  40. });
  41. }); // update original next block
  42. transformBlock(originalBlock.getNextSiblingKey(), blocks, function (block) {
  43. return block.merge({
  44. prevSibling: belowBlockKey
  45. });
  46. }); // update original block
  47. transformBlock(originalBlockKey, blocks, function (block) {
  48. return block.merge({
  49. nextSibling: belowBlockKey
  50. });
  51. }); // update below block
  52. transformBlock(belowBlockKey, blocks, function (block) {
  53. return block.merge({
  54. prevSibling: originalBlockKey
  55. });
  56. });
  57. });
  58. };
  59. var splitBlockInContentState = function splitBlockInContentState(contentState, selectionState) {
  60. !selectionState.isCollapsed() ? process.env.NODE_ENV !== "production" ? invariant(false, 'Selection range must be collapsed.') : invariant(false) : void 0;
  61. var key = selectionState.getAnchorKey();
  62. var blockMap = contentState.getBlockMap();
  63. var blockToSplit = blockMap.get(key);
  64. var text = blockToSplit.getText();
  65. if (!text) {
  66. var blockType = blockToSplit.getType();
  67. if (blockType === 'unordered-list-item' || blockType === 'ordered-list-item') {
  68. return modifyBlockForContentState(contentState, selectionState, function (block) {
  69. return block.merge({
  70. type: 'unstyled',
  71. depth: 0
  72. });
  73. });
  74. }
  75. }
  76. var offset = selectionState.getAnchorOffset();
  77. var chars = blockToSplit.getCharacterList();
  78. var keyBelow = generateRandomKey();
  79. var isExperimentalTreeBlock = blockToSplit instanceof ContentBlockNode;
  80. var blockAbove = blockToSplit.merge({
  81. text: text.slice(0, offset),
  82. characterList: chars.slice(0, offset)
  83. });
  84. var blockBelow = blockAbove.merge({
  85. key: keyBelow,
  86. text: text.slice(offset),
  87. characterList: chars.slice(offset),
  88. data: Map()
  89. });
  90. var blocksBefore = blockMap.toSeq().takeUntil(function (v) {
  91. return v === blockToSplit;
  92. });
  93. var blocksAfter = blockMap.toSeq().skipUntil(function (v) {
  94. return v === blockToSplit;
  95. }).rest();
  96. var newBlocks = blocksBefore.concat([[key, blockAbove], [keyBelow, blockBelow]], blocksAfter).toOrderedMap();
  97. if (isExperimentalTreeBlock) {
  98. !blockToSplit.getChildKeys().isEmpty() ? process.env.NODE_ENV !== "production" ? invariant(false, 'ContentBlockNode must not have children') : invariant(false) : void 0;
  99. newBlocks = updateBlockMapLinks(newBlocks, blockAbove, blockBelow);
  100. }
  101. return contentState.merge({
  102. blockMap: newBlocks,
  103. selectionBefore: selectionState,
  104. selectionAfter: selectionState.merge({
  105. anchorKey: keyBelow,
  106. anchorOffset: 0,
  107. focusKey: keyBelow,
  108. focusOffset: 0,
  109. isBackward: false
  110. })
  111. });
  112. };
  113. module.exports = splitBlockInContentState;