-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
reducer.js
407 lines (366 loc) · 9.45 KB
/
reducer.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/**
* WordPress dependencies
*/
import { combineReducers } from '@wordpress/data';
/**
* Internal dependencies
*/
import { EDITOR_SETTINGS_DEFAULTS } from './defaults';
import dataviewsReducer from '../dataviews/store/reducer';
/**
* Returns a post attribute value, flattening nested rendered content using its
* raw value in place of its original object form.
*
* @param {*} value Original value.
*
* @return {*} Raw value.
*/
export function getPostRawValue( value ) {
if ( value && 'object' === typeof value && 'raw' in value ) {
return value.raw;
}
return value;
}
/**
* Returns true if the two object arguments have the same keys, or false
* otherwise.
*
* @param {Object} a First object.
* @param {Object} b Second object.
*
* @return {boolean} Whether the two objects have the same keys.
*/
export function hasSameKeys( a, b ) {
const keysA = Object.keys( a ).sort();
const keysB = Object.keys( b ).sort();
return (
keysA.length === keysB.length &&
keysA.every( ( key, index ) => keysB[ index ] === key )
);
}
/**
* Returns true if, given the currently dispatching action and the previously
* dispatched action, the two actions are editing the same post property, or
* false otherwise.
*
* @param {Object} action Currently dispatching action.
* @param {Object} previousAction Previously dispatched action.
*
* @return {boolean} Whether actions are updating the same post property.
*/
export function isUpdatingSamePostProperty( action, previousAction ) {
return (
action.type === 'EDIT_POST' &&
hasSameKeys( action.edits, previousAction.edits )
);
}
/**
* Returns true if, given the currently dispatching action and the previously
* dispatched action, the two actions are modifying the same property such that
* undo history should be batched.
*
* @param {Object} action Currently dispatching action.
* @param {Object} previousAction Previously dispatched action.
*
* @return {boolean} Whether to overwrite present state.
*/
export function shouldOverwriteState( action, previousAction ) {
if ( action.type === 'RESET_EDITOR_BLOCKS' ) {
return ! action.shouldCreateUndoLevel;
}
if ( ! previousAction || action.type !== previousAction.type ) {
return false;
}
return isUpdatingSamePostProperty( action, previousAction );
}
export function postId( state = null, action ) {
switch ( action.type ) {
case 'SET_EDITED_POST':
return action.postId;
}
return state;
}
export function templateId( state = null, action ) {
switch ( action.type ) {
case 'SET_CURRENT_TEMPLATE_ID':
return action.id;
}
return state;
}
export function postType( state = null, action ) {
switch ( action.type ) {
case 'SET_EDITED_POST':
return action.postType;
}
return state;
}
/**
* Reducer returning whether the post blocks match the defined template or not.
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {boolean} Updated state.
*/
export function template( state = { isValid: true }, action ) {
switch ( action.type ) {
case 'SET_TEMPLATE_VALIDITY':
return {
...state,
isValid: action.isValid,
};
}
return state;
}
/**
* Reducer returning current network request state (whether a request to
* the WP REST API is in progress, successful, or failed).
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Object} Updated state.
*/
export function saving( state = {}, action ) {
switch ( action.type ) {
case 'REQUEST_POST_UPDATE_START':
case 'REQUEST_POST_UPDATE_FINISH':
return {
pending: action.type === 'REQUEST_POST_UPDATE_START',
options: action.options || {},
};
}
return state;
}
/**
* Reducer returning deleting post request state.
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Object} Updated state.
*/
export function deleting( state = {}, action ) {
switch ( action.type ) {
case 'REQUEST_POST_DELETE_START':
case 'REQUEST_POST_DELETE_FINISH':
return {
pending: action.type === 'REQUEST_POST_DELETE_START',
};
}
return state;
}
/**
* Post Lock State.
*
* @typedef {Object} PostLockState
*
* @property {boolean} isLocked Whether the post is locked.
* @property {?boolean} isTakeover Whether the post editing has been taken over.
* @property {?boolean} activePostLock Active post lock value.
* @property {?Object} user User that took over the post.
*/
/**
* Reducer returning the post lock status.
*
* @param {PostLockState} state Current state.
* @param {Object} action Dispatched action.
*
* @return {PostLockState} Updated state.
*/
export function postLock( state = { isLocked: false }, action ) {
switch ( action.type ) {
case 'UPDATE_POST_LOCK':
return action.lock;
}
return state;
}
/**
* Post saving lock.
*
* When post saving is locked, the post cannot be published or updated.
*
* @param {PostLockState} state Current state.
* @param {Object} action Dispatched action.
*
* @return {PostLockState} Updated state.
*/
export function postSavingLock( state = {}, action ) {
switch ( action.type ) {
case 'LOCK_POST_SAVING':
return { ...state, [ action.lockName ]: true };
case 'UNLOCK_POST_SAVING': {
const { [ action.lockName ]: removedLockName, ...restState } =
state;
return restState;
}
}
return state;
}
/**
* Post autosaving lock.
*
* When post autosaving is locked, the post will not autosave.
*
* @param {PostLockState} state Current state.
* @param {Object} action Dispatched action.
*
* @return {PostLockState} Updated state.
*/
export function postAutosavingLock( state = {}, action ) {
switch ( action.type ) {
case 'LOCK_POST_AUTOSAVING':
return { ...state, [ action.lockName ]: true };
case 'UNLOCK_POST_AUTOSAVING': {
const { [ action.lockName ]: removedLockName, ...restState } =
state;
return restState;
}
}
return state;
}
/**
* Reducer returning the post editor setting.
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Object} Updated state.
*/
export function editorSettings( state = EDITOR_SETTINGS_DEFAULTS, action ) {
switch ( action.type ) {
case 'UPDATE_EDITOR_SETTINGS':
return {
...state,
...action.settings,
};
}
return state;
}
export function renderingMode( state = 'post-only', action ) {
switch ( action.type ) {
case 'SET_RENDERING_MODE':
return action.mode;
}
return state;
}
/**
* Reducer returning the editing canvas device type.
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Object} Updated state.
*/
export function deviceType( state = 'Desktop', action ) {
switch ( action.type ) {
case 'SET_DEVICE_TYPE':
return action.deviceType;
}
return state;
}
/**
* Reducer storing the list of all programmatically removed panels.
*
* @param {Array} state Current state.
* @param {Object} action Action object.
*
* @return {Array} Updated state.
*/
export function removedPanels( state = [], action ) {
switch ( action.type ) {
case 'REMOVE_PANEL':
if ( ! state.includes( action.panelName ) ) {
return [ ...state, action.panelName ];
}
}
return state;
}
/**
* Reducer to set the block inserter panel open or closed.
*
* Note: this reducer interacts with the list view panel reducer
* to make sure that only one of the two panels is open at the same time.
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*/
export function blockInserterPanel( state = false, action ) {
switch ( action.type ) {
case 'SET_IS_LIST_VIEW_OPENED':
return action.isOpen ? false : state;
case 'SET_IS_INSERTER_OPENED':
return action.value;
}
return state;
}
/**
* Reducer to set the list view panel open or closed.
*
* Note: this reducer interacts with the inserter panel reducer
* to make sure that only one of the two panels is open at the same time.
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*/
export function listViewPanel( state = false, action ) {
switch ( action.type ) {
case 'SET_IS_INSERTER_OPENED':
return action.value ? false : state;
case 'SET_IS_LIST_VIEW_OPENED':
return action.isOpen;
}
return state;
}
/**
* This reducer does nothing aside initializing a ref to the list view toggle.
* We will have a unique ref per "editor" instance.
*
* @param {Object} state
* @return {Object} Reference to the list view toggle button.
*/
export function listViewToggleRef( state = { current: null } ) {
return state;
}
/**
* This reducer does nothing aside initializing a ref to the inserter sidebar toggle.
* We will have a unique ref per "editor" instance.
*
* @param {Object} state
* @return {Object} Reference to the inserter sidebar toggle button.
*/
export function inserterSidebarToggleRef( state = { current: null } ) {
return state;
}
export function publishSidebarActive( state = false, action ) {
switch ( action.type ) {
case 'OPEN_PUBLISH_SIDEBAR':
return true;
case 'CLOSE_PUBLISH_SIDEBAR':
return false;
case 'TOGGLE_PUBLISH_SIDEBAR':
return ! state;
}
return state;
}
export default combineReducers( {
postId,
postType,
templateId,
saving,
deleting,
postLock,
template,
postSavingLock,
editorSettings,
postAutosavingLock,
renderingMode,
deviceType,
removedPanels,
blockInserterPanel,
inserterSidebarToggleRef,
listViewPanel,
listViewToggleRef,
publishSidebarActive,
dataviews: dataviewsReducer,
} );