-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Mobile] List V2 - Fixes split issues (#43949)
* Mobile - List V2 - Add useEnter to fix splitting issue * Merging blocks: allow x to be merged into wrapper blocks (quote, list, group...) (#42780) * Add test for the group block (#42801) Co-authored-by: Ella van Durpe <4710635+ellatrix@users.noreply.github.com> Co-authored-by: Justin Ahinon <justiny.ahinon@gmail.com>
- Loading branch information
1 parent
59dd5de
commit f761037
Showing
14 changed files
with
250 additions
and
110 deletions.
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
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
77 changes: 77 additions & 0 deletions
77
packages/block-library/src/list-item/hooks/use-enter.native.js
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,77 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
createBlock, | ||
getDefaultBlockName, | ||
cloneBlock, | ||
} from '@wordpress/blocks'; | ||
import { useRef } from '@wordpress/element'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useOutdentListItem from './use-outdent-list-item'; | ||
|
||
export default function useEnter( props, preventDefault ) { | ||
const { replaceBlocks, selectionChange } = useDispatch( blockEditorStore ); | ||
const { getBlock, getBlockRootClientId, getBlockIndex } = | ||
useSelect( blockEditorStore ); | ||
const propsRef = useRef( props ); | ||
propsRef.current = props; | ||
const [ canOutdent, outdentListItem ] = useOutdentListItem( | ||
propsRef.current.clientId | ||
); | ||
|
||
return { | ||
onEnter() { | ||
const { content, clientId } = propsRef.current; | ||
if ( content.length ) { | ||
return; | ||
} | ||
preventDefault.current = true; | ||
if ( canOutdent ) { | ||
outdentListItem(); | ||
return; | ||
} | ||
// Here we are in top level list so we need to split. | ||
const topParentListBlock = getBlock( | ||
getBlockRootClientId( clientId ) | ||
); | ||
const blockIndex = getBlockIndex( clientId ); | ||
const head = cloneBlock( { | ||
...topParentListBlock, | ||
innerBlocks: topParentListBlock.innerBlocks.slice( | ||
0, | ||
blockIndex | ||
), | ||
} ); | ||
const middle = createBlock( getDefaultBlockName() ); | ||
// Last list item might contain a `list` block innerBlock | ||
// In that case append remaining innerBlocks blocks. | ||
const after = [ | ||
...( topParentListBlock.innerBlocks[ blockIndex ] | ||
.innerBlocks[ 0 ]?.innerBlocks || [] ), | ||
...topParentListBlock.innerBlocks.slice( blockIndex + 1 ), | ||
]; | ||
const tail = after.length | ||
? [ | ||
cloneBlock( { | ||
...topParentListBlock, | ||
innerBlocks: after, | ||
} ), | ||
] | ||
: []; | ||
replaceBlocks( | ||
topParentListBlock.clientId, | ||
[ head, middle, ...tail ], | ||
1 | ||
); | ||
// We manually change the selection here because we are replacing | ||
// a different block than the selected one. | ||
selectionChange( middle.clientId ); | ||
}, | ||
}; | ||
} |
21 changes: 0 additions & 21 deletions
21
packages/e2e-tests/specs/editor/blocks/__snapshots__/group.test.js.snap
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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
3 changes: 3 additions & 0 deletions
3
.../editor/blocks/__snapshots__/Group-can-be-created-using-the-block-inserter-1-chromium.txt
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,3 @@ | ||
<!-- wp:group --> | ||
<div class="wp-block-group"></div> | ||
<!-- /wp:group --> |
3 changes: 3 additions & 0 deletions
3
.../editor/blocks/__snapshots__/Group-can-be-created-using-the-slash-inserter-1-chromium.txt
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,3 @@ | ||
<!-- wp:group --> | ||
<div class="wp-block-group"></div> | ||
<!-- /wp:group --> |
5 changes: 5 additions & 0 deletions
5
...ots__/Group-can-have-other-blocks-appended-to-it-using-the-button-appender-1-chromium.txt
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,5 @@ | ||
<!-- wp:group --> | ||
<div class="wp-block-group"><!-- wp:paragraph --> | ||
<p>Group Block with a Paragraph</p> | ||
<!-- /wp:paragraph --></div> | ||
<!-- /wp:group --> |
9 changes: 9 additions & 0 deletions
9
...pecs/editor/blocks/__snapshots__/Group-can-merge-into-group-with-Backspace-1-chromium.txt
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,9 @@ | ||
<!-- wp:group --> | ||
<div class="wp-block-group"><!-- wp:paragraph --> | ||
<p>1</p> | ||
<!-- /wp:paragraph --></div> | ||
<!-- /wp:group --> | ||
|
||
<!-- wp:paragraph --> | ||
<p>2</p> | ||
<!-- /wp:paragraph --> |
9 changes: 9 additions & 0 deletions
9
...pecs/editor/blocks/__snapshots__/Group-can-merge-into-group-with-Backspace-2-chromium.txt
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,9 @@ | ||
<!-- wp:group --> | ||
<div class="wp-block-group"><!-- wp:paragraph --> | ||
<p>1</p> | ||
<!-- /wp:paragraph --> | ||
|
||
<!-- wp:paragraph --> | ||
<p>2</p> | ||
<!-- /wp:paragraph --></div> | ||
<!-- /wp:group --> |
Oops, something went wrong.