Skip to content

Commit

Permalink
Add shared blocks to block completer
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonpayton committed Apr 9, 2018
1 parent 6a4d45b commit 11b2a9e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
44 changes: 44 additions & 0 deletions edit-post/hooks/autocompleters/index.js
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
);
1 change: 1 addition & 0 deletions edit-post/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
import './blocks';
import './more-menu';
import './validate-use-once';
import './autocompleters';

0 comments on commit 11b2a9e

Please sign in to comment.