-
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.
Update query block creation and replacement flows
- Loading branch information
1 parent
27b4e83
commit 8125f78
Showing
5 changed files
with
193 additions
and
40 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
58 changes: 58 additions & 0 deletions
58
packages/block-library/src/query/edit/pattern-selection-modal.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,58 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { cloneBlock } from '@wordpress/blocks'; | ||
import { useAsyncList } from '@wordpress/compose'; | ||
import { | ||
__experimentalBlockPatternsList as BlockPatternsList, | ||
store as blockEditorStore, | ||
} from '@wordpress/block-editor'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getFirstQueryClientIdFromBlocks } from '../utils'; | ||
|
||
export default function PatternSelectionModal( { clientId, name } ) { | ||
const { blockPatterns } = useSelect( | ||
( select ) => { | ||
const { | ||
getBlockRootClientId, | ||
__experimentalGetPatternsByBlockTypes, | ||
} = select( blockEditorStore ); | ||
const rootClientId = getBlockRootClientId( clientId ); | ||
|
||
return { | ||
blockPatterns: __experimentalGetPatternsByBlockTypes( | ||
name, | ||
rootClientId | ||
), | ||
}; | ||
}, | ||
[ clientId, name ] | ||
); | ||
const { replaceBlock, selectBlock } = useDispatch( blockEditorStore ); | ||
|
||
const onBlockPatternSelect = ( _pattern, blocks ) => { | ||
const clonedBlocks = blocks.map( ( block ) => cloneBlock( block ) ); | ||
const firstQueryClientId = getFirstQueryClientIdFromBlocks( | ||
clonedBlocks | ||
); | ||
replaceBlock( clientId, clonedBlocks ); | ||
if ( firstQueryClientId ) { | ||
selectBlock( firstQueryClientId ); | ||
} | ||
}; | ||
const shownBlockPatterns = useAsyncList( blockPatterns, { step: 6 } ); | ||
|
||
return ( | ||
<div className="block-library-query__pattern-selection-content"> | ||
<BlockPatternsList | ||
blockPatterns={ blockPatterns } | ||
shownPatterns={ shownBlockPatterns } | ||
onClickPattern={ onBlockPatternSelect } | ||
/> | ||
</div> | ||
); | ||
} |
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