-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
effects.js
273 lines (247 loc) · 7.9 KB
/
effects.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
/**
* External dependencies
*/
import { compact, last, has } from 'lodash';
/**
* WordPress dependencies
*/
import { speak } from '@wordpress/a11y';
import {
parse,
getBlockType,
switchToBlockType,
doBlocksMatchTemplate,
synchronizeBlocksWithTemplate,
} from '@wordpress/blocks';
import { _n, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import {
setupEditorState,
replaceBlocks,
selectBlock,
resetBlocks,
setTemplateValidity,
insertDefaultBlock,
} from './actions';
import {
getBlock,
getBlockRootClientId,
getBlocks,
getBlockCount,
getPreviousBlockClientId,
getSelectedBlockClientId,
getSelectedBlockCount,
getTemplate,
getTemplateLock,
isValidTemplate,
} from './selectors';
import {
fetchReusableBlocks,
saveReusableBlocks,
deleteReusableBlocks,
convertBlockToReusable,
convertBlockToStatic,
receiveReusableBlocks,
} from './effects/reusable-blocks';
import {
requestPostUpdate,
requestPostUpdateSuccess,
requestPostUpdateFailure,
trashPost,
trashPostFailure,
refreshPost,
} from './effects/posts';
/**
* Block validity is a function of blocks state (at the point of a
* reset) and the template setting. As a compromise to its placement
* across distinct parts of state, it is implemented here as a side-
* effect of the block reset action.
*
* @param {Object} action RESET_BLOCKS action.
* @param {Object} store Store instance.
*
* @return {?Object} New validity set action if validity has changed.
*/
export function validateBlocksToTemplate( action, store ) {
const state = store.getState();
const template = getTemplate( state );
const templateLock = getTemplateLock( state );
// Unlocked templates are considered always valid because they act
// as default values only.
const isBlocksValidToTemplate = (
! template ||
templateLock !== 'all' ||
doBlocksMatchTemplate( action.blocks, template )
);
// Update if validity has changed.
if ( isBlocksValidToTemplate !== isValidTemplate( state ) ) {
return setTemplateValidity( isBlocksValidToTemplate );
}
}
/**
* Effect handler which will return a block select action to select the block
* occurring before the selected block in the previous state, unless it is the
* same block or the action includes a falsey `selectPrevious` option flag.
*
* @param {Object} action Action which had initiated the effect handler.
* @param {Object} store Store instance.
*
* @return {?Object} Block select action to select previous, if applicable.
*/
export function selectPreviousBlock( action, store ) {
// if the action says previous block should not be selected don't do anything.
if ( ! action.selectPrevious ) {
return;
}
const firstRemovedBlockClientId = action.clientIds[ 0 ];
const state = store.getState();
const selectedBlockClientId = getSelectedBlockClientId( state );
// recreate the state before the block was removed.
const previousState = { ...state, editor: { present: last( state.editor.past ) } };
// rootClientId of the removed block.
const rootClientId = getBlockRootClientId( previousState, firstRemovedBlockClientId );
// Client ID of the block that was before the removed block or the
// rootClientId if the removed block was first amongst its siblings.
const blockClientIdToSelect = getPreviousBlockClientId( previousState, firstRemovedBlockClientId ) || rootClientId;
// Dispatch select block action if the currently selected block
// is not already the block we want to be selected.
if ( blockClientIdToSelect !== selectedBlockClientId ) {
return selectBlock( blockClientIdToSelect, -1 );
}
}
/**
* Effect handler which will return a default block insertion action if there
* are no other blocks at the root of the editor. This is expected to be used
* in actions which may result in no blocks remaining in the editor (removal,
* replacement, etc).
*
* @param {Object} action Action which had initiated the effect handler.
* @param {Object} store Store instance.
*
* @return {?Object} Default block insert action, if no other blocks exist.
*/
export function ensureDefaultBlock( action, store ) {
if ( ! getBlockCount( store.getState() ) ) {
return insertDefaultBlock();
}
}
export default {
REQUEST_POST_UPDATE: ( action, store ) => {
requestPostUpdate( action, store );
},
REQUEST_POST_UPDATE_SUCCESS: requestPostUpdateSuccess,
REQUEST_POST_UPDATE_FAILURE: requestPostUpdateFailure,
TRASH_POST: ( action, store ) => {
trashPost( action, store );
},
TRASH_POST_FAILURE: trashPostFailure,
REFRESH_POST: ( action, store ) => {
refreshPost( action, store );
},
MERGE_BLOCKS( action, store ) {
const { dispatch } = store;
const state = store.getState();
const [ firstBlockClientId, secondBlockClientId ] = action.blocks;
const blockA = getBlock( state, firstBlockClientId );
const blockType = getBlockType( blockA.name );
// Only focus the previous block if it's not mergeable
if ( ! blockType.merge ) {
dispatch( selectBlock( blockA.clientId ) );
return;
}
// We can only merge blocks with similar types
// thus, we transform the block to merge first
const blockB = getBlock( state, secondBlockClientId );
const blocksWithTheSameType = blockA.name === blockB.name ?
[ blockB ] :
switchToBlockType( blockB, blockA.name );
// If the block types can not match, do nothing
if ( ! blocksWithTheSameType || ! blocksWithTheSameType.length ) {
return;
}
// Calling the merge to update the attributes and remove the block to be merged
const updatedAttributes = blockType.merge(
blockA.attributes,
blocksWithTheSameType[ 0 ].attributes
);
dispatch( selectBlock( blockA.clientId, -1 ) );
dispatch( replaceBlocks(
[ blockA.clientId, blockB.clientId ],
[
{
...blockA,
attributes: {
...blockA.attributes,
...updatedAttributes,
},
},
...blocksWithTheSameType.slice( 1 ),
]
) );
},
SETUP_EDITOR( action, store ) {
const { post, edits } = action;
const state = store.getState();
// In order to ensure maximum of a single parse during setup, edits are
// included as part of editor setup action. Assume edited content as
// canonical if provided, falling back to post.
let content;
if ( has( edits, [ 'content' ] ) ) {
content = edits.content;
} else {
content = post.content.raw;
}
let blocks = parse( content );
// Apply a template for new posts only, if exists.
const isNewPost = post.status === 'auto-draft';
const template = getTemplate( state );
if ( isNewPost && template ) {
blocks = synchronizeBlocksWithTemplate( blocks, template );
}
const setupAction = setupEditorState( post, blocks );
return compact( [
setupAction,
// TODO: This is temporary, necessary only so long as editor setup
// is a separate action from block resetting.
//
// See: https://github.com/WordPress/gutenberg/pull/9403
validateBlocksToTemplate( setupAction, store ),
] );
},
RESET_BLOCKS: [
validateBlocksToTemplate,
],
SYNCHRONIZE_TEMPLATE( action, { getState } ) {
const state = getState();
const blocks = getBlocks( state );
const template = getTemplate( state );
const updatedBlockList = synchronizeBlocksWithTemplate( blocks, template );
return resetBlocks( updatedBlockList );
},
FETCH_REUSABLE_BLOCKS: ( action, store ) => {
fetchReusableBlocks( action, store );
},
SAVE_REUSABLE_BLOCK: ( action, store ) => {
saveReusableBlocks( action, store );
},
DELETE_REUSABLE_BLOCK: ( action, store ) => {
deleteReusableBlocks( action, store );
},
RECEIVE_REUSABLE_BLOCKS: receiveReusableBlocks,
CONVERT_BLOCK_TO_STATIC: convertBlockToStatic,
CONVERT_BLOCK_TO_REUSABLE: convertBlockToReusable,
REMOVE_BLOCKS: [
selectPreviousBlock,
ensureDefaultBlock,
],
REPLACE_BLOCKS: [
ensureDefaultBlock,
],
MULTI_SELECT: ( action, { getState } ) => {
const blockCount = getSelectedBlockCount( getState() );
/* translators: %s: number of selected blocks */
speak( sprintf( _n( '%s block selected.', '%s blocks selected.', blockCount ), blockCount ), 'assertive' );
},
};