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

Post Title: Transform added for paragraph and heading #68239

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion packages/block-library/src/post-title/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { __ } from '@wordpress/i18n';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

export default function PostTitleEdit( {
attributes: { level, levelOptions, textAlign, isLink, rel, linkTarget },
setAttributes,
Expand All @@ -30,6 +29,7 @@ export default function PostTitleEdit( {
const TagName = level === 0 ? 'p' : `h${ level }`;
const isDescendentOfQueryLoop = Number.isFinite( queryId );
const userCanEdit = useSelect(
// eslint-disable-next-line no-shadow
( select ) => {
/**
* useCanEditEntity may trigger an OPTIONS request to the REST API
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/post-title/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import initBlock from '../utils/init-block';
import metadata from './block.json';
import edit from './edit';
import deprecated from './deprecated';
import transforms from './transforms';

const { name } = metadata;
export { metadata, name };
Expand All @@ -18,6 +19,7 @@ export const settings = {
icon,
edit,
deprecated,
transforms,
};

export const init = () => initBlock( { name, metadata, settings } );
49 changes: 49 additions & 0 deletions packages/block-library/src/post-title/transforms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { createBlock } from '@wordpress/blocks';
import { select } from '@wordpress/data';
// eslint-disable-next-line import/no-extraneous-dependencies
import { store as editorStore } from '@wordpress/editor';
export const transforms = {
from: [
{
type: 'block',
blocks: [ 'core/heading', 'core/paragraph' ],
transform: ( attributes ) =>
createBlock( 'core/post-title', {
textAlign: attributes.align || undefined, // Map alignment if available
} ),
},
],
to: [
{
type: 'block',
blocks: [ 'core/heading' ],
transform: ( { textAlign } ) => {
const postTitle =
select( editorStore ).getEditedPostAttribute( 'title' ) ||
__( 'Add a title…' );
return createBlock( 'core/heading', {
content: postTitle,
align: textAlign,
} );
},
},
{
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( { textAlign } ) => {
const postTitle =
select( editorStore ).getEditedPostAttribute( 'title' ) ||
__( 'Add a title…' );
return createBlock( 'core/paragraph', {
content: postTitle,
align: textAlign,
} );
},
},
],
};
export default transforms;
Loading