-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
edit-mode.js
153 lines (135 loc) · 4.52 KB
/
edit-mode.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
151
152
153
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useShortcut } from '@wordpress/keyboard-shortcuts';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { store as interfaceStore } from '@wordpress/interface';
import { createBlock } from '@wordpress/blocks';
import { store as preferencesStore } from '@wordpress/preferences';
import { store as noticesStore } from '@wordpress/notices';
/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
import { SIDEBAR_BLOCK } from '../sidebar-edit-mode/constants';
import { STORE_NAME } from '../../store/constants';
function KeyboardShortcutsEditMode() {
const { getEditorMode } = useSelect( editSiteStore );
const isListViewOpen = useSelect(
( select ) => select( editSiteStore ).isListViewOpened(),
[]
);
const isBlockInspectorOpen = useSelect(
( select ) =>
select( interfaceStore ).getActiveComplementaryArea(
editSiteStore.name
) === SIDEBAR_BLOCK,
[]
);
const { redo, undo } = useDispatch( coreStore );
const {
isFeatureActive,
setIsListViewOpened,
switchEditorMode,
toggleFeature,
setIsInserterOpened,
closeGeneralSidebar,
} = useDispatch( editSiteStore );
const { enableComplementaryArea, disableComplementaryArea } =
useDispatch( interfaceStore );
const { replaceBlocks } = useDispatch( blockEditorStore );
const { getBlockName, getSelectedBlockClientId, getBlockAttributes } =
useSelect( blockEditorStore );
const { set: setPreference } = useDispatch( preferencesStore );
const { createInfoNotice } = useDispatch( noticesStore );
const toggleDistractionFree = () => {
setPreference( 'core/edit-site', 'fixedToolbar', false );
setIsInserterOpened( false );
setIsListViewOpened( false );
closeGeneralSidebar();
};
const handleTextLevelShortcut = ( event, level ) => {
event.preventDefault();
const destinationBlockName =
level === 0 ? 'core/paragraph' : 'core/heading';
const currentClientId = getSelectedBlockClientId();
if ( currentClientId === null ) {
return;
}
const blockName = getBlockName( currentClientId );
if ( blockName !== 'core/paragraph' && blockName !== 'core/heading' ) {
return;
}
const attributes = getBlockAttributes( currentClientId );
const textAlign =
blockName === 'core/paragraph' ? 'align' : 'textAlign';
const destinationTextAlign =
destinationBlockName === 'core/paragraph' ? 'align' : 'textAlign';
replaceBlocks(
currentClientId,
createBlock( destinationBlockName, {
level,
content: attributes.content,
...{ [ destinationTextAlign ]: attributes[ textAlign ] },
} )
);
};
useShortcut( 'core/edit-site/undo', ( event ) => {
undo();
event.preventDefault();
} );
useShortcut( 'core/edit-site/redo', ( event ) => {
redo();
event.preventDefault();
} );
// Only opens the list view. Other functionality for this shortcut happens in the rendered sidebar.
useShortcut( 'core/edit-site/toggle-list-view', () => {
if ( isListViewOpen ) {
return;
}
setIsListViewOpened( true );
} );
useShortcut( 'core/edit-site/toggle-block-settings-sidebar', ( event ) => {
// This shortcut has no known clashes, but use preventDefault to prevent any
// obscure shortcuts from triggering.
event.preventDefault();
if ( isBlockInspectorOpen ) {
disableComplementaryArea( STORE_NAME );
} else {
enableComplementaryArea( STORE_NAME, SIDEBAR_BLOCK );
}
} );
useShortcut( 'core/edit-site/toggle-mode', () => {
switchEditorMode( getEditorMode() === 'visual' ? 'text' : 'visual' );
} );
useShortcut( 'core/edit-site/transform-heading-to-paragraph', ( event ) =>
handleTextLevelShortcut( event, 0 )
);
[ 1, 2, 3, 4, 5, 6 ].forEach( ( level ) => {
//the loop is based off on a constant therefore
//the hook will execute the same way every time
//eslint-disable-next-line react-hooks/rules-of-hooks
useShortcut(
`core/edit-site/transform-paragraph-to-heading-${ level }`,
( event ) => handleTextLevelShortcut( event, level )
);
} );
useShortcut( 'core/edit-site/toggle-distraction-free', () => {
toggleDistractionFree();
toggleFeature( 'distractionFree' );
createInfoNotice(
isFeatureActive( 'distractionFree' )
? __( 'Distraction free mode turned on.' )
: __( 'Distraction free mode turned off.' ),
{
id: 'core/edit-site/distraction-free-mode/notice',
type: 'snackbar',
}
);
} );
return null;
}
export default KeyboardShortcutsEditMode;