From 11fc66d7630c194b39aafe746309c7bd4920ced5 Mon Sep 17 00:00:00 2001 From: yogeshbhutkar Date: Tue, 31 Dec 2024 11:13:56 +0530 Subject: [PATCH] Editor: Implement debounced title saving and fix title retrieval for post context --- packages/block-library/src/post-title/edit.js | 16 ++++++++++++++-- packages/block-library/src/post-title/index.php | 6 +----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/post-title/edit.js b/packages/block-library/src/post-title/edit.js index fdfb8e7c2c6cd2..63b8c103171fee 100644 --- a/packages/block-library/src/post-title/edit.js +++ b/packages/block-library/src/post-title/edit.js @@ -15,11 +15,12 @@ import { HeadingLevelDropdown, useBlockEditingMode, } from '@wordpress/block-editor'; +import { useDebounce } from '@wordpress/compose'; import { ToggleControl, TextControl, PanelBody } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { createBlock, getDefaultBlockName } from '@wordpress/blocks'; import { useEntityProp, store as coreStore } from '@wordpress/core-data'; -import { useSelect } from '@wordpress/data'; +import { useSelect, useDispatch } from '@wordpress/data'; export default function PostTitleEdit( { attributes: { level, levelOptions, textAlign, isLink, rel, linkTarget }, @@ -50,12 +51,23 @@ export default function PostTitleEdit( { }, [ isDescendentOfQueryLoop, postType, postId ] ); - const [ rawTitle = '', setTitle, fullTitle ] = useEntityProp( + const [ rawTitle = '', , fullTitle ] = useEntityProp( 'postType', postType, 'title', postId ); + + const { saveEntityRecord } = useDispatch( coreStore ); + const debouncedSaveTitle = useDebounce( ( newTitle ) => { + saveEntityRecord( 'postType', 'post', { + id: postId, + title: newTitle, + } ); + }, 500 ); + const setTitle = ( newTitle ) => { + debouncedSaveTitle( newTitle ); + }; const [ link ] = useEntityProp( 'postType', postType, 'link', postId ); const onSplitAtEnd = () => { insertBlocksAfter( createBlock( getDefaultBlockName() ) ); diff --git a/packages/block-library/src/post-title/index.php b/packages/block-library/src/post-title/index.php index ed27f2e4158eb4..82e8792a492712 100644 --- a/packages/block-library/src/post-title/index.php +++ b/packages/block-library/src/post-title/index.php @@ -21,11 +21,7 @@ function render_block_core_post_title( $attributes, $content, $block ) { return ''; } - /** - * The `$post` argument is intentionally omitted so that changes are reflected when previewing a post. - * See: https://github.com/WordPress/gutenberg/pull/37622#issuecomment-1000932816. - */ - $title = get_the_title(); + $title = get_the_title( $block->context['postId'] ); if ( ! $title ) { return '';