-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
320 lines (285 loc) · 9.29 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/**
* External dependencies
*/
import { throttle } from 'lodash';
import classnames from 'classnames';
import scrollIntoView from 'dom-scroll-into-view';
/**
* WordPress dependencies
*/
import { __, sprintf, _n } from '@wordpress/i18n';
import { Component, createRef } from '@wordpress/element';
import { UP, DOWN, ENTER, TAB } from '@wordpress/keycodes';
import { Spinner, withSpokenMessages, Popover } from '@wordpress/components';
import { withInstanceId, withSafeTimeout, compose } from '@wordpress/compose';
import { withSelect } from '@wordpress/data';
// Since URLInput is rendered in the context of other inputs, but should be
// considered a separate modal node, prevent keyboard events from propagating
// as being considered from the input.
const stopEventPropagation = ( event ) => event.stopPropagation();
class URLInput extends Component {
constructor( { autocompleteRef } ) {
super( ...arguments );
this.onChange = this.onChange.bind( this );
this.onKeyDown = this.onKeyDown.bind( this );
this.autocompleteRef = autocompleteRef || createRef();
this.inputRef = createRef();
this.updateSuggestions = throttle( this.updateSuggestions.bind( this ), 200 );
this.suggestionNodes = [];
this.state = {
suggestions: [],
showSuggestions: false,
selectedSuggestion: null,
};
}
componentDidUpdate() {
const { showSuggestions, selectedSuggestion } = this.state;
// only have to worry about scrolling selected suggestion into view
// when already expanded
if ( showSuggestions && selectedSuggestion !== null && ! this.scrollingIntoView ) {
this.scrollingIntoView = true;
scrollIntoView( this.suggestionNodes[ selectedSuggestion ], this.autocompleteRef.current, {
onlyScrollIfNeeded: true,
} );
this.props.setTimeout( () => {
this.scrollingIntoView = false;
}, 100 );
}
}
componentWillUnmount() {
delete this.suggestionsRequest;
}
bindSuggestionNode( index ) {
return ( ref ) => {
this.suggestionNodes[ index ] = ref;
};
}
updateSuggestions( value ) {
const { fetchLinkSuggestions } = this.props;
if ( ! fetchLinkSuggestions ) {
return;
}
// Show the suggestions after typing at least 2 characters
// and also for URLs
if ( value.length < 2 || /^https?:/.test( value ) ) {
this.setState( {
showSuggestions: false,
selectedSuggestion: null,
loading: false,
} );
return;
}
this.setState( {
showSuggestions: true,
selectedSuggestion: null,
loading: true,
} );
const request = fetchLinkSuggestions( value );
request.then( ( suggestions ) => {
// A fetch Promise doesn't have an abort option. It's mimicked by
// comparing the request reference in on the instance, which is
// reset or deleted on subsequent requests or unmounting.
if ( this.suggestionsRequest !== request ) {
return;
}
this.setState( {
suggestions,
loading: false,
} );
if ( !! suggestions.length ) {
this.props.debouncedSpeak( sprintf( _n(
'%d result found, use up and down arrow keys to navigate.',
'%d results found, use up and down arrow keys to navigate.',
suggestions.length
), suggestions.length ), 'assertive' );
} else {
this.props.debouncedSpeak( __( 'No results.' ), 'assertive' );
}
} ).catch( () => {
if ( this.suggestionsRequest === request ) {
this.setState( {
loading: false,
} );
}
} );
this.suggestionsRequest = request;
}
onChange( event ) {
const inputValue = event.target.value;
this.props.onChange( inputValue );
this.updateSuggestions( inputValue );
}
onKeyDown( event ) {
const { showSuggestions, selectedSuggestion, suggestions, loading } = this.state;
// If the suggestions are not shown or loading, we shouldn't handle the arrow keys
// We shouldn't preventDefault to allow block arrow keys navigation
if ( ! showSuggestions || ! suggestions.length || loading ) {
// In the Windows version of Firefox the up and down arrows don't move the caret
// within an input field like they do for Mac Firefox/Chrome/Safari. This causes
// a form of focus trapping that is disruptive to the user experience. This disruption
// only happens if the caret is not in the first or last position in the text input.
// See: https://github.com/WordPress/gutenberg/issues/5693#issuecomment-436684747
switch ( event.keyCode ) {
// When UP is pressed, if the caret is at the start of the text, move it to the 0
// position.
case UP: {
if ( 0 !== event.target.selectionStart ) {
event.stopPropagation();
event.preventDefault();
// Set the input caret to position 0
event.target.setSelectionRange( 0, 0 );
}
break;
}
// When DOWN is pressed, if the caret is not at the end of the text, move it to the
// last position.
case DOWN: {
if ( this.props.value.length !== event.target.selectionStart ) {
event.stopPropagation();
event.preventDefault();
// Set the input caret to the last position
event.target.setSelectionRange( this.props.value.length, this.props.value.length );
}
break;
}
}
return;
}
const suggestion = this.state.suggestions[ this.state.selectedSuggestion ];
switch ( event.keyCode ) {
case UP: {
event.stopPropagation();
event.preventDefault();
const previousIndex = ! selectedSuggestion ? suggestions.length - 1 : selectedSuggestion - 1;
this.setState( {
selectedSuggestion: previousIndex,
} );
break;
}
case DOWN: {
event.stopPropagation();
event.preventDefault();
const nextIndex = selectedSuggestion === null || ( selectedSuggestion === suggestions.length - 1 ) ? 0 : selectedSuggestion + 1;
this.setState( {
selectedSuggestion: nextIndex,
} );
break;
}
case TAB: {
if ( this.state.selectedSuggestion !== null ) {
this.selectLink( suggestion );
// Announce a link has been selected when tabbing away from the input field.
this.props.speak( __( 'Link selected.' ) );
}
break;
}
case ENTER: {
if ( this.state.selectedSuggestion !== null ) {
event.stopPropagation();
this.selectLink( suggestion );
}
break;
}
}
}
selectLink( suggestion ) {
this.props.onChange( suggestion.url, suggestion );
this.setState( {
selectedSuggestion: null,
showSuggestions: false,
} );
}
handleOnClick( suggestion ) {
this.selectLink( suggestion );
// Move focus to the input field when a link suggestion is clicked.
this.inputRef.current.focus();
}
static getDerivedStateFromProps( { disableSuggestions }, { showSuggestions } ) {
return {
showSuggestions: disableSuggestions === true ? false : showSuggestions,
};
}
render() {
const { value = '', autoFocus = true, instanceId, className, id, isFullWidth, hasBorder } = this.props;
const { showSuggestions, suggestions, selectedSuggestion, loading } = this.state;
const suggestionsListboxId = `block-editor-url-input-suggestions-${ instanceId }`;
const suggestionOptionIdPrefix = `block-editor-url-input-suggestion-${ instanceId }`;
/* eslint-disable jsx-a11y/no-autofocus */
return (
<div className={ classnames( 'editor-url-input block-editor-url-input', className, {
'is-full-width': isFullWidth,
'has-border': hasBorder,
} ) }>
<input
id={ id }
autoFocus={ autoFocus }
type="text"
aria-label={ __( 'URL' ) }
required
value={ value }
onChange={ this.onChange }
onInput={ stopEventPropagation }
placeholder={ __( 'Paste URL or type to search' ) }
onKeyDown={ this.onKeyDown }
role="combobox"
aria-expanded={ showSuggestions }
aria-autocomplete="list"
aria-owns={ suggestionsListboxId }
aria-activedescendant={ selectedSuggestion !== null ? `${ suggestionOptionIdPrefix }-${ selectedSuggestion }` : undefined }
ref={ this.inputRef }
/>
{ ( loading ) && <Spinner /> }
{ showSuggestions && !! suggestions.length &&
<Popover
position="bottom"
noArrow
focusOnMount={ false }
>
<div
className={ classnames(
'editor-url-input__suggestions',
'block-editor-url-input__suggestions',
`${ className }__suggestions`
) }
id={ suggestionsListboxId }
ref={ this.autocompleteRef }
role="listbox"
>
{ suggestions.map( ( suggestion, index ) => (
<button
key={ suggestion.id }
role="option"
tabIndex="-1"
id={ `${ suggestionOptionIdPrefix }-${ index }` }
ref={ this.bindSuggestionNode( index ) }
className={ classnames( 'editor-url-input__suggestion block-editor-url-input__suggestion', {
'is-selected': index === selectedSuggestion,
} ) }
onClick={ () => this.handleOnClick( suggestion ) }
aria-selected={ index === selectedSuggestion }
>
{ suggestion.title }
</button>
) ) }
</div>
</Popover>
}
</div>
);
/* eslint-enable jsx-a11y/no-autofocus */
}
}
/**
* @see https://github.com/WordPress/gutenberg/blob/master/packages/block-editor/src/components/url-input/README.md
*/
export default compose(
withSafeTimeout,
withSpokenMessages,
withInstanceId,
withSelect( ( select ) => {
const { getSettings } = select( 'core/block-editor' );
return {
fetchLinkSuggestions: getSettings().__experimentalFetchLinkSuggestions,
};
} )
)( URLInput );