From a5b5c75d5febef809a30ae6de504c06e5c936a6b Mon Sep 17 00:00:00 2001 From: Adam Cassis Date: Thu, 19 Sep 2024 13:46:14 +0200 Subject: [PATCH] feat(homepage-posts): add a show-full-content attribute --- includes/class-newspack-blocks-api.php | 1 + src/blocks/homepage-articles/block.json | 14 ++--- src/blocks/homepage-articles/edit.tsx | 56 ++++++++++++++----- .../homepage-articles/templates/article.php | 5 +- src/blocks/homepage-articles/utils.ts | 1 + src/types/index.d.ts | 3 +- 6 files changed, 57 insertions(+), 23 deletions(-) diff --git a/includes/class-newspack-blocks-api.php b/includes/class-newspack-blocks-api.php index eba26975a..c45d077ba 100644 --- a/includes/class-newspack-blocks-api.php +++ b/includes/class-newspack-blocks-api.php @@ -296,6 +296,7 @@ public static function posts_endpoint( $request ) { 'excerpt' => [ 'rendered' => post_password_required( $post ) ? '' : $excerpt, ], + 'full_content' => get_the_content( $post->ID ), 'featured_media' => (int) get_post_thumbnail_id( $post->ID ), 'id' => $post->ID, 'meta' => $meta->get_value( $post->ID, $request ), diff --git a/src/blocks/homepage-articles/block.json b/src/blocks/homepage-articles/block.json index de26216cc..a27c1778f 100644 --- a/src/blocks/homepage-articles/block.json +++ b/src/blocks/homepage-articles/block.json @@ -10,6 +10,10 @@ "type": "boolean", "default": true }, + "showFullContent": { + "type": "boolean", + "default": false + }, "excerptLength": { "type": "number", "default": 55 @@ -214,9 +218,7 @@ }, "postType": { "type": "array", - "default": [ - "post" - ], + "default": ["post"], "items": { "type": "string" } @@ -227,9 +229,7 @@ }, "includedPostStatuses": { "type": "array", - "default": [ - "publish" - ], + "default": ["publish"], "items": { "type": "string" } @@ -239,4 +239,4 @@ "default": true } } -} \ No newline at end of file +} diff --git a/src/blocks/homepage-articles/edit.tsx b/src/blocks/homepage-articles/edit.tsx index 476a1b3a2..1442a51ea 100644 --- a/src/blocks/homepage-articles/edit.tsx +++ b/src/blocks/homepage-articles/edit.tsx @@ -111,6 +111,7 @@ class Edit extends Component< HomepageArticlesProps > { showCaption, showCredit, showExcerpt, + showFullContent, showReadMore, readMoreLabel, showSubtitle, @@ -206,11 +207,16 @@ class Edit extends Component< HomepageArticlesProps > { { post.meta.newspack_post_subtitle || '' } ) } - { showExcerpt && ( + { showExcerpt && ! showFullContent && ( { post.excerpt.rendered } ) } + { ! showExcerpt && showFullContent && ( + + { post.full_content } + + ) } { showReadMore && post.post_link && ( { readMoreLabel } @@ -281,6 +287,7 @@ class Edit extends Component< HomepageArticlesProps > { minHeight, moreButton, showExcerpt, + showFullContent, showReadMore, readMoreLabel, excerptLength, @@ -547,23 +554,44 @@ class Edit extends Component< HomepageArticlesProps > { setAttributes( { showExcerpt: ! showExcerpt } ) } + onChange={ () => { + setAttributes({ + showExcerpt: !showExcerpt, + showFullContent: showFullContent ? false : showFullContent + }) + } } /> { showExcerpt && ( - setAttributes( { excerptLength: value } ) } - min={ 10 } - max={ 100 } - /> + + setAttributes( { excerptLength: value } ) } + min={ 10 } + max={ 100 } + /> + ) } - setAttributes( { showReadMore: ! showReadMore } ) } - /> + + { + setAttributes({ + showFullContent: !showFullContent, + showExcerpt: showExcerpt ? false : showExcerpt + }) + } } + /> + + + setAttributes( { showReadMore: ! showReadMore } ) } + /> + { showReadMore && ( diff --git a/src/blocks/homepage-articles/utils.ts b/src/blocks/homepage-articles/utils.ts index 1110768d0..d73a0637c 100644 --- a/src/blocks/homepage-articles/utils.ts +++ b/src/blocks/homepage-articles/utils.ts @@ -161,6 +161,7 @@ const generatePreviewPost = ( id: PostId ) => { excerpt: { rendered: '

' + __( 'The post excerpt.', 'newspack-blocks' ) + '

', }, + full_content: __('Full post content.', 'newspack-blocks'), post_link: '/', featured_media: '1', id, diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 94d0c39f3..96c479111 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -65,6 +65,7 @@ declare global { excerpt: { rendered: string; }; + full_content: string; meta: { newspack_post_subtitle: string; }; @@ -104,6 +105,7 @@ declare global { postType: PostType[]; showImage: boolean; showExcerpt: boolean; + showFullContent: boolean; tags: TagId[]; customTaxonomies: Taxonomy[]; specificPosts: string[]; @@ -112,7 +114,6 @@ declare global { categoryExclusions: CategoryId[]; customTaxonomyExclusions: Taxonomy[]; className: string; - showExcerpt: boolean; excerptLength: number; showReadMore: boolean; readMoreLabel: string;