-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
219 lines (189 loc) · 5.93 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* External dependencies
*/
import { over, includes } from 'lodash';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';
import { isTextField } from '@wordpress/dom';
import {
UP,
RIGHT,
DOWN,
LEFT,
ENTER,
BACKSPACE,
ESCAPE,
} from '@wordpress/keycodes';
import { withSafeTimeout, compose } from '@wordpress/compose';
/**
* Set of key codes upon which typing is to be initiated on a keydown event.
*
* @type {number[]}
*/
const KEY_DOWN_ELIGIBLE_KEY_CODES = [ UP, RIGHT, DOWN, LEFT, ENTER, BACKSPACE ];
/**
* Returns true if a given keydown event can be inferred as intent to start
* typing, or false otherwise. A keydown is considered eligible if it is a
* text navigation without shift active.
*
* @param {KeyboardEvent} event Keydown event to test.
*
* @return {boolean} Whether event is eligible to start typing.
*/
function isKeyDownEligibleForStartTyping( event ) {
const { keyCode, shiftKey } = event;
return ! shiftKey && includes( KEY_DOWN_ELIGIBLE_KEY_CODES, keyCode );
}
class ObserveTyping extends Component {
constructor() {
super( ...arguments );
this.stopTypingOnSelectionUncollapse = this.stopTypingOnSelectionUncollapse.bind( this );
this.stopTypingOnMouseMove = this.stopTypingOnMouseMove.bind( this );
this.startTypingInTextField = this.startTypingInTextField.bind( this );
this.stopTypingOnNonTextField = this.stopTypingOnNonTextField.bind( this );
this.stopTypingOnEscapeKey = this.stopTypingOnEscapeKey.bind( this );
this.onKeyDown = over( [
this.startTypingInTextField,
this.stopTypingOnEscapeKey,
] );
this.lastMouseMove = null;
}
componentDidMount() {
this.toggleEventBindings( this.props.isTyping );
}
componentDidUpdate( prevProps ) {
if ( this.props.isTyping !== prevProps.isTyping ) {
this.toggleEventBindings( this.props.isTyping );
}
}
componentWillUnmount() {
this.toggleEventBindings( false );
}
/**
* Bind or unbind events to the document when typing has started or stopped
* respectively, or when component has become unmounted.
*
* @param {boolean} isBound Whether event bindings should be applied.
*/
toggleEventBindings( isBound ) {
const bindFn = isBound ? 'addEventListener' : 'removeEventListener';
document[ bindFn ]( 'selectionchange', this.stopTypingOnSelectionUncollapse );
document[ bindFn ]( 'mousemove', this.stopTypingOnMouseMove );
}
/**
* On mouse move, unset typing flag if user has moved cursor.
*
* @param {MouseEvent} event Mousemove event.
*/
stopTypingOnMouseMove( event ) {
const { clientX, clientY } = event;
// We need to check that the mouse really moved because Safari triggers
// mousemove events when shift or ctrl are pressed.
if ( this.lastMouseMove ) {
const {
clientX: lastClientX,
clientY: lastClientY,
} = this.lastMouseMove;
if ( lastClientX !== clientX || lastClientY !== clientY ) {
this.props.onStopTyping();
}
}
this.lastMouseMove = { clientX, clientY };
}
/**
* On selection change, unset typing flag if user has made an uncollapsed
* (shift) selection.
*/
stopTypingOnSelectionUncollapse() {
const selection = window.getSelection();
const isCollapsed = selection.rangeCount > 0 && selection.getRangeAt( 0 ).collapsed;
if ( ! isCollapsed ) {
this.props.onStopTyping();
}
}
/**
* Unsets typing flag if user presses Escape while typing flag is active.
*
* @param {KeyboardEvent} event Keypress or keydown event to interpret.
*/
stopTypingOnEscapeKey( event ) {
if ( this.props.isTyping && event.keyCode === ESCAPE ) {
this.props.onStopTyping();
}
}
/**
* Handles a keypress or keydown event to infer intention to start typing.
*
* @param {KeyboardEvent} event Keypress or keydown event to interpret.
*/
startTypingInTextField( event ) {
const { isTyping, onStartTyping } = this.props;
const { type, target } = event;
// Abort early if already typing, or key press is incurred outside a
// text field (e.g. arrow-ing through toolbar buttons).
// Ignore typing in a block toolbar
if ( isTyping || ! isTextField( target ) || target.closest( '.block-editor-block-toolbar' ) ) {
return;
}
// Special-case keydown because certain keys do not emit a keypress
// event. Conversely avoid keydown as the canonical event since there
// are many keydown which are explicitly not targeted for typing.
if ( type === 'keydown' && ! isKeyDownEligibleForStartTyping( event ) ) {
return;
}
onStartTyping();
}
/**
* Stops typing when focus transitions to a non-text field element.
*
* @param {FocusEvent} event Focus event.
*/
stopTypingOnNonTextField( event ) {
event.persist();
// Since focus to a non-text field via arrow key will trigger before
// the keydown event, wait until after current stack before evaluating
// whether typing is to be stopped. Otherwise, typing will re-start.
this.props.setTimeout( () => {
const { isTyping, onStopTyping } = this.props;
const { target } = event;
if ( isTyping && ! isTextField( target ) ) {
onStopTyping();
}
} );
}
render() {
const { children } = this.props;
// Disable reason: This component is responsible for capturing bubbled
// keyboard events which are interpreted as typing intent.
/* eslint-disable jsx-a11y/no-static-element-interactions */
return (
<div
onFocus={ this.stopTypingOnNonTextField }
onKeyPress={ this.startTypingInTextField }
onKeyDown={ this.onKeyDown }
>
{ children }
</div>
);
/* eslint-enable jsx-a11y/no-static-element-interactions */
}
}
export default compose( [
withSelect( ( select ) => {
const { isTyping } = select( 'core/block-editor' );
return {
isTyping: isTyping(),
};
} ),
withDispatch( ( dispatch ) => {
const { startTyping, stopTyping } = dispatch( 'core/block-editor' );
return {
onStartTyping: startTyping,
onStopTyping: stopTyping,
};
} ),
withSafeTimeout,
] )( ObserveTyping );