Skip to content

Commit

Permalink
Replace the post title component with the post title block
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Oct 21, 2020
1 parent bea03ab commit f174617
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 168 deletions.
4 changes: 2 additions & 2 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ export const __experimentalRegisterExperimentalCoreBlocks =
[
navigation,
navigationLink,
postTitle,
postContent,

// Register Full Site Editing Blocks.
...( __experimentalEnableFullSiteEditing
Expand All @@ -220,8 +222,6 @@ export const __experimentalRegisterExperimentalCoreBlocks =
query,
queryLoop,
queryPagination,
postTitle,
postContent,
postAuthor,
postComment,
postCommentAuthor,
Expand Down
11 changes: 1 addition & 10 deletions packages/block-library/src/post-content/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { useBlockProps } from '@wordpress/block-editor';

/**
Expand All @@ -13,19 +12,11 @@ import PostContentInnerBlocks from './inner-blocks';
export default function PostContentEdit( {
context: { postId: contextPostId, postType: contextPostType },
} ) {
const { id: currentPostId, type: currentPostType } = useSelect(
( select ) => select( 'core/editor' ).getCurrentPost() ?? {}
);
const blockProps = useBlockProps();

// Only render InnerBlocks if the context is different from the active post
// to avoid infinite recursion of post content.
if (
contextPostId &&
contextPostType &&
contextPostId !== currentPostId &&
contextPostType !== currentPostType
) {
if ( contextPostId && contextPostType ) {
return (
<div { ...blockProps }>
<PostContentInnerBlocks
Expand Down
39 changes: 22 additions & 17 deletions packages/block-library/src/post-title/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';
import {
AlignmentToolbar,
BlockControls,
InspectorControls,
useBlockProps,
PlainText,
} from '@wordpress/block-editor';
import {
ToolbarGroup,
Expand All @@ -33,31 +34,35 @@ export default function PostTitleEdit( {
} ) {
const TagName = 0 === level ? 'p' : 'h' + level;

const post = useSelect(
( select ) =>
select( 'core' ).getEditedEntityRecord(
'postType',
postType,
postId
),
[ postType, postId ]
const [ title, setTitle ] = useEntityProp(
'postType',
postType,
'title',
postId
);
const [ link ] = useEntityProp( 'postType', postType, 'title', postId );

const blockProps = useBlockProps( {
className: classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );

if ( ! post ) {
return null;
}
let titleElement = (
<PlainText
tagName={ TagName }
placeholder={ __( 'No Title' ) }
{ ...blockProps }
value={ title }
onChange={ setTitle }
__experimentalVersion={ 2 }
/>
);

let title = post.title || __( 'Post Title' );
if ( isLink ) {
title = (
<a href={ post.link } target={ linkTarget } rel={ rel }>
{ title }
titleElement = (
<a href={ link } target={ linkTarget } rel={ rel }>
{ titleElement }
</a>
);
}
Expand Down Expand Up @@ -109,7 +114,7 @@ export default function PostTitleEdit( {
) }
</PanelBody>
</InspectorControls>
<TagName { ...blockProps }>{ title }</TagName>
{ titleElement }
</>
);
}
8 changes: 1 addition & 7 deletions packages/edit-post/src/components/visual-editor/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/**
* WordPress dependencies
*/
import {
PostTitle,
VisualEditorGlobalKeyboardShortcuts,
} from '@wordpress/editor';
import { VisualEditorGlobalKeyboardShortcuts } from '@wordpress/editor';
import {
WritingFlow,
Typewriter,
Expand Down Expand Up @@ -43,9 +40,6 @@ function VisualEditor() {
<CopyHandler>
<WritingFlow>
<ObserveTyping>
<div className="edit-post-visual-editor__post-title-wrapper">
<PostTitle />
</div>
<BlockList />
</ObserveTyping>
</WritingFlow>
Expand Down
Loading

0 comments on commit f174617

Please sign in to comment.