-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix widget insertion from sidebar block library by using a declared i…
…nsertionPoint prop (#25763) * Fix widget insertion from sidebar block library by using a declared insertionPoint prop * Add comments * Optimize by avoiding `getEntityRecord` call unless needed
- Loading branch information
Showing
8 changed files
with
134 additions
and
48 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
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
37 changes: 0 additions & 37 deletions
37
packages/edit-widgets/src/hooks/use-last-selected-root-id.js
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
packages/edit-widgets/src/hooks/use-last-selected-widget-area.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,53 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { buildWidgetAreasPostId, KIND, POST_TYPE } from '../store/utils'; | ||
|
||
/** | ||
* A react hook that returns the client id of the last widget area to have | ||
* been selected, or to have a selected block within it. | ||
* | ||
* @return {string} clientId of the widget area last selected. | ||
*/ | ||
const useLastSelectedWidgetArea = () => | ||
useSelect( ( select ) => { | ||
const { getBlockSelectionEnd, getBlockParents, getBlockName } = select( | ||
'core/block-editor' | ||
); | ||
const blockSelectionEndClientId = getBlockSelectionEnd(); | ||
|
||
// If the selected block is a widget area, return its clientId. | ||
if ( | ||
getBlockName( blockSelectionEndClientId ) === 'core/widget-area' | ||
) { | ||
return blockSelectionEndClientId; | ||
} | ||
|
||
// Otherwise, find the clientId of the top-level widget area by looking | ||
// through the selected block's parents. | ||
const blockParents = getBlockParents( blockSelectionEndClientId ); | ||
const rootWidgetAreaClientId = blockParents.find( | ||
( clientId ) => getBlockName( clientId ) === 'core/widget-area' | ||
); | ||
|
||
if ( rootWidgetAreaClientId ) { | ||
return rootWidgetAreaClientId; | ||
} | ||
|
||
// If no widget area has been selected, return the clientId of the first | ||
// area. | ||
const { getEntityRecord } = select( 'core' ); | ||
const widgetAreasPost = getEntityRecord( | ||
KIND, | ||
POST_TYPE, | ||
buildWidgetAreasPostId() | ||
); | ||
return widgetAreasPost?.blocks[ 0 ]?.clientId; | ||
}, [] ); | ||
|
||
export default useLastSelectedWidgetArea; |
54 changes: 54 additions & 0 deletions
54
packages/edit-widgets/src/hooks/use-widget-library-insertion-point.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,54 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { buildWidgetAreasPostId, KIND, POST_TYPE } from '../store/utils'; | ||
|
||
const useWidgetLibraryInsertionPoint = () => { | ||
const firstRootId = useSelect( ( select ) => { | ||
// Default to the first widget area | ||
const { getEntityRecord } = select( 'core' ); | ||
const widgetAreasPost = getEntityRecord( | ||
KIND, | ||
POST_TYPE, | ||
buildWidgetAreasPostId() | ||
); | ||
return widgetAreasPost?.blocks[ 0 ]?.clientId; | ||
}, [] ); | ||
|
||
return useSelect( | ||
( select ) => { | ||
const { | ||
getBlockRootClientId, | ||
getBlockSelectionEnd, | ||
getBlockOrder, | ||
getBlockIndex, | ||
} = select( 'core/block-editor' ); | ||
|
||
const clientId = getBlockSelectionEnd() || firstRootId; | ||
const rootClientId = getBlockRootClientId( clientId ); | ||
|
||
// If the selected block is at the root level, it's a widget area and | ||
// blocks can't be inserted here. Return this block as the root and the | ||
// last child clientId indicating insertion at the end. | ||
if ( clientId && rootClientId === '' ) { | ||
return { | ||
rootClientId: clientId, | ||
insertionIndex: getBlockOrder( clientId ).length, | ||
}; | ||
} | ||
|
||
return { | ||
rootClientId, | ||
insertionIndex: getBlockIndex( clientId, rootClientId ) + 1, | ||
}; | ||
}, | ||
[ firstRootId ] | ||
); | ||
}; | ||
|
||
export default useWidgetLibraryInsertionPoint; |