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

Try: Add toggle editor mode shortcut #3755

Merged
Merged
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
1 change: 1 addition & 0 deletions components/menu-items/menu-items-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function MenuItemsGroup( { label, value, choices = [], onSelect, children, insta
key={ item.value }
label={ item.label }
isSelected={ isSelected }
shortcut={ item.shortcut }
onClick={ () => {
if ( ! isSelected ) {
onSelect( item.value );
Expand Down
10 changes: 10 additions & 0 deletions components/menu-items/menu-items-shortcut.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function MenuItemsShortcut( { shortcut } ) {
if ( ! shortcut ) {
return null;
}
return (
<span style={ { float: 'right', opacity: .5 } }>{ shortcut }</span>
);
}

export default MenuItemsShortcut;
7 changes: 5 additions & 2 deletions components/menu-items/menu-items-toggle.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/**
* Internal dependencies
*/
import IconButton from '../icon-button';
import Button from '../button';
import Shortcut from './menu-items-shortcut';
import IconButton from '../icon-button';
import './style.scss';

function MenuItemsToggle( { label, isSelected, onClick } ) {
function MenuItemsToggle( { label, isSelected, onClick, shortcut } ) {
if ( isSelected ) {
return (
<IconButton
Expand All @@ -14,6 +15,7 @@ function MenuItemsToggle( { label, isSelected, onClick } ) {
onClick={ onClick }
>
{ label }
<Shortcut shortcut={ shortcut } />
</IconButton>
);
}
Expand All @@ -24,6 +26,7 @@ function MenuItemsToggle( { label, isSelected, onClick } ) {
onClick={ onClick }
>
{ label }
<Shortcut shortcut={ shortcut } />
</Button>
);
}
Expand Down
16 changes: 11 additions & 5 deletions editor/edit-post/header/mode-switcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import { MenuItemsGroup } from '@wordpress/components';
/**
* Internal dependencies
*/
import shortcuts from '../../keyboard-shortcuts';
import { getEditorMode } from '../../../store/selectors';
import { switchEditorMode } from '../../../store/actions';

/**
* Set of available mode options.
Expand All @@ -31,10 +33,17 @@ const MODES = [
];

function ModeSwitcher( { onSwitch, mode } ) {
const choices = MODES.map( choice => {
if ( choice.value !== mode ) {
return { ...choice, shortcut: shortcuts.toggleEditorMode.label };
}
return choice;
} );

return (
<MenuItemsGroup
label={ __( 'Editor' ) }
choices={ MODES }
choices={ choices }
value={ mode }
onSelect={ onSwitch }
/>
Expand All @@ -47,10 +56,7 @@ export default connect(
} ),
( dispatch, ownProps ) => ( {
onSwitch( mode ) {
dispatch( {
type: 'SWITCH_MODE',
mode: mode,
} );
dispatch( switchEditorMode( mode ) );
ownProps.onSelect( mode );
},
} )
Expand Down
6 changes: 6 additions & 0 deletions editor/edit-post/keyboard-shortcuts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
toggleEditorMode: {
value: 'mod+shift+alt+m',
label: '⌘+Shift+Alt+M',
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: I wonder if we should detect the platform and replace with ctrl if not on MacOS.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it reliable to use navigator.platform? I can't find any example in the Gutenberg code.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is but that's the first time we need it :)

},
};
2 changes: 2 additions & 0 deletions editor/edit-post/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Sidebar from '../sidebar';
import TextEditor from '../modes/text-editor';
import VisualEditor from '../modes/visual-editor';
import DocumentTitle from '../document-title';
import EditorModeKeyboardShortcuts from '../modes/keyboard-shortcuts';
import {
MetaBoxes,
AutosaveMonitor,
Expand Down Expand Up @@ -57,6 +58,7 @@ function Layout( {
<div className="editor-layout__content" role="region" aria-label={ __( 'Editor content' ) } tabIndex="-1">
<EditorNotices />
<div className="editor-layout__editor">
<EditorModeKeyboardShortcuts />
{ mode === 'text' && <TextEditor /> }
{ mode === 'visual' && <VisualEditor /> }
</div>
Expand Down
54 changes: 54 additions & 0 deletions editor/edit-post/modes/keyboard-shortcuts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
Copy link
Member

Choose a reason for hiding this comment

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

While I'd like for us to flatten and eliminate the modes directory altogether, I don't particularly like the creation of a new folder here, because as it was prior to these changes, the modes directory contains exclusively the supported editing modes for the editor: visual and text. The shortcuts are not in themselves a mode.

* External dependencies
*/
import { connect } from 'react-redux';

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

/**
* Internal dependencies
*/
import shortcuts from '../../keyboard-shortcuts';
import { getEditorMode } from '../../../store/selectors';
import { switchEditorMode } from '../../../store/actions';

class EditorModeKeyboardShortcuts extends Component {
constructor() {
super( ...arguments );

this.toggleMode = this.toggleMode.bind( this );
}

toggleMode() {
const { mode, switchMode } = this.props;
switchMode( mode === 'visual' ? 'text' : 'visual' );
}

render() {
return (
<KeyboardShortcuts shortcuts={ {
[ shortcuts.toggleEditorMode.value ]: this.toggleMode,
} } />
);
}
}

export default connect(
( state ) => {
return {
mode: getEditorMode( state ),
};
},
( dispatch ) => {
return {
switchMode: ( mode ) => {
dispatch( switchEditorMode( mode ) );
},
};
},

)( EditorModeKeyboardShortcuts );
7 changes: 7 additions & 0 deletions editor/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,3 +601,10 @@ export function appendDefaultBlock() {
type: 'APPEND_DEFAULT_BLOCK',
};
}

export function switchEditorMode( mode ) {
return {
type: 'SWITCH_MODE',
mode,
};
}