Skip to content

Commit

Permalink
Autocomplete: reduce work before finding trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Feb 22, 2023
1 parent 278be06 commit 411534b
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions packages/components/src/autocomplete/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down Expand Up @@ -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;
}
Expand All @@ -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]*)$` ) );
Expand Down

0 comments on commit 411534b

Please sign in to comment.