Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -1082,8 +1082,8 @@ Insert poetry. Use special spacing formats. Or quote song lyrics. ([Source](http

- **Name:** core/verse
- **Category:** text
- **Supports:** anchor, background (backgroundImage, backgroundSize), color (background, gradients, link, text), dimensions (minHeight), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight)
- **Attributes:** content, textAlign
- **Supports:** anchor, background (backgroundImage, backgroundSize), color (background, gradients, link, text), dimensions (minHeight), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight, textAlign)
- **Attributes:** content

## Video

Expand Down
4 changes: 1 addition & 3 deletions packages/block-library/src/verse/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
"selector": "pre",
"__unstablePreserveWhiteSpace": true,
"role": "content"
},
"textAlign": {
"type": "string"
}
},
"supports": {
Expand Down Expand Up @@ -46,6 +43,7 @@
"fontSize": true,
"__experimentalFontFamily": true,
"lineHeight": true,
"textAlign": true,
"__experimentalFontStyle": true,
"__experimentalFontWeight": true,
"__experimentalLetterSpacing": true,
Expand Down
86 changes: 82 additions & 4 deletions packages/block-library/src/verse/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { RichText, useBlockProps } from '@wordpress/block-editor';
* Internal dependencies
*/
import migrateFontFamily from '../utils/migrate-font-family';
import migrateTextAlign from '../utils/migrate-text-align';

const v1 = {
attributes: {
Expand All @@ -36,6 +37,7 @@ const v1 = {
/>
);
},
migrate: migrateTextAlign,
};

const v2 = {
Expand Down Expand Up @@ -79,9 +81,85 @@ const v2 = {
</pre>
);
},
migrate: migrateFontFamily,
isEligible( { style } ) {
return style?.typography?.fontFamily;
migrate( attributes ) {
return migrateTextAlign( migrateFontFamily( attributes ) );
},
isEligible( { style, textAlign } ) {
return style?.typography?.fontFamily || !! textAlign;
},
};

const v3 = {
attributes: {
content: {
type: 'rich-text',
source: 'rich-text',
selector: 'pre',
__unstablePreserveWhiteSpace: true,
role: 'content',
},
textAlign: {
type: 'string',
},
},
supports: {
anchor: true,
background: {
backgroundImage: true,
backgroundSize: true,
},
color: {
gradients: true,
link: true,
},
dimensions: {
minHeight: true,
},
typography: {
fontSize: true,
__experimentalFontFamily: true,
lineHeight: true,
__experimentalFontStyle: true,
__experimentalFontWeight: true,
__experimentalLetterSpacing: true,
__experimentalTextTransform: true,
__experimentalTextDecoration: true,
__experimentalWritingMode: true,
},
spacing: {
margin: true,
padding: true,
},
__experimentalBorder: {
radius: true,
width: true,
color: true,
style: true,
},
interactivity: {
clientNavigation: true,
},
},
save( { attributes } ) {
const { textAlign, content } = attributes;

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

return (
<pre { ...useBlockProps.save( { className } ) }>
<RichText.Content value={ content } />
</pre>
);
},
migrate: migrateTextAlign,
isEligible( attributes, innerBlocks, { block } ) {
// Only migrate blocks that have the v3 structure (with has-text-align-* class).
// Older v1/v2 blocks used different structures and should be handled by their own deprecations.
const hasTextAlignClass =
block?.originalContent?.includes( 'has-text-align-' );
return !! attributes.textAlign && hasTextAlignClass;
},
Comment thread
SirLouen marked this conversation as resolved.
Resolved
};

Expand All @@ -93,4 +171,4 @@ const v2 = {
*
* See block-deprecation.md
*/
export default [ v2, v1 ];
export default [ v3, v2, v1 ];
93 changes: 37 additions & 56 deletions packages/block-library/src/verse/edit.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,48 @@
/**
* External dependencies
*/
import clsx from 'clsx';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
RichText,
BlockControls,
AlignmentToolbar,
useBlockProps,
} from '@wordpress/block-editor';
import { RichText, useBlockProps } from '@wordpress/block-editor';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';

export default function VerseEdit( {
attributes,
setAttributes,
mergeBlocks,
onRemove,
insertBlocksAfter,
style,
} ) {
const { textAlign, content } = attributes;
const blockProps = useBlockProps( {
className: clsx( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
/**
* Internal dependencies
*/
import useDeprecatedTextAlign from '../utils/deprecated-text-align-attributes';

export default function VerseEdit( props ) {
const {
attributes,
setAttributes,
mergeBlocks,
onRemove,
insertBlocksAfter,
style,
} );
} = props;
const { content } = attributes;
useDeprecatedTextAlign( props );
const blockProps = useBlockProps( { style } );

return (
<>
<BlockControls>
<AlignmentToolbar
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
</BlockControls>
<RichText
tagName="pre"
identifier="content"
preserveWhiteSpace
value={ content }
onChange={ ( nextContent ) => {
setAttributes( {
content: nextContent,
} );
} }
aria-label={ __( 'Verse text' ) }
placeholder={ __( 'Write verse…' ) }
onRemove={ onRemove }
onMerge={ mergeBlocks }
textAlign={ textAlign }
{ ...blockProps }
__unstablePastePlainText
__unstableOnSplitAtDoubleLineEnd={ () =>
insertBlocksAfter( createBlock( getDefaultBlockName() ) )
}
/>
</>
<RichText
tagName="pre"
identifier="content"
preserveWhiteSpace
value={ content }
onChange={ ( nextContent ) => {
setAttributes( {
content: nextContent,
} );
} }
aria-label={ __( 'Verse text' ) }
placeholder={ __( 'Write verse…' ) }
onRemove={ onRemove }
onMerge={ mergeBlocks }
{ ...blockProps }
__unstablePastePlainText
__unstableOnSplitAtDoubleLineEnd={ () =>
insertBlocksAfter( createBlock( getDefaultBlockName() ) )
}
/>
);
}
13 changes: 2 additions & 11 deletions packages/block-library/src/verse/save.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
/**
* External dependencies
*/
import clsx from 'clsx';

/**
* WordPress dependencies
*/
import { RichText, useBlockProps } from '@wordpress/block-editor';

export default function save( { attributes } ) {
const { textAlign, content } = attributes;

const className = clsx( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} );
const { content } = attributes;

return (
<pre { ...useBlockProps.save( { className } ) }>
<pre { ...useBlockProps.save() }>
<RichText.Content value={ content } />
</pre>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"isValid": true,
"attributes": {
"content": "A <em>verse</em>…<br>And more!",
"textAlign": "left"
"style": {
"typography": {
"textAlign": "left"
}
}
},
"innerBlocks": []
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:verse {"textAlign":"left"} -->
<!-- wp:verse {"style":{"typography":{"textAlign":"left"}}} -->
<pre class="wp-block-verse has-text-align-left">A <em>verse</em>…<br>And more!</pre>
<!-- /wp:verse -->
11 changes: 11 additions & 0 deletions test/integration/fixtures/blocks/core__verse__deprecated-v3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- wp:verse {"textAlign":"left"} -->
<pre class="wp-block-verse has-text-align-left">A <em>verse</em>…<br>And more!</pre>
<!-- /wp:verse -->

<!-- wp:verse {"textAlign":"center"} -->
<pre class="wp-block-verse has-text-align-center">A <em>verse</em>…<br>And more!</pre>
<!-- /wp:verse -->

<!-- wp:verse {"textAlign":"right"} -->
<pre class="wp-block-verse has-text-align-right">A <em>verse</em>…<br>And more!</pre>
<!-- /wp:verse -->
41 changes: 41 additions & 0 deletions test/integration/fixtures/blocks/core__verse__deprecated-v3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[
{
"name": "core/verse",
"isValid": true,
"attributes": {
"content": "A <em>verse</em>…<br>And more!",
"style": {
"typography": {
"textAlign": "left"
}
}
},
"innerBlocks": []
},
{
"name": "core/verse",
"isValid": true,
"attributes": {
"content": "A <em>verse</em>…<br>And more!",
"style": {
"typography": {
"textAlign": "center"
}
}
},
"innerBlocks": []
},
{
"name": "core/verse",
"isValid": true,
"attributes": {
"content": "A <em>verse</em>…<br>And more!",
"style": {
"typography": {
"textAlign": "right"
}
}
},
"innerBlocks": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[
{
"blockName": "core/verse",
"attrs": {
"textAlign": "left"
},
"innerBlocks": [],
"innerHTML": "\n<pre class=\"wp-block-verse has-text-align-left\">A <em>verse</em>…<br>And more!</pre>\n",
"innerContent": [
"\n<pre class=\"wp-block-verse has-text-align-left\">A <em>verse</em>…<br>And more!</pre>\n"
]
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n\n",
"innerContent": [ "\n\n" ]
},
{
"blockName": "core/verse",
"attrs": {
"textAlign": "center"
},
"innerBlocks": [],
"innerHTML": "\n<pre class=\"wp-block-verse has-text-align-center\">A <em>verse</em>…<br>And more!</pre>\n",
"innerContent": [
"\n<pre class=\"wp-block-verse has-text-align-center\">A <em>verse</em>…<br>And more!</pre>\n"
]
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n\n",
"innerContent": [ "\n\n" ]
},
{
"blockName": "core/verse",
"attrs": {
"textAlign": "right"
},
"innerBlocks": [],
"innerHTML": "\n<pre class=\"wp-block-verse has-text-align-right\">A <em>verse</em>…<br>And more!</pre>\n",
"innerContent": [
"\n<pre class=\"wp-block-verse has-text-align-right\">A <em>verse</em>…<br>And more!</pre>\n"
]
}
]
Loading
Loading