fix(editor): preserve SQL selection when executing from toolbar

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-08-30 03:05:43 +08:00
1 parent 22775f3639
commit d5f4eadf7a
6 files changed
+69 -6

No files matched your search

+16 -2
View File
@@ -423,6 +423,7 @@ async function resolveActiveExecutableSql(snapshot?: SqlExecutionSnapshot) {
const blockDangerousRedisCommands = ref(true); const blockDangerousRedisCommands = ref(true);
const databaseRequiredSignal = ref(0); const databaseRequiredSignal = ref(0);
const databaseRequiredTabId = ref<string | null>(null); const databaseRequiredTabId = ref<string | null>(null);
const pendingToolbarExecutionSnapshot = ref<SqlExecutionSnapshot>();
const sqlExecutionDangerStore = useSqlExecutionDangerStore(); const sqlExecutionDangerStore = useSqlExecutionDangerStore();
const productionSafetyStore = useProductionSafetyStore(); const productionSafetyStore = useProductionSafetyStore();
@@ -466,7 +467,19 @@ const {
onExecutionStarted: (editorViewportRequestId) => contentAreaRef.value?.acceptQueryEditorExecutionViewport(editorViewportRequestId), onExecutionStarted: (editorViewportRequestId) => contentAreaRef.value?.acceptQueryEditorExecutionViewport(editorViewportRequestId),
}); });
function requestActiveEditorExecute() { function captureActiveEditorExecutionSnapshot() {
pendingToolbarExecutionSnapshot.value = contentAreaRef.value?.captureQueryEditorExecutionSnapshot?.();
}
function requestActiveEditorExecute(source?: "pointer" | "keyboard") {
const snapshot = pendingToolbarExecutionSnapshot.value;
pendingToolbarExecutionSnapshot.value = undefined;
if (source === "pointer") {
if (snapshot) {
void tryExecute(snapshot);
return;
}
}
if (contentAreaRef.value?.requestQueryEditorExecute?.()) return; if (contentAreaRef.value?.requestQueryEditorExecute?.()) return;
void tryExecute(); void tryExecute();
} }
@@ -3088,7 +3101,8 @@ onUnmounted(() => {
@commit="activeTab && queryStore.commitTransaction(activeTab.id)" @commit="activeTab && queryStore.commitTransaction(activeTab.id)"
@rollback="activeTab && queryStore.rollbackTransaction(activeTab.id)" @rollback="activeTab && queryStore.rollbackTransaction(activeTab.id)"
@dismiss-txn-rolled-back="activeTab && (activeTab.txnAutoRolledBack = false)" @dismiss-txn-rolled-back="activeTab && (activeTab.txnAutoRolledBack = false)"
@execute="requestActiveEditorExecute()" @execute-pointer-down="captureActiveEditorExecutionSnapshot()"
@execute="requestActiveEditorExecute($event)"
@multi-execute="requestMultiDbExecute()" @multi-execute="requestMultiDbExecute()"
@cancel="cancelActiveExecution()" @cancel="cancelActiveExecution()"
@explain="tryExplain()" @explain="tryExplain()"
@@ -870,6 +870,18 @@ function emitExecutionRequest(source: SqlExecutionOverride, openInNewResultTab =
} }
} }
/**
* Captures a manual selection before a toolbar click can cause a platform
* focus transition. The snapshot is immutable, so downstream execution keeps
* the exact SQL and source offsets that were visible when the button was
* pressed.
*/
function captureExecutionSnapshot(): SqlExecutionSnapshot | undefined {
const currentView = view.value;
if (!currentView || currentView.state.selection.main.empty) return undefined;
return sqlExecutionSnapshotFromView(currentView);
}
function requestExecute(options: RequestExecuteOptions = {}) { function requestExecute(options: RequestExecuteOptions = {}) {
executionViewportOwnership.cancelPendingRequest(); executionViewportOwnership.cancelPendingRequest();
const currentView = view.value; const currentView = view.value;
@@ -6525,6 +6537,7 @@ defineExpose({
shouldBlockExecutionShortcut, shouldBlockExecutionShortcut,
requestExecute, requestExecute,
requestExecuteInNewResultTab, requestExecuteInNewResultTab,
captureExecutionSnapshot,
pasteClipboardAsSqlInCondition, pasteClipboardAsSqlInCondition,
focusStatementRange, focusStatementRange,
previewStatementRange, previewStatementRange,
@@ -1123,6 +1123,10 @@ function requestQueryEditorExecute() {
return queryEditorRef.value?.requestExecute(); return queryEditorRef.value?.requestExecute();
} }
function captureQueryEditorExecutionSnapshot() {
return queryEditorRef.value?.captureExecutionSnapshot();
}
function requestQueryEditorExecuteInNewResultTab() { function requestQueryEditorExecuteInNewResultTab() {
return queryEditorRef.value?.requestExecuteInNewResultTab(); return queryEditorRef.value?.requestExecuteInNewResultTab();
} }
@@ -1175,6 +1179,7 @@ defineExpose({
refreshQueryEditorCompletionCache, refreshQueryEditorCompletionCache,
handleModRTarget, handleModRTarget,
requestQueryEditorExecute, requestQueryEditorExecute,
captureQueryEditorExecutionSnapshot,
requestQueryEditorExecuteInNewResultTab, requestQueryEditorExecuteInNewResultTab,
shouldBlockQueryEditorExecutionShortcut, shouldBlockQueryEditorExecutionShortcut,
acceptQueryEditorExecutionViewport, acceptQueryEditorExecutionViewport,
@@ -43,7 +43,8 @@ const props = defineProps<{
}>(); }>();
const emit = defineEmits<{ const emit = defineEmits<{
execute: []; execute: [source: "pointer" | "keyboard"];
executePointerDown: [];
cancel: []; cancel: [];
explain: []; explain: [];
"update:explainMode": [mode: "explain" | "autotrace"]; "update:explainMode": [mode: "explain" | "autotrace"];
@@ -251,6 +252,20 @@ function databaseOptionIsProduction(database: string): boolean {
if (!database || props.activeConnection?.is_production) return false; if (!database || props.activeConnection?.is_production) return false;
return productionContextForDatabase(props.activeConnection, database).reason === "database"; return productionContextForDatabase(props.activeConnection, database).reason === "database";
} }
function onExecutePointerDown(event: MouseEvent) {
if (props.activeTab.isExecuting || event.button !== 0) return;
emit("executePointerDown");
}
function onExecuteClick(event: MouseEvent) {
if (props.activeTab.isExecuting) {
emit("cancel");
return;
}
emit("execute", event.detail > 0 ? "pointer" : "keyboard");
}
async function changeCatalog(selectedCatalog: string) { async function changeCatalog(selectedCatalog: string) {
const connection = props.activeConnection; const connection = props.activeConnection;
if (!connection) return; if (!connection) return;
@@ -276,8 +291,8 @@ async function changeCatalog(selectedCatalog: string) {
class="h-6 w-6" class="h-6 w-6"
:class="executeButtonClass" :class="executeButtonClass"
:disabled="activeTab.isCancelling || activeTab.isExplaining || (!activeTab.isExecuting && !executableSql.trim())" :disabled="activeTab.isCancelling || activeTab.isExplaining || (!activeTab.isExecuting && !executableSql.trim())"
@mousedown.prevent @mousedown.prevent="onExecutePointerDown"
@click="activeTab.isExecuting ? emit('cancel') : emit('execute')" @click="onExecuteClick"
> >
<Loader2 v-if="activeTab.isCancelling" class="h-3.5 w-3.5 animate-spin" /> <Loader2 v-if="activeTab.isCancelling" class="h-3.5 w-3.5 animate-spin" />
<Square v-else-if="activeTab.isExecuting" class="h-3.5 w-3.5 fill-current" /> <Square v-else-if="activeTab.isExecuting" class="h-3.5 w-3.5 fill-current" />
@@ -4,6 +4,7 @@ import { createQueryEditorExecutionViewportOwnership, isQueryEditorPositionVisib
const queryEditorSource = readFileSync(new URL("../../../components/editor/QueryEditor.vue", import.meta.url), "utf8"); const queryEditorSource = readFileSync(new URL("../../../components/editor/QueryEditor.vue", import.meta.url), "utf8");
const contentAreaSource = readFileSync(new URL("../../../components/layout/ContentArea.vue", import.meta.url), "utf8"); const contentAreaSource = readFileSync(new URL("../../../components/layout/ContentArea.vue", import.meta.url), "utf8");
const editorToolbarSource = readFileSync(new URL("../../../components/layout/EditorToolbar.vue", import.meta.url), "utf8");
const appSource = readFileSync(new URL("../../../App.vue", import.meta.url), "utf8"); const appSource = readFileSync(new URL("../../../App.vue", import.meta.url), "utf8");
const sqlExecutionSource = readFileSync(new URL("../../../composables/useSqlExecution.ts", import.meta.url), "utf8"); const sqlExecutionSource = readFileSync(new URL("../../../composables/useSqlExecution.ts", import.meta.url), "utf8");
const queryStoreSource = readFileSync(new URL("../../../stores/queryStore.ts", import.meta.url), "utf8"); const queryStoreSource = readFileSync(new URL("../../../stores/queryStore.ts", import.meta.url), "utf8");
@@ -50,6 +51,21 @@ describe("QueryEditor execution routing", () => {
expect(executeModeBranch).toBeGreaterThan(selectionBranch); expect(executeModeBranch).toBeGreaterThan(selectionBranch);
}); });
it("captures the toolbar selection before the click event can change focus", () => {
expect(queryEditorSource).toContain("function captureExecutionSnapshot(): SqlExecutionSnapshot | undefined");
expect(queryEditorSource).toContain("return sqlExecutionSnapshotFromView(currentView);");
expect(contentAreaSource).toContain("function captureQueryEditorExecutionSnapshot()");
expect(contentAreaSource).toContain("queryEditorRef.value?.captureExecutionSnapshot();");
expect(appSource).toContain("const pendingToolbarExecutionSnapshot = ref<SqlExecutionSnapshot>();");
expect(appSource).toContain("pendingToolbarExecutionSnapshot.value = contentAreaRef.value?.captureQueryEditorExecutionSnapshot?.();");
expect(appSource).toContain('if (source === "pointer")');
expect(appSource).toContain("pendingToolbarExecutionSnapshot.value = undefined;");
expect(appSource).toContain("void tryExecute(snapshot);");
expect(appSource).toContain('@execute-pointer-down="captureActiveEditorExecutionSnapshot()"');
expect(appSource).toContain('@execute="requestActiveEditorExecute($event)"');
expect(editorToolbarSource).toContain("function onExecutePointerDown(event: MouseEvent)");
});
it("uses the opt-in blank-line fallback and otherwise reports the missing cursor statement", () => { it("uses the opt-in blank-line fallback and otherwise reports the missing cursor statement", () => {
expect(queryEditorSource).toContain("executeAllOnBlankLine: settingsStore.editorSettings.executeAllOnBlankLine"); expect(queryEditorSource).toContain("executeAllOnBlankLine: settingsStore.editorSettings.executeAllOnBlankLine");
expect(queryEditorSource).toContain('toast(t("editor.noExecutableStatementAtCursor"), 3000)'); expect(queryEditorSource).toContain('toast(t("editor.noExecutableStatementAtCursor"), 3000)');
@@ -51,7 +51,7 @@ describe("QueryEditor auto focus wiring", () => {
describe("QueryEditor toolbar focus", () => { describe("QueryEditor toolbar focus", () => {
it("does not move focus from the editor when clicking execute", () => { it("does not move focus from the editor when clicking execute", () => {
expect(editorToolbarSource).toMatch(/:disabled="activeTab\.isCancelling[\s\S]*?@mousedown\.prevent[\s\S]*?@click="activeTab\.isExecuting \? emit\('cancel'\) : emit\('execute'\)"/); expect(editorToolbarSource).toMatch(/:disabled="activeTab\.isCancelling[\s\S]*?@mousedown\.prevent="onExecutePointerDown"[\s\S]*?@click="onExecuteClick"/);
expect(queryEditorSource).toMatch(/function requestExecute\([\s\S]*?const currentView = view\.value;[\s\S]*?currentView\.focus\(\);[\s\S]*?requestExecuteFromView\(currentView/); expect(queryEditorSource).toMatch(/function requestExecute\([\s\S]*?const currentView = view\.value;[\s\S]*?currentView\.focus\(\);[\s\S]*?requestExecuteFromView\(currentView/);
}); });
}); });