-
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.
Add shared blocks to block completer
- Loading branch information
1 parent
6a4d45b
commit 11b2a9e
Showing
2 changed files
with
45 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* WordPress dependencies. | ||
*/ | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { select } from '@wordpress/data'; | ||
import { createBlock } from '@wordpress/blocks'; | ||
|
||
// Export for unit test | ||
export function getBlockOptions( query ) { | ||
const editor = select( 'core/editor' ); | ||
const isInitialQuery = ! query; | ||
|
||
// TODO: Fetch reusable blocks if necessary (they aren't fetched initially) | ||
return isInitialQuery ? | ||
// Before we have a query, offer frecent blocks as a sensible default. | ||
editor.getFrecentInserterItems() : | ||
editor.getInserterItems(); | ||
} | ||
|
||
// Export for unit test | ||
export function getBlockCompletion( inserterItem ) { | ||
const { name, initialAttributes } = inserterItem; | ||
return { | ||
action: 'replace', | ||
value: createBlock( name, initialAttributes ), | ||
}; | ||
} | ||
|
||
// Export for unit test | ||
export function addReusableBlocksCompletion( completers ) { | ||
const blocksCompleter = completers.find( c => 'blocks' === c.name ); | ||
if ( blocksCompleter ) { | ||
blocksCompleter.options = getBlockOptions; | ||
blocksCompleter.getOptionCompletion = getBlockCompletion; | ||
} | ||
|
||
return completers; | ||
} | ||
|
||
addFilter( | ||
'blocks.Autocomplete.completers', | ||
'core/edit-post/autocompleters/shared-blocks', | ||
addReusableBlocksCompletion | ||
); |
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
import './blocks'; | ||
import './more-menu'; | ||
import './validate-use-once'; | ||
import './autocompleters'; |