-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathupdater.js
218 lines (197 loc) · 5.88 KB
/
updater.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* External dependencies
*/
import { findIndex, uniqueId, isArray } from 'lodash';
import { getBlock } from 'wp-blocks';
const blockLevelUpdater = ( state, command ) => {
const currentUID = command.uid;
const currentIndex = findIndex( state.blocks, b => b.uid === currentUID );
// Ignore commands for removed blocks
if ( currentIndex === -1 ) {
return state;
}
const currentBlock = state.blocks[ currentIndex ];
const mergeStates = newState => Object.assign( {}, state, newState );
const blockCommandHandlers = {
change: ( { changes } ) => {
const newBlocks = [ ...state.blocks ];
newBlocks[ currentIndex ] = Object.assign( {}, currentBlock, changes );
return mergeStates( { blocks: newBlocks } );
},
append: ( { block: commandBlock } ) => {
const createdBlock = commandBlock ? commandBlock : { blockType: 'text', content: '' };
const appenedBlockId = uniqueId();
const newBlocks = [
...state.blocks.slice( 0, currentIndex + 1 ),
Object.assign( {}, createdBlock, { uid: appenedBlockId } ),
...state.blocks.slice( currentIndex + 1 )
];
const focus = { uid: appenedBlockId, config: { start: true } };
const selected = null;
return mergeStates( {
blocks: newBlocks,
selected,
focus
} );
},
remove: ( { removedUID: commandUID } ) => {
const uidToRemove = commandUID === undefined ? currentUID : commandUID;
const indexToRemove = findIndex( state.blocks, b => b.uid === uidToRemove );
if ( ! commandUID && indexToRemove === 0 ) {
return state;
}
const newBlocks = [
...state.blocks.slice( 0, indexToRemove ),
...state.blocks.slice( indexToRemove + 1 ),
];
const focus = indexToRemove && state.focus.uid === uidToRemove
? { uid: state.blocks[ indexToRemove - 1 ].uid, config: { end: true } }
: state.focus;
const selected = null;
return mergeStates( {
blocks: newBlocks,
selected,
focus
} );
},
mergeWithPrevious: () => {
const previousBlock = state.blocks[ currentIndex - 1 ];
const previousBlockDefinition = getBlock( previousBlock.blockType );
if ( ! previousBlock || ! previousBlockDefinition.merge ) {
return state;
}
const mergeDefinition = isArray( previousBlockDefinition.merge )
? previousBlockDefinition.merge.find( def => def.blocks.indexOf( currentBlock.blockType ) !== -1 )
: previousBlockDefinition.merge;
if ( ! mergeDefinition ) {
return state;
}
const mergedState = mergeStates( mergeDefinition.merge( state, currentIndex - 1 ) );
return Object.assign( mergedState, { selected: null } );
},
focus: ( { config } ) => {
return mergeStates( {
focus: { uid: currentUID, config }
} );
},
moveCursorUp: () => {
const previousBlock = state.blocks[ currentIndex - 1 ];
return mergeStates( {
focus: previousBlock ? { uid: previousBlock.uid, config: { end: true } } : state.focus,
selected: null
} );
},
moveCursorDown: () => {
const nextBlock = state.blocks[ currentIndex + 1 ];
return mergeStates( {
focus: nextBlock ? { uid: nextBlock.uid, config: { start: true } } : state.focus,
selected: null
} );
},
select: () => {
return mergeStates( {
selected: currentUID
} );
},
unselect: () => {
return mergeStates( {
selected: null
} );
},
moveBlockUp: () => {
if ( currentIndex === 0 ) {
return state;
}
const newBlocks = [
...state.blocks.slice( 0, currentIndex - 1 ),
state.blocks[ currentIndex ],
state.blocks[ currentIndex - 1 ],
...state.blocks.slice( currentIndex + 1 )
];
return mergeStates( {
blocks: newBlocks,
selected: currentUID
} );
},
moveBlockDown: () => {
if ( currentIndex === state.blocks.length - 1 ) {
return state;
}
const newBlocks = [
...state.blocks.slice( 0, currentIndex ),
state.blocks[ currentIndex + 1 ],
state.blocks[ currentIndex ],
...state.blocks.slice( currentIndex + 2 )
];
return mergeStates( {
blocks: newBlocks,
selected: currentUID
} );
},
replace: ( { id } ) => {
const newBlockUid = uniqueId();
const blockDefinition = getBlock( id );
const newBlock = Object.assign( { uid: newBlockUid }, blockDefinition.create() );
const newBlocks = [
...state.blocks.slice( 0, currentIndex ),
newBlock,
...state.blocks.slice( currentIndex + 1 )
];
return mergeStates( {
blocks: newBlocks,
focus: { uid: newBlockUid, config: {} }
} );
},
transform: ( { id } ) => {
const newBlockUid = uniqueId();
const blockDefinition = getBlock( id );
const transformation = blockDefinition.transformations
.find( t => t.blocks.indexOf( currentBlock.blockType ) !== -1 );
if ( ! transformation ) {
return state;
}
const newBlock = Object.assign( { uid: newBlockUid }, transformation.transform( currentBlock ) );
const newBlocks = [
...state.blocks.slice( 0, currentIndex ),
newBlock,
...state.blocks.slice( currentIndex + 1 )
];
return mergeStates( {
blocks: newBlocks,
focus: { uid: newBlockUid, config: {} }
} );
}
};
if ( blockCommandHandlers[ command.type ] ) {
return blockCommandHandlers[ command.type ]( command );
}
return state;
};
const globalLevelUpdater = ( state, command ) => {
const commandHandlers = {
addBlock: ( { id } ) => {
const newBlockUID = uniqueId();
const blockDefinition = getBlock( id );
const newBlock = Object.assign( { uid: newBlockUID }, blockDefinition.create() );
const newBlocks = [
...state.blocks,
newBlock
];
return Object.assign( {}, state, {
blocks: newBlocks,
focus: { uid: newBlockUID, config: {} },
selected: newBlockUID
} );
}
};
if ( commandHandlers[ command.type ] ) {
return commandHandlers[ command.type ]( command );
}
return state;
};
export default ( state, command ) => {
if ( command.uid ) {
return blockLevelUpdater( state, command );
}
return globalLevelUpdater( state, command );
};