浏览代码

[metadata] Add esc handler to cancel predictions and shift-tab to force indent

Johan Ahlen 4 年之前
父节点
当前提交
f36d8985b9
共有 1 个文件被更改,包括 28 次插入5 次删除
  1. 28 5
      desktop/core/src/desktop/js/apps/editor/components/aceEditor/acePredict.ts

+ 28 - 5
desktop/core/src/desktop/js/apps/editor/components/aceEditor/acePredict.ts

@@ -51,6 +51,7 @@ export const attachPredictTypeahead = (editor: Ace.Editor, connector: Connector)
   };
 
   let activePredictPromise: CancellablePromise<PredictResponse> | undefined;
+  let lastPrediction: string | undefined;
 
   const updatePredictTypeahead = () => {
     if (activePredictPromise) {
@@ -73,12 +74,15 @@ export const attachPredictTypeahead = (editor: Ace.Editor, connector: Connector)
             afterCursor: editor.getTextAfterCursor()
           })
           .then(({ prediction }) => {
-            const beforeCursor = editor.getTextBeforeCursor();
-            if (prediction && prediction.toLowerCase().startsWith(beforeCursor.toLowerCase())) {
-              setActivePredict(beforeCursor + prediction.slice(beforeCursor.length));
-            } else {
-              removeActivePredict();
+            if (prediction !== lastPrediction) {
+              const beforeCursor = editor.getTextBeforeCursor();
+              if (prediction && prediction.toLowerCase().startsWith(beforeCursor.toLowerCase())) {
+                setActivePredict(beforeCursor + prediction.slice(beforeCursor.length));
+              } else {
+                removeActivePredict();
+              }
             }
+            lastPrediction = prediction;
           })
           .catch(() => {
             removeActivePredict();
@@ -102,6 +106,25 @@ export const attachPredictTypeahead = (editor: Ace.Editor, connector: Connector)
     scrollIntoView: 'selectionPart'
   });
 
+  editor.commands.addCommand({
+    name: 'forceIndent',
+    bindKey: { win: 'Shift-Tab', mac: 'Shift-Tab' },
+    exec: () => {
+      removeActivePredict();
+      editor.indent();
+    },
+    multiSelectAction: 'forEach',
+    scrollIntoView: 'selectionPart'
+  });
+
+  editor.commands.addCommand({
+    name: 'cancelPredict',
+    bindKey: { win: 'Escape', mac: 'Escape' },
+    exec: () => {
+      removeActivePredict();
+    }
+  });
+
   const predictOnInput = () => {
     updatePredictTypeahead();
   };