From aa2fe831b627d584d7964b6b42d68822c51f743b Mon Sep 17 00:00:00 2001 From: lxk955 <164191365+lxk955@users.noreply.github.com> Date: Sat, 3 Oct 2026 23:44:45 +0800 Subject: [PATCH] feat(editor): add fold-all shortcuts and toolbar actions --- .../editor/EditorSettingsDialog.vue | 2 + .../src/components/editor/QueryEditor.vue | 43 +++++++++- .../editor/QueryEditorContextMenu.vue | 59 ++++++++++++- ...yEditor.componentSplit.integration.spec.ts | 18 ++++ ...ryEditor.largeDocument.integration.spec.ts | 39 ++++++++- ...QueryEditorContextMenu.integration.spec.ts | 24 ++++++ .../editor/queryEditorCodeMirrorRuntime.ts | 10 ++- .../src/components/layout/ContentArea.vue | 10 +++ .../src/components/layout/EditorGroup.vue | 2 + .../src/components/layout/EditorToolbar.vue | 37 ++++++++ .../components/layout/QueryEditorSurface.vue | 2 + .../__tests__/EditorToolbar.mount.spec.ts | 61 ++++++++++++++ .../src/components/layout/querySurfaces.ts | 2 + apps/desktop/src/i18n/locales/az.ts | 8 ++ apps/desktop/src/i18n/locales/en.ts | 8 ++ apps/desktop/src/i18n/locales/es.ts | 8 ++ apps/desktop/src/i18n/locales/id.ts | 8 ++ apps/desktop/src/i18n/locales/it.ts | 8 ++ apps/desktop/src/i18n/locales/ja.ts | 8 ++ apps/desktop/src/i18n/locales/ko.ts | 9 ++ apps/desktop/src/i18n/locales/pt-BR.ts | 8 ++ apps/desktop/src/i18n/locales/ru.ts | 8 ++ apps/desktop/src/i18n/locales/tr.ts | 8 ++ apps/desktop/src/i18n/locales/zh-CN.ts | 8 ++ apps/desktop/src/i18n/locales/zh-TW.ts | 8 ++ .../editor/queryEditorFoldKeymapMac.spec.ts | 84 +++++++++++++++++++ .../__tests__/editor/shortcutRegistry.spec.ts | 17 ++++ .../src/lib/editor/queryEditorFoldKeymap.ts | 12 +++ .../src/lib/editor/shortcutRegistry.ts | 22 +++++ 29 files changed, 535 insertions(+), 6 deletions(-) create mode 100644 apps/desktop/src/lib/__tests__/editor/queryEditorFoldKeymapMac.spec.ts create mode 100644 apps/desktop/src/lib/editor/queryEditorFoldKeymap.ts diff --git a/apps/desktop/src/components/editor/EditorSettingsDialog.vue b/apps/desktop/src/components/editor/EditorSettingsDialog.vue index 2202bafa33..c2a67cecbc 100644 --- a/apps/desktop/src/components/editor/EditorSettingsDialog.vue +++ b/apps/desktop/src/components/editor/EditorSettingsDialog.vue @@ -2108,6 +2108,8 @@ const formatterEditorShortcutIds: ShortcutActionId[] = [ "uppercaseSelection", "lowercaseSelection", "toggleFold", + "foldAll", + "unfoldAll", ]; const formatterEditorShortcutDefinitions = computed(() => formatterEditorShortcutIds.map((id) => SHORTCUT_DEFINITIONS.find((definition) => definition.id === id)).filter((definition): definition is (typeof SHORTCUT_DEFINITIONS)[number] => !!definition)); const filteredShortcutDefinitions = computed(() => { diff --git a/apps/desktop/src/components/editor/QueryEditor.vue b/apps/desktop/src/components/editor/QueryEditor.vue index d7d82c7a34..bfe58f40e8 100644 --- a/apps/desktop/src/components/editor/QueryEditor.vue +++ b/apps/desktop/src/components/editor/QueryEditor.vue @@ -102,6 +102,7 @@ import { buildQueryEditorLineNumbersExtension, createQueryEditorLineNumberAlignm import { keepGuttersAttachedDuringSync } from "@/lib/editor/codemirrorGutterSync"; import { searchKeymapWithoutModD } from "@/lib/editor/codemirrorSearchKeymap"; import { defaultKeymapForGlobalShortcuts } from "@/lib/editor/codemirrorDefaultKeymap"; +import { createQueryEditorFoldShortcutBindings, foldKeymapWithoutAllBindings } from "@/lib/editor/queryEditorFoldKeymap"; import { createShowWhitespaceExtension } from "@/lib/editor/codemirrorShowWhitespace"; import { clampEditorFontSize, createEditorWheelZoomGestureGuard, createEditorZoomCommitScheduler, fontSizeFromGestureScale, fontSizeFromWheelDelta } from "@/lib/editor/editorZoom"; @@ -1216,6 +1217,15 @@ const contextMenuActions: QueryEditorContextMenuActions = { sendSelectionToAi: () => { if (selectedSql.value.trim()) emit("sendSelectionToAi", selectedSql.value); }, + toggleFoldFromContextMenu: () => { + toggleFold(); + }, + foldAllFromContextMenu: () => { + foldAll(); + }, + unfoldAllFromContextMenu: () => { + unfoldAll(); + }, }; function getContextMenuState(): QueryEditorContextMenuState { @@ -1383,6 +1393,7 @@ function runKeymapExtension(codeMirrorKeymap: (typeof import("@codemirror/view") return codeMirrorRuntime.codeMirrorToggleBlockComment?.(view) ?? false; }), ...binding(shortcuts.toggleFold, (view) => codeMirrorRuntime.codeMirrorToggleFold?.(view) ?? false), + ...createQueryEditorFoldShortcutBindings(shortcuts, foldAllForView, (view) => codeMirrorRuntime.codeMirrorUnfoldAll?.(view) ?? false), ...binding(shortcuts.exPasteSqlInCondition, () => { if (!supportsSqlInListPaste(props.databaseType)) return false; void pasteClipboardAsSqlInCondition(); @@ -1978,7 +1989,7 @@ const codeMirrorLifecycle = useQueryEditorCodeMirror({ // Vim must be mounted before DBX/default keymaps so normal-mode keys are handled first. initializedRuntime.vimModeComp.of(vimModeExtension(initialSettings.vimModeEnabled)), initializedRuntime.defaultKeymapComp.of(defaultKeymapExtension()), - keymap.of([...searchKeymapWithoutModD(searchKeymap), ...historyKeymap, ...foldKeymap, ...completionKeymap]), + keymap.of([...searchKeymapWithoutModD(searchKeymap), ...historyKeymap, ...foldKeymapWithoutAllBindings(foldKeymap, initializedRuntime.codeMirrorFoldAll, initializedRuntime.codeMirrorUnfoldAll), ...completionKeymap]), Prec.highest(keymap.of([{ key: "Space", run: acceptSqlServerCompletionOnSpace }])), initializedRuntime.sqlLanguageComp.of(sqlExtensions.buildSqlLanguageExtension()), initializedRuntime.sqlSemanticHighlightComp.of(sqlExtensions.buildSqlSemanticHighlightExtension()), @@ -2738,6 +2749,33 @@ function cancelGutterExecutionViewport(requestId: number) { return executionViewportOwnership.cancelPendingRequest(requestId); } +function toggleFold(): boolean { + if (!view.value) return false; + return codeMirrorRuntime.codeMirrorToggleFold?.(view.value) ?? false; +} + +function foldAllForView(currentView: EditorViewType): boolean { + const command = codeMirrorRuntime.codeMirrorFoldAll; + if (!command) return false; + if (!shouldUseQueryEditorLargeDocumentModeForSize(currentView.state.doc.length, currentView.state.doc.lines)) return command(currentView); + const expectedDoc = currentView.state.doc; + void statementBoundaries.ensureStatementCache(currentView.state).then((result) => { + if (!result || view.value !== currentView || currentView.state.doc !== expectedDoc) return; + command(currentView); + }); + return true; +} + +function foldAll(): boolean { + if (!view.value) return false; + return foldAllForView(view.value); +} + +function unfoldAll(): boolean { + if (!view.value) return false; + return codeMirrorRuntime.codeMirrorUnfoldAll?.(view.value) ?? false; +} + function shouldBlockExecutionShortcut(event?: KeyboardEvent, currentView: EditorViewType | null = view.value): boolean { return (currentView ? isEditorComposing(currentView) : false) || (event ? postCompositionKeyGuard.blocks(event) : false); } @@ -2759,6 +2797,9 @@ defineExpose({ focusErrorPosition, previewStatementRange, refreshCompletionCache, + toggleFold, + foldAll, + unfoldAll, }); diff --git a/apps/desktop/src/components/editor/QueryEditorContextMenu.vue b/apps/desktop/src/components/editor/QueryEditorContextMenu.vue index d022c6992d..e1ed67c619 100644 --- a/apps/desktop/src/components/editor/QueryEditorContextMenu.vue +++ b/apps/desktop/src/components/editor/QueryEditorContextMenu.vue @@ -1,7 +1,37 @@ diff --git a/apps/desktop/src/components/layout/EditorGroup.vue b/apps/desktop/src/components/layout/EditorGroup.vue index 8d823afa65..4ca64214d5 100644 --- a/apps/desktop/src/components/layout/EditorGroup.vue +++ b/apps/desktop/src/components/layout/EditorGroup.vue @@ -215,6 +215,8 @@ const groupExecutableSql = computed(() => { @explain="activeTab && toolbar.explain(activeTab.id)" @format-sql="activeTab && toolbar.formatSql(activeTab.id)" @compress-sql="activeTab && toolbar.compressSql(activeTab.id)" + @fold-all="activeSurfaceRef?.foldAll?.()" + @unfold-all="activeSurfaceRef?.unfoldAll?.()" @toggle-sql-keyword-case="toolbar.toggleSqlKeywordCase()" @save-sql="(tabId: string) => toolbar.saveSql(tabId)" @open-sql="toolbar.openSqlFile()" diff --git a/apps/desktop/src/components/layout/EditorToolbar.vue b/apps/desktop/src/components/layout/EditorToolbar.vue index f2e917e556..5eabe16365 100644 --- a/apps/desktop/src/components/layout/EditorToolbar.vue +++ b/apps/desktop/src/components/layout/EditorToolbar.vue @@ -21,6 +21,8 @@ import { AlertTriangle, ClipboardPaste, Minimize2, + FoldVertical, + UnfoldVertical, SpellCheck2, Layers, MoreHorizontal, @@ -99,6 +101,8 @@ const emit = defineEmits<{ "update:explainMode": [mode: "explain" | "autotrace"]; formatSql: []; compressSql: []; + foldAll: []; + unfoldAll: []; toggleSqlKeywordCase: []; saveSql: [tabId: string]; openSql: []; @@ -459,6 +463,15 @@ const canFormatSql = computed(() => canFormatSqlForDatabaseType(props.activeConn const showFormatButton = computed(() => canFormatSql.value && toolbarTier.value < 2); const showExplainAnalyzeToggle = computed(() => toolbarTier.value < 3); const showCompressButton = computed(() => toolbarTier.value < 1); +const showFoldButtons = computed(() => toolbarTier.value < 1); +const foldAllTooltip = computed(() => { + const shortcut = formatShortcutDisplay(settingsStore.editorSettings.shortcuts.foldAll); + return shortcut ? `${t("toolbar.foldAll")} (${shortcut})` : t("toolbar.foldAll"); +}); +const unfoldAllTooltip = computed(() => { + const shortcut = formatShortcutDisplay(settingsStore.editorSettings.shortcuts.unfoldAll); + return shortcut ? `${t("toolbar.unfoldAll")} (${shortcut})` : t("toolbar.unfoldAll"); +}); const showKeywordCaseButton = computed(() => toolbarTier.value < 1); const showWordWrapButton = computed(() => toolbarTier.value < 1); const showSemanticDiagnosticsButton = computed(() => supportsSqlSemanticDiagnosticsToggle.value && toolbarTier.value < 1); @@ -616,6 +629,22 @@ async function changeCatalog(selectedCatalog: string) { {{ t("toolbar.compressSql") }} + + + + + {{ foldAllTooltip }} + + + + + + {{ unfoldAllTooltip }} +