Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(sql-editor): Support CMD+Up/Down arrow to find previous/next commands (#4417)

Liu Ji7a4d6852f2c9
diff --git a/frontend/src/components/MonacoEditor/useMonaco.ts b/frontend/src/components/MonacoEditor/useMonaco.ts
index f50225888d..d73ea57293 100644
--- a/frontend/src/components/MonacoEditor/useMonaco.ts
+++ b/frontend/src/components/MonacoEditor/useMonaco.ts
@@ -61,8 +61,6 @@ export const useMonaco = async () => {
forceMoveMarkers: true,
},
]);
- // reset the selection
- editorInstance.setSelection(new monaco.Range(0, 0, 0, 0));
};
const formatContent = (
diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts
index eb1f84065b..8609b4c34d 100644
--- a/frontend/src/utils/index.ts
+++ b/frontend/src/utils/index.ts
@@ -23,3 +23,4 @@ export * from "./pev2";
export * from "./web-storage";
export * from "./dom";
export * from "./schemaEditor";
+export * from "./math";
diff --git a/frontend/src/utils/math.ts b/frontend/src/utils/math.ts
new file mode 100644
index 0000000000..35b27be1fe
--- /dev/null
+++ b/frontend/src/utils/math.ts
@@ -0,0 +1,5 @@
+export const minmax = (value: number, min: number, max: number) => {
+ if (value < min) return min;
+ if (value > max) return max;
+ return value;
+};
diff --git a/frontend/src/views/sql-editor/TerminalPanel/CompactSQLEditor.vue b/frontend/src/views/sql-editor/TerminalPanel/CompactSQLEditor.vue
index 083bb78d6d..a86dd1d44b 100644
--- a/frontend/src/views/sql-editor/TerminalPanel/CompactSQLEditor.vue
+++ b/frontend/src/views/sql-editor/TerminalPanel/CompactSQLEditor.vue
@@ -53,6 +53,7 @@ const emit = defineEmits<{
config: ExecuteConfig,
option?: ExecuteOption
): void;
+ (e: "history", direction: "up" | "down"): void;
}>();
const MIN_EDITOR_HEIGHT = 40; // ~= 1 line
@@ -144,12 +145,22 @@ const handleSaveSheet = () => {
const handleEditorReady = async () => {
const monaco = await import("monaco-editor");
const editor = editorRef.value?.editorInstance;
+ const readonly = editor?.createContextKey<boolean>(
+ "readonly",
+ props.readonly
+ );
+ watch(
+ () => props.readonly,
+ () => readonly?.set(props.readonly)
+ );
+
editor?.addAction({
id: "RunQuery",
label: "Run Query",
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter],
contextMenuGroupId: "operation",
contextMenuOrder: 0,
+ precondition: "!readonly",
run: async () => {
emit("execute", props.sql, {
databaseType: selectedInstanceEngine.value,
@@ -163,6 +174,7 @@ const handleEditorReady = async () => {
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyE],
contextMenuGroupId: "operation",
contextMenuOrder: 0,
+ precondition: "!readonly",
run: async () => {
emit(
"execute",
@@ -206,7 +218,6 @@ const handleEditorReady = async () => {
}
cursorAtLast?.set(false);
});
-
editor?.addCommand(
monaco.KeyCode.Enter,
() => {
@@ -221,7 +232,56 @@ const handleEditorReady = async () => {
},
// Tell the editor this should be only
// triggered when both of the two conditions are satisfied.
- "endsWithSemicolon && cursorAtLast"
+ "!readonly && endsWithSemicolon && cursorAtLast"
+ );
+
+ const cursorAtFirstLine = editor?.createContextKey<boolean>(
+ "cursorAtFirstLine",
+ false
+ );
+ const cursorAtLastLine = editor?.createContextKey<boolean>(
+ "cursorAtLastLine",
+ false
+ );
+ const updateCursorPosition = () => {
+ if (!editor) return;
+ const model = editor.getModel();
+ if (model) {
+ const maxLine = model.getLineCount();
+ const cursor = editor.getPosition();
+ cursorAtFirstLine?.set(cursor?.lineNumber === 1);
+ cursorAtLastLine?.set(cursor?.lineNumber === maxLine);
+ return;
+ }
+ cursorAtLast?.set(false);
+ };
+ updateCursorPosition();
+ editor?.onDidChangeCursorPosition(updateCursorPosition);
+ editor?.addCommand(
+ monaco.KeyMod.CtrlCmd | monaco.KeyCode.UpArrow,
+ () => {
+ // When
+ // - the cursor is at the first line
+ // - then press "CtrlCmd + Up"
+ // We trigger the "history" event
+ emit("history", "up");
+ },
+ // Tell the editor this should be only
+ // triggered when both of the two conditions are satisfied.
+ "!readonly && !suggestWidgetVisible && cursorAtFirstLine"
+ );
+ editor?.addCommand(
+ monaco.KeyMod.CtrlCmd | monaco.KeyCode.DownArrow,
+ () => {
+ // When
+ // - the cursor is at the last line
+ // - then press "CtrlCmd + Down"
+ // We trigger the "history" event
+ emit("history", "down");
+ },
+ // Tell the editor this should be only
+ // triggered when both of the two conditions are satisfied.
+ "!readonly && !suggestWidgetVisible && cursorAtLastLine"
);
watchEffect(async () => {
diff --git a/frontend/src/views/sql-editor/TerminalPanel/TerminalPanel.vue b/frontend/src/views/sql-editor/TerminalPanel/TerminalPanel.vue
index 89f22b3b09..3d004c7fe9 100644
--- a/frontend/src/views/sql-editor/TerminalPanel/TerminalPanel.vue
+++ b/frontend/src/views/sql-editor/TerminalPanel/TerminalPanel.vue
@@ -22,6 +22,7 @@
class="min-h-[2rem]"
:readonly="!isEditableQueryItem(query)"
@execute="handleExecute"
+ @history="handleHistory"
/>
<div v-if="query.queryResult" class="max-h-[20rem] overflow-y-auto">
<TableView
@@ -58,7 +59,7 @@
</template>
<script lang="ts" setup>
-import { computed, ref, watch } from "vue";
+import { computed, reactive, ref, watch } from "vue";
import { useElementSize } from "@vueuse/core";
import { ExecuteConfig, ExecuteOption, WebTerminalQueryItem } from "@/types";
@@ -72,12 +73,21 @@ import {
} from "../EditorCommon";
import { useExecuteSQL } from "@/composables/useExecuteSQL";
import { useCancelableTimeout } from "@/composables/useCancelableTimeout";
+import { minmax } from "@/components/SchemaDiagram/common";
+
+type LocalState = {
+ historyIndex: number;
+};
const QUERY_TIMEOUT_MS = 5000;
const tabStore = useTabStore();
const webTerminalStore = useWebTerminalStore();
+const state = reactive<LocalState>({
+ historyIndex: -1,
+});
+
const queryList = computed(() => {
return webTerminalStore.getQueryListByTab(tabStore.currentTab);
});
@@ -141,6 +151,34 @@ const handleExecute = async (
}
};
+watch(
+ () => queryList.value.length,
+ (len) => {
+ state.historyIndex = len - 1;
+ },
+ { immediate: true }
+);
+const handleHistory = (direction: "up" | "down") => {
+ if (currentQuery.value.status !== "IDLE") {
+ return;
+ }
+ const list = queryList.value;
+ const delta = direction === "up" ? -1 : 1;
+ const nextIndex = minmax(state.historyIndex + delta, 0, list.length - 1);
+ if (nextIndex === state.historyIndex) {
+ return;
+ }
+ if (nextIndex === list.length - 1) {
+ currentQuery.value.sql = "";
+ } else {
+ const historyQuery = list[nextIndex];
+ if (historyQuery) {
+ currentQuery.value.sql = historyQuery.sql;
+ }
+ }
+ state.historyIndex = nextIndex;
+};
+
const handleCancelQuery = async () => {
queryTimer.stop();
currentQuery.value.status = "CANCELLED";