-
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: Fix 'Browse All' in Quick Inserter #26443
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b7b208b
Inserter: Fix 'Browse All' in Quick Inserter
noisysocks c5817dd
Inserter: Fix typo
noisysocks e707a30
Call getBlockInsertionPoint once
talldan 0ac181d
Add useSelect dependencies
talldan 88d026a
Make setInsertionPoint unstable
talldan 6debf83
Avoid setting `isVisible` state when `SET_INSERTION_POINT` is triggered
talldan a092a95
Make index required and clarify rootClientId usage
talldan 90a1820
Split insertionPoint into two reducers
talldan f7c8084
Fix getInsertionPoint selector that expects default state of reducer …
talldan 938c7a5
Avoid resetting insertionPoint state on HIDE_INSERTION_POINT
talldan 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 |
---|---|---|
@@ -1,8 +1,3 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { pick } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
|
@@ -14,10 +9,17 @@ import { speak } from '@wordpress/a11y'; | |
/** | ||
* @typedef WPInserterConfig | ||
* | ||
* @property {string=} rootClientId Inserter Root Client ID. | ||
* @property {string=} clientId Inserter Client ID. | ||
* @property {boolean} isAppender Whether the inserter is an appender or not. | ||
* @property {boolean} selectBlockOnInsert Whether the block should be selected on insert. | ||
* @property {string=} rootClientId If set, insertion will be into the | ||
* block with this ID. | ||
* @property {number=} insertionIndex If set, insertion will be into this | ||
* explicit position. | ||
* @property {string=} clientId If set, insertion will be after the | ||
* block with this ID. | ||
* @property {boolean=} isAppender Whether the inserter is an appender | ||
* or not. | ||
* @property {boolean=} selectBlockOnInsert Whether the block should be | ||
* selected on insert. | ||
* @property {Function=} onSelect Called after insertion. | ||
*/ | ||
|
||
/** | ||
|
@@ -27,77 +29,74 @@ import { speak } from '@wordpress/a11y'; | |
* @return {Array} Insertion Point State (rootClientID, onInsertBlocks and onToggle). | ||
*/ | ||
function useInsertionPoint( { | ||
onSelect, | ||
rootClientId, | ||
insertionIndex, | ||
clientId, | ||
isAppender, | ||
selectBlockOnInsert, | ||
insertionIndex, | ||
onSelect, | ||
} ) { | ||
const { | ||
selectedBlock, | ||
destinationRootClientId, | ||
getSelectedBlock, | ||
getBlockIndex, | ||
getBlockSelectionEnd, | ||
getBlockOrder, | ||
destinationIndex, | ||
} = useSelect( | ||
( select ) => { | ||
const { | ||
getSettings, | ||
getBlockRootClientId, | ||
getBlockSelectionEnd: _getBlockSelectionEnd, | ||
getSelectedBlock, | ||
getBlockIndex, | ||
getBlockOrder, | ||
getBlockInsertionPoint, | ||
} = select( 'core/block-editor' ); | ||
|
||
let destRootClientId = rootClientId; | ||
if ( ! destRootClientId && ! clientId && ! isAppender ) { | ||
const end = _getBlockSelectionEnd(); | ||
if ( end ) { | ||
destRootClientId = getBlockRootClientId( end ); | ||
let _destinationRootClientId, _destinationIndex; | ||
|
||
if ( rootClientId || insertionIndex || clientId || isAppender ) { | ||
// If any of these arguments are set, we're in "manual mode" | ||
// meaning the insertion point is set by the caller. | ||
|
||
_destinationRootClientId = rootClientId; | ||
|
||
if ( insertionIndex ) { | ||
// Insert into a specific index. | ||
_destinationIndex = insertionIndex; | ||
} else if ( clientId ) { | ||
// Insert after a specific client ID. | ||
_destinationIndex = getBlockIndex( | ||
clientId, | ||
_destinationRootClientId | ||
); | ||
} else { | ||
// Insert at the end of the list. | ||
_destinationIndex = getBlockOrder( | ||
_destinationRootClientId | ||
).length; | ||
} | ||
} else { | ||
// Otherwise, we're in "auto mode" where the insertion point is | ||
// decided by getBlockInsertionPoint(). | ||
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. Do you think we can simplify further and make this the default mode: Maybe by "setting the insertion point" when we open the inserter (all inserters) |
||
const insertionPoint = getBlockInsertionPoint(); | ||
_destinationRootClientId = insertionPoint.rootClientId; | ||
_destinationIndex = insertionPoint.index; | ||
} | ||
|
||
return { | ||
hasPatterns: !! getSettings().__experimentalBlockPatterns | ||
?.length, | ||
destinationRootClientId: destRootClientId, | ||
...pick( select( 'core/block-editor' ), [ | ||
'getSelectedBlock', | ||
'getBlockIndex', | ||
'getBlockSelectionEnd', | ||
'getBlockOrder', | ||
] ), | ||
selectedBlock: getSelectedBlock(), | ||
destinationRootClientId: _destinationRootClientId, | ||
destinationIndex: _destinationIndex, | ||
}; | ||
}, | ||
[ isAppender, clientId, rootClientId ] | ||
[ rootClientId, insertionIndex, clientId, isAppender ] | ||
); | ||
|
||
const { | ||
replaceBlocks, | ||
insertBlocks, | ||
showInsertionPoint, | ||
hideInsertionPoint, | ||
} = useDispatch( 'core/block-editor' ); | ||
|
||
function getInsertionIndex() { | ||
if ( insertionIndex !== undefined ) { | ||
return insertionIndex; | ||
} | ||
|
||
// If the clientId is defined, we insert at the position of the block. | ||
if ( clientId ) { | ||
return getBlockIndex( clientId, destinationRootClientId ); | ||
} | ||
|
||
// If there's a selected block, and the selected block is not the destination root block, we insert after the selected block. | ||
const end = getBlockSelectionEnd(); | ||
if ( ! isAppender && end ) { | ||
return getBlockIndex( end, destinationRootClientId ) + 1; | ||
} | ||
|
||
// Otherwise, we insert at the end of the current rootClientId | ||
return getBlockOrder( destinationRootClientId ).length; | ||
} | ||
|
||
const onInsertBlocks = ( blocks, meta ) => { | ||
const selectedBlock = getSelectedBlock(); | ||
if ( | ||
! isAppender && | ||
selectedBlock && | ||
|
@@ -107,7 +106,7 @@ function useInsertionPoint( { | |
} else { | ||
insertBlocks( | ||
blocks, | ||
getInsertionIndex(), | ||
destinationIndex, | ||
destinationRootClientId, | ||
selectBlockOnInsert, | ||
meta | ||
|
@@ -131,8 +130,7 @@ function useInsertionPoint( { | |
|
||
const onToggleInsertionPoint = ( show ) => { | ||
if ( show ) { | ||
const index = getInsertionIndex(); | ||
showInsertionPoint( destinationRootClientId, index ); | ||
showInsertionPoint( destinationRootClientId, destinationIndex ); | ||
} else { | ||
hideInsertionPoint(); | ||
} | ||
|
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.
There was a relevant comment here about these props - #25763 (comment).
So the idea would to refactor inserters to just use rootClientId and blockIndex. The insertion point state seems to fit well with that, which is nice (no action required here 😄 ).
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.
I like that idea. It's definitely a little crazy right now.