Selaa lähdekoodia

[editor] Port Ace editor v1 to use the new js formatter

Some copy paste from Editor but unclear on how to have a single formatter function.
Romain Rigaux 4 vuotta sitten
vanhempi
commit
66a87fd415

+ 1 - 1
desktop/core/src/desktop/js/apps/editor/components/ko.snippetEditorActions.js

@@ -47,7 +47,7 @@ const TEMPLATE = `
         <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-fw fa-link"></i> ${I18n('Get shareable link')}
+          <i class="fa fa-fw fa-link"></i> ${I18n('Shareable link')}
         </a>
       </li>
       <!-- /ko -->

+ 11 - 31
desktop/core/src/desktop/js/ko/bindings/ace/ko.aceEditor.js

@@ -17,6 +17,7 @@
 import $ from 'jquery';
 import * as ko from 'knockout';
 import ace from 'ext/aceHelper';
+import { format } from '@gethue/sql-formatter';
 
 import AceLocationHandler, {
   REFRESH_STATEMENT_LOCATIONS_EVENT
@@ -519,37 +520,16 @@ registerBinding(NAME, {
         mac: 'Command-i|Ctrl-i|Ctrl-Shift-f|Command-Shift-f|Ctrl-Shift-l|Cmd-Shift-l'
       },
       exec: function () {
-        if (
-          [
-            'ace/mode/hive',
-            'ace/mode/impala',
-            'ace/mode/sql',
-            'ace/mode/mysql',
-            'ace/mode/pgsql',
-            'ace/mode/sqlite',
-            'ace/mode/oracle'
-          ].indexOf(snippet.getAceMode()) > -1
-        ) {
-          $.post(
-            '/notebook/api/format',
-            {
-              statements:
-                editor.getSelectedText() !== '' ? editor.getSelectedText() : editor.getValue()
-            },
-            data => {
-              if (data.status === 0) {
-                if (editor.getSelectedText() !== '') {
-                  editor.session.replace(
-                    editor.session.selection.getRange(),
-                    data.formatted_statements
-                  );
-                } else {
-                  editor.setValue(data.formatted_statements);
-                  snippet.statement_raw(removeUnicodes(editor.getValue()));
-                }
-              }
-            }
-          );
+        const formatted_statements = format(
+          editor.getSelectedText() !== '' ? editor.getSelectedText() : editor.getValue(),
+          { uppercase: true, linesBetweenQueries: 2, indentQuerySeparator: true }
+        );
+
+        if (editor.getSelectedText() !== '') {
+          editor.session.replace(editor.session.selection.getRange(), formatted_statements);
+        } else {
+          editor.setValue(formatted_statements);
+          snippet.statement_raw(removeUnicodes(editor.getValue()));
         }
       }
     });