Skip to content

Commit

Permalink
Experimental: PoC for placeholder insert, we should drop this commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gwwar committed Mar 16, 2021
1 parent aa60000 commit 4e5f6e4
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function BlockListAppender( {
renderAppender: CustomAppender,
className,
selectedBlockClientId,
replaceClientId,
tagName: TagName = 'div',
} ) {
if ( isLocked || CustomAppender === false ) {
Expand All @@ -42,7 +43,13 @@ function BlockListAppender( {
let appender;
if ( CustomAppender ) {
// Prefer custom render prop if provided.
appender = <CustomAppender />;
//TODO: why did we not pass any props, temp pass through for PoC
appender = (
<CustomAppender
replaceClientId={ replaceClientId }
replaceRootId={ rootClientId }
/>
);
} else {
const isDocumentAppender = ! rootClientId;
const isParentSelected = selectedBlockClientId === rootClientId;
Expand Down
13 changes: 12 additions & 1 deletion packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import BlockCrashBoundary from './block-crash-boundary';
import BlockHtml from './block-html';
import { useBlockProps } from './use-block-props';
import { store as blockEditorStore } from '../../store';
import BlockListAppender from '../block-list-appender';
import PlaceholderBlockAppender from '../placeholder-block-appender';

export const BlockListBlockContext = createContext();

Expand Down Expand Up @@ -154,7 +156,16 @@ function BlockListBlock( {

let block;

if ( ! isValid ) {
//TODO: roll this back, this is a quick PoC
if ( name === 'core/placeholder' ) {
block = (
<BlockListAppender
className={ 'block-editor-placeholder-appender' }
replaceClientId={ clientId }
renderAppender={ PlaceholderBlockAppender }
/>
);
} else if ( ! isValid ) {
block = (
<Block className="has-warning">
<BlockInvalidWarning clientId={ clientId } />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function useInsertionPoint( {
isAppender,
onSelect,
shouldFocusBlock = true,
replaceClientId,
} ) {
const {
destinationRootClientId,
Expand Down Expand Up @@ -109,7 +110,15 @@ function useInsertionPoint( {
( blocks, meta, shouldForceFocusBlock = false ) => {
const selectedBlock = getSelectedBlock();

if (
if ( replaceClientId ) {
replaceBlocks(
replaceClientId,
blocks,
null,
shouldFocusBlock || shouldForceFocusBlock ? 0 : null,
meta
);
} else if (
! isAppender &&
selectedBlock &&
isUnmodifiedDefaultBlock( selectedBlock )
Expand Down Expand Up @@ -155,6 +164,7 @@ function useInsertionPoint( {
destinationIndex,
onSelect,
shouldFocusBlock,
replaceClientId,
]
);

Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class Inserter extends Component {
// This prop is experimental to give some time for the quick inserter to mature
// Feel free to make them stable after a few releases.
__experimentalIsQuick: isQuick,
replaceClientId,
} = this.props;

if ( isQuick ) {
Expand All @@ -147,6 +148,7 @@ class Inserter extends Component {
rootClientId={ rootClientId }
clientId={ clientId }
isAppender={ isAppender }
replaceClientId={ replaceClientId }
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ export default function QuickInserter( {
rootClientId,
clientId,
isAppender,
replaceClientId,
} ) {
const [ filterValue, setFilterValue ] = useState( '' );
const [ destinationRootClientId, onInsertBlocks ] = useInsertionPoint( {
onSelect,
rootClientId,
clientId,
isAppender,
replaceClientId,
} );

const [ blockTypes ] = useBlockTypesState(
destinationRootClientId,
onInsertBlocks
Expand Down Expand Up @@ -107,6 +110,7 @@ export default function QuickInserter( {
maxBlockPatterns={ showPatterns ? SHOWN_BLOCK_PATTERNS : 0 }
maxBlockTypes={ SHOWN_BLOCK_TYPES }
isDraggable={ false }
replaceClientId={ replaceClientId }
/>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function InserterSearchResults( {
showBlockDirectory = false,
isDraggable = true,
shouldFocusBlock = true,
replaceClientId,
} ) {
const debouncedSpeak = useDebounce( speak, 500 );

Expand All @@ -47,6 +48,7 @@ function InserterSearchResults( {
clientId,
isAppender,
shouldFocusBlock,
replaceClientId,
} );
const [
blockTypes,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { Button, Tooltip, VisuallyHidden } from '@wordpress/components';
import { forwardRef } from '@wordpress/element';
import { _x, sprintf } from '@wordpress/i18n';
import { Icon, plus } from '@wordpress/icons';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import Inserter from '../inserter';
import { store as blockEditorStore } from '../../store';

function PlaceholderBlockAppender(
{ replaceClientId, className, onFocus, tabIndex },
ref
) {
const rootClientId = useSelect(
( select ) => {
const { getBlockRootClientId } = select( blockEditorStore );
return getBlockRootClientId( replaceClientId );
},
[ replaceClientId ]
);
return (
<Inserter
position="bottom center"
rootClientId={ rootClientId }
__experimentalIsQuick
replaceClientId={ replaceClientId }
renderToggle={ ( {
onToggle,
disabled,
isOpen,
blockTitle,
hasSingleBlockType,
} ) => {
let label;
if ( hasSingleBlockType ) {
label = sprintf(
// translators: %s: the name of the block when there is only one
_x( 'Add %s', 'directly add the only allowed block' ),
blockTitle
);
} else {
label = _x(
'Add block',
'Generic label for block inserter button'
);
}
const isToggleButton = ! hasSingleBlockType;

//TODO: allow a pass through
let inserterButton = (
<Button
ref={ ref }
onFocus={ onFocus }
tabIndex={ tabIndex }
className={ classnames(
className,
'block-editor-placeholder-block-appender'
) }
onClick={ onToggle }
aria-haspopup={ isToggleButton ? 'true' : undefined }
aria-expanded={ isToggleButton ? isOpen : undefined }
disabled={ disabled }
label={ label }
>
{ ! hasSingleBlockType && (
<VisuallyHidden as="span">{ label }</VisuallyHidden>
) }
<Icon icon={ plus } />
</Button>
);

if ( isToggleButton || hasSingleBlockType ) {
inserterButton = (
<Tooltip text={ label }>{ inserterButton }</Tooltip>
);
}
return inserterButton;
} }
isAppender
/>
);
}

export default forwardRef( PlaceholderBlockAppender );
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//throwaway styling for basic PoC
.wp-block.block-list-appender.block-editor-placeholder-appender {
height: 36px;
width: 36px;
border: $border-width solid $gray-900;
margin-left: 0;
margin-right: 8px;

.block-editor-inserter {
margin-top: 6px;
margin-left: auto;
margin-right: auto;
padding: 6px;
}
}

1 change: 1 addition & 0 deletions packages/block-editor/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
@import "./components/media-replace-flow/style.scss";
@import "./components/media-placeholder/style.scss";
@import "./components/multi-selection-inspector/style.scss";
@import "./components/placeholder-block-appender/style.scss";
@import "./components/plain-text/style.scss";
@import "./components/responsive-block-control/style.scss";
@import "./components/rich-text/style.scss";
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import * as missing from './missing';
import * as more from './more';
import * as nextpage from './nextpage';
import * as pageList from './page-list';
import * as placeholder from './placeholder';
import * as preformatted from './preformatted';
import * as pullquote from './pullquote';
import * as reusableBlock from './block';
Expand Down Expand Up @@ -153,6 +154,7 @@ export const __experimentalGetCoreBlocks = () => [
more,
nextpage,
pageList,
placeholder,
preformatted,
pullquote,
rss,
Expand Down
11 changes: 11 additions & 0 deletions packages/block-library/src/placeholder/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"apiVersion": 2,
"name": "core/placeholder",
"category": "design",
"attributes": {},
"supports": {
"html": false
},
"editorStyle": "wp-block-placeholder-editor",
"style": "wp-block-placeholder"
}
5 changes: 5 additions & 0 deletions packages/block-library/src/placeholder/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const Placeholder = () => {
return '';
};

export default Placeholder;
23 changes: 23 additions & 0 deletions packages/block-library/src/placeholder/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* WordPress dependencies
*/
import { __, _x } from '@wordpress/i18n';
import { create as icon } from '@wordpress/icons';

/**
* Internal dependencies
*/
import metadata from './block.json';
import edit from './edit';

const { name } = metadata;
export { metadata, name };

export const settings = {
title: _x( 'Block Placeholder', 'block placeholder' ),
description: __(
'A block placeholder to quickly add blocks. Click on it to replace it with a specific block.'
),
icon,
edit,
};
3 changes: 2 additions & 1 deletion packages/block-library/src/social-links/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
import { __ } from '@wordpress/i18n';
import { check } from '@wordpress/icons';

const ALLOWED_BLOCKS = [ 'core/social-link' ];
const ALLOWED_BLOCKS = [ 'core/social-link', 'core/placeholder' ];

const sizeOptions = [
{ name: __( 'Small' ), value: 'has-small-icon-size' },
Expand Down Expand Up @@ -94,6 +94,7 @@ export function SocialLinksEdit( props ) {
const blockProps = useBlockProps( { className } );
const innerBlocksProps = useInnerBlocksProps( blockProps, {
allowedBlocks: ALLOWED_BLOCKS,
__experimentalDefaultBlock: [ 'core/placeholder' ],
orientation: 'horizontal',
placeholder: SocialPlaceholder,
templateLock: false,
Expand Down

0 comments on commit 4e5f6e4

Please sign in to comment.