Skip to content

Commit

Permalink
Editor: Implement debounced title saving and fix title retrieval for …
Browse files Browse the repository at this point in the history
…post context
  • Loading branch information
yogeshbhutkar committed Dec 31, 2024
1 parent b4304f8 commit 11fc66d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 14 additions & 2 deletions packages/block-library/src/post-title/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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() ) );
Expand Down
6 changes: 1 addition & 5 deletions packages/block-library/src/post-title/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
Expand Down

0 comments on commit 11fc66d

Please sign in to comment.