Skip to content
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
4 changes: 2 additions & 2 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,8 @@ Start with the basic building block of all narrative. ([Source](https://github.c

- **Name:** core/paragraph
- **Category:** text
- **Supports:** __unstablePasteTextInline, anchor, color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), splitting, typography (fitText, fontSize, lineHeight), ~~className~~
- **Attributes:** align, content, direction, dropCap, placeholder
- **Supports:** __unstablePasteTextInline, anchor, color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), splitting, typography (fitText, fontSize, lineHeight, textAlign), ~~className~~
- **Attributes:** content, direction, dropCap, placeholder

## Pattern Placeholder

Expand Down
32 changes: 23 additions & 9 deletions packages/block-library/src/block-keyboard-shortcuts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,33 @@ function BlockKeyboardShortcuts() {
) {
return;
}
const newAttributes = {
content: attributes.content,
};

const textAlign =
blockName === 'core/paragraph' ? 'align' : 'textAlign';
const destinationTextAlign =
destinationBlockName === 'core/paragraph' ? 'align' : 'textAlign';
// Read textAlign from source block (could be in old or new format)
const sourceTextAlign =
attributes.textAlign || attributes.style?.typography?.textAlign;
Comment on lines +54 to +55

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

At first glance, I thought that if the source block was a paragraph block, we should check attributes.align instead of attributes.textAlign, but it seems that the migration of the paragraph block has already been processed at this point.


// When destination is heading, preserve heading format (textAlign attribute + level)
// When destination is paragraph, use block support format (style.typography.textAlign)
if ( destinationBlockName === 'core/heading' ) {
newAttributes.level = level;
if ( sourceTextAlign ) {
newAttributes.textAlign = sourceTextAlign;
}
} else if ( sourceTextAlign ) {
// Destination is paragraph
newAttributes.style = {
typography: {
textAlign: sourceTextAlign,
},
};
}

replaceBlocks(
currentClientId,
createBlock( destinationBlockName, {
level,
content: attributes.content,
...{ [ destinationTextAlign ]: attributes[ textAlign ] },
} )
createBlock( destinationBlockName, newAttributes )
);
};

Expand Down
18 changes: 15 additions & 3 deletions packages/block-library/src/cover/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,11 @@ const v3 = {
[
createBlock( 'core/paragraph', {
content: attributes.title,
align: attributes.contentAlign,
style: {
typography: {
textAlign: attributes.contentAlign,
},
},
fontSize: 'large',
placeholder: __( 'Write title…' ),
} ),
Expand Down Expand Up @@ -1933,7 +1937,11 @@ const v2 = {
[
createBlock( 'core/paragraph', {
content: attributes.title,
align: attributes.contentAlign,
style: {
typography: {
textAlign: attributes.contentAlign,
},
},
fontSize: 'large',
placeholder: __( 'Write title…' ),
} ),
Expand Down Expand Up @@ -1993,7 +2001,11 @@ const v1 = {
[
createBlock( 'core/paragraph', {
content: attributes.title,
align: attributes.contentAlign,
style: {
typography: {
textAlign: attributes.contentAlign,
},
},
fontSize: 'large',
placeholder: __( 'Write title…' ),
} ),
Expand Down
12 changes: 10 additions & 2 deletions packages/block-library/src/cover/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ const transforms = {
createBlock( 'core/paragraph', {
content: caption,
fontSize: 'large',
align: 'center',
style: {
typography: {
textAlign: 'center',
},
},
} ),
]
),
Expand All @@ -60,7 +64,11 @@ const transforms = {
createBlock( 'core/paragraph', {
content: caption,
fontSize: 'large',
align: 'center',
style: {
typography: {
textAlign: 'center',
},
},
} ),
]
),
Expand Down
13 changes: 10 additions & 3 deletions packages/block-library/src/heading/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const transforms = {
blocks: [ 'core/paragraph' ],
transform: ( attributes ) =>
attributes.map( ( _attributes ) => {
const { content, anchor, align: textAlign } = _attributes;
const { content, anchor, style } = _attributes;
const textAlign = style?.typography?.textAlign;
return createBlock( 'core/heading', {
...getTransformedAttributes(
_attributes,
Expand Down Expand Up @@ -92,7 +93,7 @@ const transforms = {
blocks: [ 'core/paragraph' ],
transform: ( attributes ) =>
attributes.map( ( _attributes ) => {
const { content, textAlign: align } = _attributes;
const { content, textAlign } = _attributes;
return createBlock( 'core/paragraph', {
...getTransformedAttributes(
_attributes,
Expand All @@ -102,7 +103,13 @@ const transforms = {
} )
),
content,
align,
...( textAlign && {
style: {
typography: {
textAlign,
},
},
} ),
} );
} ),
},
Expand Down
4 changes: 1 addition & 3 deletions packages/block-library/src/paragraph/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
"keywords": [ "text" ],
"textdomain": "default",
"attributes": {
"align": {
"type": "string"
},
"content": {
"type": "rich-text",
"source": "rich-text",
Expand Down Expand Up @@ -58,6 +55,7 @@
"typography": {
"fontSize": true,
"lineHeight": true,
"textAlign": true,
"__experimentalFontFamily": true,
"__experimentalTextDecoration": true,
"__experimentalFontStyle": true,
Expand Down
107 changes: 87 additions & 20 deletions packages/block-library/src/paragraph/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,62 @@ const migrateCustomColorsAndFontSizes = ( attributes ) => {
};
};

const migrateTextAlign = ( attributes ) => {
const { align, ...restAttributes } = attributes;
if ( ! align ) {
return attributes;
}
return {
...restAttributes,
style: {
...attributes.style,
typography: {
...attributes.style?.typography,
textAlign: align,
},
},
};
};

const { style, ...restBlockAttributes } = blockAttributes;

const deprecated = [
// Version with `align` attribute.
{
supports: {
className: false,
typography: {
fontSize: true,
},
},
attributes: blockAttributes,
isEligible( attributes ) {
return (
!! attributes.align ||
!! attributes.className?.match(
/\bhas-text-align-(left|center|right)\b/
)
);
},
save( { attributes } ) {
const { align, content, dropCap, direction } = attributes;
const className = clsx( {
'has-drop-cap':
align === ( isRTL() ? 'left' : 'right' ) ||
align === 'center'
? false
: dropCap,
[ `has-text-align-${ align }` ]: align,
} );

return (
<p { ...useBlockProps.save( { className, dir: direction } ) }>
<RichText.Content value={ content } />
</p>
);
},
migrate: migrateTextAlign,
},
// Version without drop cap on aligned text.
{
supports,
Expand All @@ -108,6 +161,7 @@ const deprecated = [
type: 'number',
},
},
migrate: migrateTextAlign,
save( { attributes } ) {
const { align, content, dropCap, direction } = attributes;
const className = clsx( {
Expand Down Expand Up @@ -140,7 +194,11 @@ const deprecated = [
type: 'number',
},
},
migrate: migrateCustomColorsAndFontSizes,
migrate( attributes ) {
return migrateCustomColorsAndFontSizes(
migrateTextAlign( attributes )
);
},
save( { attributes } ) {
const {
align,
Expand Down Expand Up @@ -205,7 +263,11 @@ const deprecated = [
type: 'number',
},
},
migrate: migrateCustomColorsAndFontSizes,
migrate( attributes ) {
return migrateCustomColorsAndFontSizes(
migrateTextAlign( attributes )
);
},
save( { attributes } ) {
const {
align,
Expand Down Expand Up @@ -273,7 +335,11 @@ const deprecated = [
type: 'string',
},
},
migrate: migrateCustomColorsAndFontSizes,
migrate( attributes ) {
return migrateCustomColorsAndFontSizes(
migrateTextAlign( attributes )
);
},
save( { attributes } ) {
const {
width,
Expand Down Expand Up @@ -363,21 +429,24 @@ const deprecated = [
);
},
migrate( attributes ) {
return migrateCustomColorsAndFontSizes( {
...attributes,
customFontSize: Number.isFinite( attributes.fontSize )
? attributes.fontSize
: undefined,
customTextColor:
attributes.textColor && '#' === attributes.textColor[ 0 ]
? attributes.textColor
: undefined,
customBackgroundColor:
attributes.backgroundColor &&
'#' === attributes.backgroundColor[ 0 ]
? attributes.backgroundColor
return migrateCustomColorsAndFontSizes(
migrateTextAlign( {
...attributes,
customFontSize: Number.isFinite( attributes.fontSize )
? attributes.fontSize
: undefined,
} );
customTextColor:
attributes.textColor &&
'#' === attributes.textColor[ 0 ]
? attributes.textColor
: undefined,
customBackgroundColor:
attributes.backgroundColor &&
'#' === attributes.backgroundColor[ 0 ]
? attributes.backgroundColor
: undefined,
} )
);
},
},
{
Expand All @@ -393,9 +462,7 @@ const deprecated = [
save( { attributes } ) {
return <RawHTML>{ attributes.content }</RawHTML>;
},
migrate( attributes ) {
return attributes;
},
migrate: ( attributes ) => attributes,
},
];

Expand Down
25 changes: 7 additions & 18 deletions packages/block-library/src/paragraph/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import {
AlignmentControl,
BlockControls,
InspectorControls,
RichText,
Expand Down Expand Up @@ -58,10 +57,11 @@ function DropCapControl( { clientId, attributes, setAttributes, name } ) {
return null;
}

const { align, dropCap } = attributes;
const { style, dropCap } = attributes;
const textAlign = style?.typography?.textAlign;

let helpText;
if ( hasDropCapDisabled( align ) ) {
if ( hasDropCapDisabled( textAlign ) ) {
helpText = __( 'Not available for aligned text.' );
} else if ( dropCap ) {
helpText = __( 'Showing large initial letter.' );
Expand Down Expand Up @@ -91,7 +91,7 @@ function DropCapControl( { clientId, attributes, setAttributes, name } ) {
checked={ !! dropCap }
onChange={ () => setAttributes( { dropCap: ! dropCap } ) }
help={ helpText }
disabled={ hasDropCapDisabled( align ) }
disabled={ hasDropCapDisabled( textAlign ) }
/>
</ToolsPanelItem>
</InspectorControls>
Expand All @@ -108,12 +108,12 @@ function ParagraphBlock( {
isSelected: isSingleSelected,
name,
} ) {
const { align, content, direction, dropCap, placeholder } = attributes;
const { content, direction, dropCap, placeholder, style } = attributes;
const textAlign = style?.typography?.textAlign;
const blockProps = useBlockProps( {
ref: useOnEnter( { clientId, content } ),
className: clsx( {
'has-drop-cap': hasDropCapDisabled( align ) ? false : dropCap,
[ `has-text-align-${ align }` ]: align,
'has-drop-cap': hasDropCapDisabled( textAlign ) ? false : dropCap,
} ),
style: { direction },
} );
Expand All @@ -123,17 +123,6 @@ function ParagraphBlock( {
<>
{ blockEditingMode === 'default' && (
<BlockControls group="block">
<AlignmentControl
value={ align }
onChange={ ( newAlign ) =>
setAttributes( {
align: newAlign,
dropCap: hasDropCapDisabled( newAlign )
? false
: dropCap,
} )
}
/>
<ParagraphRTLControl
direction={ direction }
setDirection={ ( newDirection ) =>
Expand Down
Loading