🐛 fix(query-editor): 限制快捷键仅执行选中SQL

Fixes #596
This commit is contained in:
Syngnat
2026-06-27 10:36:46 +08:00
parent bf51003c66
commit a24f4a2bc1
2 changed files with 132 additions and 5 deletions

View File

@@ -2573,7 +2573,9 @@ const QueryEditor: React.FC<{ tab: TabData; isActive?: boolean }> = ({ tab, isAc
label: buildQueryEditorMonacoActionLabel('app.shortcuts.action.runQuery.label'),
keybindings: [keyBinding.keyMod | keyBinding.keyCode],
run: () => {
window.dispatchEvent(new CustomEvent('gonavi:run-active-query'));
window.dispatchEvent(new CustomEvent('gonavi:run-active-query', {
detail: { requireSelection: true },
}));
},
});
}
@@ -4629,6 +4631,14 @@ const QueryEditor: React.FC<{ tab: TabData; isActive?: boolean }> = ({ tab, isAc
}
};
const handleRunSelectedShortcut = async () => {
if (!getSelectedSQL().trim()) {
message.info(translate('query_editor.message.no_selectable_sql'));
return;
}
await handleRun();
};
const handleCancel = async () => {
if (!currentQueryIdRef.current) {
message.warning(translate('query_editor.message.cancel_no_running'));
@@ -4710,7 +4720,7 @@ const QueryEditor: React.FC<{ tab: TabData; isActive?: boolean }> = ({ tab, isAc
}
event.preventDefault();
event.stopPropagation();
void handleRun();
void handleRunSelectedShortcut();
};
window.addEventListener('keydown', handleRunShortcut, true);
@@ -4758,7 +4768,9 @@ const QueryEditor: React.FC<{ tab: TabData; isActive?: boolean }> = ({ tab, isAc
label: buildQueryEditorMonacoActionLabel('app.shortcuts.action.runQuery.label'),
keybindings: [keyBinding.keyMod | keyBinding.keyCode],
run: () => {
window.dispatchEvent(new CustomEvent('gonavi:run-active-query'));
window.dispatchEvent(new CustomEvent('gonavi:run-active-query', {
detail: { requireSelection: true },
}));
},
});
}
@@ -4882,10 +4894,14 @@ const QueryEditor: React.FC<{ tab: TabData; isActive?: boolean }> = ({ tab, isAc
}, [languagePreference, toggleQueryResultsPanelShortcutBinding, toggleResultPanelVisibility]);
useEffect(() => {
const handleRunActiveQuery = () => {
const handleRunActiveQuery = (event: Event) => {
if (!isActive) {
return;
}
if ((event as CustomEvent<{ requireSelection?: boolean }>).detail?.requireSelection) {
void handleRunSelectedShortcut();
return;
}
void handleRun();
};
@@ -4893,7 +4909,7 @@ const QueryEditor: React.FC<{ tab: TabData; isActive?: boolean }> = ({ tab, isAc
return () => {
window.removeEventListener('gonavi:run-active-query', handleRunActiveQuery as EventListener);
};
}, [isActive, handleRun]);
}, [isActive, handleRun, handleRunSelectedShortcut]);
// 监听由 TabManager 分发的专用注入事件
useEffect(() => {