-
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
Move the post title selection state to the store and update PostTitle #16704
Changes from 5 commits
630d474
4c7ca6a
e1cc965
e787c58
cc71382
6b38663
1e248f1
243cd5d
5424076
db39db5
b58b35a
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 |
---|---|---|
|
@@ -32,27 +32,12 @@ class PostTitle extends Component { | |
super( ...arguments ); | ||
|
||
this.onChange = this.onChange.bind( this ); | ||
this.onSelect = this.onSelect.bind( this ); | ||
this.onUnselect = this.onUnselect.bind( this ); | ||
this.onKeyDown = this.onKeyDown.bind( this ); | ||
this.redirectHistory = this.redirectHistory.bind( this ); | ||
|
||
this.state = { | ||
isSelected: false, | ||
}; | ||
} | ||
|
||
handleFocusOutside() { | ||
this.onUnselect(); | ||
} | ||
|
||
onSelect() { | ||
this.setState( { isSelected: true } ); | ||
this.props.clearSelectedBlock(); | ||
} | ||
|
||
onUnselect() { | ||
this.setState( { isSelected: false } ); | ||
this.props.onUnselect(); | ||
} | ||
|
||
onChange( event ) { | ||
|
@@ -93,11 +78,11 @@ class PostTitle extends Component { | |
isCleanNewPost, | ||
isFocusMode, | ||
isPostTypeViewable, | ||
isSelected, | ||
instanceId, | ||
placeholder, | ||
title, | ||
} = this.props; | ||
const { isSelected } = this.state; | ||
|
||
// The wp-block className is important for editor styles. | ||
const className = classnames( 'wp-block editor-post-title__block', { | ||
|
@@ -126,9 +111,9 @@ class PostTitle extends Component { | |
value={ title } | ||
onChange={ this.onChange } | ||
placeholder={ decodedPlaceholder || __( 'Add title' ) } | ||
onFocus={ this.onSelect } | ||
onFocus={ this.props.onSelect } | ||
onKeyDown={ this.onKeyDown } | ||
onKeyPress={ this.onUnselect } | ||
onKeyPress={ this.props.onUnselect } | ||
/* | ||
Only autofocus the title when the post is entirely empty. | ||
This should only happen for a new post, which means we | ||
|
@@ -149,14 +134,15 @@ class PostTitle extends Component { | |
} | ||
|
||
const applyWithSelect = withSelect( ( select ) => { | ||
const { getEditedPostAttribute, isCleanNewPost } = select( 'core/editor' ); | ||
const { getEditedPostAttribute, isCleanNewPost, isPostTitleSelected } = select( 'core/editor' ); | ||
const { getSettings } = select( 'core/block-editor' ); | ||
const { getPostType } = select( 'core' ); | ||
const postType = getPostType( getEditedPostAttribute( 'type' ) ); | ||
const { titlePlaceholder, focusMode, hasFixedToolbar } = getSettings(); | ||
|
||
return { | ||
isCleanNewPost: isCleanNewPost(), | ||
isSelected: isPostTitleSelected(), | ||
title: getEditedPostAttribute( 'title' ), | ||
isPostTypeViewable: get( postType, [ 'viewable' ], false ), | ||
placeholder: titlePlaceholder, | ||
|
@@ -174,6 +160,7 @@ const applyWithDispatch = withDispatch( ( dispatch ) => { | |
editPost, | ||
undo, | ||
redo, | ||
togglePostTitleSelection, | ||
} = dispatch( 'core/editor' ); | ||
|
||
return { | ||
|
@@ -185,7 +172,13 @@ const applyWithDispatch = withDispatch( ( dispatch ) => { | |
}, | ||
onUndo: undo, | ||
onRedo: redo, | ||
clearSelectedBlock, | ||
onSelect() { | ||
togglePostTitleSelection( true ); | ||
clearSelectedBlock(); | ||
}, | ||
onUnselect() { | ||
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. I didn't know es6 allowed us to declare functions this way. Thanks for the TIL! 😎 |
||
togglePostTitleSelection( false ); | ||
}, | ||
}; | ||
} ); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ import { isEmpty } from 'lodash'; | |
import { Component } from '@wordpress/element'; | ||
import { RichText } from '@wordpress/rich-text'; | ||
import { decodeEntities } from '@wordpress/html-entities'; | ||
import { withDispatch } from '@wordpress/data'; | ||
import { withDispatch, withSelect } from '@wordpress/data'; | ||
import { withFocusOutside } from '@wordpress/components'; | ||
import { withInstanceId, compose } from '@wordpress/compose'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
|
@@ -91,27 +91,43 @@ class PostTitle extends Component { | |
} | ||
} | ||
|
||
const applyWithDispatch = withDispatch( ( dispatch ) => { | ||
const { | ||
undo, | ||
redo, | ||
} = dispatch( 'core/editor' ); | ||
export default compose( | ||
withSelect( ( dispatch ) => { | ||
const { | ||
isPostTitleSelected, | ||
} = dispatch( 'core/editor' ); | ||
|
||
const { | ||
insertDefaultBlock, | ||
} = dispatch( 'core/block-editor' ); | ||
return { | ||
isSelected: isPostTitleSelected(), | ||
}; | ||
} ), | ||
withDispatch( ( dispatch ) => { | ||
const { | ||
undo, | ||
redo, | ||
togglePostTitleSelection, | ||
} = dispatch( 'core/editor' ); | ||
|
||
return { | ||
onEnterPress() { | ||
insertDefaultBlock( undefined, undefined, 0 ); | ||
}, | ||
onUndo: undo, | ||
onRedo: redo, | ||
}; | ||
} ); | ||
const { | ||
clearSelectedBlock, | ||
insertDefaultBlock, | ||
} = dispatch( 'core/block-editor' ); | ||
|
||
export default compose( | ||
applyWithDispatch, | ||
return { | ||
onEnterPress() { | ||
insertDefaultBlock( undefined, undefined, 0 ); | ||
}, | ||
onUndo: undo, | ||
onRedo: redo, | ||
onSelect() { | ||
togglePostTitleSelection( true ); | ||
clearSelectedBlock(); | ||
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. Asking more for my understanding: is it worth considering handling the 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. A reducer should only ever update the state so I don't think it would be appropriate. An action could be calling another action but I think it's more explicit to have it here, especially since it's a different store! 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. Good point about the reducer just updating the store, and I totally overlooked the different store. Thanks! 👍 |
||
}, | ||
onUnselect() { | ||
togglePostTitleSelection( false ); | ||
}, | ||
}; | ||
} ), | ||
withInstanceId, | ||
withFocusOutside | ||
)( PostTitle ); |
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.
Just curious, is there a reason to use the
isPostTitleSelected
selector here in visual-editor and pass the resulting value down to theBlockList
as a prop instead of just using theisPostTitleSelected
selector directly inside ofBlockList
itself?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 simply wanted to minimize the changes to the native part here. #16677 will be responsible of refactoring the VisualEditor and moving the block picker out of BlockList into an
Inserter
component.