Skip to content

Commit 996b343

Browse files
committed
Fields: Add excerpt field
1 parent 8544281 commit 996b343

8 files changed

Lines changed: 78 additions & 3 deletions

File tree

‎packages/dataviews/src/types/dataform.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import type { ReactElement } from 'react';
5+
16
/**
27
* Internal dependencies
38
*/
@@ -143,15 +148,15 @@ export type NormalizedSummaryField =
143148
export type FormField = {
144149
id: string;
145150
label?: string;
146-
description?: string;
151+
description?: string | ReactElement;
147152
layout?: Layout;
148153
children?: Array< FormField | string >;
149154
};
150155
export type NormalizedFormField = {
151156
id: string;
152157
layout: NormalizedLayout;
153158
label?: string;
154-
description?: string;
159+
description?: string | ReactElement;
155160
children?: NormalizedFormField[];
156161
};
157162

‎packages/dataviews/src/types/field-api.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export type Field< Item > = {
200200
/**
201201
* A description of the field.
202202
*/
203-
description?: string;
203+
description?: string | ReactElement;
204204

205205
/**
206206
* Placeholder for the field.

‎packages/editor/src/components/sidebar/dataform-post-summary.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const form = {
3838
labelPosition: 'none',
3939
},
4040
},
41+
'excerpt',
4142
{
4243
id: 'status',
4344
label: __( 'Status' ),

‎packages/editor/src/dataviews/store/private-actions.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
resetPost,
2020
deletePost,
2121
duplicateTemplatePart,
22+
excerptField,
2223
featuredImageField,
2324
dateField,
2425
parentField,
@@ -267,6 +268,11 @@ export const registerPostTypeSchema =
267268
! DESIGN_POST_TYPES.includes( postTypeConfig.slug ) &&
268269
scheduledDateField,
269270
slugField,
271+
// There are special cases where we want to label the excerpt as a description.
272+
// So for now skip registering the excerpt field for some post types.
273+
! DESIGN_POST_TYPES.includes( postTypeConfig.slug ) &&
274+
postTypeConfig.supports?.excerpt &&
275+
excerptField,
270276
postTypeConfig.supports?.[ 'page-attributes' ] && parentField,
271277
postTypeConfig.supports?.comments && commentStatusField,
272278
postTypeConfig.supports?.trackbacks && pingStatusField,

‎packages/fields/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ Duplicate action for BasePost.
6363

6464
Duplicate action for TemplatePart.
6565

66+
### excerptField
67+
68+
Excerpt field for BasePost.
69+
6670
### exportPattern
6771

6872
Export action as JSON for Pattern.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import type { Field } from '@wordpress/dataviews';
5+
import { decodeEntities } from '@wordpress/html-entities';
6+
import { __ } from '@wordpress/i18n';
7+
import {
8+
ExternalLink,
9+
__experimentalText as Text,
10+
} from '@wordpress/components';
11+
12+
/**
13+
* Internal dependencies
14+
*/
15+
import type { BasePost } from '../../types';
16+
17+
const excerptField: Field< BasePost > = {
18+
id: 'excerpt',
19+
type: 'text',
20+
label: __( 'Excerpt' ),
21+
placeholder: __( 'Add an excerpt' ),
22+
description: (
23+
<ExternalLink
24+
href={ __(
25+
'https://wordpress.org/documentation/article/page-post-settings-sidebar/#excerpt'
26+
) }
27+
>
28+
{ __( 'Learn more about manual excerpts' ) }
29+
</ExternalLink>
30+
),
31+
render: ( { item } ) => {
32+
let excerpt;
33+
if ( typeof item.excerpt === 'string' ) {
34+
excerpt = !! item.excerpt
35+
? decodeEntities( item.excerpt )
36+
: __( 'Add an excerpt' );
37+
} else {
38+
excerpt = decodeEntities( item.excerpt?.raw || '' );
39+
}
40+
return (
41+
<Text align="left" numberOfLines={ 4 } truncate>
42+
{ excerpt }
43+
</Text>
44+
);
45+
},
46+
Edit: {
47+
control: 'textarea',
48+
rows: 4,
49+
},
50+
enableSorting: false,
51+
filterBy: false,
52+
};
53+
54+
/**
55+
* Excerpt field for BasePost.
56+
*/
57+
export default excerptField;

‎packages/fields/src/fields/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ export { default as dateField } from './date';
1616
export { default as scheduledDateField } from './date/scheduled';
1717
export { default as authorField } from './author';
1818
export { default as notesField } from './notes';
19+
export { default as excerptField } from './excerpt';
1920
export { default as formatField } from './format';
2021
export { default as postContentInfoField } from './post-content-info';

‎packages/fields/src/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export interface PostType {
127127
supports?: {
128128
'page-attributes'?: boolean;
129129
title?: boolean;
130+
excerpt?: boolean;
130131
revisions?: boolean;
131132
author?: string;
132133
thumbnail?: string;

0 commit comments

Comments
 (0)