Skip to content

Commit

Permalink
All template usage for bulk editing
Browse files Browse the repository at this point in the history
  • Loading branch information
louwie17 committed Dec 10, 2024
1 parent 58b2e98 commit f6e3ec4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
1 change: 0 additions & 1 deletion packages/fields/src/fields/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const templateField: Field< BasePost > = {
label: __( 'Template' ),
Edit: TemplateEdit,
enableSorting: false,
supportsBulkEditing: false,
};

/**
Expand Down
43 changes: 27 additions & 16 deletions packages/fields/src/fields/template/template-edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useCallback, useMemo, useState } from '@wordpress/element';
import { parse } from '@wordpress/blocks';
import type { WpTemplate } from '@wordpress/core-data';
import { store as coreStore } from '@wordpress/core-data';
import type { DataFormControlPropsWithoutBulkEditing } from '@wordpress/dataviews';
import type { DataFormControlProps } from '@wordpress/dataviews';

/**
* Internal dependencies
Expand All @@ -33,12 +33,10 @@ export const TemplateEdit = ( {
field,
onChange,
value,
}: DataFormControlPropsWithoutBulkEditing< BasePost, string > ) => {
}: DataFormControlProps< BasePost, string > ) => {
const { id } = field;
const postType = data.type;
const postId =
typeof data.id === 'number' ? data.id : parseInt( data.id, 10 );
const slug = data.slug;
const postType = Array.isArray( data ) ? data[ 0 ].type : data.type;
const slug = Array.isArray( data ) ? '' : data.slug;

const { availableTemplates, templates } = useSelect(
( select ) => {
Expand All @@ -56,11 +54,18 @@ export const TemplateEdit = ( {
select( coreStore )
);

const isPostsPage = +getPostsPageId() === postId;
const isFrontPage =
postType === 'page' && +getHomePage()?.postId === postId;
const posts = Array.isArray( data ) ? data : [ data ];

const allowSwitchingTemplate = ! isPostsPage && ! isFrontPage;
const allowSwitchingTemplate = posts.every( ( post ) => {
const postId =
typeof post.id === 'number'
? post.id
: parseInt( post.id, 10 );
const isPostsPage = +getPostsPageId() === postId;
const isFrontPage =
postType === 'page' && +getHomePage()?.postId === postId;
return ! isPostsPage && ! isFrontPage;
} );

return {
templates: allTemplates,
Expand All @@ -74,7 +79,7 @@ export const TemplateEdit = ( {
: [],
};
},
[ value, postId, postType ]
[ value, postType, data ]
);

const templatesAsPatterns = useMemo(
Expand Down Expand Up @@ -140,6 +145,13 @@ export const TemplateEdit = ( {
[ id, onChange ]
);

let buttonLabel = '';
if ( typeof value === 'symbol' ) {
buttonLabel = __( 'Mixed' );
} else if ( currentTemplate ) {
buttonLabel = getItemTitle( currentTemplate );
}

return (
<fieldset className="fields-controls__template">
<Dropdown
Expand All @@ -152,12 +164,11 @@ export const TemplateEdit = ( {
onClick={ onToggle }
accessibleWhenDisabled
disabled={
availableTemplates.length === 0 && value === ''
availableTemplates.length === 0 &&
( value === '' || typeof value === 'symbol' )
}
>
{ currentTemplate
? getItemTitle( currentTemplate )
: '' }
{ buttonLabel }
</Button>
) }
renderContent={ ( { onToggle } ) => (
Expand All @@ -174,7 +185,7 @@ export const TemplateEdit = ( {
) }
{
// The default template in a post is indicated by an empty string
value !== '' && (
value !== '' && typeof value !== 'symbol' && (
<MenuItem
onClick={ () => {
onChangeControl( '' );
Expand Down

0 comments on commit f6e3ec4

Please sign in to comment.