Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude focused block from block completer options #7273

Merged
merged 2 commits into from Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 33 additions & 6 deletions editor/components/autocompleters/block.js
Expand Up @@ -10,12 +10,34 @@ import { createBlock } from '@wordpress/blocks';
import './style.scss';
import BlockIcon from '../block-icon';

function defaultGetBlockInsertionPoint() {
return select( 'core/editor' ).getBlockInsertionPoint();
/**
* Get the UID of the parent where a newly inserted block would be placed.
*
* @return {string} The UID of the parent where a newly inserted block would be placed.
*/
function defaultGetBlockInsertionParent() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we name it defaultGetBlockInsertionParentUID to make it easier to follow?

return select( 'core/editor' ).getBlockInsertionPoint().rootUID;
}

/**
* Get the inserter items for the specified parent block.
*
* @param {string} parentUID The UID of the block for which to retrieve inserter items.
*
* @return {Array<Editor.InserterItem>} The inserter items for the specified parent.
*/
function defaultGetInserterItems( parentUID ) {
return select( 'core/editor' ).getInserterItems( parentUID );
}

function defaultGetInserterItems( { rootUID } ) {
return select( 'core/editor' ).getInserterItems( rootUID );
/**
* Get the name of the currently selected block.
*
* @return {string?} The name of the currently selected block or `null` if no block is selected.
*/
function defaultGetSelectedBlockName() {
const selectedBlock = select( 'core/editor' ).getSelectedBlock();
return selectedBlock ? selectedBlock.name : null;
}

/**
Expand All @@ -25,15 +47,20 @@ function defaultGetInserterItems( { rootUID } ) {
*/
export function createBlockCompleter( {
// Allow store-based selectors to be overridden for unit test.
getBlockInsertionPoint = defaultGetBlockInsertionPoint,
getBlockInsertionParent = defaultGetBlockInsertionParent,
getInserterItems = defaultGetInserterItems,
getSelectedBlockName = defaultGetSelectedBlockName,
} = {} ) {
return {
name: 'blocks',
className: 'editor-autocompleters__block',
triggerPrefix: '/',
options() {
return getInserterItems( getBlockInsertionPoint() );
const selectedBlockName = getSelectedBlockName();
return getInserterItems( getBlockInsertionParent() ).filter(
// Avoid offering to replace the current block with a block of the same type.
( inserterItem ) => selectedBlockName !== inserterItem.name
);
},
getOptionKeywords( inserterItem ) {
const { title, keywords = [] } = inserterItem;
Expand Down
23 changes: 19 additions & 4 deletions editor/components/autocompleters/test/block.js
Expand Up @@ -11,18 +11,33 @@ import blockCompleter, { createBlockCompleter } from '../block';
describe( 'block', () => {
it( 'should retrieve block options for current insertion point', () => {
const expectedOptions = [ {}, {}, {} ];
const mockGetBlockInsertionPoint = jest.fn( () => 'expected-insertion-point' );
const mockGetBlockInsertionParent = jest.fn( () => 'expected-insertion-point' );
const mockGetInserterItems = jest.fn( () => expectedOptions );

const completer = createBlockCompleter( {
getBlockInsertionPoint: mockGetBlockInsertionPoint,
getBlockInsertionParent: mockGetBlockInsertionParent,
getInserterItems: mockGetInserterItems,
getSelectedBlockName: () => 'non-existent-block-name',
} );

const actualOptions = completer.options();
expect( mockGetBlockInsertionPoint ).toHaveBeenCalled();
expect( mockGetBlockInsertionParent ).toHaveBeenCalled();
expect( mockGetInserterItems ).toHaveBeenCalledWith( 'expected-insertion-point' );
expect( actualOptions ).toBe( expectedOptions );
expect( actualOptions ).toEqual( expectedOptions );
} );

it( 'should exclude the currently selected block from the options', () => {
const option1 = { name: 'block-1' };
const option2CurrentlySelected = { name: 'block-2-currently-selected' };
const option3 = { name: 'block-3' };

const completer = createBlockCompleter( {
getBlockInsertionParent: () => 'ignored',
getInserterItems: () => [ option1, option2CurrentlySelected, option3 ],
getSelectedBlockName: () => 'block-2-currently-selected',
} );

expect( completer.options() ).toEqual( [ option1, option3 ] );
} );

it( 'should derive option keywords from block keywords and block title', () => {
Expand Down