@atlaskit/editor-plugin-autocomplete 4.0.1 → 4.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @atlaskit/editor-plugin-autocomplete
2
2
 
3
+ ## 4.0.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [`56263a9d5bb6d`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/56263a9d5bb6d) -
8
+ Gate editor autocomplete to English locales so suggestions and model loading are disabled for
9
+ non-English users.
10
+
3
11
  ## 4.0.1
4
12
 
5
13
  ### Patch Changes
@@ -230,7 +230,16 @@ var getTypedLengthForPrediction = function getTypedLengthForPrediction(textBefor
230
230
  var trailingToken = (_textBefore$match$ = (_textBefore$match = textBefore.match(/([^\s.,;:!?]*)$/)) === null || _textBefore$match === void 0 ? void 0 : _textBefore$match[1]) !== null && _textBefore$match$ !== void 0 ? _textBefore$match$ : '';
231
231
  return trailingToken.length;
232
232
  };
233
+ var isEnglishLocale = function isEnglishLocale(locale) {
234
+ if (!locale) {
235
+ return false;
236
+ }
237
+ return locale.toLowerCase().startsWith('en');
238
+ };
233
239
  var createAutocompletePlugin = exports.createAutocompletePlugin = function createAutocompletePlugin(options, api) {
240
+ var _options$locale;
241
+ var locale = (_options$locale = options === null || options === void 0 ? void 0 : options.locale) !== null && _options$locale !== void 0 ? _options$locale : typeof navigator !== 'undefined' ? navigator.language : undefined;
242
+ var isAutocompleteEnabled = isEnglishLocale(locale);
234
243
  var debounceTimer = null;
235
244
  var hasIngestedPage = false;
236
245
  var resolvedContext;
@@ -620,6 +629,9 @@ var createAutocompletePlugin = exports.createAutocompletePlugin = function creat
620
629
  }),
621
630
  handleDOMEvents: {
622
631
  blur: function blur(view) {
632
+ if (!isAutocompleteEnabled) {
633
+ return false;
634
+ }
623
635
  var pluginState = autocompletePluginKey.getState(view.state);
624
636
  if (pluginState !== null && pluginState !== void 0 && pluginState.ghostText) {
625
637
  clearGhostText(view.state, view.dispatch);
@@ -629,6 +641,9 @@ var createAutocompletePlugin = exports.createAutocompletePlugin = function creat
629
641
  },
630
642
  focus: function focus() {
631
643
  var _options$useLocalMode, _options$useLocalMode2;
644
+ if (!isAutocompleteEnabled) {
645
+ return false;
646
+ }
632
647
  (0, _textPredictor.loadDefaultVocabulary)({
633
648
  isLocalLLM: (_options$useLocalMode = options === null || options === void 0 ? void 0 : options.useLocalModel) !== null && _options$useLocalMode !== void 0 ? _options$useLocalMode : false
634
649
  }).catch(function (error) {
@@ -658,6 +673,9 @@ var createAutocompletePlugin = exports.createAutocompletePlugin = function creat
658
673
  view: function view() {
659
674
  return {
660
675
  update: function update(view, prevState) {
676
+ if (!isAutocompleteEnabled) {
677
+ return;
678
+ }
661
679
  currentView = view;
662
680
  if (!prevState.doc.eq(view.state.doc)) {
663
681
  if (justAccepted) {
@@ -218,7 +218,16 @@ const getTypedLengthForPrediction = textBefore => {
218
218
  const trailingToken = (_textBefore$match$ = (_textBefore$match = textBefore.match(/([^\s.,;:!?]*)$/)) === null || _textBefore$match === void 0 ? void 0 : _textBefore$match[1]) !== null && _textBefore$match$ !== void 0 ? _textBefore$match$ : '';
219
219
  return trailingToken.length;
220
220
  };
221
+ const isEnglishLocale = locale => {
222
+ if (!locale) {
223
+ return false;
224
+ }
225
+ return locale.toLowerCase().startsWith('en');
226
+ };
221
227
  export const createAutocompletePlugin = (options, api) => {
228
+ var _options$locale;
229
+ const locale = (_options$locale = options === null || options === void 0 ? void 0 : options.locale) !== null && _options$locale !== void 0 ? _options$locale : typeof navigator !== 'undefined' ? navigator.language : undefined;
230
+ const isAutocompleteEnabled = isEnglishLocale(locale);
222
231
  let debounceTimer = null;
223
232
  let hasIngestedPage = false;
224
233
  let resolvedContext;
@@ -607,6 +616,9 @@ export const createAutocompletePlugin = (options, api) => {
607
616
  }),
608
617
  handleDOMEvents: {
609
618
  blur: view => {
619
+ if (!isAutocompleteEnabled) {
620
+ return false;
621
+ }
610
622
  const pluginState = autocompletePluginKey.getState(view.state);
611
623
  if (pluginState !== null && pluginState !== void 0 && pluginState.ghostText) {
612
624
  clearGhostText(view.state, view.dispatch);
@@ -616,6 +628,9 @@ export const createAutocompletePlugin = (options, api) => {
616
628
  },
617
629
  focus: () => {
618
630
  var _options$useLocalMode, _options$useLocalMode2;
631
+ if (!isAutocompleteEnabled) {
632
+ return false;
633
+ }
619
634
  loadDefaultVocabulary({
620
635
  isLocalLLM: (_options$useLocalMode = options === null || options === void 0 ? void 0 : options.useLocalModel) !== null && _options$useLocalMode !== void 0 ? _options$useLocalMode : false
621
636
  }).catch(error => {
@@ -644,6 +659,9 @@ export const createAutocompletePlugin = (options, api) => {
644
659
  },
645
660
  view: () => ({
646
661
  update: (view, prevState) => {
662
+ if (!isAutocompleteEnabled) {
663
+ return;
664
+ }
647
665
  currentView = view;
648
666
  if (!prevState.doc.eq(view.state.doc)) {
649
667
  if (justAccepted) {
@@ -223,7 +223,16 @@ var getTypedLengthForPrediction = function getTypedLengthForPrediction(textBefor
223
223
  var trailingToken = (_textBefore$match$ = (_textBefore$match = textBefore.match(/([^\s.,;:!?]*)$/)) === null || _textBefore$match === void 0 ? void 0 : _textBefore$match[1]) !== null && _textBefore$match$ !== void 0 ? _textBefore$match$ : '';
224
224
  return trailingToken.length;
225
225
  };
226
+ var isEnglishLocale = function isEnglishLocale(locale) {
227
+ if (!locale) {
228
+ return false;
229
+ }
230
+ return locale.toLowerCase().startsWith('en');
231
+ };
226
232
  export var createAutocompletePlugin = function createAutocompletePlugin(options, api) {
233
+ var _options$locale;
234
+ var locale = (_options$locale = options === null || options === void 0 ? void 0 : options.locale) !== null && _options$locale !== void 0 ? _options$locale : typeof navigator !== 'undefined' ? navigator.language : undefined;
235
+ var isAutocompleteEnabled = isEnglishLocale(locale);
227
236
  var debounceTimer = null;
228
237
  var hasIngestedPage = false;
229
238
  var resolvedContext;
@@ -613,6 +622,9 @@ export var createAutocompletePlugin = function createAutocompletePlugin(options,
613
622
  }),
614
623
  handleDOMEvents: {
615
624
  blur: function blur(view) {
625
+ if (!isAutocompleteEnabled) {
626
+ return false;
627
+ }
616
628
  var pluginState = autocompletePluginKey.getState(view.state);
617
629
  if (pluginState !== null && pluginState !== void 0 && pluginState.ghostText) {
618
630
  clearGhostText(view.state, view.dispatch);
@@ -622,6 +634,9 @@ export var createAutocompletePlugin = function createAutocompletePlugin(options,
622
634
  },
623
635
  focus: function focus() {
624
636
  var _options$useLocalMode, _options$useLocalMode2;
637
+ if (!isAutocompleteEnabled) {
638
+ return false;
639
+ }
625
640
  loadDefaultVocabulary({
626
641
  isLocalLLM: (_options$useLocalMode = options === null || options === void 0 ? void 0 : options.useLocalModel) !== null && _options$useLocalMode !== void 0 ? _options$useLocalMode : false
627
642
  }).catch(function (error) {
@@ -651,6 +666,9 @@ export var createAutocompletePlugin = function createAutocompletePlugin(options,
651
666
  view: function view() {
652
667
  return {
653
668
  update: function update(view, prevState) {
669
+ if (!isAutocompleteEnabled) {
670
+ return;
671
+ }
654
672
  currentView = view;
655
673
  if (!prevState.doc.eq(view.state.doc)) {
656
674
  if (justAccepted) {
@@ -40,6 +40,11 @@ export interface AutocompletePluginOptions {
40
40
  * Use this to serve vectors from a CDN or media service in production.
41
41
  */
42
42
  getVectorsBinaryUrl?: () => Promise<string>;
43
+ /**
44
+ * User locale used to determine whether autocomplete should run.
45
+ * Defaults to browser locale when omitted.
46
+ */
47
+ locale?: string;
43
48
  /**
44
49
  * When true, uses on-device inference via WebGPU (MLC WebLLM) instead of
45
50
  * the network-based slow-lane backend. Defaults to false (network client).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-plugin-autocomplete",
3
- "version": "4.0.1",
3
+ "version": "4.0.2",
4
4
  "description": "Client-side text autocomplete plugin for @atlaskit/editor-core",
5
5
  "author": "Atlassian Pty Ltd",
6
6
  "license": "Apache-2.0",
@@ -214,6 +214,11 @@ export interface AutocompletePluginOptions {
214
214
  * Use this to serve vectors from a CDN or media service in production.
215
215
  */
216
216
  getVectorsBinaryUrl?: () => Promise<string>;
217
+ /**
218
+ * User locale used to determine whether autocomplete should run.
219
+ * Defaults to browser locale when omitted.
220
+ */
221
+ locale?: string;
217
222
  /**
218
223
  * When true, uses on-device inference via WebGPU (MLC WebLLM) instead of
219
224
  * the network-based slow-lane backend. Defaults to false (network client).
@@ -299,10 +304,22 @@ const getTypedLengthForPrediction = (textBefore: string): number => {
299
304
  return trailingToken.length;
300
305
  };
301
306
 
307
+ const isEnglishLocale = (locale?: string): boolean => {
308
+ if (!locale) {
309
+ return false;
310
+ }
311
+
312
+ return locale.toLowerCase().startsWith('en');
313
+ };
314
+
302
315
  export const createAutocompletePlugin = (
303
316
  options?: AutocompletePluginOptions,
304
317
  api?: ExtractInjectionAPI<AutocompletePlugin>,
305
318
  ): SafePlugin<AutocompletePluginState> => {
319
+ const locale =
320
+ options?.locale ?? (typeof navigator !== 'undefined' ? navigator.language : undefined);
321
+ const isAutocompleteEnabled = isEnglishLocale(locale);
322
+
306
323
  let debounceTimer: ReturnType<typeof setTimeout> | null = null;
307
324
  let hasIngestedPage = false;
308
325
  let resolvedContext: AutocompleteContext | undefined;
@@ -730,6 +747,10 @@ export const createAutocompletePlugin = (
730
747
 
731
748
  handleDOMEvents: {
732
749
  blur: (view: EditorView) => {
750
+ if (!isAutocompleteEnabled) {
751
+ return false;
752
+ }
753
+
733
754
  const pluginState = autocompletePluginKey.getState(view.state) as
734
755
  | AutocompletePluginState
735
756
  | undefined;
@@ -740,6 +761,10 @@ export const createAutocompletePlugin = (
740
761
  return false;
741
762
  },
742
763
  focus: () => {
764
+ if (!isAutocompleteEnabled) {
765
+ return false;
766
+ }
767
+
743
768
  loadDefaultVocabulary({ isLocalLLM: options?.useLocalModel ?? false }).catch((error) => {
744
769
  logException(error as Error, {
745
770
  location: 'editor-plugin-autocomplete/loadDefaultVocabulary',
@@ -767,6 +792,10 @@ export const createAutocompletePlugin = (
767
792
 
768
793
  view: () => ({
769
794
  update: (view: EditorView, prevState: EditorState) => {
795
+ if (!isAutocompleteEnabled) {
796
+ return;
797
+ }
798
+
770
799
  currentView = view;
771
800
  if (!prevState.doc.eq(view.state.doc)) {
772
801
  if (justAccepted) {