From 85161bef63fcdad2da5ae88543970f969445a945 Mon Sep 17 00:00:00 2001 From: scruffian Date: Fri, 8 Dec 2023 19:35:23 +0000 Subject: [PATCH] don't update the value that comes from the useSelect --- .../src/post-featured-image/edit.js | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/packages/block-library/src/post-featured-image/edit.js b/packages/block-library/src/post-featured-image/edit.js index d82e1e5b1a77a6..be8380851ff569 100644 --- a/packages/block-library/src/post-featured-image/edit.js +++ b/packages/block-library/src/post-featured-image/edit.js @@ -26,6 +26,7 @@ import { store as blockEditorStore, __experimentalUseBorderProps as useBorderProps, } from '@wordpress/block-editor'; +import { useMemo } from '@wordpress/element'; import { __, sprintf } from '@wordpress/i18n'; import { upload } from '@wordpress/icons'; import { store as noticesStore } from '@wordpress/notices'; @@ -67,7 +68,8 @@ export default function PostFeaturedImageEdit( { linkTarget, useFirstImageFromPost, } = attributes; - let [ featuredImage, setFeaturedImage ] = useEntityProp( + + const [ storedFeaturedImage, setFeaturedImage ] = useEntityProp( 'postType', postTypeSlug, 'featured_media', @@ -83,22 +85,32 @@ export default function PostFeaturedImageEdit( { postId ); - if ( ! featuredImage && useFirstImageFromPost && postContent ) { + const featuredImage = useMemo( () => { + if ( storedFeaturedImage ) { + return storedFeaturedImage; + } + + if ( ! useFirstImageFromPost ) { + return; + } + const firstImageCloser = //.exec( postContent ); - const content = firstImageCloser - ? postContent.slice( - 0, - firstImageCloser.index + firstImageCloser[ 0 ].length - ) - : ''; + + if ( ! firstImageCloser ) { + return; + } + + const content = postContent.slice( + 0, + firstImageCloser.index + firstImageCloser[ 0 ].length + ); + const blocks = parse( content ); const imageBlock = blocks.find( ( { name } ) => name === 'core/image' ); - if ( imageBlock?.attributes?.id ) { - featuredImage = imageBlock.attributes.id; - } - } + return imageBlock?.attributes?.id; + }, [ storedFeaturedImage, useFirstImageFromPost, postContent ] ); const { media, postType, postPermalink } = useSelect( ( select ) => {