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

Show inserter on slash: /+SEARCH #2284

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions blocks/editable/patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default function( editor ) {
const {
paste: pastePatterns,
enter: enterPatterns,
character: characterPatterns,
undefined: spacePatterns,
} = groupBy( getBlockTypes().reduce( ( acc, blockType ) => {
const transformsFrom = get( blockType, 'transforms.from', [] );
Expand Down Expand Up @@ -72,6 +73,7 @@ export default function( editor ) {
setTimeout( () => searchFirstText( spacePatterns ) );
} else if ( keyCode > 47 && ! ( keyCode >= 91 && keyCode <= 93 ) ) {
setTimeout( inline );
setTimeout( character );
}
}, true );

Expand Down Expand Up @@ -219,6 +221,37 @@ export default function( editor ) {
onReplace( [ block ] );
}

function character() {
if ( ! onReplace ) {
return;
}

// Merge text nodes.
editor.getBody().normalize();
Copy link
Member

Choose a reason for hiding this comment

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

How expensive an operation is this for something which will occur on every single keypress? Edit: I guess it's every keypress for a block which implements onReplace (inserter block).


const content = getContent();

if ( content.length !== 1 ) {
return;
}

const firstText = content[ 0 ];

if ( firstText.length !== 1 ) {
return;
}

const pattern = find( characterPatterns, { character: firstText } );

if ( ! pattern ) {
return;
}

const block = pattern.transform();

onReplace( [ block ] );
}

function enter() {
if ( ! onReplace ) {
return;
Expand Down
1 change: 1 addition & 0 deletions blocks/library/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ import './categories';
import './cover-image';
import './cover-text';
import './verse';
import './inserter';
1 change: 1 addition & 0 deletions blocks/library/inserter/block.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

73 changes: 73 additions & 0 deletions blocks/library/inserter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { keycodes } from '@wordpress/utils';

/**
* Internal dependencies
*/
import './block.scss';
import { registerBlockType, createBlock } from '../../api';
import InserterMenu from '../../../editor/inserter/menu';

const { BACKSPACE, ESCAPE } = keycodes;

registerBlockType( 'core/inserter', {
Copy link
Member

Choose a reason for hiding this comment

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

I've not tested this, but what's the expected behavior with this being implemented as a block? A user has to insert the inserter block, then they have the option to / to trigger the menu? I'd not really expected this to be a block, but rather something built-in to either Editable or the default paragraph block.

title: __( 'Inserter' ),

transforms: {
from: [
{
type: 'pattern',
trigger: 'character',
character: '/',
transform: () => createBlock( 'core/inserter' ),
},
],
},

edit( { className, onReplace } ) {
const onSelect = ( name ) => {
if ( ! name ) {
return;
}

onReplace( [
createBlock( name ),
] );
};

const onKeyDown = ( event ) => {
const { keyCode, target } = event;

if ( ( keyCode === BACKSPACE && target.value === '' ) || keyCode === ESCAPE ) {
event.preventDefault();
onReplace( [
createBlock( 'core/paragraph' ),
] );
}
};

const style = {
margin: 0,
border: '1px solid #e2e4e7',
};

/* eslint-disable jsx-a11y/no-static-element-interactions */
return (
<div
className={ `editor-inserter ${ className }` }
style={ style }
onKeyDown={ onKeyDown }
>
<InserterMenu onSelect={ onSelect } />
</div>
);
/* eslint-enable jsx-a11y/no-static-element-interactions */
},

save() {
return null;
},
} );
1 change: 1 addition & 0 deletions blocks/test/fixtures/core__inserter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:core/inserter /-->
8 changes: 8 additions & 0 deletions blocks/test/fixtures/core__inserter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"uid": "_uid_0",
"name": "core/inserter",
"isValid": true,
"attributes": {}
}
]
11 changes: 11 additions & 0 deletions blocks/test/fixtures/core__inserter.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"blockName": "core/inserter",
"attrs": null,
"rawContent": ""
},
{
"attrs": {},
"rawContent": "\n"
}
]
1 change: 1 addition & 0 deletions blocks/test/fixtures/core__inserter.serialized.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:core/inserter /-->
20 changes: 14 additions & 6 deletions editor/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { __, _n, sprintf } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { Popover, withFocusReturn, withInstanceId, withSpokenMessages } from '@wordpress/components';
import { keycodes } from '@wordpress/utils';
import { getCategories, getBlockTypes, BlockIcon } from '@wordpress/blocks';
import { getCategories, getBlockTypes, BlockIcon } from '../../blocks';
Copy link
Member

Choose a reason for hiding this comment

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

Why this change?


/**
* Internal dependencies
Expand All @@ -31,6 +31,14 @@ export const searchBlocks = ( blocks, searchTerm ) => {
);
};

const Wrapper = ( { position, children, ...props } ) => {
if ( ! position ) {
return <div { ...props }>{ children }</div>;
}

return <Popover position={ position } { ...props }>{ children }</Popover>;
};

export class InserterMenu extends Component {
constructor() {
super( ...arguments );
Expand Down Expand Up @@ -291,8 +299,8 @@ export class InserterMenu extends Component {
onClick={ this.selectBlock( block.name ) }
ref={ this.bindReferenceNode( block.name ) }
tabIndex="-1"
onMouseEnter={ ! disabled && this.props.showInsertionPoint }
onMouseLeave={ ! disabled && this.props.hideInsertionPoint }
onMouseEnter={ !! this.props.position && ! disabled && this.props.showInsertionPoint }
onMouseLeave={ !! this.props.position && ! disabled && this.props.hideInsertionPoint }
disabled={ disabled }
>
<BlockIcon icon={ block.icon } />
Expand All @@ -306,13 +314,13 @@ export class InserterMenu extends Component {
}

render() {
const { position, instanceId } = this.props;
const { instanceId, position } = this.props;
const isSearching = this.state.filterValue;
const visibleBlocksByCategory = this.getVisibleBlocksByCategory( this.getBlocksForCurrentTab() );

/* eslint-disable jsx-a11y/no-autofocus */
return (
<Popover position={ position } className="editor-inserter__menu">
<Wrapper position={ position } className="editor-inserter__menu">
<label htmlFor={ `editor-inserter__search-${ instanceId }` } className="screen-reader-text">
{ __( 'Search blocks' ) }
</label>
Expand Down Expand Up @@ -422,7 +430,7 @@ export class InserterMenu extends Component {
</button>
</div>
}
</Popover>
</Wrapper>
);
/* eslint-enable jsx-a11y/no-autofocus */
}
Expand Down
5 changes: 5 additions & 0 deletions editor/modes/visual-editor/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ class VisualEditorBlock extends Component {

componentDidMount() {
if ( this.props.focus ) {
// Already handled.
if ( this.node.contains( document.activeElement ) ) {
return;
}

this.node.focus();
}

Expand Down
2 changes: 1 addition & 1 deletion editor/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import createSelector from 'rememo';
/**
* WordPress dependencies
*/
import { getBlockType } from '@wordpress/blocks';
import { getBlockType } from '../blocks';
import { __ } from '@wordpress/i18n';

/**
Expand Down