Skip to content

Commit

Permalink
Autocomplete: reduce work before finding trigger (#48327)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Feb 23, 2023
1 parent ec09ddc commit 8c68626
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

1 comment on commit 8c68626

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 8c68626.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4251623533
📝 Reported issues:

Please sign in to comment.