@@ -14,46 +14,62 @@ let { SelectControl } = wp.components;
1414let statuses = window . EditFlowCustomStatuses . map ( s => ( { label : s . name , value : s . slug } ) ) ;
1515
1616/**
17- * Hack :(
18- *
19- * @see https://github.com/WordPress/gutenberg/issues/3144
20- *
21- * Gutenberg overrides the label of the Save button after save (i.e. "Save Draft"). But there's no way to subscribe to a "post save" message.
17+ * Subscribe to changes so we can set a default status and update a button's text.
18+ */
19+ let buttonTextObserver = null ;
20+ subscribe ( function ( ) {
21+ const postId = select ( 'core/editor' ) . getCurrentPostId ( ) ;
22+ if ( ! postId ) {
23+ // Post isn't ready yet so don't do anything.
24+ return ;
25+ }
26+
27+ // For new posts, we need to force the default custom status.
28+ const isCleanNewPost = select ( 'core/editor' ) . isCleanNewPost ( ) ;
29+ if ( isCleanNewPost ) {
30+ dispatch ( 'core/editor' ) . editPost ( {
31+ status : ef_default_custom_status
32+ } ) ;
33+ }
34+
35+ // If the save button exists, let's update the text if needed.
36+ maybeUpdateButtonText ( document . querySelector ( '.editor-post-save-draft' ) ) ;
37+
38+ // The post is being saved, so we need to set up an observer to update the button text when it's back.
39+ if ( buttonTextObserver === null && window . MutationObserver && select ( 'core/editor' ) . isSavingPost ( ) ) {
40+ buttonTextObserver = createButtonObserver ( document . querySelector ( '.edit-post-header__settings' ) ) ;
41+ }
42+ } ) ;
43+
44+ /**
45+ * Create a mutation observer that will update the
46+ * save button text right away when it's changed/re-added.
2247 *
23- * So instead, we're keeping the button label generic ("Save"). There's a brief period where it still flips to "Save Draft" but that's something we need to work upstream to find a good fix for.
48+ * Ideally there will be better ways to go about this in the future.
49+ * @see https://github.com/Automattic/Edit-Flow/issues/583
2450 */
25- let sideEffectL10nManipulation = ( ) => {
26- let node = document . querySelector ( '.editor-post-save-draft' ) ;
27- if ( node ) {
28- document . querySelector ( '.editor-post-save-draft' ) . innerText = `${ __ ( 'Save' ) } `
29- }
51+ function createButtonObserver ( parentNode ) {
52+ if ( ! parentNode ) {
53+ return null ;
54+ }
55+
56+ const observer = new MutationObserver ( ( mutationsList ) => {
57+ for ( const mutation of mutationsList ) {
58+ for ( const node of mutation . addedNodes ) {
59+ maybeUpdateButtonText ( node ) ;
60+ }
61+ }
62+ } ) ;
63+
64+ observer . observe ( parentNode , { childList : true } ) ;
65+ return observer ;
3066}
3167
32- // Set the status to the default custom status.
33- subscribe ( function ( ) {
34- const postId = select ( 'core/editor' ) . getCurrentPostId ( ) ;
35- // Post isn't ready yet so don't do anything.
36- if ( ! postId ) {
37- return ;
38- }
39-
40- // For new posts, we need to force the our default custom status.
41- // Otherwise WordPress will force it to "Draft".
42- const isCleanNewPost = select ( 'core/editor' ) . isCleanNewPost ( ) ;
43- if ( isCleanNewPost ) {
44- dispatch ( 'core/editor' ) . editPost ( {
45- status : ef_default_custom_status
46- } ) ;
47-
48- return ;
49- }
50-
51- // Update the "Save" button.
52- var status = select ( 'core/editor' ) . getEditedPostAttribute ( 'status' ) ;
53- if ( typeof status !== 'undefined' && status !== 'publish' ) {
54- sideEffectL10nManipulation ( ) ;
55- }
56- } ) ;
68+ function maybeUpdateButtonText ( saveButton ) {
69+ if ( saveButton && ( saveButton . innerText === __ ( 'Save Draft' ) || saveButton . innerText === __ ( 'Save as Pending' ) ) ) {
70+ saveButton . innerText = __ ( 'Save' ) ;
71+ }
72+ }
5773
5874/**
5975 * Custom status component
@@ -88,7 +104,6 @@ const mapDispatchToProps = ( dispatch ) => {
88104 return {
89105 onUpdate ( status ) {
90106 dispatch ( 'core/editor' ) . editPost ( { status } ) ;
91- sideEffectL10nManipulation ( ) ;
92107 } ,
93108 } ;
94109} ;
0 commit comments