Skip to content
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

MetaBoxes: Moving meta boxes out of the generic editor module #5175

Merged
merged 2 commits into from
Feb 23, 2018

Conversation

youknowriad
Copy link
Contributor

The editor module is a generic module meant to be used to build any editing interface (template editor, post editor, p2 editor...) it shouldn't be concerned about the meta boxes which are post editor specific.

I'm pretty happy with this PR, I was able to extract all the meta box logic out of the editor module by doing:

  • Moving the components to the edit-post module
  • Moving the metabox state to the edit-post module
  • Hooking into the PostSavedStatus component to show the isLoading state when the meta boxes are loading and to always consider the post dirty if there are meta boxes on the page
  • Triggering meta boxes saving by watching isSavingPost selector. When the post was being saved and it's not anymore, trigger the meta boxes save behavior.

Testing instructions

  • Add some metaboxes
  • The "save" link should always be visible
  • When you save a post, the meta boxes are saved as well
  • When the meta boxes are saving, the top bar shows a loading state.

@youknowriad youknowriad added the [Feature] Meta Boxes A draggable box shown on the post editing screen label Feb 21, 2018
@youknowriad youknowriad self-assigned this Feb 21, 2018
@@ -52,6 +53,7 @@ function Layout( {
<div className={ className }>
<DocumentTitle />
<UnsavedChangesWarning />
<MetaBoxesUnsavedWarning />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic to prevent from leaving the page needed a separate component for preventing leaving the page when the meta boxes are dirty.

@youknowriad
Copy link
Contributor Author

youknowriad commented Feb 21, 2018

The changes are not so easy to understand but basically, it's just moving pieces aside what I mention in the PR description.

@youknowriad youknowriad mentioned this pull request Feb 21, 2018
3 tasks
@youknowriad
Copy link
Contributor Author

Also with this change, I considered building a separate JavaScript module to handle the meta boxes, the edit-post could include depend on the module and any other WPAdmin screen having metaboxes could use it to add meta-boxes support.

If you think I should go this way, I can update th PR to do so.

@youknowriad youknowriad force-pushed the refactor/move-meta-box-out-of-editor branch 2 times, most recently from 4effe6b to 4ea8dd0 Compare February 21, 2018 14:40
Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Largely looks good and works well 👍

post: getCurrentPost( state ),
isNew: isEditedPostNew( state ),
isPublished: isCurrentPostPublished( state ),
isDirty: isEditedPostDirty( state ),
isSaving: isSavingPost( state ),
isDirty: isEditedPostDirty( state ) || forceIsDirty,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: In this case the selector implementation is trivial, but caught my attention to have the function call as the first part of the condition, as if we expect forceIsDirty to be a simple truthy check, we could put it first and take advantage of short-circuit evaluation.

@@ -17,7 +18,7 @@ import { mobileMiddleware } from '../utils/mobile';
*/
function applyMiddlewares( store ) {
const middlewares = [
mobileMiddleware,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this just never existed and did nothing?

(Note: Will conflict with #5206)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was removed in the plugin API sidebar but the files was kept


class MetaBoxesUnsavedWarning extends Component {
/**
* @inheritdoc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we care for these JSDoc? I'm guessing from copy paste of underlying UnsavedChangesWarning.

Speaking of, it would be nice it this wasn't so non-DRY 😬

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I was wondering if I should create another component or provide an extensibility APi or a prop or something for the other one, I thought this one is simpler to understand, but I can update to introduce a callaback to enhance the isDirty check.

}
}

export default connect(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With #5137, maybe update to use withSelect ?

@@ -86,4 +86,9 @@ function mapStateToProps( state ) {
};
}

export default connect( mapStateToProps )( MetaBoxesArea );
export default connect(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With #5137, maybe update to use withSelect ? Might require registering more/all selectors for edit-post (also done in #5206).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should do a separate PR and move all of these at once :)

@@ -49,7 +53,10 @@ function Header( {
<HeaderToolbar />
{ ! isPublishSidebarOpen && (
<div className="edit-post-header__settings">
<PostSavedState />
<PostSavedState
forceIsDirty={ hasActiveMetaboxes }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: Curious if we need to distinguish this from the presentational isDirty prop, or if we ought to just pass this as isDirty and defer to use it in the mapStateToProps.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean shortcut the isDirty selector entirely right? which would also mean add a check for post dirtiness here. I don't feel strongly either way.

@@ -966,7 +920,8 @@ export function isBlockInsertionPointVisible( state ) {
* @return {boolean} Whether post is being saved.
*/
export function isSavingPost( state ) {
return state.saving.requesting || isSavingMetaBoxes( state );
// TODO: filter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what way or reason are you suggesting "filter" ?

Edit: I guess so we wouldn't have to pass forceIsDirty and forceIsSaving as props?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, actually I added these comments before adding the props, I think I can remove them right now :)

@youknowriad youknowriad force-pushed the refactor/move-meta-box-out-of-editor branch from 4ea8dd0 to 35cdb5c Compare February 23, 2018 08:33
@youknowriad youknowriad merged commit ee3a61c into master Feb 23, 2018
@youknowriad youknowriad deleted the refactor/move-meta-box-out-of-editor branch February 23, 2018 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Meta Boxes A draggable box shown on the post editing screen
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants