From 1949a4a23e55930b11101f43bd6933a8908ff39f Mon Sep 17 00:00:00 2001 From: Marcelo Serpa <81248+fullofcaffeine@users.noreply.github.com> Date: Wed, 7 Apr 2021 19:59:07 -0500 Subject: [PATCH 1/2] Ignore completers when reaching >= 4 words after trigger char --- packages/components/src/autocomplete/index.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/components/src/autocomplete/index.js b/packages/components/src/autocomplete/index.js index af1b9a7c611b7..55eb61528a529 100644 --- a/packages/components/src/autocomplete/index.js +++ b/packages/components/src/autocomplete/index.js @@ -462,6 +462,17 @@ function Autocomplete( { return false; } + // Escape hatch for inline completer triggers. Allows up to 3 words to + // be matched and will bail out on the 4th word onwards. An example is + // the "user" completer. Its trigger char isn't removed when completing + // is done, so it's always present on the page. Without this hatch, the + // autocompleter will keep trying to match everything from the trigger + // onwards, up to infinity, slowing down the editor. This limit of words + // should work well with other completers. + if ( textWithoutTrigger.trim().split( /\s/ ).length >= 4 ) { + return false; + } + return /[\u0000-\uFFFF]*$/.test( textWithoutTrigger ); } ); From 2016c4f4815290a3e520becacc54bf4e046f4e9b Mon Sep 17 00:00:00 2001 From: Marcelo Serpa <81248+fullofcaffeine@users.noreply.github.com> Date: Wed, 7 Apr 2021 20:38:46 -0500 Subject: [PATCH 2/2] Adapt allowContext to be used as a completer filter that takes into account the text and not only the surrounding context --- packages/components/src/autocomplete/index.js | 25 +++++++------------ .../src/components/autocompleters/user.js | 11 ++++++++ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/packages/components/src/autocomplete/index.js b/packages/components/src/autocomplete/index.js index 55eb61528a529..40cd43bdf7a4e 100644 --- a/packages/components/src/autocomplete/index.js +++ b/packages/components/src/autocomplete/index.js @@ -444,17 +444,21 @@ function Autocomplete( { return false; } + const textWithoutTrigger = text.slice( + index + triggerPrefix.length + ); + if ( allowContext && - ! allowContext( text.slice( 0, index ), textAfterSelection ) + ! allowContext( + text.slice( 0, index ), + textAfterSelection, + textWithoutTrigger + ) ) { return false; } - const textWithoutTrigger = text.slice( - index + triggerPrefix.length - ); - if ( /^\s/.test( textWithoutTrigger ) || /\s\s+$/.test( textWithoutTrigger ) @@ -462,17 +466,6 @@ function Autocomplete( { return false; } - // Escape hatch for inline completer triggers. Allows up to 3 words to - // be matched and will bail out on the 4th word onwards. An example is - // the "user" completer. Its trigger char isn't removed when completing - // is done, so it's always present on the page. Without this hatch, the - // autocompleter will keep trying to match everything from the trigger - // onwards, up to infinity, slowing down the editor. This limit of words - // should work well with other completers. - if ( textWithoutTrigger.trim().split( /\s/ ).length >= 4 ) { - return false; - } - return /[\u0000-\uFFFF]*$/.test( textWithoutTrigger ); } ); diff --git a/packages/editor/src/components/autocompleters/user.js b/packages/editor/src/components/autocompleters/user.js index 4b8c1f2dbcf37..9c4c9de6c5d35 100644 --- a/packages/editor/src/components/autocompleters/user.js +++ b/packages/editor/src/components/autocompleters/user.js @@ -48,6 +48,17 @@ export default { , ]; }, + allowContext( before, after, textWithoutTrigger ) { + // Escape hatch for inline completer triggers. Allows up to 3 words to + // be matched and will bail out on the 4th word onwards. An example is + // this "user" completer. Its trigger char isn't removed when completing + // is done, so it's always present on the page. Without this hatch, the + // autocompleter will keep trying to match everything from the trigger + // onwards, up to infinity, slowing down the editor. + const reachedWordLimit = + textWithoutTrigger.trim().split( /\s/ ).length >= 4; + return ! reachedWordLimit; + }, getOptionCompletion( user ) { return `@${ user.slug }`; },