1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var ContentBlockNode = require("./ContentBlockNode");
|
14 |
|
15 | var generateRandomKey = require("./generateRandomKey");
|
16 |
|
17 | var Immutable = require("immutable");
|
18 |
|
19 | var OrderedMap = Immutable.OrderedMap;
|
20 |
|
21 | var randomizeContentBlockNodeKeys = function randomizeContentBlockNodeKeys(blockMap) {
|
22 | var newKeysRef = {};
|
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();
|
32 |
|
33 | var key = generateRandomKey();
|
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 |
|
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 |
|
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 |
|
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 |
|
92 | var 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 |
|
99 | var 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 |
|
109 | module.exports = randomizeBlockMapKeys; |
\ | No newline at end of file |