-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Implement the nested block behavior in list block v2 #39487
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 2, | ||
"name": "core/list-item", | ||
"title": "List item", | ||
"category": "text", | ||
"parent": [ "core/list" ], | ||
"description": "Create a list item.", | ||
"textdomain": "default", | ||
"attributes": { | ||
"placeholder": { | ||
"type": "string" | ||
}, | ||
"content": { | ||
"type": "string", | ||
"source": "html", | ||
"selector": "li", | ||
"default": "", | ||
"__experimentalRole": "content" | ||
} | ||
}, | ||
"supports": { | ||
"className": false, | ||
"__experimentalSelector": "li" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
RichText, | ||
useBlockProps, | ||
useInnerBlocksProps, | ||
} from '@wordpress/block-editor'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
export default function ListItemEdit( { attributes, setAttributes } ) { | ||
const blockProps = useBlockProps(); | ||
const innerBlocksProps = useInnerBlocksProps( blockProps, { | ||
allowedBlocks: [ 'core/list' ], | ||
} ); | ||
Comment on lines
+13
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: |
||
|
||
return ( | ||
<li { ...innerBlocksProps }> | ||
<RichText | ||
identifier="content" | ||
tagName="div" | ||
onChange={ ( nextContent ) => | ||
setAttributes( { content: nextContent } ) | ||
} | ||
value={ attributes.content } | ||
aria-label={ __( 'List text' ) } | ||
placeholder={ attributes.placeholder || __( 'List' ) } | ||
/> | ||
{ innerBlocksProps.children } | ||
</li> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { list as icon } from '@wordpress/icons'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import metadata from './block.json'; | ||
import edit from './edit'; | ||
import save from './save'; | ||
|
||
const { name } = metadata; | ||
|
||
export { metadata, name }; | ||
|
||
export const settings = { | ||
icon, | ||
edit, | ||
save, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { InnerBlocks, RichText, useBlockProps } from '@wordpress/block-editor'; | ||
|
||
export default function save( { attributes } ) { | ||
return ( | ||
<li { ...useBlockProps.save() }> | ||
<RichText.Content value={ attributes.content } /> | ||
<InnerBlocks.Content /> | ||
</li> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,18 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; | ||
|
||
const TEMPLATE = [ [ 'core/list-item' ] ]; | ||
|
||
function Edit() { | ||
return <div>List block v2</div>; | ||
const blockProps = useBlockProps(); | ||
const innerBlocksProps = useInnerBlocksProps( blockProps, { | ||
allowedBlocks: [ 'core/list-item' ], | ||
template: TEMPLATE, | ||
} ); | ||
|
||
return <ul { ...innerBlocksProps } />; | ||
} | ||
|
||
export default Edit; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
function Save() { | ||
return <div>List block v2</div>; | ||
} | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor'; | ||
|
||
export default Save; | ||
export default function save() { | ||
return ( | ||
<ul { ...useBlockProps.save() }> | ||
<InnerBlocks.Content /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add some checks to return null if there are no inner blocks? It is very easy to create an empty list item for the user is very hard to remove. Or maybe we can automatically remove the list block if it becomes empty? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should add a shortcut later that removes it on backspace |
||
</ul> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:list-item --><li></li><!-- /wp:list-item --> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"name": "core/list-item", | ||
"isValid": true, | ||
"attributes": { | ||
"content": "" | ||
}, | ||
"innerBlocks": [] | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[ | ||
{ | ||
"blockName": "core/list-item", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "<li></li>", | ||
"innerContent": [ "<li></li>" ] | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<!-- wp:list-item --> | ||
<li></li> | ||
<!-- /wp:list-item --> |
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.
Indentation here is off.