Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Show toolbar on meta key press
  • Loading branch information
ellatrix committed May 23, 2017
commit c6ca48231a806d378677735acf5cb535c155ac0c
32 changes: 29 additions & 3 deletions editor/modes/visual-editor/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@ import {
isTypingInBlock,
} from '../../selectors';

function metaKeyPressed( event ) {
return event.metaKey || ( event.ctrlKey && ! event.altKey );
}

class VisualEditorBlock extends wp.element.Component {
constructor() {
super( ...arguments );
this.bindBlockNode = this.bindBlockNode.bind( this );
this.setAttributes = this.setAttributes.bind( this );
this.maybeHover = this.maybeHover.bind( this );
this.maybeStartTyping = this.maybeStartTyping.bind( this );
this.removeOrDeselect = this.removeOrDeselect.bind( this );
this.handleKeyDown = this.handleKeyDown.bind( this );
this.handleKeyUp = this.handleKeyUp.bind( this );
this.mergeBlocks = this.mergeBlocks.bind( this );
this.selectAndStopPropagation = this.selectAndStopPropagation.bind( this );
this.previousOffset = null;
this.metaCount = 0;
}

bindBlockNode( node ) {
Expand Down Expand Up @@ -89,7 +95,7 @@ class VisualEditorBlock extends wp.element.Component {
}
}

removeOrDeselect( event ) {
handleKeyDown( event ) {
const { keyCode, target } = event;

// Remove block on backspace
Expand All @@ -104,6 +110,20 @@ class VisualEditorBlock extends wp.element.Component {
if ( 27 /* Escape */ === event.keyCode ) {
this.props.onDeselect();
}

if ( metaKeyPressed( event ) ) {
this.metaCount++;
} else {
this.metaCount = 0;
}
}

handleKeyUp() {
if ( this.metaCount === 1 ) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe a bit odd to expect, but if I hold down a non-meta character (say, c), then press a meta character, when releasing, metaCount is === 1.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for testing! 👍

this.props.onStopTyping();
}

this.metaCount = 0;
}

mergeBlocks( forward = false ) {
Expand Down Expand Up @@ -189,7 +209,8 @@ class VisualEditorBlock extends wp.element.Component {
ref={ this.bindBlockNode }
onClick={ this.selectAndStopPropagation }
onFocus={ onSelect }
onKeyDown={ this.removeOrDeselect }
onKeyDown={ this.handleKeyDown }
onKeyUp={ this.handleKeyUp }
onMouseEnter={ onHover }
onMouseMove={ this.maybeHover }
onMouseLeave={ onMouseLeave }
Expand Down Expand Up @@ -266,6 +287,11 @@ export default connect(
uid: ownProps.uid,
} );
},
onStopTyping() {
dispatch( {
type: 'STOP_TYPING',
} );
},
onHover() {
dispatch( {
type: 'TOGGLE_BLOCK_HOVERED',
Expand Down
6 changes: 6 additions & 0 deletions editor/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ export function selectedBlock( state = {}, action ) {
typing: true,
};

case 'STOP_TYPING':
return {
...state,
typing: false,
};

case 'REPLACE_BLOCKS':
if ( ! action.blocks || ! action.blocks.length || action.uids.indexOf( state.uid ) === -1 ) {
return state;
Expand Down