Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
DataViews: Move template title field
  • Loading branch information
ntsekouras committed Dec 5, 2024
commit f6d947a4b09663089ba0209ff901ab024acfecfb
19 changes: 1 addition & 18 deletions packages/edit-site/src/components/page-templates/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { useAddedBy } from './hooks';
import usePatternSettings from '../page-patterns/use-pattern-settings';
import { unlock } from '../../lock-unlock';

const { useLink, Link } = unlock( routerPrivateApis );
const { useLink } = unlock( routerPrivateApis );
const { useGlobalStyle } = unlock( blockEditorPrivateApis );

function PreviewField( { item } ) {
Expand Down Expand Up @@ -75,23 +75,6 @@ export const previewField = {
enableSorting: false,
};

function TitleField( { item } ) {
return (
<Link to={ `/${ item.type }/${ item.id }?canvas=edit` }>
{ decodeEntities( item.title?.rendered ) || __( '(no title)' ) }
</Link>
);
}

export const titleField = {
label: __( 'Template' ),
id: 'title',
getValue: ( { item } ) => item.title?.rendered,
render: TitleField,
enableHiding: false,
enableGlobalSearch: true,
};

export const descriptionField = {
label: __( 'Description' ),
id: 'description',
Expand Down
14 changes: 7 additions & 7 deletions packages/edit-site/src/components/page-templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { privateApis as corePrivateApis } from '@wordpress/core-data';
import { DataViews, filterSortAndPaginate } from '@wordpress/dataviews';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
import { templateTitleField } from '@wordpress/fields';
import { addQueryArgs } from '@wordpress/url';

/**
Expand All @@ -23,12 +24,7 @@ import {
} from '../../utils/constants';
import { unlock } from '../../lock-unlock';
import { useEditPostAction } from '../dataviews-actions';
import {
authorField,
descriptionField,
previewField,
titleField,
} from './fields';
import { authorField, descriptionField, previewField } from './fields';

const { usePostActions } = unlock( editorPrivateApis );
const { useHistory, useLocation } = unlock( routerPrivateApis );
Expand Down Expand Up @@ -172,7 +168,7 @@ export default function PageTemplates() {
const fields = useMemo(
() => [
previewField,
titleField,
templateTitleField,
descriptionField,
{
...authorField,
Expand Down Expand Up @@ -227,6 +223,10 @@ export default function PageTemplates() {
view={ view }
onChangeView={ onChangeView }
onChangeSelection={ onChangeSelection }
isItemClickable={ () => true }
onClickItem={ ( { id } ) => {
history.navigate( `/wp_template/${ id }?canvas=edit` );
} }
selection={ selection }
defaultLayouts={ defaultLayouts }
/>
Expand Down
4 changes: 4 additions & 0 deletions packages/fields/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ Status field for BasePost.

Template field for BasePost.

### templateTitleField

Undocumented declaration.

### titleField

Title field for BasePost.
Expand Down
1 change: 1 addition & 0 deletions packages/fields/src/fields/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { default as slugField } from './slug';
export { default as titleField } from './title';
export { default as templateTitleField } from './template-title';
export { default as orderField } from './order';
export { default as featuredImageField } from './featured-image';
export { default as templateField } from './template';
Expand Down
23 changes: 23 additions & 0 deletions packages/fields/src/fields/template-title/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* WordPress dependencies
*/
import type { Field } from '@wordpress/dataviews';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import type { Template } from '../../types';
import { getItemTitle } from '../../actions/utils';
import TitleView from '../title/view';

const templateTitleField: Field< Template > = {
label: __( 'Template' ),
id: 'title',
getValue: ( { item } ) => getItemTitle( item ),
render: TitleView,
enableHiding: false,
enableGlobalSearch: true,
};

export default templateTitleField;
4 changes: 2 additions & 2 deletions packages/fields/src/fields/title/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import { __ } from '@wordpress/i18n';
*/
import type { BasePost } from '../../types';
import { getItemTitle } from '../../actions/utils';
import TitleView from './title-view';
import { PostTitleView } from './view';

const titleField: Field< BasePost > = {
type: 'text',
id: 'title',
label: __( 'Title' ),
placeholder: __( 'No title' ),
getValue: ( { item } ) => getItemTitle( item ),
render: TitleView,
render: PostTitleView,
enableHiding: false,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/**
* External dependencies
*/
import type { ReactNode } from 'react';

/**
* WordPress dependencies
*/
import { __experimentalHStack as HStack } from '@wordpress/components';
import { decodeEntities } from '@wordpress/html-entities';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
Expand All @@ -11,10 +15,10 @@ import type { Settings } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import type { BasePost } from '../../types';
import type { CommonPost } from '../../types';
import { getItemTitle } from '../../actions/utils';

const TitleView = ( { item }: { item: BasePost } ) => {
export function PostTitleView( { item }: { item: CommonPost } ) {
const { frontPageId, postsPageId } = useSelect( ( select ) => {
const { getEntityRecord } = select( coreStore );
const siteSettings = getEntityRecord(
Expand All @@ -26,9 +30,6 @@ const TitleView = ( { item }: { item: BasePost } ) => {
postsPageId: siteSettings?.page_for_posts,
};
}, [] );

const renderedTitle = getItemTitle( item );

let suffix;
if ( item.id === frontPageId ) {
suffix = (
Expand All @@ -43,19 +44,29 @@ const TitleView = ( { item }: { item: BasePost } ) => {
</span>
);
}
return <BaseTitleView item={ item }>{ suffix }</BaseTitleView>;
}

export function BaseTitleView( {
item,
children,
}: {
item: CommonPost;
children?: ReactNode;
} ) {
const renderedTitle = getItemTitle( item );
return (
<HStack
className="edit-site-post-list__title"
alignment="center"
justify="flex-start"
>
<span>
{ decodeEntities( renderedTitle ) || __( '(no title)' ) }
</span>
{ suffix }
<span>{ renderedTitle || __( '(no title)' ) }</span>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getItemTitle decode entities already.

{ children }
</HStack>
);
};
}

export default TitleView;
export default function TitleView( { item }: { item: CommonPost } ) {
return <BaseTitleView item={ item } />;
}