-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy patheffects.js
128 lines (110 loc) · 4.2 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
/**
* External dependencies
*/
import { reduce } from 'lodash';
/**
* WordPress dependencies
*/
import { select, subscribe, dispatch } from '@wordpress/data';
import { speak } from '@wordpress/a11y';
import { __ } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
/**
* Internal dependencies
*/
import { metaBoxUpdatesSuccess, requestMetaBoxUpdates } from './actions';
import { getActiveMetaBoxLocations } from './selectors';
import { getMetaBoxContainer } from '../utils/meta-boxes';
let saveMetaboxUnsubscribe;
const effects = {
SET_META_BOXES_PER_LOCATIONS( action, store ) {
// Allow toggling metaboxes panels
// We need to wait for all scripts to load
// If the meta box loads the post script, it will already trigger this.
// After merge in Core, make sure to drop the timeout and update the postboxes script
// to avoid the double binding.
setTimeout( () => {
const postType = select( 'core/editor' ).getCurrentPostType();
if ( window.postboxes.page !== postType ) {
window.postboxes.add_postbox_toggles( postType );
}
} );
let wasSavingPost = select( 'core/editor' ).isSavingPost();
let wasAutosavingPost = select( 'core/editor' ).isAutosavingPost();
// Meta boxes are initialized once at page load. It is not necessary to
// account for updates on each state change.
//
// See: https://github.com/WordPress/WordPress/blob/5.1.1/wp-admin/includes/post.php#L2307-L2309
const hasActiveMetaBoxes = select( 'core/edit-post' ).hasMetaBoxes();
// First remove any existing subscription in order to prevent multiple saves
if ( !! saveMetaboxUnsubscribe ) {
saveMetaboxUnsubscribe();
}
// Save metaboxes when performing a full save on the post.
saveMetaboxUnsubscribe = subscribe( () => {
const isSavingPost = select( 'core/editor' ).isSavingPost();
const isAutosavingPost = select( 'core/editor' ).isAutosavingPost();
// Save metaboxes on save completion, except for autosaves that are not a post preview.
const shouldTriggerMetaboxesSave = (
hasActiveMetaBoxes && (
( wasSavingPost && ! isSavingPost && ! wasAutosavingPost )
)
);
// Save current state for next inspection.
wasSavingPost = isSavingPost;
wasAutosavingPost = isAutosavingPost;
if ( shouldTriggerMetaboxesSave ) {
store.dispatch( requestMetaBoxUpdates() );
}
} );
},
REQUEST_META_BOX_UPDATES( action, store ) {
// Saves the wp_editor fields
if ( window.tinyMCE ) {
window.tinyMCE.triggerSave();
}
const state = store.getState();
// Additional data needed for backward compatibility.
// If we do not provide this data, the post will be overridden with the default values.
const post = select( 'core/editor' ).getCurrentPost( state );
const additionalData = [
post.comment_status ? [ 'comment_status', post.comment_status ] : false,
post.ping_status ? [ 'ping_status', post.ping_status ] : false,
post.sticky ? [ 'sticky', post.sticky ] : false,
post.author ? [ 'post_author', post.author ] : false,
].filter( Boolean );
// We gather all the metaboxes locations data and the base form data
const baseFormData = new window.FormData( document.querySelector( '.metabox-base-form' ) );
const formDataToMerge = [
baseFormData,
...getActiveMetaBoxLocations( state ).map( ( location ) => (
new window.FormData( getMetaBoxContainer( location ) )
) ),
];
// Merge all form data objects into a single one.
const formData = reduce( formDataToMerge, ( memo, currentFormData ) => {
for ( const [ key, value ] of currentFormData ) {
memo.append( key, value );
}
return memo;
}, new window.FormData() );
additionalData.forEach( ( [ key, value ] ) => formData.append( key, value ) );
// Save the metaboxes
apiFetch( {
url: window._wpMetaBoxUrl,
method: 'POST',
body: formData,
parse: false,
} )
.then( () => store.dispatch( metaBoxUpdatesSuccess() ) );
},
SWITCH_MODE( action ) {
// Unselect blocks when we switch to the code editor.
if ( action.mode !== 'visual' ) {
dispatch( 'core/block-editor' ).clearSelectedBlock();
}
const message = action.mode === 'visual' ? __( 'Visual editor selected' ) : __( 'Code editor selected' );
speak( message, 'assertive' );
},
};
export default effects;