Pārlūkot izejas kodu

[editor] Remove usage of global huePubSub in the Ace code

Johan Ahlen 5 gadi atpakaļ
vecāks
revīzija
491b23396d

+ 52 - 47
desktop/core/src/desktop/js/apps/notebook2/components/aceEditor/autocomplete/AceAutocomplete.vue

@@ -205,58 +205,63 @@
 
       this.autocompleteResults = this.autocompleter.autocompleteResults;
 
-      this.subTracker.subscribe(
-        'hue.ace.autocompleter.show',
-        async (details: {
-          editor: Ace.Editor;
-          lineHeight: number;
-          position: { top: number; left: number };
-        }) => {
-          // The autocomplete can be triggered right after insertion of a suggestion
-          // when live autocomplete is enabled, hence if already active we ignore.
-          if (this.active || details.editor !== this.editor || !this.autocompleter) {
-            return;
-          }
-          const session = this.editor.getSession();
-          const pos = this.editor.getCursorPosition();
-          const line = session.getLine(pos.row);
-          const prefix = aceUtil.retrievePrecedingIdentifier(line, pos.column);
-          const newBase = session.doc.createAnchor(pos.row, pos.column - prefix.length);
-
-          if (!this.base || newBase.column !== this.base.column || newBase.row !== this.base.row) {
-            this.positionAutocompleteDropdown();
-            try {
-              this.loading = true;
-              const parseResult = await this.autocompleter.autocomplete();
-              if (hueDebug.showParseResult) {
-                // eslint-disable-next-line no-restricted-syntax
-                console.log(parseResult);
-              }
+      const showAutocomplete = async () => {
+        // The autocomplete can be triggered right after insertion of a suggestion
+        // when live autocomplete is enabled, hence if already active we ignore.
+        if (this.active || !this.autocompleter) {
+          return;
+        }
+        const session = this.editor.getSession();
+        const pos = this.editor.getCursorPosition();
+        const line = session.getLine(pos.row);
+        const prefix = aceUtil.retrievePrecedingIdentifier(line, pos.column);
+        const newBase = session.doc.createAnchor(pos.row, pos.column - prefix.length);
+
+        if (!this.base || newBase.column !== this.base.column || newBase.row !== this.base.row) {
+          this.positionAutocompleteDropdown();
+          try {
+            this.loading = true;
+            const parseResult = await this.autocompleter.autocomplete();
+            if (hueDebug.showParseResult) {
+              // eslint-disable-next-line no-restricted-syntax
+              console.log(parseResult);
+            }
 
-              if (parseResult && this.autocompleteResults) {
-                this.suggestions = [];
-                this.autocompleteResults.update(parseResult, this.suggestions).finally(() => {
-                  this.loading = false;
-                });
-
-                this.selectedIndex = 0;
-                newBase.$insertRight = true;
-                this.base = newBase;
-                if (this.autocompleteResults) {
-                  this.filter = prefix;
-                }
-                this.active = true;
-                this.attach();
+            if (parseResult && this.autocompleteResults) {
+              this.suggestions = [];
+              this.autocompleteResults.update(parseResult, this.suggestions).finally(() => {
+                this.loading = false;
+              });
+
+              this.selectedIndex = 0;
+              newBase.$insertRight = true;
+              this.base = newBase;
+              if (this.autocompleteResults) {
+                this.filter = prefix;
               }
-            } catch (err) {
-              if (typeof console.warn !== 'undefined') {
-                console.warn(err);
-              }
-              this.detach();
+              this.active = true;
+              this.attach();
+            }
+          } catch (err) {
+            if (typeof console.warn !== 'undefined') {
+              console.warn(err);
             }
+            this.detach();
           }
         }
-      );
+      };
+
+      this.editor.on('showAutocomplete', showAutocomplete);
+      const onHideAutocomplete = this.detach.bind(this);
+      this.editor.on('hideAutocomplete', onHideAutocomplete);
+      this.subTracker.subscribe('hue.ace.autocompleter.hide', onHideAutocomplete);
+
+      this.subTracker.addDisposable({
+        dispose: () => {
+          this.editor.off('showAutocomplete', showAutocomplete);
+          this.editor.off('hideAutocomplete', onHideAutocomplete);
+        }
+      });
 
       this.subTracker.subscribe(
         'editor.autocomplete.temporary.sort.override',

+ 3 - 5
desktop/core/src/desktop/js/ext/ace/ext-language_tools.js

@@ -1635,8 +1635,7 @@ Autocomplete.startCommand = {
             pos.top += rect.top - renderer.layerConfig.offset;
             pos.left += rect.left - editor.renderer.scrollLeft;
             pos.left += renderer.gutterWidth;
-
-            huePubSub.publish('hue.ace.autocompleter.show', { editor: editor, position: pos, lineHeight: lineHeight });
+            editor._signal('showAutocomplete', { position: pos, lineHeight: lineHeight });
             return;
         }
         if (!editor.completer)
@@ -1947,10 +1946,9 @@ var doLiveAutocomplete = function(e) {
             pos.top += rect.top - renderer.layerConfig.offset;
             pos.left += rect.left - editor.renderer.scrollLeft;
             pos.left += renderer.gutterWidth;
-
-            huePubSub.publish('hue.ace.autocompleter.show', { editor: editor, position: pos, lineHeight: lineHeight });
+            editor._signal('showAutocomplete', { position: pos, lineHeight: lineHeight });
         } else if (e.command.name === "backspace" && !prefix) {
-            huePubSub.publish('hue.ace.autocompleter.hide');
+            editor._signal('hideAutocomplete');
         }
         return;
     }

+ 1 - 2
desktop/core/src/desktop/js/ko/components/simpleAceEditor/ko.simpleAceEditor.js

@@ -21,7 +21,6 @@ import ace from 'ext/aceHelper';
 import AceLocationHandler from 'ko/bindings/ace/aceLocationHandler';
 import componentUtils from 'ko/components/componentUtils';
 import { UUID } from 'utils/hueUtils';
-import huePubSub from 'utils/huePubSub';
 import { hueLocalStorage } from 'utils/storageUtils';
 import SolrFormulaAutocompleter from './solrFormulaAutocompleter';
 import SolrQueryAutocompleter from './solrQueryAutocompleter';
@@ -91,7 +90,7 @@ class SimpleAceEditor {
         name: 'execute',
         bindKey: { win: 'Ctrl-Enter', mac: 'Command-Enter|Ctrl-Enter' },
         exec: async () => {
-          huePubSub.publish('hue.ace.autocompleter.hide');
+          editor._signal('hideAutocomplete');
           const executable = params.activeExecutable();
           if (executable) {
             await executable.reset();

+ 11 - 9
desktop/core/src/desktop/templates/hue_ace_autocompleter.mako

@@ -576,10 +576,7 @@ from desktop.views import _ko
           autocompleterDoneSub.remove();
         });
 
-        var autocompleterShowSub = huePubSub.subscribe('hue.ace.autocompleter.show', function (data) {
-          if (data.editor !== self.editor()) {
-            return;
-          }
+        var onShowAutocomplete = function (data) {
           var session = self.editor().getSession();
           var pos = self.editor().getCursorPosition();
           var line = session.getLine(pos.row);
@@ -604,18 +601,23 @@ from desktop.views import _ko
           } else {
             afterAutocomp();
           }
-        });
+        };
+
+        self.editor().on('showAutocomplete', onShowAutocomplete)
 
         self.disposeFunctions.push(function () {
-          autocompleterShowSub.remove();
-        });
+          self.editor().off('showAutocomplete', onShowAutocomplete);
+        })
 
-        var autocompleterHideSub = huePubSub.subscribe('hue.ace.autocompleter.hide', function () {
+        var onHideAutocomplete = function () {
           self.detach();
-        });
+        };
+        self.editor().on('hideAutocomplete', onHideAutocomplete)
+        var autocompleterHideSub = huePubSub.subscribe('hue.ace.autocompleter.hide', onHideAutocomplete);
 
         self.disposeFunctions.push(function () {
           autocompleterHideSub.remove();
+          self.editor().off('hideAutocomplete', onHideAutocomplete);
         });
       }
 

+ 1 - 2
tools/ace-editor/lib/ace/autocomplete.js

@@ -419,8 +419,7 @@ Autocomplete.startCommand = {
             pos.top += rect.top - renderer.layerConfig.offset;
             pos.left += rect.left - editor.renderer.scrollLeft;
             pos.left += renderer.gutterWidth;
-
-            huePubSub.publish('hue.ace.autocompleter.show', { editor: editor, position: pos, lineHeight: lineHeight });
+            editor._signal('showAutocomplete', { position: pos, lineHeight: lineHeight });
             return;
         }
         if (!editor.completer)

+ 2 - 3
tools/ace-editor/lib/ace/ext/language_tools.js

@@ -165,10 +165,9 @@ var doLiveAutocomplete = function(e) {
             pos.top += rect.top - renderer.layerConfig.offset;
             pos.left += rect.left - editor.renderer.scrollLeft;
             pos.left += renderer.gutterWidth;
-
-            huePubSub.publish('hue.ace.autocompleter.show', { editor: editor, position: pos, lineHeight: lineHeight });
+            editor._signal('showAutocomplete', { position: pos, lineHeight: lineHeight });
         } else if (e.command.name === "backspace" && !prefix) {
-            huePubSub.publish('hue.ace.autocompleter.hide');
+            editor._signal('hideAutocomplete');
         }
         return;
     }