Skip to content

Commit

Permalink
Try a new inserterItem block API
Browse files Browse the repository at this point in the history
Show workflows in inserter

Add selector for getting inserter workflow items

Stub out workflow items in inserter

Avoid computing draggable blocks for non-draggable items

Spread items to flatten them rather than creating a nested array

Fix error thrown for non-draggable inserter block

Show workflow component when selecting workflow

Fix order of items returned from hook

Pass root client id to workflows

Remove unused property

Make header and footer the active workflows

Use proper area label

Insert the template part block

Use same classname as other modal

Show existing template parts in modal

Add non-area template part workflow

Avoid showing empty sections

Change inserter panel title

Remove existing template parts from modal

Iteration 2: Try an `insert` block API

Add `insert` property to inserter items selector result

Show `insert` component for inserter items that have one

Refactor callbacks

Use pattern name for created template part

Iteration 3: Launch modal from inserted block

Reorganise components and utils

Revert "Iteration 3: Launch modal from inserted block"

This reverts commit 3d04accc51fd04e169fd4567f76cb4a5b85bfa92.

Refactor template part selection modal to be more generic

More refactoring

Fix close button

Fix non-pluralized text

Fix naming nitpick

Refactor to use `inserterItem` API
  • Loading branch information
talldan committed Jul 28, 2022
1 parent 3f5d5a5 commit dda4fb5
Show file tree
Hide file tree
Showing 20 changed files with 535 additions and 334 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function chunk( array, size ) {

function BlockTypesList( {
items = [],
rootClientId,
onSelect,
onHover = () => {},
children,
Expand All @@ -35,6 +36,7 @@ function BlockTypesList( {
{ row.map( ( item, j ) => (
<InserterListItem
key={ item.id }
rootClientId={ rootClientId }
item={ item }
className={ getBlockMenuDefaultClassName(
item.id
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export { default as DefaultBlockAppender } from './default-block-appender';
export { default as __unstableEditorStyles } from './editor-styles';
export { default as Inserter } from './inserter';
export { default as __experimentalLibrary } from './inserter/library';
export { default as __experimentalInserterListItemWithModal } from './inserter-list-item/with-modal';
export { default as BlockEditorKeyboardShortcuts } from './keyboard-shortcuts';
export { MultiSelectScrollIntoView } from './selection-scroll-into-view';
export { default as NavigableToolbar } from './navigable-toolbar';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ const InserterDraggableBlocks = ( { isEnabled, blocks, icon, children } ) => {
__experimentalTransferDataType="wp-blocks"
transferData={ transferData }
__experimentalDragComponent={
<BlockDraggableChip count={ blocks.length } icon={ icon } />
!! isEnabled && (
<BlockDraggableChip
count={ blocks?.length }
icon={ icon }
/>
)
}
>
{ ( { onDraggableStart, onDraggableEnd } ) => {
Expand Down
134 changes: 134 additions & 0 deletions packages/block-editor/src/components/inserter-list-item/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { __experimentalTruncate as Truncate } from '@wordpress/components';
import { useMemo, useRef, memo } from '@wordpress/element';
import {
createBlock,
createBlocksFromInnerBlocksTemplate,
} from '@wordpress/blocks';
import { ENTER, isAppleOS } from '@wordpress/keycodes';

/**
* Internal dependencies
*/
import BlockIcon from '../block-icon';
import { InserterListboxItem } from '../inserter-listbox';
import InserterDraggableBlocks from '../inserter-draggable-blocks';

function InserterListItem( {
className,
isFirst,
item,
onSelect,
onHover,
isDraggable,
...props
} ) {
const isDragging = useRef( false );
const itemIconStyle = item.icon
? {
backgroundColor: item.icon.background,
color: item.icon.foreground,
}
: {};
const blocks = useMemo( () => {
return [
createBlock(
item.name,
item.initialAttributes,
createBlocksFromInnerBlocksTemplate( item.innerBlocks )
),
];
}, [ item.name, item.initialAttributes, item.initialAttributes ] );

return (
<InserterDraggableBlocks
isEnabled={ isDraggable && ! item.disabled }
blocks={ blocks }
icon={ item.icon }
>
{ ( { draggable, onDragStart, onDragEnd } ) => (
<div
className="block-editor-block-types-list__list-item"
draggable={ draggable }
onDragStart={ ( event ) => {
isDragging.current = true;
if ( onDragStart ) {
onHover( null );
onDragStart( event );
}
} }
onDragEnd={ ( event ) => {
isDragging.current = false;
if ( onDragEnd ) {
onDragEnd( event );
}
} }
>
<InserterListboxItem
isFirst={ isFirst }
className={ classnames(
'block-editor-block-types-list__item',
className
) }
disabled={ item.isDisabled }
onClick={ ( event ) => {
event.preventDefault();
onSelect(
item,
isAppleOS() ? event.metaKey : event.ctrlKey
);
onHover( null );
} }
onKeyDown={ ( event ) => {
const { keyCode } = event;
if ( keyCode === ENTER ) {
event.preventDefault();
onSelect(
item,
isAppleOS() ? event.metaKey : event.ctrlKey
);
onHover( null );
}
} }
onFocus={ () => {
if ( isDragging.current ) {
return;
}
onHover( item );
} }
onMouseEnter={ () => {
if ( isDragging.current ) {
return;
}
onHover( item );
} }
onMouseLeave={ () => onHover( null ) }
onBlur={ () => onHover( null ) }
{ ...props }
>
<span
className="block-editor-block-types-list__item-icon"
style={ itemIconStyle }
>
<BlockIcon icon={ item.icon } showColors />
</span>
<span className="block-editor-block-types-list__item-title">
<Truncate numberOfLines={ 3 }>
{ item.title }
</Truncate>
</span>
</InserterListboxItem>
</div>
) }
</InserterDraggableBlocks>
);
}

export default memo( InserterListItem );
136 changes: 8 additions & 128 deletions packages/block-editor/src/components/inserter-list-item/index.js
Original file line number Diff line number Diff line change
@@ -1,134 +1,14 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { useMemo, useRef, memo } from '@wordpress/element';
import {
createBlock,
createBlocksFromInnerBlocksTemplate,
} from '@wordpress/blocks';
import { __experimentalTruncate as Truncate } from '@wordpress/components';
import { ENTER, isAppleOS } from '@wordpress/keycodes';

/**
* Internal dependencies
*/
import BlockIcon from '../block-icon';
import { InserterListboxItem } from '../inserter-listbox';
import InserterDraggableBlocks from '../inserter-draggable-blocks';
import InserterListItemBase from './base';

function InserterListItem( {
className,
isFirst,
item,
onSelect,
onHover,
isDraggable,
...props
} ) {
const isDragging = useRef( false );
const itemIconStyle = item.icon
? {
backgroundColor: item.icon.background,
color: item.icon.foreground,
}
: {};
const blocks = useMemo( () => {
return [
createBlock(
item.name,
item.initialAttributes,
createBlocksFromInnerBlocksTemplate( item.innerBlocks )
),
];
}, [ item.name, item.initialAttributes, item.initialAttributes ] );
export default function InserterListItem( props ) {
const { inserterItem: InserterItem } = props.item;

return (
<InserterDraggableBlocks
isEnabled={ isDraggable && ! item.disabled }
blocks={ blocks }
icon={ item.icon }
>
{ ( { draggable, onDragStart, onDragEnd } ) => (
<div
className="block-editor-block-types-list__list-item"
draggable={ draggable }
onDragStart={ ( event ) => {
isDragging.current = true;
if ( onDragStart ) {
onHover( null );
onDragStart( event );
}
} }
onDragEnd={ ( event ) => {
isDragging.current = false;
if ( onDragEnd ) {
onDragEnd( event );
}
} }
>
<InserterListboxItem
isFirst={ isFirst }
className={ classnames(
'block-editor-block-types-list__item',
className
) }
disabled={ item.isDisabled }
onClick={ ( event ) => {
event.preventDefault();
onSelect(
item,
isAppleOS() ? event.metaKey : event.ctrlKey
);
onHover( null );
} }
onKeyDown={ ( event ) => {
const { keyCode } = event;
if ( keyCode === ENTER ) {
event.preventDefault();
onSelect(
item,
isAppleOS() ? event.metaKey : event.ctrlKey
);
onHover( null );
}
} }
onFocus={ () => {
if ( isDragging.current ) {
return;
}
onHover( item );
} }
onMouseEnter={ () => {
if ( isDragging.current ) {
return;
}
onHover( item );
} }
onMouseLeave={ () => onHover( null ) }
onBlur={ () => onHover( null ) }
{ ...props }
>
<span
className="block-editor-block-types-list__item-icon"
style={ itemIconStyle }
>
<BlockIcon icon={ item.icon } showColors />
</span>
<span className="block-editor-block-types-list__item-title">
<Truncate numberOfLines={ 3 }>
{ item.title }
</Truncate>
</span>
</InserterListboxItem>
</div>
) }
</InserterDraggableBlocks>
);
}
if ( InserterItem ) {
return <InserterItem { ...props } />;
}

export default memo( InserterListItem );
return <InserterListItemBase { ...props } />;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* WordPress dependencies
*/
import { Modal } from '@wordpress/components';
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import InserterListItem from './base';

export default function InserterListItemWithModal( {
modalProps,
children,
...props
} ) {
const [ isModalVisible, setIsModalVisible ] = useState( false );

return (
<>
<InserterListItem
{ ...props }
onSelect={ () => setIsModalVisible( true ) }
aria-haspopup="dialog"
aria-expanded={ isModalVisible }
/>
{ isModalVisible && (
<Modal
{ ...modalProps }
onRequestClose={ () => {
setIsModalVisible( false );
} }
>
{ children }
</Modal>
) }
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export function BlockTypesTab( {
{ showMostUsedBlocks && !! suggestedItems.length && (
<InserterPanel title={ _x( 'Most used', 'blocks' ) }>
<BlockTypesList
rootClientId={ rootClientId }
items={ suggestedItems }
onSelect={ onSelectItem }
onHover={ onHover }
Expand All @@ -124,6 +125,7 @@ export function BlockTypesTab( {
icon={ category.icon }
>
<BlockTypesList
rootClientId={ rootClientId }
items={ categoryItems }
onSelect={ onSelectItem }
onHover={ onHover }
Expand All @@ -139,6 +141,7 @@ export function BlockTypesTab( {
title={ __( 'Uncategorized' ) }
>
<BlockTypesList
rootClientId={ rootClientId }
items={ uncategorizedItems }
onSelect={ onSelectItem }
onHover={ onHover }
Expand All @@ -162,6 +165,7 @@ export function BlockTypesTab( {
icon={ collection.icon }
>
<BlockTypesList
rootClientId={ rootClientId }
items={ collectionItems }
onSelect={ onSelectItem }
onHover={ onHover }
Expand Down
Loading

0 comments on commit dda4fb5

Please sign in to comment.