-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
150 lines (138 loc) · 3.46 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { Modal } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import {
useShortcut,
store as keyboardShortcutsStore,
} from '@wordpress/keyboard-shortcuts';
import { useDispatch, useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import { textFormattingShortcuts } from './config';
import Shortcut from './shortcut';
import DynamicShortcut from './dynamic-shortcut';
const ShortcutList = ( { shortcuts } ) => (
/*
* Disable reason: The `list` ARIA role is redundant but
* Safari+VoiceOver won't announce the list otherwise.
*/
/* eslint-disable jsx-a11y/no-redundant-roles */
<ul
className="customize-widgets-keyboard-shortcut-help-modal__shortcut-list"
role="list"
>
{ shortcuts.map( ( shortcut, index ) => (
<li
className="customize-widgets-keyboard-shortcut-help-modal__shortcut"
key={ index }
>
{ typeof shortcut === 'string' ? (
<DynamicShortcut name={ shortcut } />
) : (
<Shortcut { ...shortcut } />
) }
</li>
) ) }
</ul>
/* eslint-enable jsx-a11y/no-redundant-roles */
);
const ShortcutSection = ( { title, shortcuts, className } ) => (
<section
className={ classnames(
'customize-widgets-keyboard-shortcut-help-modal__section',
className
) }
>
{ !! title && (
<h2 className="customize-widgets-keyboard-shortcut-help-modal__section-title">
{ title }
</h2>
) }
<ShortcutList shortcuts={ shortcuts } />
</section>
);
const ShortcutCategorySection = ( {
title,
categoryName,
additionalShortcuts = [],
} ) => {
const categoryShortcuts = useSelect(
( select ) => {
return select( keyboardShortcutsStore ).getCategoryShortcuts(
categoryName
);
},
[ categoryName ]
);
return (
<ShortcutSection
title={ title }
shortcuts={ categoryShortcuts.concat( additionalShortcuts ) }
/>
);
};
export default function KeyboardShortcutHelpModal( {
isModalActive,
toggleModal,
} ) {
const { registerShortcut } = useDispatch( keyboardShortcutsStore );
registerShortcut( {
name: 'core/customize-widgets/keyboard-shortcuts',
category: 'main',
description: __( 'Display these keyboard shortcuts.' ),
keyCombination: {
modifier: 'access',
character: 'h',
},
} );
useShortcut( 'core/customize-widgets/keyboard-shortcuts', toggleModal );
if ( ! isModalActive ) {
return null;
}
return (
<Modal
className="customize-widgets-keyboard-shortcut-help-modal"
title={ __( 'Keyboard shortcuts' ) }
closeLabel={ __( 'Close' ) }
onRequestClose={ toggleModal }
>
<ShortcutSection
className="customize-widgets-keyboard-shortcut-help-modal__main-shortcuts"
shortcuts={ [ 'core/customize-widgets/keyboard-shortcuts' ] }
/>
<ShortcutCategorySection
title={ __( 'Global shortcuts' ) }
categoryName="global"
/>
<ShortcutCategorySection
title={ __( 'Selection shortcuts' ) }
categoryName="selection"
/>
<ShortcutCategorySection
title={ __( 'Block shortcuts' ) }
categoryName="block"
additionalShortcuts={ [
{
keyCombination: { character: '/' },
description: __(
'Change the block type after adding a new paragraph.'
),
/* translators: The forward-slash character. e.g. '/'. */
ariaLabel: __( 'Forward-slash' ),
},
] }
/>
<ShortcutSection
title={ __( 'Text formatting' ) }
shortcuts={ textFormattingShortcuts }
/>
</Modal>
);
}