-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathindex.js
258 lines (227 loc) · 6.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* WordPress dependencies
*/
import { useRefEffect, useMergeRefs } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';
import { isTextField } from '@wordpress/dom';
import {
UP,
RIGHT,
DOWN,
LEFT,
ENTER,
BACKSPACE,
ESCAPE,
TAB,
} from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
/**
* Set of key codes upon which typing is to be initiated on a keydown event.
*
* @type {Set<number>}
*/
const KEY_DOWN_ELIGIBLE_KEY_CODES = new Set( [
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 && KEY_DOWN_ELIGIBLE_KEY_CODES.has( keyCode );
}
/**
* Removes the `isTyping` flag when the mouse moves in the document of the given
* element.
*/
export function useMouseMoveTypingReset() {
const isTyping = useSelect(
( select ) => select( blockEditorStore ).isTyping(),
[]
);
const { stopTyping } = useDispatch( blockEditorStore );
return useRefEffect(
( node ) => {
if ( ! isTyping ) {
return;
}
const { ownerDocument } = node;
let lastClientX;
let lastClientY;
/**
* On mouse move, unset typing flag if user has moved cursor.
*
* @param {MouseEvent} event Mousemove event.
*/
function 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 (
lastClientX &&
lastClientY &&
( lastClientX !== clientX || lastClientY !== clientY )
) {
stopTyping();
}
lastClientX = clientX;
lastClientY = clientY;
}
ownerDocument.addEventListener(
'mousemove',
stopTypingOnMouseMove
);
return () => {
ownerDocument.removeEventListener(
'mousemove',
stopTypingOnMouseMove
);
};
},
[ isTyping, stopTyping ]
);
}
/**
* Sets and removes the `isTyping` flag based on user actions:
*
* - Sets the flag if the user types within the given element.
* - Removes the flag when the user selects some text, focuses a non-text
* field, presses ESC or TAB, or moves the mouse in the document.
*/
export function useTypingObserver() {
const { isTyping } = useSelect( ( select ) => {
const { isTyping: _isTyping } = select( blockEditorStore );
return {
isTyping: _isTyping(),
};
}, [] );
const { startTyping, stopTyping } = useDispatch( blockEditorStore );
const ref1 = useMouseMoveTypingReset();
const ref2 = useRefEffect(
( node ) => {
const { ownerDocument } = node;
const { defaultView } = ownerDocument;
const selection = defaultView.getSelection();
// Listeners to stop typing should only be added when typing.
// Listeners to start typing should only be added when not typing.
if ( isTyping ) {
let timerId;
/**
* Stops typing when focus transitions to a non-text field element.
*
* @param {FocusEvent} event Focus event.
*/
function stopTypingOnNonTextField( event ) {
const { target } = event;
// 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.
timerId = defaultView.setTimeout( () => {
if ( ! isTextField( target ) ) {
stopTyping();
}
} );
}
/**
* Unsets typing flag if user presses Escape while typing flag is
* active.
*
* @param {KeyboardEvent} event Keypress or keydown event to
* interpret.
*/
function stopTypingOnEscapeKey( event ) {
const { keyCode } = event;
if ( keyCode === ESCAPE || keyCode === TAB ) {
stopTyping();
}
}
/**
* On selection change, unset typing flag if user has made an
* uncollapsed (shift) selection.
*/
function stopTypingOnSelectionUncollapse() {
if ( ! selection.isCollapsed ) {
stopTyping();
}
}
node.addEventListener( 'focus', stopTypingOnNonTextField );
node.addEventListener( 'keydown', stopTypingOnEscapeKey );
ownerDocument.addEventListener(
'selectionchange',
stopTypingOnSelectionUncollapse
);
return () => {
defaultView.clearTimeout( timerId );
node.removeEventListener(
'focus',
stopTypingOnNonTextField
);
node.removeEventListener(
'keydown',
stopTypingOnEscapeKey
);
ownerDocument.removeEventListener(
'selectionchange',
stopTypingOnSelectionUncollapse
);
};
}
/**
* Handles a keypress or keydown event to infer intention to start
* typing.
*
* @param {KeyboardEvent} event Keypress or keydown event to interpret.
*/
function startTypingInTextField( event ) {
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 if outside the current DOM container
if ( ! isTextField( target ) || ! node.contains( target ) ) {
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;
}
startTyping();
}
node.addEventListener( 'keypress', startTypingInTextField );
node.addEventListener( 'keydown', startTypingInTextField );
return () => {
node.removeEventListener( 'keypress', startTypingInTextField );
node.removeEventListener( 'keydown', startTypingInTextField );
};
},
[ isTyping, startTyping, stopTyping ]
);
return useMergeRefs( [ ref1, ref2 ] );
}
function ObserveTyping( { children } ) {
return <div ref={ useTypingObserver() }>{ children }</div>;
}
/**
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/observe-typing/README.md
*/
export default ObserveTyping;