-
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.
Adds a modal version of the block inserter only in Distraction Free m…
…ode.
- Loading branch information
1 parent
583ac8f
commit eaccab4
Showing
5 changed files
with
178 additions
and
2 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
113 changes: 113 additions & 0 deletions
113
packages/block-editor/src/components/inserter/modal-inserter.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,113 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { SearchControl } from '@wordpress/components'; | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import InserterSearchResults from './search-results'; | ||
import useInsertionPoint from './hooks/use-insertion-point'; | ||
import usePatternsState from './hooks/use-patterns-state'; | ||
import useBlockTypesState from './hooks/use-block-types-state'; | ||
import { store as blockEditorStore } from '../../store'; | ||
|
||
const SEARCH_THRESHOLD = 6; | ||
const SHOWN_BLOCK_TYPES = 6; | ||
const SHOWN_BLOCK_PATTERNS = 2; | ||
const SHOWN_BLOCK_PATTERNS_WITH_PRIORITIZATION = 4; | ||
|
||
export default function ModalInserter( | ||
{ onSelect, rootClientId, clientId, isAppender, prioritizePatterns }, | ||
ref | ||
) { | ||
const [ filterValue, setFilterValue ] = useState( '' ); | ||
const [ destinationRootClientId, onInsertBlocks ] = useInsertionPoint( { | ||
onSelect, | ||
rootClientId, | ||
clientId, | ||
isAppender, | ||
} ); | ||
const [ blockTypes ] = useBlockTypesState( | ||
destinationRootClientId, | ||
onInsertBlocks | ||
); | ||
|
||
const [ patterns ] = usePatternsState( | ||
onInsertBlocks, | ||
destinationRootClientId | ||
); | ||
|
||
const { setInserterIsOpened } = useSelect( | ||
( select ) => { | ||
const { getSettings, getBlockIndex, getBlockCount } = | ||
select( blockEditorStore ); | ||
const settings = getSettings(); | ||
const index = getBlockIndex( clientId ); | ||
const blockCount = getBlockCount(); | ||
|
||
return { | ||
setInserterIsOpened: settings.__experimentalSetIsInserterOpened, | ||
insertionIndex: index === -1 ? blockCount : index, | ||
}; | ||
}, | ||
[ clientId ] | ||
); | ||
|
||
const showPatterns = | ||
patterns.length && ( !! filterValue || prioritizePatterns ); | ||
const showSearch = | ||
( showPatterns && patterns.length > SEARCH_THRESHOLD ) || | ||
blockTypes.length > SEARCH_THRESHOLD; | ||
|
||
let maxBlockPatterns = 0; | ||
if ( showPatterns ) { | ||
maxBlockPatterns = prioritizePatterns | ||
? SHOWN_BLOCK_PATTERNS_WITH_PRIORITIZATION | ||
: SHOWN_BLOCK_PATTERNS; | ||
} | ||
|
||
return ( | ||
<div | ||
className={ classnames( 'block-editor-inserter__quick-inserter', { | ||
'has-search': showSearch, | ||
'has-expand': setInserterIsOpened, | ||
} ) } | ||
> | ||
{ showSearch && ( | ||
<SearchControl | ||
ref={ ref } | ||
className="block-editor-inserter__search" | ||
value={ filterValue } | ||
onChange={ ( value ) => { | ||
setFilterValue( value ); | ||
} } | ||
label={ __( 'Search for blocks and patterns' ) } | ||
placeholder={ __( 'Search' ) } | ||
/> | ||
) } | ||
|
||
<div className="block-editor-inserter__quick-inserter-results"> | ||
<InserterSearchResults | ||
filterValue={ filterValue } | ||
onSelect={ onSelect } | ||
rootClientId={ rootClientId } | ||
clientId={ clientId } | ||
isAppender={ isAppender } | ||
maxBlockPatterns={ maxBlockPatterns } | ||
maxBlockTypes={ SHOWN_BLOCK_TYPES } | ||
isDraggable={ false } | ||
prioritizePatterns={ prioritizePatterns } | ||
/> | ||
</div> | ||
</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
24 changes: 24 additions & 0 deletions
24
packages/edit-post/src/components/layout/modal-inserter.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,24 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { Modal } from '@wordpress/components'; | ||
import { Inserter } from '@wordpress/block-editor'; | ||
import { useDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editPostStore } from '../../store'; | ||
|
||
export function ModalInserter() { | ||
const { setIsInserterOpened } = useDispatch( editPostStore ); | ||
const closeModal = () => { | ||
setIsInserterOpened( false ); | ||
}; | ||
return ( | ||
<Modal onRequestClose={ closeModal } title={ __( 'Insert block' ) }> | ||
<Inserter __experimentalIsModal /> | ||
</Modal> | ||
); | ||
} |