Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the post title block editable #27240

Merged
merged 3 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 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 { useSelect, useDispatch } from '@wordpress/data';
import {
AlignmentToolbar,
BlockControls,
InspectorControls,
useBlockProps,
PlainText,
} from '@wordpress/block-editor';
import {
ToolbarGroup,
Expand Down Expand Up @@ -42,6 +43,7 @@ export default function PostTitleEdit( {
),
[ postType, postId ]
);
const { editEntityRecord } = useDispatch( 'core' );

const blockProps = useBlockProps( {
className: classnames( {
Expand All @@ -53,11 +55,33 @@ export default function PostTitleEdit( {
return null;
}

let title = post.title || __( 'Post Title' );
const { title, link } = post;

let titleElement = (
<PlainText
tagName={ TagName }
placeholder={ __( 'No Title' ) }
value={ title }
onChange={ ( value ) =>
editEntityRecord( 'postType', postType, postId, {
title: value,
} )
}
__experimentalVersion={ 2 }
{ ...( isLink ? {} : blockProps ) }
/>
);

if ( isLink ) {
title = (
<a href={ post.link } target={ linkTarget } rel={ rel }>
{ title }
titleElement = (
<a
href={ link }
target={ linkTarget }
rel={ rel }
onClick={ ( event ) => event.preventDefault() }
{ ...blockProps }
>
{ titleElement }
</a>
);
}
Expand Down Expand Up @@ -109,7 +133,7 @@ export default function PostTitleEdit( {
) }
</PanelBody>
</InspectorControls>
<TagName { ...blockProps }>{ title }</TagName>
{ titleElement }
</>
);
}
44 changes: 44 additions & 0 deletions packages/e2e-tests/specs/experiments/blocks/post-title.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* WordPress dependencies
*/
import {
activateTheme,
createNewPost,
insertBlock,
pressKeyWithModifier,
saveDraft,
} from '@wordpress/e2e-test-utils';

describe( 'Post Title block', () => {
beforeAll( async () => {
await activateTheme( 'twentytwentyone-blocks' );
} );

afterAll( async () => {
await activateTheme( 'twentytwentyone' );
} );

beforeEach( async () => {
await createNewPost();
} );

it( 'Can edit the post title', async () => {
// Create a block with some text that will trigger a list creation.
await insertBlock( 'Post Title' );

// Select all of the text in the post title block.
await pressKeyWithModifier( 'primary', 'a' );

// Create a second list item.
await page.keyboard.type( 'Just tweaking the post title' );

await saveDraft();
await page.reload();
await page.waitForSelector( '.edit-post-layout' );
const title = await page.$eval(
'.editor-post-title__input',
( element ) => element.value
);
expect( title ).toEqual( 'Just tweaking the post title' );
} );
} );
21 changes: 7 additions & 14 deletions packages/edit-site/src/components/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,15 @@ function Editor() {
const {
__experimentalGetTemplateInfo: getTemplateInfo,
} = select( 'core/editor' );
entitiesToSave.forEach( ( { kind, name, key } ) => {
entitiesToSave.forEach( ( { kind, name, key, title } ) => {
const record = getEditedEntityRecord( kind, name, key );

if ( 'postType' === kind && name === 'wp_template' ) {
const { title } = getTemplateInfo( record );
return editEntityRecord( kind, name, key, {
status: 'publish',
title,
} );
if ( kind === 'postType' && name === 'wp_template' ) {
( { title } = getTemplateInfo( record ) );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a weird notation. Are the parenthesis necessary?

Copy link
Contributor

@ntsekouras ntsekouras Nov 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

const edits = record.slug
? { status: 'publish', title: record.slug }
: { status: 'publish' };

editEntityRecord( kind, name, key, edits );
editEntityRecord( kind, name, key, {
status: 'publish',
title: title || record.slug,
} );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this supposed to do? I mean the whole function closeEntitiesSavedStates? Why are we setting status and title here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think setting the status ensures that auto-draft templates are set to publish.

Title part is needed to give a title for wp_template_part. Since they don't have custom titles, they are using the slug as a title in every case. On the other hand we want to preserve the title if the entity has one. (Basically everything except wp_template_part

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So something to be done before "saving" right. I don't understand why it's in closeEntitiesSavedStates

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the close prop is misnamed. It should be something more like preSaveCallback

} );
}
setIsEntitiesSavedStatesOpen( false );
Expand Down