浏览代码

HUE-9066 [gist] Port functionality to Editor 2

Romain 6 年之前
父节点
当前提交
77a41e832e

+ 26 - 0
desktop/core/src/desktop/js/api/apiHelper.js

@@ -2127,6 +2127,32 @@ class ApiHelper {
     });
   }
 
+  /**
+   *
+   * @param {Object} options
+   * @param {statement} options.statement
+   * @param {doc_type} options.doc_type
+   * @param {name} options.name
+   * @param {description} options.description
+   *
+   * @return {CancellablePromise<string>}
+   */
+  async createGistAsync(options) {
+    const data = {
+      statement: options.statement,
+      doc_type: options.doc_type,
+      name: options.name,
+      description: options.description
+    };
+    return new Promise((resolve, reject) => {
+      this.simplePost(GIST_API + 'create', data, options)
+        .done(response => {
+          resolve(response);
+        })
+        .fail(reject);
+    });
+  }
+
   /**
    *
    * @param {Object} options

+ 27 - 2
desktop/core/src/desktop/js/apps/notebook2/components/ko.snippetEditorActions.js

@@ -40,7 +40,7 @@ const TEMPLATE = `
         </a>
       </li>
       <li>
-        <a href="javascript:void(0)" data-bind="click: format, css: { 'disabled': !createGistEnabled() }" title="${I18n(
+        <a href="javascript:void(0)" data-bind="click: createGist, css: { 'disabled': !createGistEnabled() }" title="${I18n(
           'Share the query selection via a link'
         )}">
           <i class="fa fa-wf fa-link"></i> ${I18n('Share link')}
@@ -100,7 +100,9 @@ class SnippetEditorActions {
       () => this.snippet.type() === 'hive' || this.snippet.type() === 'impala'
     );
 
-    this.createGistEnabled = ko.pureComputed(() => this.snippet.statement() !== '');
+    this.createGistEnabled = ko.pureComputed(
+      () => this.snippet.isSqlDialect() && this.snippet.statement() !== ''
+    );
 
     this.explainEnabled = ko.pureComputed(
       () =>
@@ -152,6 +154,29 @@ class SnippetEditorActions {
     this.snippet.currentQueryTab('queryExplain');
   }
 
+  async createGist() {
+    if (!this.createGistEnabled()) {
+      return;
+    }
+    hueAnalytics.log('notebook', 'createGist');
+
+    const gist = await apiHelper.createGistAsync({
+      statement:
+        this.snippet.ace().getSelectedText() != '' ? this.snippet.ace().getSelectedText() : this.snippet.statement_raw(),
+      doc_type: this.snippet.type(),
+      name: this.snippet.name(),
+      description: ''
+    });
+
+    if (gist.status == 0) {
+      $(document).trigger('showGistModal', {
+        link: gist.link
+      });
+    } else {
+      this.snippet.handleAjaxError(gist);
+    }
+  }
+
   format() {
     if (!this.formatEnabled()) {
       return;

+ 7 - 3
desktop/core/src/desktop/js/apps/notebook2/editorViewModel.js

@@ -285,12 +285,15 @@ class EditorViewModel {
   init() {
     if (this.editorId) {
       this.openNotebook(this.editorId);
+    } else if (
+      window.location.getParameter('gist') !== '' ||
+      window.location.getParameter('type') !== ''
+    ) {
+      this.newNotebook(window.location.getParameter('type'));
     } else if (window.location.getParameter('editor') !== '') {
       this.openNotebook(window.location.getParameter('editor'));
     } else if (this.notebooks.length > 0) {
       this.loadNotebook(this.notebooks[0]); // Old way of loading json for /browse
-    } else if (window.location.getParameter('type') !== '') {
-      this.newNotebook(window.location.getParameter('type'));
     } else {
       this.newNotebook();
     }
@@ -361,7 +364,8 @@ class EditorViewModel {
       });
       $.post('/notebook/api/create_notebook', {
         type: editorType || this.editorType(),
-        directory_uuid: window.location.getParameter('directory_uuid')
+        directory_uuid: window.location.getParameter('directory_uuid'),
+        gist: window.location.getParameter('gist')
       })
         .then(data => {
           this.loadNotebook(data.notebook);