-
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
Inserter: show all blocks (alternative) #62169
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
aab85e5
Inserter: show all block
ellatrix aaa6532
Fix parents
ellatrix ef99120
Use section root as fallback
ellatrix 3cf78ba
Remove heading, remove reusable blocks
ellatrix 9a3f379
Add private selector option
ellatrix afa0d6e
Fix quick inserter
ellatrix 9c52bd6
Fix appender inserter
ellatrix 08b1397
Fix most used, fix quick inserter
ellatrix 80d6c1b
Fix widgets page
ellatrix 118f146
Fix e2e tests
ellatrix 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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { useDispatch, useRegistry, useSelect } from '@wordpress/data'; | ||
import { isUnmodifiedDefaultBlock } from '@wordpress/blocks'; | ||
import { _n, sprintf } from '@wordpress/i18n'; | ||
import { speak } from '@wordpress/a11y'; | ||
|
@@ -13,6 +13,34 @@ import { useCallback } from '@wordpress/element'; | |
import { store as blockEditorStore } from '../../../store'; | ||
import { unlock } from '../../../lock-unlock'; | ||
|
||
function getIndex( { | ||
destinationRootClientId, | ||
destinationIndex, | ||
rootClientId, | ||
registry, | ||
} ) { | ||
if ( rootClientId === destinationRootClientId ) { | ||
return destinationIndex; | ||
} | ||
const parents = [ | ||
'', | ||
...registry | ||
.select( blockEditorStore ) | ||
.getBlockParents( destinationRootClientId ), | ||
destinationRootClientId, | ||
]; | ||
const parentIndex = parents.indexOf( rootClientId ); | ||
if ( parentIndex !== -1 ) { | ||
return ( | ||
registry | ||
.select( blockEditorStore ) | ||
.getBlockIndex( parents[ parentIndex + 1 ] ) + 1 | ||
); | ||
} | ||
return registry.select( blockEditorStore ).getBlockOrder( rootClientId ) | ||
.length; | ||
} | ||
|
||
/** | ||
* @typedef WPInserterConfig | ||
* | ||
|
@@ -42,6 +70,7 @@ function useInsertionPoint( { | |
shouldFocusBlock = true, | ||
selectBlockOnInsert = true, | ||
} ) { | ||
const registry = useRegistry(); | ||
const { getSelectedBlock } = useSelect( blockEditorStore ); | ||
const { destinationRootClientId, destinationIndex } = useSelect( | ||
( select ) => { | ||
|
@@ -91,7 +120,7 @@ function useInsertionPoint( { | |
} = unlock( useDispatch( blockEditorStore ) ); | ||
|
||
const onInsertBlocks = useCallback( | ||
( blocks, meta, shouldForceFocusBlock = false ) => { | ||
( blocks, meta, shouldForceFocusBlock = false, _rootClientId ) => { | ||
// When we are trying to move focus or select a new block on insert, we also | ||
// need to clear the last focus to avoid the focus being set to the wrong block | ||
// when tabbing back into the canvas if the block was added from outside the | ||
|
@@ -121,8 +150,13 @@ function useInsertionPoint( { | |
} else { | ||
insertBlocks( | ||
blocks, | ||
destinationIndex, | ||
destinationRootClientId, | ||
getIndex( { | ||
destinationRootClientId, | ||
destinationIndex, | ||
rootClientId: _rootClientId, | ||
registry, | ||
} ), | ||
_rootClientId, | ||
selectBlockOnInsert, | ||
shouldFocusBlock || shouldForceFocusBlock ? 0 : null, | ||
meta | ||
|
@@ -154,9 +188,17 @@ function useInsertionPoint( { | |
); | ||
|
||
const onToggleInsertionPoint = useCallback( | ||
( show ) => { | ||
if ( show ) { | ||
showInsertionPoint( destinationRootClientId, destinationIndex ); | ||
( item ) => { | ||
if ( item?.hasOwnProperty( 'rootClientId' ) ) { | ||
showInsertionPoint( | ||
item.rootClientId, | ||
getIndex( { | ||
destinationRootClientId, | ||
destinationIndex, | ||
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. At some point we should remove all this |
||
rootClientId: item.rootClientId, | ||
registry, | ||
} ) | ||
); | ||
} else { | ||
hideInsertionPoint(); | ||
} | ||
|
Oops, something went wrong.
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.
This is added because we now split the blocks tab in two panels, one for blocks inserted in the current selection insertion point one that can be inserted in the nearest available insertion point relative to the current selection.