-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ import './categories'; | |
import './cover-image'; | ||
import './cover-text'; | ||
import './verse'; | ||
import './inserter'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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; | ||
}, | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:core/inserter /--> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[ | ||
{ | ||
"uid": "_uid_0", | ||
"name": "core/inserter", | ||
"isValid": true, | ||
"attributes": {} | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[ | ||
{ | ||
"blockName": "core/inserter", | ||
"attrs": null, | ||
"rawContent": "" | ||
}, | ||
{ | ||
"attrs": {}, | ||
"rawContent": "\n" | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:core/inserter /--> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change? |
||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -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 ); | ||
|
@@ -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 } /> | ||
|
@@ -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> | ||
|
@@ -422,7 +430,7 @@ export class InserterMenu extends Component { | |
</button> | ||
</div> | ||
} | ||
</Popover> | ||
</Wrapper> | ||
); | ||
/* eslint-enable jsx-a11y/no-autofocus */ | ||
} | ||
|
There was a problem hiding this comment.
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).