UNPKG

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