-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
116 lines (105 loc) · 2.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* External dependencies
*/
import { castArray } from 'lodash';
/**
* WordPress dependencies
*/
import { Fragment } from '@wordpress/element';
import { Modal, KeyboardShortcuts } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { rawShortcut } from '@wordpress/keycodes';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
/**
* Internal dependencies
*/
import shortcutConfig from './config';
const MODAL_NAME = 'edit-post/keyboard-shortcut-help';
const mapKeyCombination = ( keyCombination ) => keyCombination.map( ( character, index ) => {
if ( character === '+' ) {
return (
<Fragment key={ index }>
{ character }
</Fragment>
);
}
return (
<kbd
key={ index }
className="edit-post-keyboard-shortcut-help__shortcut-key"
>
{ character }
</kbd>
);
} );
const ShortcutList = ( { shortcuts } ) => (
<dl className="edit-post-keyboard-shortcut-help__shortcut-list">
{ shortcuts.map( ( { keyCombination, description, ariaLabel }, index ) => (
<div
className="edit-post-keyboard-shortcut-help__shortcut"
key={ index }
>
<dt className="edit-post-keyboard-shortcut-help__shortcut-term">
<kbd className="edit-post-keyboard-shortcut-help__shortcut-key-combination" aria-label={ ariaLabel }>
{ mapKeyCombination( castArray( keyCombination ) ) }
</kbd>
</dt>
<dd className="edit-post-keyboard-shortcut-help__shortcut-description">
{ description }
</dd>
</div>
) ) }
</dl>
);
const ShortcutSection = ( { title, shortcuts } ) => (
<section className="edit-post-keyboard-shortcut-help__section">
<h2 className="edit-post-keyboard-shortcut-help__section-title">
{ title }
</h2>
<ShortcutList shortcuts={ shortcuts } />
</section>
);
export function KeyboardShortcutHelpModal( { isModalActive, toggleModal } ) {
const title = (
<span className="edit-post-keyboard-shortcut-help__title">
{ __( 'Keyboard Shortcuts' ) }
</span>
);
return (
<Fragment>
<KeyboardShortcuts
bindGlobal
shortcuts={ {
[ rawShortcut.access( 'h' ) ]: toggleModal,
} }
/>
{ isModalActive && (
<Modal
className="edit-post-keyboard-shortcut-help"
title={ title }
closeLabel={ __( 'Close' ) }
onRequestClose={ toggleModal }
>
{ shortcutConfig.map( ( config, index ) => (
<ShortcutSection key={ index } { ...config } />
) ) }
</Modal>
) }
</Fragment>
);
}
export default compose( [
withSelect( ( select ) => ( {
isModalActive: select( 'core/edit-post' ).isModalActive( MODAL_NAME ),
} ) ),
withDispatch( ( dispatch, { isModalActive } ) => {
const {
openModal,
closeModal,
} = dispatch( 'core/edit-post' );
return {
toggleModal: () => isModalActive ? closeModal() : openModal( MODAL_NAME ),
};
} ),
] )( KeyboardShortcutHelpModal );