Skip to content

Commit

Permalink
Some test fixes, more proper async code
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Jul 21, 2023
1 parent 9eef4da commit 1dde417
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 61 deletions.
4 changes: 3 additions & 1 deletion packages/block-editor/src/autocompleters/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useSelect } from '@wordpress/data';
import {
createBlock,
createBlocksFromInnerBlocksTemplate,
loadBlockType,
} from '@wordpress/blocks';
import { useMemo } from '@wordpress/element';

Expand Down Expand Up @@ -115,8 +116,9 @@ function createBlockCompleter() {
allowContext( before, after ) {
return ! ( /\S/.test( before ) || /\S/.test( after ) );
},
getOptionCompletion( inserterItem ) {
async getOptionCompletion( inserterItem ) {
const { name, initialAttributes, innerBlocks } = inserterItem;
await loadBlockType( name );
return {
action: 'replace',
value: createBlock(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ function ConvertToGroupButton( {
onClose = () => {},
} ) {
const { replaceBlocks } = useDispatch( blockEditorStore );
const onConvertToGroup = () => {
const onConvertToGroup = async () => {
// Activate the `transform` on the Grouping Block which does the conversion.
const newBlocks = switchToBlockType(
const newBlocks = await switchToBlockType(
blocksSelection,
groupingBlockName
);
Expand Down
13 changes: 8 additions & 5 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ export const synchronizeTemplate =
*/
export const __unstableDeleteSelection =
( isForward ) =>
( { registry, select, dispatch } ) => {
async ( { registry, select, dispatch } ) => {
const selectionAnchor = select.getSelectionStart();
const selectionFocus = select.getSelectionEnd();

Expand Down Expand Up @@ -798,7 +798,10 @@ export const __unstableDeleteSelection =
const blocksWithTheSameType =
blockA.name === blockB.name
? [ followingBlock ]
: switchToBlockType( followingBlock, targetBlockType.name );
: await switchToBlockType(
followingBlock,
targetBlockType.name
);

// If the block types can not match, do nothing
if ( ! blocksWithTheSameType || ! blocksWithTheSameType.length ) {
Expand Down Expand Up @@ -1003,7 +1006,7 @@ export const __unstableExpandSelection =
*/
export const mergeBlocks =
( firstBlockClientId, secondBlockClientId ) =>
( { registry, select, dispatch } ) => {
async ( { registry, select, dispatch } ) => {
const blocks = [ firstBlockClientId, secondBlockClientId ];
dispatch( { type: 'MERGE_BLOCKS', blocks } );

Expand All @@ -1018,7 +1021,7 @@ export const mergeBlocks =
if ( blockAType && ! blockAType.merge ) {
// If there's no merge function defined, attempt merging inner
// blocks.
const blocksWithTheSameType = switchToBlockType(
const blocksWithTheSameType = await switchToBlockType(
blockB,
blockAType.name
);
Expand Down Expand Up @@ -1102,7 +1105,7 @@ export const mergeBlocks =
const blocksWithTheSameType =
blockA.name === blockB.name
? [ cloneB ]
: switchToBlockType( cloneB, blockA.name );
: await switchToBlockType( cloneB, blockA.name );

// If the block types can not match, do nothing.
if ( ! blocksWithTheSameType || ! blocksWithTheSameType.length ) {
Expand Down
40 changes: 15 additions & 25 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2145,31 +2145,21 @@ export const getBlockTransformItems = createSelector(
*
* @return {boolean} Items that appear in inserter.
*/
export const hasInserterItems = createRegistrySelector(
( select ) =>
( state, rootClientId = null ) => {
const { getBlockNames, getBootstrappedBlockType } =
select( blocksStore );
const hasBlockType = getBlockNames().some( ( blockName ) => {
return canIncludeBlockTypeInInserter(
state,
getBootstrappedBlockType( blockName ),
rootClientId
);
} );
if ( hasBlockType ) {
return true;
}
const hasReusableBlock =
canInsertBlockTypeUnmemoized(
state,
'core/block',
rootClientId
) && getReusableBlocks( state ).length > 0;

return hasReusableBlock;
}
);
export const hasInserterItems = ( state, rootClientId = null ) => {
const hasBlockType = getBootstrappedBlockTypes().some( ( blockType ) =>
canIncludeBlockTypeInInserter( state, blockType, rootClientId )
);

if ( hasBlockType ) {
return true;
}

const hasReusableBlock =
canInsertBlockTypeUnmemoized( state, 'core/block', rootClientId ) &&
getReusableBlocks( state ).length > 0;

return hasReusableBlock;
};

/**
* Returns the list of allowed inserter blocks for inner blocks children.
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,10 @@ export default function Image( {
const canEditImage = id && naturalWidth && naturalHeight && imageEditing;
const allowCrop = ! multiImageSelection && canEditImage && ! isEditingImage;

function switchToCover() {
async function switchToCover() {
replaceBlocks(
clientId,
switchToBlockType( getBlock( clientId ), 'core/cover' )
await switchToBlockType( getBlock( clientId ), 'core/cover' )
);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/list-item/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ export default function ListItemEdit( {
onMerge={ onMerge }
onReplace={
onReplace
? ( blocks, ...args ) => {
? async ( blocks, ...args ) => {
onReplace(
convertToListItems( blocks ),
await convertToListItems( blocks ),
...args
);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/block-library/src/list-item/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ export default function ListItemEdit( {
[ clientId, onSplit ]
);
const onReplaceList = useCallback(
( blocks, ...args ) => {
async ( blocks, ...args ) => {
if ( ! preventDefault.current ) {
onReplace( convertToListItems( blocks ), ...args );
onReplace( await convertToListItems( blocks ), ...args );
}
},
[ clientId, onReplace, convertToListItems ]
[ onReplace ]
);
const onLayout = useCallback( ( { nativeEvent } ) => {
setContentWidth( ( prevState ) => {
Expand Down
18 changes: 11 additions & 7 deletions packages/block-library/src/list-item/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,27 @@ export function createListItem( listItemAttributes, listAttributes, children ) {
);
}

function convertBlockToList( block ) {
const list = switchToBlockType( block, listName );
if ( list ) return list;
const paragraph = switchToBlockType( block, paragraphName );
if ( paragraph ) return switchToBlockType( paragraph, listName );
async function convertBlockToList( block ) {
const list = await switchToBlockType( block, listName );
if ( list ) {
return list;
}
const paragraph = await switchToBlockType( block, paragraphName );
if ( paragraph ) {
return await switchToBlockType( paragraph, listName );
}
return null;
}

export function convertToListItems( blocks ) {
export async function convertToListItems( blocks ) {
const listItems = [];

for ( let block of blocks ) {
if ( block.name === listItemName ) {
listItems.push( block );
} else if ( block.name === listName ) {
listItems.push( ...block.innerBlocks );
} else if ( ( block = convertBlockToList( block ) ) ) {
} else if ( ( block = await convertBlockToList( block ) ) ) {
for ( const { innerBlocks } of block ) {
listItems.push( ...innerBlocks );
}
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/navigation-link/link-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ function LinkControlTransforms( { clientId } ) {
return (
<Button
key={ `transform-${ index }` }
onClick={ () =>
onClick={ async () =>
replaceBlock(
clientId,
switchToBlockType(
await switchToBlockType(
getBlock( clientId ),
item.name
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async function switchLegacyWidgetType( block ) {
return undefined;
}

return switchToBlockType( block, transforms[ 0 ].name );
return await switchToBlockType( block, transforms[ 0 ].name );
}

function transformInnerBlocks( innerBlocks = [] ) {
Expand Down
12 changes: 7 additions & 5 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,13 @@ export function registerBlockType( blockNameOrMetadata, settings ) {
}
} catch {}

if ( isObject( blockNameOrMetadata ) ) {
unstable__bootstrapServerSideBlockDefinitions( {
[ name ]: getBlockSettingsFromMetadata( blockNameOrMetadata ),
} );
}
const metadata = isObject( blockNameOrMetadata )
? getBlockSettingsFromMetadata( blockNameOrMetadata )
: getBlockSettingsFromMetadata( settings );

unstable__bootstrapServerSideBlockDefinitions( {
[ name ]: metadata,
} );

const blockType = {
name,
Expand Down
7 changes: 5 additions & 2 deletions packages/components/src/autocomplete/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,18 @@ export function useAutocomplete( {
onChange( insert( record, toInsert, start, end ) );
}

function select( option: KeyedOption ) {
async function select( option: KeyedOption ) {
const { getOptionCompletion } = autocompleter || {};

if ( option.isDisabled ) {
return;
}

if ( getOptionCompletion ) {
const completion = getOptionCompletion( option.value, filterValue );
const completion = await getOptionCompletion(
option.value,
filterValue
);

const isCompletionObject = (
obj: OptionCompletion
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/entity-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export function useEntityBlockEditor( kind, name, { id: _id } = {} ) {
return () => {
mounted = false;
};
}, [ blocks, editedBlocks, content ] );
}, [ editedBlocks, content ] );

const updateFootnotes = useCallback(
( _blocks ) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { Editor } from './index';
*/
export async function transformBlockTo( this: Editor, name: string ) {
await this.page.evaluate(
( [ blockName ] ) => {
async ( [ blockName ] ) => {
const clientIds = window.wp.data
.select( 'core/block-editor' )
.getSelectedBlockClientIds();
Expand All @@ -22,7 +22,10 @@ export async function transformBlockTo( this: Editor, name: string ) {
.dispatch( 'core/block-editor' )
.replaceBlocks(
clientIds,
window.wp.blocks.switchToBlockType( blocks, blockName )
await window.wp.blocks.switchToBlockType(
blocks,
blockName
)
);
},
[ name ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe( 'Block Grouping', () => {
} );

describe( 'Group creation', () => {
it.only( 'creates a group from multiple blocks of the same type via block transforms', async () => {
it( 'creates a group from multiple blocks of the same type via block transforms', async () => {
// Creating test blocks.
await insertBlocksOfSameType();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ describe( 'Inserting blocks', () => {

it( 'should insert block with the slash inserter when using multiple words', async () => {
await page.keyboard.press( 'Enter' );
await canvas().waitForSelector(
'[data-type="core/paragraph"][data-empty="true"'
);
await page.keyboard.type( '/tag cloud' );
await page.waitForXPath(
`//*[contains(@class, "components-autocomplete__result") and contains(@class, "is-selected") and contains(text(), 'Tag Cloud')]`
Expand Down

0 comments on commit 1dde417

Please sign in to comment.