From 8c686268aba4dc886a7cffccaeb9091e8a527746 Mon Sep 17 00:00:00 2001 From: Ella <4710635+ellatrix@users.noreply.github.com> Date: Thu, 23 Feb 2023 12:24:24 +0200 Subject: [PATCH] Autocomplete: reduce work before finding trigger (#48327) --- packages/components/src/autocomplete/index.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/components/src/autocomplete/index.js b/packages/components/src/autocomplete/index.js index 13c27d49b81b15..0dd4238d8c20e3 100644 --- a/packages/components/src/autocomplete/index.js +++ b/packages/components/src/autocomplete/index.js @@ -285,19 +285,15 @@ function useAutocomplete( { return; } - const text = removeAccents( textContent ); - const textAfterSelection = getTextContent( - slice( record, undefined, getTextContent( record ).length ) - ); const completer = completers?.find( ( { triggerPrefix, allowContext } ) => { - const index = text.lastIndexOf( triggerPrefix ); + const index = textContent.lastIndexOf( triggerPrefix ); if ( index === -1 ) { return false; } - const textWithoutTrigger = text.slice( + const textWithoutTrigger = textContent.slice( index + triggerPrefix.length ); @@ -339,9 +335,16 @@ function useAutocomplete( { return false; } + const textAfterSelection = getTextContent( + slice( record, undefined, getTextContent( record ).length ) + ); + if ( allowContext && - ! allowContext( text.slice( 0, index ), textAfterSelection ) + ! allowContext( + textContent.slice( 0, index ), + textAfterSelection + ) ) { return false; } @@ -363,6 +366,7 @@ function useAutocomplete( { } const safeTrigger = escapeRegExp( completer.triggerPrefix ); + const text = removeAccents( textContent ); const match = text .slice( text.lastIndexOf( completer.triggerPrefix ) ) .match( new RegExp( `${ safeTrigger }([\u0000-\uFFFF]*)$` ) );