-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
122 lines (108 loc) · 3.13 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
/**
* WordPress dependencies
*/
import { useShortcut } from '@wordpress/keyboard-shortcuts';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as interfaceStore } from '@wordpress/interface';
import { store as blockEditorStore } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import { store as editorStore } from '../../store';
/**
* Handles the keyboard shortcuts for the editor.
*
* It provides functionality for various keyboard shortcuts such as toggling editor mode,
* toggling distraction-free mode, undo/redo, saving the post, toggling list view,
* and toggling the sidebar.
*/
export default function EditorKeyboardShortcuts() {
const isModeToggleDisabled = useSelect( ( select ) => {
const { richEditingEnabled, codeEditingEnabled } =
select( editorStore ).getEditorSettings();
return ! richEditingEnabled || ! codeEditingEnabled;
}, [] );
const { getBlockSelectionStart } = useSelect( blockEditorStore );
const { getActiveComplementaryArea } = useSelect( interfaceStore );
const { enableComplementaryArea, disableComplementaryArea } =
useDispatch( interfaceStore );
const {
redo,
undo,
savePost,
setIsListViewOpened,
switchEditorMode,
toggleDistractionFree,
} = useDispatch( editorStore );
const {
isEditedPostDirty,
isPostSavingLocked,
isListViewOpened,
getEditorMode,
} = useSelect( editorStore );
useShortcut(
'core/editor/toggle-mode',
() => {
switchEditorMode(
getEditorMode() === 'visual' ? 'text' : 'visual'
);
},
{
isDisabled: isModeToggleDisabled,
}
);
useShortcut( 'core/editor/toggle-distraction-free', () => {
toggleDistractionFree();
} );
useShortcut( 'core/editor/undo', ( event ) => {
undo();
event.preventDefault();
} );
useShortcut( 'core/editor/redo', ( event ) => {
redo();
event.preventDefault();
} );
useShortcut( 'core/editor/save', ( event ) => {
event.preventDefault();
/**
* Do not save the post if post saving is locked.
*/
if ( isPostSavingLocked() ) {
return;
}
// TODO: This should be handled in the `savePost` effect in
// considering `isSaveable`. See note on `isEditedPostSaveable`
// selector about dirtiness and meta-boxes.
//
// See: `isEditedPostSaveable`
if ( ! isEditedPostDirty() ) {
return;
}
savePost();
} );
// Only opens the list view. Other functionality for this shortcut happens in the rendered sidebar.
useShortcut( 'core/editor/toggle-list-view', ( event ) => {
if ( ! isListViewOpened() ) {
event.preventDefault();
setIsListViewOpened( true );
}
} );
useShortcut( 'core/editor/toggle-sidebar', ( event ) => {
// This shortcut has no known clashes, but use preventDefault to prevent any
// obscure shortcuts from triggering.
event.preventDefault();
const isEditorSidebarOpened = [
'edit-post/document',
'edit-post/block',
].includes( getActiveComplementaryArea( 'core' ) );
if ( isEditorSidebarOpened ) {
disableComplementaryArea( 'core' );
} else {
const sidebarToOpen = getBlockSelectionStart()
? 'edit-post/block'
: 'edit-post/document';
enableComplementaryArea( 'core', sidebarToOpen );
}
} );
return null;
}