From e82722f9d0d3a8a501e6f926e4ff49944fbc2ef4 Mon Sep 17 00:00:00 2001 From: benazeer1909 Date: Mon, 23 Dec 2024 13:52:36 +0530 Subject: [PATCH 1/2] Transform added for paragraph and heading --- packages/block-library/src/post-title/edit.js | 46 ++++++++++++++++++- .../block-library/src/post-title/index.js | 3 +- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/post-title/edit.js b/packages/block-library/src/post-title/edit.js index fdfb8e7c2c6cd..62c65782af46b 100644 --- a/packages/block-library/src/post-title/edit.js +++ b/packages/block-library/src/post-title/edit.js @@ -19,8 +19,9 @@ 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, select } from '@wordpress/data'; +// eslint-disable-next-line import/no-extraneous-dependencies +import { store as editorStore } from '@wordpress/editor'; export default function PostTitleEdit( { attributes: { level, levelOptions, textAlign, isLink, rel, linkTarget }, setAttributes, @@ -30,6 +31,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 @@ -180,3 +182,43 @@ export default function PostTitleEdit( { ); } +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, + } ); + }, + }, + ], +}; diff --git a/packages/block-library/src/post-title/index.js b/packages/block-library/src/post-title/index.js index 86bdab0dbccbf..17fc6111a0dd6 100644 --- a/packages/block-library/src/post-title/index.js +++ b/packages/block-library/src/post-title/index.js @@ -8,7 +8,7 @@ import { title as icon } from '@wordpress/icons'; */ import initBlock from '../utils/init-block'; import metadata from './block.json'; -import edit from './edit'; +import edit, { transforms } from './edit'; import deprecated from './deprecated'; const { name } = metadata; @@ -18,6 +18,7 @@ export const settings = { icon, edit, deprecated, + transforms, }; export const init = () => initBlock( { name, metadata, settings } ); From 1e353619a2090b070c9e13120bf1f08f36172cd4 Mon Sep 17 00:00:00 2001 From: benazeer-ben Date: Fri, 28 Feb 2025 15:37:18 +0530 Subject: [PATCH 2/2] Feedback changes - moved code to transforms.js --- packages/block-library/src/post-title/edit.js | 44 +---------------- .../block-library/src/post-title/index.js | 3 +- .../src/post-title/transforms.js | 49 +++++++++++++++++++ 3 files changed, 52 insertions(+), 44 deletions(-) create mode 100644 packages/block-library/src/post-title/transforms.js diff --git a/packages/block-library/src/post-title/edit.js b/packages/block-library/src/post-title/edit.js index 62c65782af46b..cd42a5c260061 100644 --- a/packages/block-library/src/post-title/edit.js +++ b/packages/block-library/src/post-title/edit.js @@ -19,9 +19,7 @@ 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, select } from '@wordpress/data'; -// eslint-disable-next-line import/no-extraneous-dependencies -import { store as editorStore } from '@wordpress/editor'; +import { useSelect } from '@wordpress/data'; export default function PostTitleEdit( { attributes: { level, levelOptions, textAlign, isLink, rel, linkTarget }, setAttributes, @@ -182,43 +180,3 @@ export default function PostTitleEdit( { ); } -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, - } ); - }, - }, - ], -}; diff --git a/packages/block-library/src/post-title/index.js b/packages/block-library/src/post-title/index.js index 17fc6111a0dd6..51093536dacf3 100644 --- a/packages/block-library/src/post-title/index.js +++ b/packages/block-library/src/post-title/index.js @@ -8,8 +8,9 @@ import { title as icon } from '@wordpress/icons'; */ import initBlock from '../utils/init-block'; import metadata from './block.json'; -import edit, { transforms } from './edit'; +import edit from './edit'; import deprecated from './deprecated'; +import transforms from './transforms'; const { name } = metadata; export { metadata, name }; diff --git a/packages/block-library/src/post-title/transforms.js b/packages/block-library/src/post-title/transforms.js new file mode 100644 index 0000000000000..33de573f11a9b --- /dev/null +++ b/packages/block-library/src/post-title/transforms.js @@ -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;