-
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
Add home template details to inspector controls #61762
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
839a3af
Add home template details to inspector controls
ntsekouras 14795c1
move components to own folders
ntsekouras 988e486
remove wrapper component and local state in the other components
ntsekouras 1003e8f
Move the panels to the right place
youknowriad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { debounce } from '@wordpress/compose'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { decodeEntities } from '@wordpress/html-entities'; | ||
import { | ||
Button, | ||
Dropdown, | ||
__experimentalInputControl as InputControl, | ||
} from '@wordpress/components'; | ||
import { useState, useMemo } from '@wordpress/element'; | ||
import { __experimentalInspectorPopoverHeader as InspectorPopoverHeader } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { TEMPLATE_POST_TYPE } from '../../store/constants'; | ||
import PostPanelRow from '../post-panel-row'; | ||
import { store as editorStore } from '../../store'; | ||
|
||
const EMPTY_OBJECT = {}; | ||
|
||
export default function BlogTitle() { | ||
const { editEntityRecord } = useDispatch( coreStore ); | ||
const { postsPageTitle, postsPageId, isTemplate, postSlug } = useSelect( | ||
( select ) => { | ||
const { getEntityRecord, getEditedEntityRecord } = | ||
select( coreStore ); | ||
const siteSettings = getEntityRecord( 'root', 'site' ); | ||
const _postsPageRecord = siteSettings?.page_for_posts | ||
? getEditedEntityRecord( | ||
'postType', | ||
'page', | ||
siteSettings?.page_for_posts | ||
) | ||
: EMPTY_OBJECT; | ||
const { getEditedPostAttribute, getCurrentPostType } = | ||
select( editorStore ); | ||
return { | ||
postsPageId: _postsPageRecord?.id, | ||
postsPageTitle: _postsPageRecord?.title, | ||
isTemplate: getCurrentPostType() === TEMPLATE_POST_TYPE, | ||
postSlug: getEditedPostAttribute( 'slug' ), | ||
}; | ||
}, | ||
[] | ||
); | ||
// Use internal state instead of a ref to make sure that the component | ||
// re-renders when the popover's anchor updates. | ||
const [ popoverAnchor, setPopoverAnchor ] = useState( null ); | ||
// Memoize popoverProps to avoid returning a new object every time. | ||
const popoverProps = useMemo( | ||
() => ( { | ||
// Anchor the popover to the middle of the entire row so that it doesn't | ||
// move around when the label changes. | ||
anchor: popoverAnchor, | ||
placement: 'left-start', | ||
offset: 36, | ||
shift: true, | ||
} ), | ||
[ popoverAnchor ] | ||
); | ||
|
||
if ( | ||
! isTemplate || | ||
! [ 'home', 'index' ].includes( postSlug ) || | ||
! postsPageId | ||
) { | ||
return null; | ||
} | ||
|
||
const setPostsPageTitle = ( newValue ) => { | ||
editEntityRecord( 'postType', 'page', postsPageId, { | ||
title: newValue, | ||
} ); | ||
}; | ||
const decodedTitle = decodeEntities( postsPageTitle ); | ||
return ( | ||
<PostPanelRow label={ __( 'Blog title' ) } ref={ setPopoverAnchor }> | ||
<Dropdown | ||
popoverProps={ popoverProps } | ||
contentClassName="editor-blog-title-dropdown__content" | ||
focusOnMount | ||
renderToggle={ ( { isOpen, onToggle } ) => ( | ||
<Button | ||
size="compact" | ||
variant="tertiary" | ||
aria-expanded={ isOpen } | ||
aria-label={ sprintf( | ||
// translators: %s: Current post link. | ||
__( 'Change blog title: %s' ), | ||
decodedTitle | ||
) } | ||
onClick={ onToggle } | ||
> | ||
{ decodedTitle } | ||
</Button> | ||
) } | ||
renderContent={ ( { onClose } ) => ( | ||
<> | ||
<InspectorPopoverHeader | ||
title={ __( 'Blog title' ) } | ||
onClose={ onClose } | ||
/> | ||
<InputControl | ||
placeholder={ __( 'No Title' ) } | ||
size={ '__unstable-large' } | ||
value={ postsPageTitle } | ||
onChange={ debounce( setPostsPageTitle, 300 ) } | ||
label={ __( 'Blog title' ) } | ||
help={ __( | ||
'Set the Posts Page title. Appears in search results, and when the page is shared on social media.' | ||
) } | ||
hideLabelFromVision | ||
/> | ||
</> | ||
) } | ||
/> | ||
</PostPanelRow> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.editor-blog-title-dropdown__content .components-popover__content { | ||
min-width: 320px; | ||
padding: $grid-unit-20; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { | ||
Button, | ||
Dropdown, | ||
__experimentalNumberControl as NumberControl, | ||
} from '@wordpress/components'; | ||
import { useState, useMemo } from '@wordpress/element'; | ||
import { __experimentalInspectorPopoverHeader as InspectorPopoverHeader } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { TEMPLATE_POST_TYPE } from '../../store/constants'; | ||
import { store as editorStore } from '../../store'; | ||
import PostPanelRow from '../post-panel-row'; | ||
|
||
export default function PostsPerPage() { | ||
const { editEntityRecord } = useDispatch( coreStore ); | ||
const { postsPerPage, isTemplate, postSlug } = useSelect( ( select ) => { | ||
const { getEditedPostAttribute, getCurrentPostType } = | ||
select( editorStore ); | ||
const { getEditedEntityRecord } = select( coreStore ); | ||
const siteSettings = getEditedEntityRecord( 'root', 'site' ); | ||
return { | ||
isTemplate: getCurrentPostType() === TEMPLATE_POST_TYPE, | ||
postSlug: getEditedPostAttribute( 'slug' ), | ||
postsPerPage: siteSettings?.posts_per_page || 1, | ||
}; | ||
}, [] ); | ||
// Use internal state instead of a ref to make sure that the component | ||
// re-renders when the popover's anchor updates. | ||
const [ popoverAnchor, setPopoverAnchor ] = useState( null ); | ||
// Memoize popoverProps to avoid returning a new object every time. | ||
const popoverProps = useMemo( | ||
() => ( { | ||
// Anchor the popover to the middle of the entire row so that it doesn't | ||
// move around when the label changes. | ||
anchor: popoverAnchor, | ||
placement: 'left-start', | ||
offset: 36, | ||
shift: true, | ||
} ), | ||
[ popoverAnchor ] | ||
); | ||
|
||
if ( ! isTemplate || ! [ 'home', 'index' ].includes( postSlug ) ) { | ||
return null; | ||
} | ||
const setPostsPerPage = ( newValue ) => { | ||
editEntityRecord( 'root', 'site', undefined, { | ||
posts_per_page: newValue, | ||
} ); | ||
}; | ||
return ( | ||
<PostPanelRow label={ __( 'Posts per page' ) } ref={ setPopoverAnchor }> | ||
<Dropdown | ||
popoverProps={ popoverProps } | ||
contentClassName="editor-posts-per-page-dropdown__content" | ||
focusOnMount | ||
renderToggle={ ( { isOpen, onToggle } ) => ( | ||
<Button | ||
size="compact" | ||
variant="tertiary" | ||
aria-expanded={ isOpen } | ||
aria-label={ __( 'Change posts per page' ) } | ||
onClick={ onToggle } | ||
> | ||
{ postsPerPage } | ||
</Button> | ||
) } | ||
renderContent={ ( { onClose } ) => ( | ||
<> | ||
<InspectorPopoverHeader | ||
title={ __( 'Posts per page' ) } | ||
onClose={ onClose } | ||
/> | ||
<NumberControl | ||
placeholder={ 0 } | ||
value={ postsPerPage } | ||
size={ '__unstable-large' } | ||
spinControls="custom" | ||
step="1" | ||
min="1" | ||
onChange={ setPostsPerPage } | ||
label={ __( 'Posts per page' ) } | ||
help={ __( | ||
'Set the default number of posts to display on blog pages, including categories and tags. Some templates may override this setting.' | ||
) } | ||
hideLabelFromVision | ||
/> | ||
</> | ||
) } | ||
/> | ||
</PostPanelRow> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.editor-posts-per-page-dropdown__content .components-popover__content { | ||
min-width: 320px; | ||
padding: $grid-unit-20; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
packages/editor/src/components/site-discussion/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { | ||
Button, | ||
Dropdown, | ||
RadioControl, | ||
__experimentalVStack as VStack, | ||
__experimentalText as Text, | ||
} from '@wordpress/components'; | ||
import { useState, useMemo } from '@wordpress/element'; | ||
import { __experimentalInspectorPopoverHeader as InspectorPopoverHeader } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { TEMPLATE_POST_TYPE } from '../../store/constants'; | ||
import PostPanelRow from '../post-panel-row'; | ||
import { store as editorStore } from '../../store'; | ||
|
||
const COMMENT_OPTIONS = [ | ||
{ | ||
label: ( | ||
<> | ||
{ __( 'Open' ) } | ||
<Text variant="muted" size={ 12 }> | ||
{ __( 'Visitors can add new comments and replies.' ) } | ||
</Text> | ||
</> | ||
), | ||
value: 'open', | ||
}, | ||
{ | ||
label: ( | ||
<> | ||
{ __( 'Closed' ) } | ||
<Text variant="muted" size={ 12 }> | ||
{ __( 'Visitors cannot add new comments or replies.' ) } | ||
</Text> | ||
<Text variant="muted" size={ 12 }> | ||
{ __( 'Existing comments remain visible.' ) } | ||
</Text> | ||
</> | ||
), | ||
value: '', | ||
}, | ||
]; | ||
|
||
export default function SiteDiscussion() { | ||
const { editEntityRecord } = useDispatch( coreStore ); | ||
const { allowCommentsOnNewPosts, isTemplate, postSlug } = useSelect( | ||
( select ) => { | ||
const { getEditedPostAttribute, getCurrentPostType } = | ||
select( editorStore ); | ||
const { getEditedEntityRecord } = select( coreStore ); | ||
const siteSettings = getEditedEntityRecord( 'root', 'site' ); | ||
return { | ||
isTemplate: getCurrentPostType() === TEMPLATE_POST_TYPE, | ||
postSlug: getEditedPostAttribute( 'slug' ), | ||
allowCommentsOnNewPosts: | ||
siteSettings?.default_comment_status || '', | ||
}; | ||
}, | ||
[] | ||
); | ||
// Use internal state instead of a ref to make sure that the component | ||
// re-renders when the popover's anchor updates. | ||
const [ popoverAnchor, setPopoverAnchor ] = useState( null ); | ||
// Memoize popoverProps to avoid returning a new object every time. | ||
const popoverProps = useMemo( | ||
() => ( { | ||
// Anchor the popover to the middle of the entire row so that it doesn't | ||
// move around when the label changes. | ||
anchor: popoverAnchor, | ||
placement: 'left-start', | ||
offset: 36, | ||
shift: true, | ||
} ), | ||
[ popoverAnchor ] | ||
); | ||
|
||
if ( ! isTemplate || ! [ 'home', 'index' ].includes( postSlug ) ) { | ||
return null; | ||
} | ||
const setAllowCommentsOnNewPosts = ( newValue ) => { | ||
editEntityRecord( 'root', 'site', undefined, { | ||
default_comment_status: newValue ? 'open' : null, | ||
} ); | ||
}; | ||
return ( | ||
<PostPanelRow label={ __( 'Discussion' ) } ref={ setPopoverAnchor }> | ||
<Dropdown | ||
popoverProps={ popoverProps } | ||
contentClassName="editor-site-discussion-dropdown__content" | ||
focusOnMount | ||
renderToggle={ ( { isOpen, onToggle } ) => ( | ||
<Button | ||
size="compact" | ||
variant="tertiary" | ||
aria-expanded={ isOpen } | ||
aria-label={ __( 'Change discussion settings' ) } | ||
onClick={ onToggle } | ||
> | ||
{ allowCommentsOnNewPosts | ||
? __( 'Comments open' ) | ||
: __( 'Comments closed' ) } | ||
</Button> | ||
) } | ||
renderContent={ ( { onClose } ) => ( | ||
<> | ||
<InspectorPopoverHeader | ||
title={ __( 'Discussion' ) } | ||
onClose={ onClose } | ||
/> | ||
<VStack spacing={ 3 }> | ||
<Text> | ||
{ __( | ||
'Changes will apply to new posts only. Individual posts may override these settings.' | ||
) } | ||
</Text> | ||
<RadioControl | ||
className="editor-site-discussion__options" | ||
hideLabelFromVision | ||
label={ __( 'Comment status' ) } | ||
options={ COMMENT_OPTIONS } | ||
onChange={ setAllowCommentsOnNewPosts } | ||
selected={ allowCommentsOnNewPosts } | ||
/> | ||
</VStack> | ||
</> | ||
) } | ||
/> | ||
</PostPanelRow> | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This text wraps in two lines and I'm not sure how we can address that properly, since all labels have a
width:30%
. Should we apply some truncate rules toeditor-post-panel__row-label
in general?It seems like we might have more similar use cases in the future, and it seems like a problem that could be happening already in different languages. --cc @WordPress/gutenberg-design