Przeglądaj źródła

[editor] Remove snippet dependency from Ace location handler events

Johan Ahlen 5 lat temu
rodzic
commit
dca0b927c9

+ 14 - 3
desktop/core/src/desktop/js/apps/notebook/snippet.js

@@ -38,6 +38,11 @@ import {
   ASSIST_SET_SOURCE_EVENT
 } from 'ko/components/assist/events';
 import { POST_FROM_LOCATION_WORKER_EVENT } from 'sql/sqlWorkerHandler';
+import {
+  ACTIVE_STATEMENT_CHANGED_EVENT,
+  CURSOR_POSITION_CHANGED_EVENT,
+  REFRESH_STATEMENT_LOCATIONS_EVENT
+} from 'ko/bindings/ace/aceLocationHandler';
 
 const NOTEBOOK_MAPPING = {
   ignore: [
@@ -342,7 +347,7 @@ class Snippet {
       if (newValue !== null) {
         apiHelper.setInTotalStorage('editor', 'last.selected.database', newValue);
         if (previousDatabase !== null && previousDatabase !== newValue) {
-          huePubSub.publish('editor.refresh.statement.locations', self);
+          huePubSub.publish(REFRESH_STATEMENT_LOCATIONS_EVENT, self.id());
         }
         previousDatabase = newValue;
       }
@@ -539,8 +544,14 @@ class Snippet {
     self.lastExecutedStatement = ko.observable(null);
     self.statementsList = ko.observableArray();
 
+    huePubSub.subscribe(CURSOR_POSITION_CHANGED_EVENT, details => {
+      if (details.editorId === self.id()) {
+        self.aceCursorPosition(details.position);
+      }
+    });
+
     huePubSub.subscribe(
-      'editor.active.statement.changed',
+      ACTIVE_STATEMENT_CHANGED_EVENT,
       statementDetails => {
         if (self.ace() && self.ace().container.id === statementDetails.id) {
           for (let i = statementDetails.precedingStatements.length - 1; i >= 0; i--) {
@@ -1760,7 +1771,7 @@ class Snippet {
       self.statusForButtons('executing');
 
       if (self.isSqlDialect()) {
-        huePubSub.publish('editor.refresh.statement.locations', self);
+        huePubSub.publish(REFRESH_STATEMENT_LOCATIONS_EVENT, self.id());
       }
 
       self.lastExecutedStatements = self.statement();

+ 8 - 1
desktop/core/src/desktop/js/apps/notebook2/snippet.js

@@ -43,6 +43,7 @@ import {
 } from 'apps/notebook2/execution/executable';
 import {
   ACTIVE_STATEMENT_CHANGED_EVENT,
+  CURSOR_POSITION_CHANGED_EVENT,
   REFRESH_STATEMENT_LOCATIONS_EVENT
 } from 'ko/bindings/ace/aceLocationHandler';
 import { EXECUTE_ACTIVE_EXECUTABLE_EVENT } from './components/ExecutableActions.vue';
@@ -281,7 +282,7 @@ export default class Snippet {
       if (newValue !== null) {
         apiHelper.setInTotalStorage('editor', 'last.selected.database', newValue);
         if (previousDatabase !== null && previousDatabase !== newValue) {
-          huePubSub.publish('editor.refresh.statement.locations', this);
+          huePubSub.publish(REFRESH_STATEMENT_LOCATIONS_EVENT, this.id());
         }
         previousDatabase = newValue;
       }
@@ -355,6 +356,12 @@ export default class Snippet {
       huePubSub.publish(REFRESH_STATEMENT_LOCATIONS_EVENT, this);
     };
 
+    huePubSub.subscribe(CURSOR_POSITION_CHANGED_EVENT, details => {
+      if (details.editorId === this.id()) {
+        this.aceCursorPosition(details.position);
+      }
+    });
+
     huePubSub.subscribe(
       ACTIVE_STATEMENT_CHANGED_EVENT,
       statementDetails => {

+ 9 - 5
desktop/core/src/desktop/js/ko/bindings/ace/aceLocationHandler.js

@@ -36,6 +36,7 @@ import {
 
 export const REFRESH_STATEMENT_LOCATIONS_EVENT = 'editor.refresh.statement.locations';
 export const ACTIVE_STATEMENT_CHANGED_EVENT = 'editor.active.statement.changed';
+export const CURSOR_POSITION_CHANGED_EVENT = 'editor.cursor.position.changed';
 
 const STATEMENT_COUNT_AROUND_ACTIVE = 10;
 
@@ -421,7 +422,7 @@ class AceLocationHandler {
             }
           } else if (token.syntaxError) {
             huePubSub.publish('sql.syntax.dropdown.show', {
-              snippet: self.snippet,
+              editorId: self.snippet.id(),
               data: token.syntaxError,
               editor: self.editor,
               range: range,
@@ -634,7 +635,10 @@ class AceLocationHandler {
           lastCursorPosition.row !== newCursorPosition.row ||
           lastCursorPosition.column !== newCursorPosition.column
         ) {
-          self.snippet.aceCursorPosition(newCursorPosition);
+          huePubSub.publish(CURSOR_POSITION_CHANGED_EVENT, {
+            editorId: self.snippet.id(),
+            position: newCursorPosition
+          });
           lastCursorPosition = newCursorPosition;
         }
 
@@ -667,8 +671,8 @@ class AceLocationHandler {
       }
     });
 
-    const locateSubscription = huePubSub.subscribe(REFRESH_STATEMENT_LOCATIONS_EVENT, snippet => {
-      if (snippet === self.snippet) {
+    const locateSubscription = huePubSub.subscribe(REFRESH_STATEMENT_LOCATIONS_EVENT, editorId => {
+      if (editorId === self.snippet.id()) {
         cursorChangePaused = true;
         window.clearTimeout(changeThrottle);
         window.clearTimeout(updateThrottle);
@@ -842,7 +846,7 @@ class AceLocationHandler {
       }
     });
 
-    huePubSub.publish('editor.refresh.statement.locations', self.snippet);
+    huePubSub.publish(REFRESH_STATEMENT_LOCATIONS_EVENT, self.snippet.id());
   }
 
   detachSqlSyntaxWorker() {

+ 5 - 3
desktop/core/src/desktop/js/ko/bindings/ace/ko.aceEditor.js

@@ -19,7 +19,9 @@ import * as ko from 'knockout';
 import ace from 'ext/aceHelper';
 
 import apiHelper from 'api/apiHelper';
-import AceLocationHandler from 'ko/bindings/ace/aceLocationHandler';
+import AceLocationHandler, {
+  REFRESH_STATEMENT_LOCATIONS_EVENT
+} from 'ko/bindings/ace/aceLocationHandler';
 import huePubSub from 'utils/huePubSub';
 import AceGutterHandler from 'ko/bindings/ace/aceGutterHandler';
 import { registerBinding } from 'ko/bindings/bindingUtils';
@@ -720,12 +722,12 @@ registerBinding(NAME, {
               editor.getCursorPosition().column - (questionMarkMatch[1].length - 1)
             );
             editor.removeTextBeforeCursor(1);
-            huePubSub.publish('editor.refresh.statement.locations', snippet);
+            huePubSub.publish(REFRESH_STATEMENT_LOCATIONS_EVENT, snippet.id());
             window.setTimeout(() => {
               editor.execCommand('startAutocomplete');
             }, 1);
           } else if (/\.$/.test(textBeforeCursor)) {
-            huePubSub.publish('editor.refresh.statement.locations', snippet);
+            huePubSub.publish(REFRESH_STATEMENT_LOCATIONS_EVENT, snippet.id());
             window.setTimeout(() => {
               editor.execCommand('startAutocomplete');
             }, 1);

+ 1 - 1
desktop/core/src/desktop/js/sql/sqlAutocompleter.js

@@ -122,7 +122,7 @@ class SqlAutocompleter {
             locations &&
             this.snippet.ace().lastChangeTime !== locations.editorChangeTime
           ) {
-            huePubSub.publish('editor.refresh.statement.locations', this.snippet);
+            huePubSub.publish('editor.refresh.statement.locations', this.snippet.id());
           }
         },
         this.snippet

+ 1 - 1
desktop/core/src/desktop/templates/sql_syntax_dropdown.mako

@@ -69,7 +69,7 @@ from django.utils.translation import ugettext as _
             var suppressedRules = window.apiHelper.getFromTotalStorage('hue.syntax.checker', 'suppressedRules', {});
             suppressedRules[newValue.suppressRule] = true;
             window.apiHelper.setInTotalStorage('hue.syntax.checker', 'suppressedRules', suppressedRules);
-            huePubSub.publish('editor.refresh.statement.locations', params.snippet);
+            huePubSub.publish('editor.refresh.statement.locations', params.editorId);
           } else {
             params.editor.session.replace(params.range, newValue);
           }