Skip to content

Commit

Permalink
Components: Extract Reusable Keyboard Shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Nov 24, 2017
1 parent 1856f8c commit e51fbe3
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 47 deletions.
80 changes: 80 additions & 0 deletions editor/components/editor-global-keyboard-shortcuts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';
import { first, last } from 'lodash';

/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { KeyboardShortcuts } from '@wordpress/components';

/**
* Internal dependencies
*/
import { getBlockUids, getMultiSelectedBlockUids } from '../../selectors';
import { clearSelectedBlock, multiSelect, redo, undo, removeBlocks } from '../../actions';

class EditorGlobalKeyboardShortcuts extends Component {
constructor() {
super( ...arguments );
this.selectAll = this.selectAll.bind( this );
this.undoOrRedo = this.undoOrRedo.bind( this );
this.deleteSelectedBlocks = this.deleteSelectedBlocks.bind( this );
}

selectAll( event ) {
const { uids, onMultiSelect } = this.props;
event.preventDefault();
onMultiSelect( first( uids ), last( uids ) );
}

undoOrRedo( event ) {
const { onRedo, onUndo } = this.props;
if ( event.shiftKey ) {
onRedo();
} else {
onUndo();
}

event.preventDefault();
}

deleteSelectedBlocks( event ) {
const { multiSelectedBlockUids, onRemove } = this.props;
if ( multiSelectedBlockUids.length ) {
event.preventDefault();
onRemove( multiSelectedBlockUids );
}
}

render() {
return (
<KeyboardShortcuts shortcuts={ {
'mod+a': this.selectAll,
'mod+z': this.undoOrRedo,
'mod+shift+z': this.undoOrRedo,
backspace: this.deleteSelectedBlocks,
del: this.deleteSelectedBlocks,
escape: this.props.clearSelectedBlock,
} } />
);
}
}

export default connect(
( state ) => {
return {
uids: getBlockUids( state ),
multiSelectedBlockUids: getMultiSelectedBlockUids( state ),
};
},
{
clearSelectedBlock,
onMultiSelect: multiSelect,
onRedo: redo,
onUndo: undo,
onRemove: removeBlocks,
}
)( EditorGlobalKeyboardShortcuts );
1 change: 1 addition & 0 deletions editor/components/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Post Related Components
export { default as AutosaveMonitor } from './autosave-monitor';
export { default as DocumentOutline } from './document-outline';
export { default as EditorGlobalKeyboardShortcuts } from './editor-global-keyboard-shortcuts';
export { default as EditorHistoryRedo } from './editor-history/redo';
export { default as EditorHistoryUndo } from './editor-history/undo';
export { default as MetaBoxes } from './meta-boxes';
Expand Down
51 changes: 4 additions & 47 deletions editor/edit-post/modes/visual-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,26 @@
* External dependencies
*/
import { connect } from 'react-redux';
import { first, last } from 'lodash';

/**
* WordPress dependencies
*/
import { Component, findDOMNode } from '@wordpress/element';
import { KeyboardShortcuts } from '@wordpress/components';

/**
* Internal dependencies
*/
import './style.scss';
import { BlockList, PostTitle, WritingFlow, DefaultBlockAppender } from '../../../components';
import { getBlockUids, getMultiSelectedBlockUids, isFeatureActive } from '../../../selectors';
import { clearSelectedBlock, multiSelect, redo, undo, removeBlocks } from '../../../actions';
import { BlockList, PostTitle, WritingFlow, DefaultBlockAppender, EditorGlobalKeyboardShortcuts } from '../../../components';
import { isFeatureActive } from '../../../selectors';
import { clearSelectedBlock } from '../../../actions';

class VisualEditor extends Component {
constructor() {
super( ...arguments );
this.bindContainer = this.bindContainer.bind( this );
this.bindBlocksContainer = this.bindBlocksContainer.bind( this );
this.onClick = this.onClick.bind( this );
this.selectAll = this.selectAll.bind( this );
this.undoOrRedo = this.undoOrRedo.bind( this );
this.deleteSelectedBlocks = this.deleteSelectedBlocks.bind( this );
}

bindContainer( ref ) {
Expand All @@ -47,31 +42,6 @@ class VisualEditor extends Component {
}
}

selectAll( event ) {
const { uids, onMultiSelect } = this.props;
event.preventDefault();
onMultiSelect( first( uids ), last( uids ) );
}

undoOrRedo( event ) {
const { onRedo, onUndo } = this.props;
if ( event.shiftKey ) {
onRedo();
} else {
onUndo();
}

event.preventDefault();
}

deleteSelectedBlocks( event ) {
const { multiSelectedBlockUids, onRemove } = this.props;
if ( multiSelectedBlockUids.length ) {
event.preventDefault();
onRemove( multiSelectedBlockUids );
}
}

render() {
// Disable reason: Clicking the canvas should clear the selection
/* eslint-disable jsx-a11y/no-static-element-interactions */
Expand All @@ -82,14 +52,7 @@ class VisualEditor extends Component {
onTouchStart={ this.onClick }
ref={ this.bindContainer }
>
<KeyboardShortcuts shortcuts={ {
'mod+a': this.selectAll,
'mod+z': this.undoOrRedo,
'mod+shift+z': this.undoOrRedo,
backspace: this.deleteSelectedBlocks,
del: this.deleteSelectedBlocks,
escape: this.props.clearSelectedBlock,
} } />
<EditorGlobalKeyboardShortcuts />
<WritingFlow>
<PostTitle />
<BlockList
Expand All @@ -107,16 +70,10 @@ class VisualEditor extends Component {
export default connect(
( state ) => {
return {
uids: getBlockUids( state ),
multiSelectedBlockUids: getMultiSelectedBlockUids( state ),
hasFixedToolbar: isFeatureActive( state, 'fixedToolbar' ),
};
},
{
clearSelectedBlock,
onMultiSelect: multiSelect,
onRedo: redo,
onUndo: undo,
onRemove: removeBlocks,
}
)( VisualEditor );

0 comments on commit e51fbe3

Please sign in to comment.