Skip to content

Keep save button text updated, without errors. #585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion blocks/dist/custom-status.build.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 46 additions & 38 deletions blocks/src/custom-status/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,56 @@ let { SelectControl } = wp.components;
let statuses = window.EditFlowCustomStatuses.map( s => ({ label: s.name, value: s.slug }) );

/**
* Hack :(
*
* @see https://github.com/WordPress/gutenberg/issues/3144
*
* 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.
*
* 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.
* Subscribe to changes so we can set a default status and update a button's text.
*/
let sideEffectL10nManipulation = () => {
let node = document.querySelector('.editor-post-save-draft');
if ( node ) {
document.querySelector( '.editor-post-save-draft' ).innerText = `${ __( 'Save' ) }`
}
}

// Set the status to the default custom status.
let buttonTextObserver = null;
subscribe( function () {
const postId = select( 'core/editor' ).getCurrentPostId();
// Post isn't ready yet so don't do anything.
if ( ! postId ) {
return;
}

// For new posts, we need to force the our default custom status.
// Otherwise WordPress will force it to "Draft".
const isCleanNewPost = select( 'core/editor' ).isCleanNewPost();
if ( isCleanNewPost ) {
dispatch( 'core/editor' ).editPost( {
status: ef_default_custom_status
} );

return;
}

// Update the "Save" button.
var status = select( 'core/editor' ).getEditedPostAttribute( 'status' );
if ( typeof status !== 'undefined' && status !== 'publish' ) {
sideEffectL10nManipulation();
}
const postId = select( 'core/editor' ).getCurrentPostId();
if ( ! postId ) {
// Post isn't ready yet so don't do anything.
return;
}

// For new posts, we need to force the default custom status.
const isCleanNewPost = select( 'core/editor' ).isCleanNewPost();
if ( isCleanNewPost ) {
dispatch( 'core/editor' ).editPost( {
status: ef_default_custom_status
} );
}

// If the save button exists, let's update the text if needed.
maybeUpdateButtonText( document.querySelector( '.editor-post-save-draft' ) );

// The post is being saved, so we need to set up an observer to update the button text when it's back.
if ( buttonTextObserver === null && window.MutationObserver && select( 'core/editor' ).isSavingPost() ) {
buttonTextObserver = createButtonObserver( document.querySelector( '.edit-post-header__settings' ) );
}
} );

function createButtonObserver( parentNode ) {
if ( ! parentNode ) {
return null;
}

const observer = new MutationObserver( ( mutationsList ) => {
for ( const mutation of mutationsList ) {
for ( const node of mutation.addedNodes ) {
maybeUpdateButtonText( node );
}
}
} );

observer.observe( parentNode, { childList: true } );
return observer;
}

function maybeUpdateButtonText( saveButton ) {
if ( saveButton && ( saveButton.innerText === __( 'Save Draft' ) || saveButton.innerText === __( 'Save as Pending' ) ) ) {
saveButton.innerText = __( 'Save' );
}
}

/**
* Custom status component
* @param object props
Expand Down Expand Up @@ -88,7 +97,6 @@ const mapDispatchToProps = ( dispatch ) => {
return {
onUpdate( status ) {
dispatch( 'core/editor' ).editPost( { status } );
sideEffectL10nManipulation();
},
};
};
Expand Down