-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Edit Post: Refactor state handling for the sidebar #5435
Changes from all commits
06493ed
bc8cced
6f95ede
4c9d517
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,3 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
|
@@ -13,35 +8,26 @@ import { | |
PostSavedState, | ||
PostPublishPanelToggle, | ||
} from '@wordpress/editor'; | ||
import { withDispatch, withSelect } from '@wordpress/data'; | ||
import { compose } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
import MoreMenu from './more-menu'; | ||
import HeaderToolbar from './header-toolbar'; | ||
import { | ||
getOpenedGeneralSidebar, | ||
isPublishSidebarOpened, | ||
hasMetaBoxes, | ||
isSavingMetaBoxes, | ||
} from '../../store/selectors'; | ||
import { | ||
|
||
function Header( { | ||
isEditorSidebarOpened, | ||
openGeneralSidebar, | ||
closeGeneralSidebar, | ||
isPublishSidebarOpened, | ||
togglePublishSidebar, | ||
} from '../../store/actions'; | ||
|
||
function Header( { | ||
isGeneralSidebarEditorOpen, | ||
onOpenGeneralSidebar, | ||
onCloseGeneralSidebar, | ||
isPublishSidebarOpen, | ||
onTogglePublishSidebar, | ||
hasActiveMetaboxes, | ||
isSaving, | ||
} ) { | ||
const toggleGeneralSidebar = isGeneralSidebarEditorOpen ? onCloseGeneralSidebar : onOpenGeneralSidebar; | ||
const toggleGeneralSidebar = isEditorSidebarOpened ? closeGeneralSidebar : openGeneralSidebar; | ||
|
||
return ( | ||
<div | ||
|
@@ -51,25 +37,25 @@ function Header( { | |
tabIndex="-1" | ||
> | ||
<HeaderToolbar /> | ||
{ ! isPublishSidebarOpen && ( | ||
{ ! isPublishSidebarOpened && ( | ||
<div className="edit-post-header__settings"> | ||
<PostSavedState | ||
forceIsDirty={ hasActiveMetaboxes } | ||
forceIsSaving={ isSaving } | ||
/> | ||
<PostPreviewButton /> | ||
<PostPublishPanelToggle | ||
isOpen={ isPublishSidebarOpen } | ||
onToggle={ onTogglePublishSidebar } | ||
isOpen={ isPublishSidebarOpened } | ||
onToggle={ togglePublishSidebar } | ||
forceIsDirty={ hasActiveMetaboxes } | ||
forceIsSaving={ isSaving } | ||
/> | ||
<IconButton | ||
icon="admin-generic" | ||
onClick={ toggleGeneralSidebar } | ||
isToggled={ isGeneralSidebarEditorOpen } | ||
isToggled={ isEditorSidebarOpened } | ||
label={ __( 'Settings' ) } | ||
aria-expanded={ isGeneralSidebarEditorOpen } | ||
aria-expanded={ isEditorSidebarOpened } | ||
/> | ||
<MoreMenu key="more-menu" /> | ||
</div> | ||
|
@@ -78,18 +64,16 @@ function Header( { | |
); | ||
} | ||
|
||
export default connect( | ||
( state ) => ( { | ||
isGeneralSidebarEditorOpen: getOpenedGeneralSidebar( state ) === 'editor', | ||
isPublishSidebarOpen: isPublishSidebarOpened( state ), | ||
hasActiveMetaboxes: hasMetaBoxes( state ), | ||
isSaving: isSavingMetaBoxes( state ), | ||
} ), | ||
{ | ||
onOpenGeneralSidebar: () => openGeneralSidebar( 'editor' ), | ||
onCloseGeneralSidebar: closeGeneralSidebar, | ||
onTogglePublishSidebar: togglePublishSidebar, | ||
}, | ||
undefined, | ||
{ storeKey: 'edit-post' } | ||
export default compose( | ||
withSelect( ( select ) => ( { | ||
isEditorSidebarOpened: select( 'core/edit-post' ).isEditorSidebarOpened(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not directly related to this pr, and even if we decide to some change it should not be in this PR. We are always repeating select( 'core/edit-post' ). This makes me wonder if we should not do something like: const editPostSelect = select( 'core/edit-post' ). Or add an argument to withSelect like withSelect( 'core/edit-post', ( select )... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW this is how I'd documented examples in the updated While it means you can't do an implicit arrow return, I think the whole thing ends up being more readable overall (implicit arrow returns for objects are always questionable). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, it has been discussed a few times in the past. I will find some link and share on Monday :) I might even share a similar idea you mentioned: withSelect( ( select) => {
const { mySelector } = select( 'core/edit-post' );
return {
// some code here
};
} ); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aduth I want to refactor all connect occurrences in the edit-post module next week to see if it has any impact on the module’s bundle size. I’ll follow your advice despite the fact that I personally like this short notation 😃 |
||
isPublishSidebarOpened: select( 'core/edit-post' ).isPublishSidebarOpened(), | ||
hasActiveMetaboxes: select( 'core/edit-post' ).hasMetaBoxes(), | ||
isSaving: select( 'core/edit-post' ).isSavingMetaBoxes(), | ||
} ) ), | ||
withDispatch( ( dispatch ) => ( { | ||
openGeneralSidebar: () => dispatch( 'core/edit-post' ).openGeneralSidebar( 'edit-post/document' ), | ||
closeGeneralSidebar: dispatch( 'core/edit-post' ).closeGeneralSidebar, | ||
togglePublishSidebar: dispatch( 'core/edit-post' ).togglePublishSidebar, | ||
} ) ), | ||
)( Header ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@omarreiss @xyfi @IreneStr, can we get rid of this function completely and start using
dispatch( 'edit-post' ).openGeneralSidebar( name );
directly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question 2, should we rename
openGeneralSidebar
toopenEditorSidebar
and use the name editor sidebar in other places?On the other hand, I created
isEditorSidebarOpened
selector to check if block or document settings are opened in the sidebar.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it would be fine to get rid of this function. I still see the use for a higher level API like this because it simply requires less knowledge of the user. But maybe it is a bit premature to be adding API's like this one at this stage. I would like to make a knowledge matrix further down the road to map what knowledge is needed to solve a particular kind of problem in Gutenberg as a plugin author. Based on that we could always decide to reintroduce helpers like this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will open a separate PR for that together with docs update. This PR got big once I started integrating
data
module. We can wrap it later back with something that you will find out helpful when integrating plugins.