UNPKG

2.07 kBJavaScriptView Raw
1/**
2 * WordPress dependencies
3 */
4import { Component } from '@wordpress/element';
5import { KeyboardShortcuts } from '@wordpress/components';
6import { withSelect, withDispatch } from '@wordpress/data';
7import { compose } from '@wordpress/compose';
8
9/**
10 * Internal dependencies
11 */
12import shortcuts from '../../keyboard-shortcuts';
13
14class EditorModeKeyboardShortcuts extends Component {
15 constructor() {
16 super( ...arguments );
17
18 this.toggleMode = this.toggleMode.bind( this );
19 this.toggleSidebar = this.toggleSidebar.bind( this );
20 }
21
22 toggleMode() {
23 const { mode, switchMode, isRichEditingEnabled } = this.props;
24 if ( ! isRichEditingEnabled ) {
25 return;
26 }
27 switchMode( mode === 'visual' ? 'text' : 'visual' );
28 }
29
30 toggleSidebar( event ) {
31 // This shortcut has no known clashes, but use preventDefault to prevent any
32 // obscure shortcuts from triggering.
33 event.preventDefault();
34 const { isEditorSidebarOpen, closeSidebar, openSidebar } = this.props;
35
36 if ( isEditorSidebarOpen ) {
37 closeSidebar();
38 } else {
39 openSidebar();
40 }
41 }
42
43 render() {
44 return (
45 <KeyboardShortcuts
46 bindGlobal
47 shortcuts={ {
48 [ shortcuts.toggleEditorMode.raw ]: this.toggleMode,
49 [ shortcuts.toggleSidebar.raw ]: this.toggleSidebar,
50 } }
51 />
52 );
53 }
54}
55
56export default compose( [
57 withSelect( ( select ) => ( {
58 isRichEditingEnabled: select( 'core/editor' ).getEditorSettings().richEditingEnabled,
59 mode: select( 'core/edit-post' ).getEditorMode(),
60 isEditorSidebarOpen: select( 'core/edit-post' ).isEditorSidebarOpened(),
61 } ) ),
62 withDispatch( ( dispatch, ownProps, { select } ) => ( {
63 switchMode( mode ) {
64 dispatch( 'core/edit-post' ).switchEditorMode( mode );
65 },
66 openSidebar() {
67 const { getBlockSelectionStart } = select( 'core/block-editor' );
68 const sidebarToOpen = getBlockSelectionStart() ? 'edit-post/block' : 'edit-post/document';
69 dispatch( 'core/edit-post' ).openGeneralSidebar( sidebarToOpen );
70 },
71 closeSidebar: dispatch( 'core/edit-post' ).closeGeneralSidebar,
72 } ) ),
73] )( EditorModeKeyboardShortcuts );