浏览代码

[editor] Add support for dropping text in the editor Vue component

Johan Ahlen 5 年之前
父节点
当前提交
0f4c24a72e

+ 30 - 0
desktop/core/src/desktop/js/apps/notebook2/components/aceEditor/AceEditor.vue

@@ -24,6 +24,7 @@
 </template>
 
 <script lang="ts">
+  import { INSERT_AT_CURSOR_EVENT } from 'ko/bindings/ace/ko.aceEditor';
   import AceAutocomplete from './autocomplete/AceAutocomplete.vue';
   import $ from 'jquery';
   import { EditorInterpreter } from 'types/config';
@@ -331,10 +332,39 @@
 
       editor.$blockScrolling = Infinity;
 
+      this.subTracker.subscribe(
+        INSERT_AT_CURSOR_EVENT,
+        (details: { text: string; targetEditor: Ace.Editor; cursorEndAdjust?: number }): void => {
+          if (details.targetEditor === editor) {
+            this.insertSqlAtCursor(details.text, details.cursorEndAdjust);
+          }
+        }
+      );
+
       this.editor = editor;
       this.$emit('ace-created', editor);
     }
 
+    insertSqlAtCursor(text: string, cursorEndAdjust?: number): void {
+      if (!this.editor) {
+        return;
+      }
+
+      const before = this.editor.getTextBeforeCursor();
+
+      const textToInsert = /\S+$/.test(before) ? ' ' + text : text;
+      this.editor.session.insert(this.editor.getCursorPosition(), textToInsert);
+      if (cursorEndAdjust) {
+        const positionAfterInsert = this.editor.getCursorPosition();
+        this.editor.moveCursorToPosition({
+          row: positionAfterInsert.row,
+          column: positionAfterInsert.column + cursorEndAdjust
+        });
+      }
+      this.editor.clearSelection();
+      this.editor.focus();
+    }
+
     destroyed(): void {
       this.subTracker.dispose();
     }

+ 3 - 0
desktop/core/src/desktop/js/ext/ace.d.ts

@@ -21,6 +21,7 @@ declare namespace Ace {
     $blockScrolling: number;
     addError(message:string, line:number): void;
     addWarning(message:string, line:number): void;
+    clearSelection(): void;
     container: HTMLElement;
     commands: {
       addCommand(command: {
@@ -49,6 +50,7 @@ declare namespace Ace {
       removeKeyboardHandler(hashHandler: HashHandler): void;
     }
     lastChangeTime: number;
+    moveCursorToPosition(position: Position): void;
     off(ev: string, callback: ((e: any) => any) | number): void;
     on(event: string, fn: (e: any) => any): number;
     removeTextAfterCursor(length: number): void;
@@ -138,6 +140,7 @@ declare namespace Ace {
     getTextRange(range: SimpleRange): string;
     getTokenAt(row: number, column: number): HueToken | null;
     getTokens(line?: number): HueToken[];
+    insert(position: Position, text: string): void;
     remove(range: Range): void;
     removeGutterDecoration(line: number, clazz: string): void;
     removeMarker(markerId: number): void;