-
Notifications
You must be signed in to change notification settings - Fork 4.7k
PrePublishPanel: suggest tags and postformat #8235
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
Changes from all commits
151f9e9
4d8af26
1becfb8
ed9753b
dfb60da
870b5e7
7218b33
b0c1464
c98522c
83635bc
0a0cd18
9f53604
ac4e132
f8d386d
ab0bf56
9ef07b3
b2073e8
603c7a1
e40d773
620dbbd
1baa309
053038d
5d18b23
ed42917
52ab3d3
e230479
713d5a6
c1ac898
afffd27
dc3d61a
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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { find, get, includes } from 'lodash'; | ||
|
|
||
| /** | ||
| * WordPress dependencies. | ||
| */ | ||
| import { __, sprintf } from '@wordpress/i18n'; | ||
| import { ifCondition, compose } from '@wordpress/compose'; | ||
| import { withDispatch, withSelect } from '@wordpress/data'; | ||
| import { Button, PanelBody } from '@wordpress/components'; | ||
|
|
||
| /** | ||
| * Internal dependencies. | ||
| */ | ||
| import { POST_FORMATS } from '../post-format'; | ||
|
|
||
| const PostFormatSuggestion = ( { suggestedPostFormat, suggestionText, onUpdatePostFormat } ) => ( | ||
| <Button isLink onClick={ () => onUpdatePostFormat( suggestedPostFormat ) }> | ||
| { suggestionText } | ||
| </Button> | ||
| ); | ||
|
|
||
| const PostFormatPanel = ( { suggestion, onUpdatePostFormat } ) => { | ||
| const panelBodyTitle = [ | ||
| __( 'Suggestion:' ), | ||
| ( | ||
| <span className="editor-post-publish-panel__link" key="label"> | ||
| { __( 'Use a post format' ) } | ||
| </span> | ||
| ), | ||
| ]; | ||
|
|
||
| return ( | ||
| <PanelBody initialOpen={ false } title={ panelBodyTitle } > | ||
| <p> | ||
| { __( 'Your theme uses post formats to highlight different kinds of content, like images or videos. Apply a post format to see this special styling.' ) } | ||
| </p> | ||
| <p> | ||
| <PostFormatSuggestion | ||
| onUpdatePostFormat={ onUpdatePostFormat } | ||
| suggestedPostFormat={ suggestion.id } | ||
| suggestionText={ sprintf( | ||
| __( 'Apply the "%1$s" format.' ), | ||
|
Contributor
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. This misses a translators comment. Will try to address it in #9363 |
||
| suggestion.caption | ||
| ) } | ||
| /> | ||
| </p> | ||
| </PanelBody> | ||
| ); | ||
| }; | ||
|
|
||
| const getSuggestion = ( supportedFormats, suggestedPostFormat ) => { | ||
| const formats = POST_FORMATS.filter( ( format ) => includes( supportedFormats, format.id ) ); | ||
| return find( formats, ( format ) => format.id === suggestedPostFormat ); | ||
| }; | ||
|
|
||
| export default compose( | ||
| withSelect( ( select ) => { | ||
| const { getEditedPostAttribute, getSuggestedPostFormat } = select( 'core/editor' ); | ||
| const supportedFormats = get( select( 'core' ).getThemeSupports(), [ 'formats' ], [] ); | ||
| return { | ||
| currentPostFormat: getEditedPostAttribute( 'format' ), | ||
| suggestion: getSuggestion( supportedFormats, getSuggestedPostFormat() ), | ||
| }; | ||
| } ), | ||
| withDispatch( ( dispatch ) => ( { | ||
| onUpdatePostFormat( postFormat ) { | ||
| dispatch( 'core/editor' ).editPost( { format: postFormat } ); | ||
| }, | ||
| } ) ), | ||
| ifCondition( ( { suggestion, currentPostFormat } ) => suggestion && suggestion.id !== currentPostFormat ), | ||
| )( PostFormatPanel ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { Component } from '@wordpress/element'; | ||
| import { compose, ifCondition } from '@wordpress/compose'; | ||
| import { withSelect } from '@wordpress/data'; | ||
| import { PanelBody } from '@wordpress/components'; | ||
|
|
||
| import FlatTermSelector from '../post-taxonomies/flat-term-selector'; | ||
|
|
||
| const TagsPanel = () => { | ||
| const panelBodyTitle = [ | ||
| __( 'Suggestion:' ), | ||
| ( | ||
| <span className="editor-post-publish-panel__link" key="label"> | ||
| { __( 'Add tags' ) } | ||
| </span> | ||
| ), | ||
| ]; | ||
|
|
||
| return ( | ||
| <PanelBody initialOpen={ false } title={ panelBodyTitle }> | ||
| <p> | ||
| { __( 'Tags help users and search engines navigate your site and find your content. Add a few keywords to describe your post.' ) } | ||
| </p> | ||
| <FlatTermSelector slug={ 'post_tag' } /> | ||
| </PanelBody> | ||
| ); | ||
| }; | ||
|
|
||
| class MaybeTagsPanel extends Component { | ||
| constructor( props ) { | ||
| super( props ); | ||
| this.state = { | ||
| hadTagsWhenOpeningThePanel: props.hasTags, | ||
| }; | ||
| } | ||
|
|
||
| /* | ||
| * We only want to show the tag panel if the post didn't have | ||
| * any tags when the user hit the Publish button. | ||
| * | ||
| * We can't use the prop.hasTags because it'll change to true | ||
| * if the user adds a new tag within the pre-publish panel. | ||
| * This would force a re-render and a new prop.hasTags check, | ||
| * hiding this panel and keeping the user from adding | ||
| * more than one tag. | ||
| */ | ||
| render() { | ||
| if ( ! this.state.hadTagsWhenOpeningThePanel ) { | ||
| return <TagsPanel />; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
|
|
||
| export default compose( | ||
| withSelect( ( select ) => { | ||
| const postType = select( 'core/editor' ).getCurrentPostType(); | ||
| const tagsTaxonomy = select( 'core' ).getTaxonomy( 'post_tag' ); | ||
| return { | ||
| areTagsFetched: tagsTaxonomy !== undefined, | ||
| isPostTypeSupported: tagsTaxonomy && tagsTaxonomy.types.some( ( type ) => type === postType ), | ||
| hasTags: tagsTaxonomy && select( 'core/editor' ).getEditedPostAttribute( tagsTaxonomy.rest_base ).length, | ||
|
Member
Author
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 learned that pages don't have taxonomies the hard way #9082 |
||
| }; | ||
| } ), | ||
| ifCondition( ( { areTagsFetched, isPostTypeSupported } ) => isPostTypeSupported && areTagsFetched ), | ||
| )( MaybeTagsPanel ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,8 @@ import PostVisibility from '../post-visibility'; | |
| import PostVisibilityLabel from '../post-visibility/label'; | ||
| import PostSchedule from '../post-schedule'; | ||
| import PostScheduleLabel from '../post-schedule/label'; | ||
| import MaybeTagsPanel from './maybe-tags-panel'; | ||
| import MaybePostFormatPanel from './maybe-post-format-panel'; | ||
|
|
||
| function PostPublishPanelPrepublish( { | ||
| hasPublishAction, | ||
|
|
@@ -41,6 +43,8 @@ function PostPublishPanelPrepublish( { | |
| ] }> | ||
| <PostSchedule /> | ||
| </PanelBody> | ||
| <MaybePostFormatPanel /> | ||
|
Member
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. Have you checked how https://github.com/WordPress/gutenberg/tree/master/packages/editor/src/components/post-schedule They contain two parts the components which render the panel's body and a wrapper component with suffix @youknowriad, why we don't wrap those components with checks in the publish panel? We do it in the sidebar:
Contributor
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. Because it's already taken care of in the component itself. In the sidebar, we don't want the extra panels if the check is false, here, there's no extra wrappers added.
Member
Author
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 can extract the |
||
| <MaybeTagsPanel /> | ||
| { children } | ||
| </Fragment> | ||
| ) } | ||
|
|
||
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.
Seems odd to replicate this here when it's likely in the API (single source of truth is good), but I get it's from before your commit. 🤷♂️