fix: stabilize sql execution shortcuts (#29)

Co-authored-by: zihan.wu <zihan.wu@erry.com>
This commit is contained in:
Bacon2994
2026-04-30 17:12:54 +08:00
committed by GitHub
co-authored by zihan.wu
parent a523b97e28
commit 6b97f280ec
4 changed files with 67 additions and 4 deletions
+19 -3
View File
@@ -45,6 +45,7 @@ import { getCurrentWebview } from "@tauri-apps/api/webview";
import * as api from "@/lib/tauri";
import { resolveExecutableSql } from "@/lib/sqlExecutionTarget";
import type { SqlFormatDialect } from "@/lib/sqlFormatter";
import { isCloseTabShortcut, isExecuteSqlShortcut } from "@/lib/keyboardShortcuts";
const { t } = useI18n();
const connectionStore = useConnectionStore();
@@ -508,12 +509,27 @@ function openLatestRelease() {
open(url);
}
function isQueryEditorTarget(target: EventTarget | null): boolean {
return target instanceof Element && !!target.closest("[data-query-editor-root]");
}
function handleKeydown(e: KeyboardEvent) {
if (e.metaKey && e.key === "w") {
if (isCloseTabShortcut(e)) {
e.preventDefault();
if (queryStore.activeTabId) {
queryStore.closeTab(queryStore.activeTabId);
}
return;
}
if (
activeTab.value?.mode === "query"
&& isExecuteSqlShortcut(e)
&& isQueryEditorTarget(e.target)
) {
e.preventDefault();
e.stopPropagation();
tryExecute();
}
}
@@ -521,13 +537,13 @@ onMounted(() => {
applyTheme();
connectionStore.initFromDisk();
settingsStore.initAiConfig();
window.addEventListener("keydown", handleKeydown);
window.addEventListener("keydown", handleKeydown, true);
setupFileDrop();
checkUpdates({ silent: true });
});
onUnmounted(() => {
window.removeEventListener("keydown", handleKeydown);
window.removeEventListener("keydown", handleKeydown, true);
});
const DB_EXTENSIONS = [".db", ".sqlite", ".sqlite3", ".duckdb"];
+1 -1
View File
@@ -212,5 +212,5 @@ onBeforeUnmount(() => {
</script>
<template>
<div ref="editorRef" class="h-full w-full overflow-hidden" />
<div ref="editorRef" data-query-editor-root class="h-full w-full overflow-hidden" />
</template>
+20
View File
@@ -0,0 +1,20 @@
export interface ShortcutLikeEvent {
key: string;
metaKey?: boolean;
ctrlKey?: boolean;
altKey?: boolean;
isComposing?: boolean;
}
export function isExecuteSqlShortcut(event: ShortcutLikeEvent): boolean {
if (event.isComposing) return false;
if (event.altKey) return false;
if (!event.metaKey && !event.ctrlKey) return false;
return event.key === "Enter";
}
export function isCloseTabShortcut(event: ShortcutLikeEvent): boolean {
if (event.isComposing) return false;
if (!event.metaKey) return false;
return event.key.toLowerCase() === "w";
}