mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-07-01 00:51:23 +08:00
🐛 fix(query-editor): 修复外部SQL快捷保存失效
- 放宽活跃 QueryEditor 在文档级快捷键目标下的保存触发条件 - 修复桌面端 Ctrl/Cmd+S 事件落到 document 时未真正写盘的问题 - 保持普通查询保存行为不变,并补充外部 SQL 文件快捷保存回归测试
This commit is contained in:
@@ -4329,6 +4329,47 @@ describe('QueryEditor external SQL save', () => {
|
||||
expect(messageApi.success).toHaveBeenCalledWith('查询已保存!');
|
||||
});
|
||||
|
||||
it('allows Ctrl/Cmd+S to save external SQL files from document-level targets', async () => {
|
||||
const windowListeners: Record<string, ((event?: any) => void)[]> = {};
|
||||
vi.stubGlobal('window', {
|
||||
addEventListener: vi.fn((type: string, listener: (event?: any) => void) => {
|
||||
windowListeners[type] ||= [];
|
||||
windowListeners[type].push(listener);
|
||||
}),
|
||||
removeEventListener: vi.fn(),
|
||||
dispatchEvent: vi.fn(),
|
||||
});
|
||||
|
||||
const filePath = '/Users/me/Documents/gonavi-queries/report.sql';
|
||||
editorState.hasTextFocus = false;
|
||||
|
||||
await act(async () => {
|
||||
create(<QueryEditor tab={createTab({ filePath })} />);
|
||||
});
|
||||
|
||||
editorState.value = 'select 6;';
|
||||
const isMacRuntime = /(Mac|iPhone|iPad|iPod)/i.test(`${navigator.platform || ''} ${navigator.userAgent || ''}`);
|
||||
const event = {
|
||||
ctrlKey: !isMacRuntime,
|
||||
metaKey: isMacRuntime,
|
||||
altKey: false,
|
||||
shiftKey: false,
|
||||
key: 's',
|
||||
target: document.body,
|
||||
preventDefault: vi.fn(),
|
||||
stopPropagation: vi.fn(),
|
||||
};
|
||||
|
||||
await act(async () => {
|
||||
windowListeners.keydown?.forEach((listener) => listener(event));
|
||||
});
|
||||
|
||||
expect(event.preventDefault).toHaveBeenCalled();
|
||||
expect(event.stopPropagation).toHaveBeenCalled();
|
||||
expect(backendApp.WriteSQLFile).toHaveBeenCalledWith(filePath, 'select 6;');
|
||||
expect(messageApi.success).toHaveBeenCalledWith(expect.stringContaining('SQL 文件已保存'));
|
||||
});
|
||||
|
||||
it('does not create saved queries when external SQL file writes fail', async () => {
|
||||
let renderer!: ReactTestRenderer;
|
||||
const filePath = '/Users/me/Documents/gonavi-queries/report.sql';
|
||||
|
||||
@@ -4132,7 +4132,7 @@ const QueryEditor: React.FC<{ tab: TabData; isActive?: boolean }> = ({ tab, isAc
|
||||
const targetNode = resolveEventTargetNode(event.target);
|
||||
const editorHasFocus = !!editor?.hasTextFocus?.();
|
||||
const inQueryEditor = !!(targetNode && queryEditorRootRef.current?.contains(targetNode));
|
||||
if (!editorHasFocus && !inQueryEditor) {
|
||||
if (!editorHasFocus && !inQueryEditor && !isDocumentLevelShortcutTarget(targetNode)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user