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

109 строки
3.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 ContentBlockNode = require("./ContentBlockNode");
  13. var generateRandomKey = require("./generateRandomKey");
  14. var Immutable = require("immutable");
  15. var OrderedMap = Immutable.OrderedMap;
  16. var randomizeContentBlockNodeKeys = function randomizeContentBlockNodeKeys(blockMap) {
  17. var newKeysRef = {}; // we keep track of root blocks in order to update subsequent sibling links
  18. var lastRootBlock;
  19. return OrderedMap(blockMap.withMutations(function (blockMapState) {
  20. blockMapState.forEach(function (block, index) {
  21. var oldKey = block.getKey();
  22. var nextKey = block.getNextSiblingKey();
  23. var prevKey = block.getPrevSiblingKey();
  24. var childrenKeys = block.getChildKeys();
  25. var parentKey = block.getParentKey(); // new key that we will use to build linking
  26. var key = generateRandomKey(); // we will add it here to re-use it later
  27. newKeysRef[oldKey] = key;
  28. if (nextKey) {
  29. var nextBlock = blockMapState.get(nextKey);
  30. if (nextBlock) {
  31. blockMapState.setIn([nextKey, 'prevSibling'], key);
  32. } else {
  33. // this can happen when generating random keys for fragments
  34. blockMapState.setIn([oldKey, 'nextSibling'], null);
  35. }
  36. }
  37. if (prevKey) {
  38. var prevBlock = blockMapState.get(prevKey);
  39. if (prevBlock) {
  40. blockMapState.setIn([prevKey, 'nextSibling'], key);
  41. } else {
  42. // this can happen when generating random keys for fragments
  43. blockMapState.setIn([oldKey, 'prevSibling'], null);
  44. }
  45. }
  46. if (parentKey && blockMapState.get(parentKey)) {
  47. var parentBlock = blockMapState.get(parentKey);
  48. var parentChildrenList = parentBlock.getChildKeys();
  49. blockMapState.setIn([parentKey, 'children'], parentChildrenList.set(parentChildrenList.indexOf(block.getKey()), key));
  50. } else {
  51. // blocks will then be treated as root block nodes
  52. blockMapState.setIn([oldKey, 'parent'], null);
  53. if (lastRootBlock) {
  54. blockMapState.setIn([lastRootBlock.getKey(), 'nextSibling'], key);
  55. blockMapState.setIn([oldKey, 'prevSibling'], newKeysRef[lastRootBlock.getKey()]);
  56. }
  57. lastRootBlock = blockMapState.get(oldKey);
  58. }
  59. childrenKeys.forEach(function (childKey) {
  60. var childBlock = blockMapState.get(childKey);
  61. if (childBlock) {
  62. blockMapState.setIn([childKey, 'parent'], key);
  63. } else {
  64. blockMapState.setIn([oldKey, 'children'], block.getChildKeys().filter(function (child) {
  65. return child !== childKey;
  66. }));
  67. }
  68. });
  69. });
  70. }).toArray().map(function (block) {
  71. return [newKeysRef[block.getKey()], block.set('key', newKeysRef[block.getKey()])];
  72. }));
  73. };
  74. var randomizeContentBlockKeys = function randomizeContentBlockKeys(blockMap) {
  75. return OrderedMap(blockMap.toArray().map(function (block) {
  76. var key = generateRandomKey();
  77. return [key, block.set('key', key)];
  78. }));
  79. };
  80. var randomizeBlockMapKeys = function randomizeBlockMapKeys(blockMap) {
  81. var isTreeBasedBlockMap = blockMap.first() instanceof ContentBlockNode;
  82. if (!isTreeBasedBlockMap) {
  83. return randomizeContentBlockKeys(blockMap);
  84. }
  85. return randomizeContentBlockNodeKeys(blockMap);
  86. };
  87. module.exports = randomizeBlockMapKeys;