-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
List block: Add migration from v1 to v2 #39799
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { registerBlockType, serialize } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { migrateToListV2 } from '../v2/migrate'; | ||
import * as listItem from '../../list-item'; | ||
import * as list from '../../list'; | ||
import listV2 from '../../list/v2'; | ||
|
||
describe( 'Migrate list block', () => { | ||
beforeAll( () => { | ||
const prev = window.__experimentalEnableListBlockV2; | ||
|
||
// force list and list item block registration. | ||
registerBlockType( | ||
{ name: listItem.name, ...listItem.metadata }, | ||
listItem.settings | ||
); | ||
registerBlockType( { name: list.name, ...list.metadata }, listV2 ); | ||
|
||
window.__experimentalEnableListBlockV2 = prev; | ||
} ); | ||
|
||
it( 'should migrate the values attribute to inner blocks', () => { | ||
const [ updatedAttributes, updatedInnerBlocks ] = migrateToListV2( { | ||
values: | ||
'<li>test</li><li>test</li><li>test<ol><li>test test</li><li>test est eesssss</li></ol></li>', | ||
ordered: false, | ||
} ); | ||
|
||
expect( updatedAttributes ).toEqual( { | ||
ordered: false, | ||
// Ideally the values attributes shouldn't be here | ||
// but since we didn't enable v2 by default yet, | ||
// we're keeping the old default value in block.json | ||
values: '', | ||
} ); | ||
expect( serialize( updatedInnerBlocks ) ) | ||
.toEqual( `<!-- wp:list-item --> | ||
<li>test</li> | ||
<!-- /wp:list-item --> | ||
|
||
<!-- wp:list-item --> | ||
<li>test</li> | ||
<!-- /wp:list-item --> | ||
|
||
<!-- wp:list-item --> | ||
<li>test<!-- wp:list {\"ordered\":true} --> | ||
<ol><!-- wp:list-item --> | ||
<li>test test</li> | ||
<!-- /wp:list-item --> | ||
|
||
<!-- wp:list-item --> | ||
<li>test est eesssss</li> | ||
<!-- /wp:list-item --></ol> | ||
<!-- /wp:list --></li> | ||
<!-- /wp:list-item -->` ); | ||
} ); | ||
|
||
it( 'should handle empty space properly', () => { | ||
const [ updatedAttributes, updatedInnerBlocks ] = migrateToListV2( { | ||
values: `<li>Europe</li> | ||
<li> | ||
\tAfrica | ||
<ol> | ||
<li>Algeria</li> | ||
</ol> | ||
\t | ||
</li>`, | ||
ordered: false, | ||
} ); | ||
|
||
expect( updatedAttributes ).toEqual( { | ||
ordered: false, | ||
// Ideally the values attributes shouldn't be here | ||
// but since we didn't enable v2 by default yet, | ||
// we're keeping the old default value in block.json | ||
values: '', | ||
} ); | ||
expect( serialize( updatedInnerBlocks ) ) | ||
.toEqual( `<!-- wp:list-item --> | ||
<li>Europe</li> | ||
<!-- /wp:list-item --> | ||
|
||
<!-- wp:list-item --> | ||
<li>Africa<!-- wp:list {\"ordered\":true} --> | ||
<ol><!-- wp:list-item --> | ||
<li>Algeria</li> | ||
<!-- /wp:list-item --></ol> | ||
<!-- /wp:list --></li> | ||
<!-- /wp:list-item -->` ); | ||
} ); | ||
|
||
it( 'should handle formats properly', () => { | ||
const [ updatedAttributes, updatedInnerBlocks ] = migrateToListV2( { | ||
values: `<li>Europe<ul><li>France<ul><li>Lyon <strong>Rhone</strong>s</li><li>Paris <em>Ile de france</em><ul><li><em>1er</em></li></ul></li></ul></li></ul></li></ul></li>`, | ||
ordered: false, | ||
} ); | ||
|
||
expect( updatedAttributes ).toEqual( { | ||
ordered: false, | ||
// Ideally the values attributes shouldn't be here | ||
// but since we didn't enable v2 by default yet, | ||
// we're keeping the old default value in block.json | ||
values: '', | ||
} ); | ||
expect( serialize( updatedInnerBlocks ) ) | ||
.toEqual( `<!-- wp:list-item --> | ||
<li>Europe<!-- wp:list --> | ||
<ul><!-- wp:list-item --> | ||
<li>France<!-- wp:list --> | ||
<ul><!-- wp:list-item --> | ||
<li>Lyon <strong>Rhone</strong>s</li> | ||
<!-- /wp:list-item --> | ||
|
||
<!-- wp:list-item --> | ||
<li>Paris <em>Ile de france</em><!-- wp:list --> | ||
<ul><!-- wp:list-item --> | ||
<li><em>1er</em></li> | ||
<!-- /wp:list-item --></ul> | ||
<!-- /wp:list --></li> | ||
<!-- /wp:list-item --></ul> | ||
<!-- /wp:list --></li> | ||
<!-- /wp:list-item --></ul> | ||
<!-- /wp:list --></li> | ||
<!-- /wp:list-item -->` ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { RichText, useBlockProps } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import initialDeprecations from '../deprecated'; | ||
import { migrateToListV2 } from './migrate'; | ||
|
||
const v1 = { | ||
attributes: { | ||
ordered: { | ||
type: 'boolean', | ||
default: false, | ||
__experimentalRole: 'content', | ||
}, | ||
values: { | ||
type: 'string', | ||
source: 'html', | ||
selector: 'ol,ul', | ||
multiline: 'li', | ||
__unstableMultilineWrapperTags: [ 'ol', 'ul' ], | ||
default: '', | ||
__experimentalRole: 'content', | ||
}, | ||
type: { | ||
type: 'string', | ||
}, | ||
start: { | ||
type: 'number', | ||
}, | ||
reversed: { | ||
type: 'boolean', | ||
}, | ||
placeholder: { | ||
type: 'string', | ||
}, | ||
}, | ||
supports: { | ||
anchor: true, | ||
className: false, | ||
typography: { | ||
fontSize: true, | ||
__experimentalFontFamily: true, | ||
lineHeight: true, | ||
__experimentalFontStyle: true, | ||
__experimentalFontWeight: true, | ||
__experimentalLetterSpacing: true, | ||
__experimentalTextTransform: true, | ||
__experimentalDefaultControls: { | ||
fontSize: true, | ||
}, | ||
}, | ||
color: { | ||
gradients: true, | ||
link: true, | ||
__experimentalDefaultControls: { | ||
background: true, | ||
text: true, | ||
}, | ||
}, | ||
__unstablePasteTextInline: true, | ||
__experimentalSelector: 'ol,ul', | ||
__experimentalSlashInserter: true, | ||
}, | ||
save( { attributes } ) { | ||
const { ordered, values, type, reversed, start } = attributes; | ||
const TagName = ordered ? 'ol' : 'ul'; | ||
|
||
return ( | ||
<TagName { ...useBlockProps.save( { type, reversed, start } ) }> | ||
<RichText.Content value={ values } multiline="li" /> | ||
</TagName> | ||
); | ||
}, | ||
migrate: migrateToListV2, | ||
}; | ||
|
||
/** | ||
* New deprecations need to be placed first | ||
* for them to have higher priority. | ||
* | ||
* Old deprecations may need to be updated as well. | ||
* | ||
* See block-deprecation.md | ||
*/ | ||
export default [ v1, ...initialDeprecations ]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's version 0? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a random name for the first deprecated version :P