From 7428d7e6a898b9232c01e8fe90a5b68ee6361191 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Mon, 14 Jun 2021 15:08:47 +0300 Subject: [PATCH] Use rendered title and excerpt --- .../block-library/src/post-excerpt/edit.js | 16 ++- packages/block-library/src/post-title/edit.js | 121 +++++++++--------- 2 files changed, 68 insertions(+), 69 deletions(-) diff --git a/packages/block-library/src/post-excerpt/edit.js b/packages/block-library/src/post-excerpt/edit.js index b41fbf5170b7eb..0d7648903495ea 100644 --- a/packages/block-library/src/post-excerpt/edit.js +++ b/packages/block-library/src/post-excerpt/edit.js @@ -7,7 +7,7 @@ import classnames from 'classnames'; * WordPress dependencies */ import { useEntityProp } from '@wordpress/core-data'; -import { useMemo } from '@wordpress/element'; +import { useMemo, RawHTML } from '@wordpress/element'; import { AlignmentToolbar, BlockControls, @@ -60,9 +60,9 @@ export default function PostExcerptEditor( { ); const isEditable = userCanEdit && ! isDescendentOfQueryLoop; const [ - excerpt, + rawExcerpt, setExcerpt, - { protected: isProtected } = {}, + { rendered: renderedExcerpt, protected: isProtected } = {}, ] = useEntityProp( 'postType', postType, 'excerpt', postId ); const postContentExcerpt = usePostContentExcerpt( wordCount, @@ -114,14 +114,18 @@ export default function PostExcerptEditor( { } aria-label={ __( 'Post excerpt text' ) } value={ - excerpt || + rawExcerpt || postContentExcerpt || ( isSelected ? '' : __( 'No post excerpt found' ) ) } onChange={ setExcerpt } /> ) : ( - excerpt || postContentExcerpt || __( 'No post excerpt found' ) + ( renderedExcerpt && ( + { renderedExcerpt } + ) ) || + postContentExcerpt || + __( 'No post excerpt found' ) ); return ( <> @@ -135,7 +139,7 @@ export default function PostExcerptEditor( { - { ! excerpt && ( + { ! renderedExcerpt && ( - select( coreStore ).getEditedEntityRecord( - 'postType', - postType, - postId - ), - [ postType, postId ] + const userCanEdit = useCanEditEntity( + 'root', + 'postType', + postType, + postId ); - const { editEntityRecord } = useDispatch( coreStore ); - + const [ rawTitle = '', setTitle, fullTitle ] = useEntityProp( + 'postType', + postType, + 'title', + postId + ); + const [ link ] = useEntityProp( 'postType', postType, 'link', postId ); const blockProps = useBlockProps( { className: classnames( { [ `has-text-align-${ textAlign }` ]: textAlign, } ), } ); - if ( ! post ) { - return null; - } - - const { title = '', link } = post; - let titleElement = ( { __( 'An example title' ) } @@ -60,54 +57,52 @@ export default function PostTitleEdit( { ); if ( postType && postId ) { - titleElement = ! isDescendentOfQueryLoop ? ( - - editEntityRecord( 'postType', postType, postId, { - title: value, - } ) - } - __experimentalVersion={ 2 } - { ...( isLink ? {} : blockProps ) } - /> - ) : ( - <TagName { ...( isLink ? {} : blockProps ) }>{ title }</TagName> - ); - } - - if ( isLink ) { - titleElement = ! isDescendentOfQueryLoop ? ( - <TagName { ...blockProps }> + titleElement = + userCanEdit && ! isDescendentOfQueryLoop ? ( <PlainText - tagName="a" - href={ link } - target={ linkTarget } - rel={ rel } - placeholder={ title.length === 0 ? __( 'No Title' ) : null } - value={ title } - onChange={ ( value ) => - editEntityRecord( 'postType', postType, postId, { - title: value, - } ) - } + tagName={ TagName } + placeholder={ __( 'No Title' ) } + value={ rawTitle } + onChange={ setTitle } __experimentalVersion={ 2 } + { ...( isLink ? {} : blockProps ) } /> - </TagName> - ) : ( - <TagName { ...blockProps }> - <a - href={ link } - target={ linkTarget } - rel={ rel } - onClick={ ( event ) => event.preventDefault() } - > - { title } - </a> - </TagName> - ); + ) : ( + <TagName { ...( isLink ? {} : blockProps ) }> + <RawHTML key="html">{ fullTitle.rendered }</RawHTML> + </TagName> + ); + } + + if ( isLink ) { + titleElement = + userCanEdit && ! isDescendentOfQueryLoop ? ( + <TagName { ...blockProps }> + <PlainText + tagName="a" + href={ link } + target={ linkTarget } + rel={ rel } + placeholder={ + ! rawTitle.length ? __( 'No Title' ) : null + } + value={ rawTitle } + onChange={ setTitle } + __experimentalVersion={ 2 } + /> + </TagName> + ) : ( + <TagName { ...blockProps }> + <a + href={ link } + target={ linkTarget } + rel={ rel } + onClick={ ( event ) => event.preventDefault() } + > + <RawHTML key="html">{ fullTitle.rendered }</RawHTML> + </a> + </TagName> + ); } return (