-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
158 lines (142 loc) · 4.41 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
151
152
153
154
155
156
157
158
/**
* External dependencies
*/
import { first, last, some, flow } from 'lodash';
/**
* WordPress dependencies
*/
import { Component, Fragment } from '@wordpress/element';
import { KeyboardShortcuts } from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';
import { rawShortcut, displayShortcut } from '@wordpress/keycodes';
import { compose } from '@wordpress/compose';
/**
* Internal dependencies
*/
import BlockActions from '../block-actions';
const preventDefault = ( event ) => {
event.preventDefault();
return event;
};
export const shortcuts = {
duplicate: {
raw: rawShortcut.primaryShift( 'd' ),
display: displayShortcut.primaryShift( 'd' ),
},
removeBlock: {
raw: rawShortcut.access( 'z' ),
display: displayShortcut.access( 'z' ),
},
insertBefore: {
raw: rawShortcut.primaryAlt( 't' ),
display: displayShortcut.primaryAlt( 't' ),
},
insertAfter: {
raw: rawShortcut.primaryAlt( 'y' ),
display: displayShortcut.primaryAlt( 'y' ),
},
};
class BlockEditorKeyboardShortcuts extends Component {
constructor() {
super( ...arguments );
this.selectAll = this.selectAll.bind( this );
this.deleteSelectedBlocks = this.deleteSelectedBlocks.bind( this );
this.clearMultiSelection = this.clearMultiSelection.bind( this );
}
selectAll( event ) {
const { rootBlocksClientIds, onMultiSelect } = this.props;
event.preventDefault();
onMultiSelect( first( rootBlocksClientIds ), last( rootBlocksClientIds ) );
}
deleteSelectedBlocks( event ) {
const { selectedBlockClientIds, hasMultiSelection, onRemove, isLocked } = this.props;
if ( hasMultiSelection ) {
event.preventDefault();
if ( ! isLocked ) {
onRemove( selectedBlockClientIds );
}
}
}
/**
* Clears current multi-selection, if one exists.
*/
clearMultiSelection() {
const { hasMultiSelection, clearSelectedBlock } = this.props;
if ( hasMultiSelection ) {
clearSelectedBlock();
window.getSelection().removeAllRanges();
}
}
render() {
const { selectedBlockClientIds } = this.props;
return (
<Fragment>
<KeyboardShortcuts
shortcuts={ {
[ rawShortcut.primary( 'a' ) ]: this.selectAll,
backspace: this.deleteSelectedBlocks,
del: this.deleteSelectedBlocks,
escape: this.clearMultiSelection,
} }
/>
{ selectedBlockClientIds.length > 0 && (
<BlockActions clientIds={ selectedBlockClientIds }>
{ ( { onDuplicate, onRemove, onInsertAfter, onInsertBefore } ) => (
<KeyboardShortcuts
bindGlobal
shortcuts={ {
// Prevents bookmark all Tabs shortcut in Chrome when devtools are closed.
// Prevents reposition Chrome devtools pane shortcut when devtools are open.
[ shortcuts.duplicate.raw ]: flow( preventDefault, onDuplicate ),
// Does not clash with any known browser/native shortcuts, but preventDefault
// is used to prevent any obscure unknown shortcuts from triggering.
[ shortcuts.removeBlock.raw ]: flow( preventDefault, onRemove ),
// Prevent 'view recently closed tabs' in Opera using preventDefault.
[ shortcuts.insertBefore.raw ]: flow( preventDefault, onInsertBefore ),
// Does not clash with any known browser/native shortcuts, but preventDefault
// is used to prevent any obscure unknown shortcuts from triggering.
[ shortcuts.insertAfter.raw ]: flow( preventDefault, onInsertAfter ),
} }
/>
) }
</BlockActions>
) }
</Fragment>
);
}
}
export default compose( [
withSelect( ( select ) => {
const {
getBlockOrder,
getMultiSelectedBlockClientIds,
hasMultiSelection,
getBlockRootClientId,
getTemplateLock,
getSelectedBlockClientId,
} = select( 'core/block-editor' );
const selectedBlockClientId = getSelectedBlockClientId();
const selectedBlockClientIds = selectedBlockClientId ? [ selectedBlockClientId ] : getMultiSelectedBlockClientIds();
return {
rootBlocksClientIds: getBlockOrder(),
hasMultiSelection: hasMultiSelection(),
isLocked: some(
selectedBlockClientIds,
( clientId ) => !! getTemplateLock( getBlockRootClientId( clientId ) )
),
selectedBlockClientIds,
};
} ),
withDispatch( ( dispatch ) => {
const {
clearSelectedBlock,
multiSelect,
removeBlocks,
} = dispatch( 'core/block-editor' );
return {
clearSelectedBlock,
onMultiSelect: multiSelect,
onRemove: removeBlocks,
};
} ),
] )( BlockEditorKeyboardShortcuts );