Skip to content

Commit

Permalink
Add home template details to inspector controls (WordPress#61762)
Browse files Browse the repository at this point in the history
Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: jameskoster <jameskoster@git.wordpress.org>
  • Loading branch information
4 people authored and patil-vipul committed Jun 17, 2024
1 parent 850b22e commit 9190ee5
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 7 deletions.
124 changes: 124 additions & 0 deletions packages/editor/src/components/blog-title/index.js
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>
);
}
4 changes: 4 additions & 0 deletions packages/editor/src/components/blog-title/style.scss
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;
}
101 changes: 101 additions & 0 deletions packages/editor/src/components/posts-per-page/index.js
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>
);
}
4 changes: 4 additions & 0 deletions packages/editor/src/components/posts-per-page/style.scss
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;
}
8 changes: 7 additions & 1 deletion packages/editor/src/components/sidebar/post-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import PostSyncStatus from '../post-sync-status';
import PostTemplatePanel from '../post-template/panel';
import PostTrashPanel from '../post-trash/panel';
import PostURLPanel from '../post-url/panel';
import BlogTitle from '../blog-title';
import PostsPerPage from '../posts-per-page';
import SiteDiscussion from '../site-discussion';
import { store as editorStore } from '../../store';
import {
NAVIGATION_POST_TYPE,
Expand Down Expand Up @@ -84,10 +87,13 @@ export default function PostSummary( { onActionPerformed } ) {
<PostAuthorPanel />
<PostDiscussionPanel />
<PostSyncStatus />
<BlogTitle />
<PostsPerPage />
<SiteDiscussion />
</VStack>
<PostStickyPanel />
<PostFormatPanel />
{ isTemplate && <TemplateAreas /> }
<TemplateAreas />
{ fills }
{ ! isPattern &&
! isTemplate &&
Expand Down
Loading

0 comments on commit 9190ee5

Please sign in to comment.