-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.tsx
483 lines (426 loc) · 12.3 KB
/
index.tsx
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/**
* External dependencies
*/
import removeAccents from 'remove-accents';
/**
* WordPress dependencies
*/
import {
renderToString,
useEffect,
useState,
useRef,
useMemo,
} from '@wordpress/element';
import { __, _n } from '@wordpress/i18n';
import { useInstanceId, useMergeRefs, useRefEffect } from '@wordpress/compose';
import {
create,
slice,
insert,
isCollapsed,
getTextContent,
} from '@wordpress/rich-text';
import { speak } from '@wordpress/a11y';
import { isAppleOS } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import { getAutoCompleterUI } from './autocompleter-ui';
import { escapeRegExp } from '../utils/strings';
import { withIgnoreIMEEvents } from '../utils/with-ignore-ime-events';
import type {
AutocompleteProps,
AutocompleterUIProps,
InsertOption,
KeyedOption,
OptionCompletion,
ReplaceOption,
UseAutocompleteProps,
WPCompleter,
} from './types';
const getNodeText = ( node: React.ReactNode ): string => {
if ( node === null ) {
return '';
}
switch ( typeof node ) {
case 'string':
case 'number':
return node.toString();
break;
case 'boolean':
return '';
break;
case 'object': {
if ( node instanceof Array ) {
return node.map( getNodeText ).join( '' );
}
if ( 'props' in node ) {
return getNodeText( node.props.children );
}
break;
}
default:
return '';
}
return '';
};
const EMPTY_FILTERED_OPTIONS: KeyedOption[] = [];
export function useAutocomplete( {
record,
onChange,
onReplace,
completers,
contentRef,
}: UseAutocompleteProps ) {
const instanceId = useInstanceId( useAutocomplete );
const [ selectedIndex, setSelectedIndex ] = useState( 0 );
const [ filteredOptions, setFilteredOptions ] = useState<
Array< KeyedOption >
>( EMPTY_FILTERED_OPTIONS );
const [ filterValue, setFilterValue ] =
useState< AutocompleterUIProps[ 'filterValue' ] >( '' );
const [ autocompleter, setAutocompleter ] = useState< WPCompleter | null >(
null
);
const [ AutocompleterUI, setAutocompleterUI ] = useState<
( ( props: AutocompleterUIProps ) => JSX.Element | null ) | null
>( null );
const backspacing = useRef( false );
function insertCompletion( replacement: React.ReactNode ) {
if ( autocompleter === null ) {
return;
}
const end = record.start;
const start =
end - autocompleter.triggerPrefix.length - filterValue.length;
const toInsert = create( { html: renderToString( replacement ) } );
onChange( insert( record, toInsert, start, end ) );
}
function select( option: KeyedOption ) {
const { getOptionCompletion } = autocompleter || {};
if ( option.isDisabled ) {
return;
}
if ( getOptionCompletion ) {
const completion = getOptionCompletion( option.value, filterValue );
const isCompletionObject = (
obj: OptionCompletion
): obj is InsertOption | ReplaceOption => {
return (
obj !== null &&
typeof obj === 'object' &&
'action' in obj &&
obj.action !== undefined &&
'value' in obj &&
obj.value !== undefined
);
};
const completionObject = isCompletionObject( completion )
? completion
: ( {
action: 'insert-at-caret',
value: completion,
} as InsertOption );
if ( 'replace' === completionObject.action ) {
onReplace( [ completionObject.value ] );
// When replacing, the component will unmount, so don't reset
// state (below) on an unmounted component.
return;
} else if ( 'insert-at-caret' === completionObject.action ) {
insertCompletion( completionObject.value );
}
}
// Reset autocomplete state after insertion rather than before
// so insertion events don't cause the completion menu to redisplay.
reset();
}
function reset() {
setSelectedIndex( 0 );
setFilteredOptions( EMPTY_FILTERED_OPTIONS );
setFilterValue( '' );
setAutocompleter( null );
setAutocompleterUI( null );
}
/**
* Load options for an autocompleter.
*
* @param {Array} options
*/
function onChangeOptions( options: Array< KeyedOption > ) {
setSelectedIndex(
options.length === filteredOptions.length ? selectedIndex : 0
);
setFilteredOptions( options );
}
function handleKeyDown( event: KeyboardEvent ) {
backspacing.current = event.key === 'Backspace';
if ( ! autocompleter ) {
return;
}
if ( filteredOptions.length === 0 ) {
return;
}
if ( event.defaultPrevented ) {
return;
}
switch ( event.key ) {
case 'ArrowUp': {
const newIndex =
( selectedIndex === 0
? filteredOptions.length
: selectedIndex ) - 1;
setSelectedIndex( newIndex );
// See the related PR as to why this is necessary: https://github.com/WordPress/gutenberg/pull/54902.
if ( isAppleOS() ) {
speak(
getNodeText( filteredOptions[ newIndex ].label ),
'assertive'
);
}
break;
}
case 'ArrowDown': {
const newIndex = ( selectedIndex + 1 ) % filteredOptions.length;
setSelectedIndex( newIndex );
if ( isAppleOS() ) {
speak(
getNodeText( filteredOptions[ newIndex ].label ),
'assertive'
);
}
break;
}
case 'Escape':
setAutocompleter( null );
setAutocompleterUI( null );
event.preventDefault();
break;
case 'Enter':
select( filteredOptions[ selectedIndex ] );
break;
case 'ArrowLeft':
case 'ArrowRight':
reset();
return;
default:
return;
}
// Any handled key should prevent original behavior. This relies on
// the early return in the default case.
event.preventDefault();
}
// textContent is a primitive (string), memoizing is not strictly necessary
// but this is a preemptive performance improvement, since the autocompleter
// is a potential bottleneck for the editor type metric.
const textContent = useMemo( () => {
if ( isCollapsed( record ) ) {
return getTextContent( slice( record, 0 ) );
}
return '';
}, [ record ] );
useEffect( () => {
if ( ! textContent ) {
if ( autocompleter ) {
reset();
}
return;
}
// Find the completer with the highest triggerPrefix index in the
// textContent.
const completer = completers.reduce< WPCompleter | null >(
( lastTrigger, currentCompleter ) => {
const triggerIndex = textContent.lastIndexOf(
currentCompleter.triggerPrefix
);
const lastTriggerIndex =
lastTrigger !== null
? textContent.lastIndexOf( lastTrigger.triggerPrefix )
: -1;
return triggerIndex > lastTriggerIndex
? currentCompleter
: lastTrigger;
},
null
);
if ( ! completer ) {
if ( autocompleter ) {
reset();
}
return;
}
const { allowContext, triggerPrefix } = completer;
const triggerIndex = textContent.lastIndexOf( triggerPrefix );
const textWithoutTrigger = textContent.slice(
triggerIndex + triggerPrefix.length
);
const tooDistantFromTrigger = textWithoutTrigger.length > 50; // 50 chars seems to be a good limit.
// This is a final barrier to prevent the effect from completing with
// an extremely long string, which causes the editor to slow-down
// significantly. This could happen, for example, if `matchingWhileBackspacing`
// is true and one of the "words" end up being too long. If that's the case,
// it will be caught by this guard.
if ( tooDistantFromTrigger ) {
return;
}
const mismatch = filteredOptions.length === 0;
const wordsFromTrigger = textWithoutTrigger.split( /\s/ );
// We need to allow the effect to run when not backspacing and if there
// was a mismatch. i.e when typing a trigger + the match string or when
// clicking in an existing trigger word on the page. We do that if we
// detect that we have one word from trigger in the current textual context.
//
// Ex.: "Some text @a" <-- "@a" will be detected as the trigger word and
// allow the effect to run. It will run until there's a mismatch.
const hasOneTriggerWord = wordsFromTrigger.length === 1;
// This is used to allow the effect to run when backspacing and if
// "touching" a word that "belongs" to a trigger. We consider a "trigger
// word" any word up to the limit of 3 from the trigger character.
// Anything beyond that is ignored if there's a mismatch. This allows
// us to "escape" a mismatch when backspacing, but still imposing some
// sane limits.
//
// Ex: "Some text @marcelo sekkkk" <--- "kkkk" caused a mismatch, but
// if the user presses backspace here, it will show the completion popup again.
const matchingWhileBackspacing =
backspacing.current && wordsFromTrigger.length <= 3;
if ( mismatch && ! ( matchingWhileBackspacing || hasOneTriggerWord ) ) {
if ( autocompleter ) {
reset();
}
return;
}
const textAfterSelection = getTextContent(
slice( record, undefined, getTextContent( record ).length )
);
if (
allowContext &&
! allowContext(
textContent.slice( 0, triggerIndex ),
textAfterSelection
)
) {
if ( autocompleter ) {
reset();
}
return;
}
if (
/^\s/.test( textWithoutTrigger ) ||
/\s\s+$/.test( textWithoutTrigger )
) {
if ( autocompleter ) {
reset();
}
return;
}
if ( ! /[\u0000-\uFFFF]*$/.test( textWithoutTrigger ) ) {
if ( autocompleter ) {
reset();
}
return;
}
const safeTrigger = escapeRegExp( completer.triggerPrefix );
const text = removeAccents( textContent );
const match = text
.slice( text.lastIndexOf( completer.triggerPrefix ) )
.match( new RegExp( `${ safeTrigger }([\u0000-\uFFFF]*)$` ) );
const query = match && match[ 1 ];
setAutocompleter( completer );
setAutocompleterUI( () =>
completer !== autocompleter
? getAutoCompleterUI( completer )
: AutocompleterUI
);
setFilterValue( query === null ? '' : query );
// Temporarily disabling exhaustive-deps to avoid introducing unexpected side effecst.
// See https://github.com/WordPress/gutenberg/pull/41820
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ textContent ] );
const { key: selectedKey = '' } = filteredOptions[ selectedIndex ] || {};
const { className } = autocompleter || {};
const isExpanded = !! autocompleter && filteredOptions.length > 0;
const listBoxId = isExpanded
? `components-autocomplete-listbox-${ instanceId }`
: undefined;
const activeId = isExpanded
? `components-autocomplete-item-${ instanceId }-${ selectedKey }`
: null;
const hasSelection = record.start !== undefined;
return {
listBoxId,
activeId,
onKeyDown: withIgnoreIMEEvents( handleKeyDown ),
popover: hasSelection && AutocompleterUI && (
<AutocompleterUI
className={ className }
filterValue={ filterValue }
instanceId={ instanceId }
listBoxId={ listBoxId }
selectedIndex={ selectedIndex }
onChangeOptions={ onChangeOptions }
onSelect={ select }
value={ record }
contentRef={ contentRef }
reset={ reset }
/>
),
};
}
function useLastDifferentValue( value: UseAutocompleteProps[ 'record' ] ) {
const history = useRef< Set< typeof value > >( new Set() );
history.current.add( value );
// Keep the history size to 2.
if ( history.current.size > 2 ) {
history.current.delete( Array.from( history.current )[ 0 ] );
}
return Array.from( history.current )[ 0 ];
}
export function useAutocompleteProps( options: UseAutocompleteProps ) {
const ref = useRef< HTMLElement >( null );
const onKeyDownRef = useRef< ( event: KeyboardEvent ) => void >();
const { record } = options;
const previousRecord = useLastDifferentValue( record );
const { popover, listBoxId, activeId, onKeyDown } = useAutocomplete( {
...options,
contentRef: ref,
} );
onKeyDownRef.current = onKeyDown;
const mergedRefs = useMergeRefs( [
ref,
useRefEffect( ( element: HTMLElement ) => {
function _onKeyDown( event: KeyboardEvent ) {
onKeyDownRef.current?.( event );
}
element.addEventListener( 'keydown', _onKeyDown );
return () => {
element.removeEventListener( 'keydown', _onKeyDown );
};
}, [] ),
] );
// We only want to show the popover if the user has typed something.
const didUserInput = record.text !== previousRecord?.text;
if ( ! didUserInput ) {
return { ref: mergedRefs };
}
return {
ref: mergedRefs,
children: popover,
'aria-autocomplete': listBoxId ? 'list' : undefined,
'aria-owns': listBoxId,
'aria-activedescendant': activeId,
};
}
export default function Autocomplete( {
children,
isSelected,
...options
}: AutocompleteProps ) {
const { popover, ...props } = useAutocomplete( options );
return (
<>
{ children( props ) }
{ isSelected && popover }
</>
);
}