1 |
|
2 |
|
3 |
|
4 | import { Component } from '@wordpress/element';
|
5 | import { KeyboardShortcuts } from '@wordpress/components';
|
6 | import { withSelect, withDispatch } from '@wordpress/data';
|
7 | import { compose } from '@wordpress/compose';
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | import shortcuts from '../../keyboard-shortcuts';
|
13 |
|
14 | class 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 |
|
32 |
|
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 |
|
56 | export 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 );
|