From 1dd8ea9b3b642b2d40dd71d5210219cbc610f169 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Wed, 2 Dec 2026 19:16:28 +0800 Subject: [PATCH] feat(markdown): zoom the focused preview with Cmd/Ctrl+= and Cmd/Ctrl+- (#7523) With the Markdown preview focused, the editor's expand / collapse keys (Cmd/Ctrl+= and Cmd/Ctrl+-) run the new Increase / Decrease Preview Font Size actions, gated by a markdownPreviewFocus context key; in the editor they still expand and collapse folds. The Eclipse keymaps already give these keys to Increase / Decrease Font Size, which zooms the focused preview, so they leave the new actions unbound. Fixes #7380 --- src/features/viewers/index.ts | 3 +- .../viewers/markdown/MarkdownEditor.tsx | 7 +++ .../viewers/markdown/previewFontActions.ts | 22 +++++++++ src/features/viewers/markdown/zoom.test.tsx | 49 ++++++++++++++++++- src/platform/keymaps/eclipse-macos.ts | 3 ++ src/platform/keymaps/eclipse-windows.ts | 3 ++ src/platform/keymaps/intellij-macos.ts | 2 + src/platform/keymaps/intellij-windows.ts | 2 + src/platform/keymaps/vscode-macos.ts | 2 + src/platform/keymaps/vscode-windows.ts | 2 + src/platform/locales/text/en.json | 4 +- src/platform/locales/text/zh.json | 4 +- 12 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 src/features/viewers/markdown/previewFontActions.ts diff --git a/src/features/viewers/index.ts b/src/features/viewers/index.ts index 34d22546ed..8b03c51840 100644 --- a/src/features/viewers/index.ts +++ b/src/features/viewers/index.ts @@ -44,6 +44,7 @@ import { registerRstLiveSource } from "./rst/live"; import { rstModes, RstLayoutActions } from "./rst/RstLayoutActions"; import { SvgLayoutActions, svgModes } from "./svg/SvgEditor"; import { MarkdownLayoutActions } from "./markdown/MarkdownLayoutActions"; +import { markdownPreviewFontActions } from "./markdown/previewFontActions"; import { canExtractImages, extractImages, EXTRACT_IMAGES, EXTRACT_IMAGES_TITLE } from "./markdown/extractImages"; const titleOf = (input: EditorInput) => basename(input.path ?? input.key); @@ -197,7 +198,7 @@ export function activate(): () => void { items: (_menu, { paths: [path] }) => path === activeEditor()?.ctx.path && canExtractImages() ? [{ kind: "action", action: EXTRACT_IMAGES }] : [], }), - registerActions([...imageActions(), ...markdownActions()]), + registerActions([...imageActions(), ...markdownActions(), ...markdownPreviewFontActions()]), ]; return () => disposers.reverse().forEach((d) => d()); } diff --git a/src/features/viewers/markdown/MarkdownEditor.tsx b/src/features/viewers/markdown/MarkdownEditor.tsx index 9b09ac3bd2..9ebcb80042 100644 --- a/src/features/viewers/markdown/MarkdownEditor.tsx +++ b/src/features/viewers/markdown/MarkdownEditor.tsx @@ -94,6 +94,7 @@ import { writeClipboardText } from "@/platform/clipboard"; import { tx } from "@/platform/i18n"; import { isVirtualUri } from "@/platform/virtualDocs"; import { wheelZoomStep } from "@/platform/wheelZoom"; +import { setContext } from "@/platform/context"; import { iconNode } from "@/ui/iconNode"; import { create } from "zustand"; import { isMarkdownPath, isMdxPath } from "../fileKinds"; @@ -1360,6 +1361,8 @@ function MarkdownPreview({ root.removeEventListener("wheel", onWheel, { capture: true }); root.removeEventListener("wheel", onPreviewWheel); release(); + const focused = document.activeElement; + if (!focused || focused === document.body || root.contains(focused)) setContext("markdownPreviewFocus", false); }; }, [path]); @@ -1442,6 +1445,10 @@ function MarkdownPreview({ tabIndex={-1} role="document" aria-label={tx("Markdown preview")} + onFocus={() => setContext("markdownPreviewFocus", true)} + onBlur={(e) => { + if (!e.currentTarget.contains(e.relatedTarget as Node | null)) setContext("markdownPreviewFocus", false); + }} onClick={onClick} onScroll={onScroll} /> diff --git a/src/features/viewers/markdown/previewFontActions.ts b/src/features/viewers/markdown/previewFontActions.ts new file mode 100644 index 0000000000..c88e64a3f1 --- /dev/null +++ b/src/features/viewers/markdown/previewFontActions.ts @@ -0,0 +1,22 @@ +import type { Action } from "@/platform/actions"; +import { stepFocusedSurfaceFontZoom } from "@/workbench/contributions"; + +/** + * Increase / Decrease Preview Font Size: the focused Markdown preview takes the zoom keys + * (⌘/Ctrl+= and ⌘/Ctrl+-) that expand and collapse folds in the editor, as in IntelliJ IDEA. + */ +export function markdownPreviewFontActions(): Action[] { + const action = (id: string, title: string, delta: number): Action => ({ + id, + title, + category: "Markdown", + keywords: ["zoom", "font size"], + when: "markdownPreviewFocus", + keyWhen: "markdownPreviewFocus", + run: () => void stepFocusedSurfaceFontZoom(delta), + }); + return [ + action("markdown.preview.increaseFontSize", "Increase Preview Font Size", 1), + action("markdown.preview.decreaseFontSize", "Decrease Preview Font Size", -1), + ]; +} diff --git a/src/features/viewers/markdown/zoom.test.tsx b/src/features/viewers/markdown/zoom.test.tsx index a63c38929a..ff14b5f2e8 100644 --- a/src/features/viewers/markdown/zoom.test.tsx +++ b/src/features/viewers/markdown/zoom.test.tsx @@ -13,7 +13,8 @@ import { import { fontZoom, resetAllEditorsFontZoom, setFontZoom } from "@/editor/core/cm/prefs"; import { fontExtension } from "@/editor/core/cm/setup"; import type { EditorSlot } from "@/editor/core/model/slot"; -import { executeAction } from "@/platform/actions"; +import { executeAction, registerActions } from "@/platform/actions"; +import { applyKeymapSettings, installKeyboardDispatcher } from "@/platform/keymap"; import { useContextKeys } from "@/platform/context"; import { intellijMacos } from "@/platform/keymaps/intellij-macos"; import { intellijWindows } from "@/platform/keymaps/intellij-windows"; @@ -37,6 +38,7 @@ const { registerEditorActions } = await import("@/editor/core/actions"); const { registerMarkdownLiveSource, useLiveMarkdown } = await import("./live"); const { MarkdownEditor } = await import("./MarkdownEditor"); const { useMarkdownModes } = await import("./modes"); +const { markdownPreviewFontActions } = await import("./previewFontActions"); const mdCtx: EditorDocContext = { doc: 1, path: "/p/README.md", language: "markdown", uiMode: null, readOnly: false }; const tsCtx: EditorDocContext = { doc: 2, path: "/p/a.ts", language: "typescript", uiMode: null, readOnly: false }; @@ -244,6 +246,51 @@ describe("editor font size actions", () => { expect(preview.style.getPropertyValue("--markdown-zoom-scale")).toBe("1"); }); + it("takes ⌘/Ctrl+= and ⌘/Ctrl+- in the focused preview, leaving them to fold in the editor (#7380)", async () => { + const md = open(mdCtx); + activate(md); + act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "split" } }))); + render( + +
+ , + ); + disposers.push(registerActions(markdownPreviewFontActions())); + const unfold = vi.fn(); + const fold = vi.fn(); + disposers.push( + registerActions([ + { id: "code.unfold", title: "Expand", keyWhen: "editorFocus", run: unfold }, + { id: "code.fold", title: "Collapse", keyWhen: "editorFocus", run: fold }, + ]), + ); + disposers.push(installKeyboardDispatcher(window)); + const press = async (preset: string, key: string, mod: "metaKey" | "ctrlKey") => { + applyKeymapSettings(preset, {}); + document.activeElement!.dispatchEvent( + new KeyboardEvent("keydown", { key, code: key === "=" ? "Equal" : "Minus", [mod]: true, bubbles: true }), + ); + await act(() => Promise.resolve()); + }; + const preview = screen.getByRole("document", { name: "Markdown preview" }); + + act(() => preview.focus()); + await press("intellij-macos", "=", "metaKey"); + await press("intellij-windows", "=", "ctrlKey"); + expect(preview.style.getPropertyValue("--markdown-font-zoom")).toBe("2px"); + await press("intellij-macos", "-", "metaKey"); + expect(preview.style.getPropertyValue("--markdown-font-zoom")).toBe("1px"); + expect(unfold).not.toHaveBeenCalled(); + expect(fold).not.toHaveBeenCalled(); + + act(() => preview.blur()); + expect(useContextKeys.getState().markdownPreviewFocus).toBe(false); + act(() => md.contentDOM.focus()); + await press("intellij-windows", "=", "ctrlKey"); + expect(unfold).toHaveBeenCalledOnce(); + expect(preview.style.getPropertyValue("--markdown-font-zoom")).toBe("1px"); + }); + it("sizes embedded excalidraw.svg, graph SVGs and raster images so they scale with font zoom", async () => { const box = (img: HTMLImageElement) => img.closest(".image-zoom")!; const excalidrawSvg = diff --git a/src/platform/keymaps/eclipse-macos.ts b/src/platform/keymaps/eclipse-macos.ts index a88c6567df..eccdb9f20f 100644 --- a/src/platform/keymaps/eclipse-macos.ts +++ b/src/platform/keymaps/eclipse-macos.ts @@ -174,5 +174,8 @@ export const eclipseMacos: Record = { "toolwindow.resizeUp": [], "toolwindow.resizeDown": [], "editor.decreaseFontSize": ["Meta+-"], + // Increase / Decrease Font Size already take these keys, and zoom the focused Markdown preview. + "markdown.preview.increaseFontSize": [], + "markdown.preview.decreaseFontSize": [], "editor.increaseFontSize": ["Meta+="], }; diff --git a/src/platform/keymaps/eclipse-windows.ts b/src/platform/keymaps/eclipse-windows.ts index dcdc315f4d..7ee6b1a432 100644 --- a/src/platform/keymaps/eclipse-windows.ts +++ b/src/platform/keymaps/eclipse-windows.ts @@ -178,5 +178,8 @@ export const eclipseWindows: Record = { "toolwindow.resizeUp": [], "toolwindow.resizeDown": [], "editor.decreaseFontSize": ["Ctrl+-"], + // Increase / Decrease Font Size already take these keys, and zoom the focused Markdown preview. + "markdown.preview.increaseFontSize": [], + "markdown.preview.decreaseFontSize": [], "editor.increaseFontSize": ["Ctrl+="], }; diff --git a/src/platform/keymaps/intellij-macos.ts b/src/platform/keymaps/intellij-macos.ts index df0537e257..de3bbcdc63 100644 --- a/src/platform/keymaps/intellij-macos.ts +++ b/src/platform/keymaps/intellij-macos.ts @@ -455,6 +455,8 @@ export const intellijMacos: Record = { "code.expressionTypeInfo": ["Ctrl+Shift+P"], "code.implementMethods": ["Ctrl+I"], "code.overrideMethods": ["Ctrl+O"], + "markdown.preview.increaseFontSize": ["Meta+="], + "markdown.preview.decreaseFontSize": ["Meta+-"], "image.zoomIn": ["Meta+="], "image.zoomOut": ["Meta+-"], "image.actualSize": ["Meta+/"], diff --git a/src/platform/keymaps/intellij-windows.ts b/src/platform/keymaps/intellij-windows.ts index b48600833f..4365bfbf0b 100644 --- a/src/platform/keymaps/intellij-windows.ts +++ b/src/platform/keymaps/intellij-windows.ts @@ -453,6 +453,8 @@ export const intellijWindows: Record = { "code.expressionTypeInfo": ["Ctrl+Shift+P"], "code.implementMethods": ["Ctrl+I"], "code.overrideMethods": ["Ctrl+O"], + "markdown.preview.increaseFontSize": ["Ctrl+="], + "markdown.preview.decreaseFontSize": ["Ctrl+-"], "image.zoomIn": ["Ctrl+="], "image.zoomOut": ["Ctrl+-"], "image.actualSize": ["Ctrl+/"], diff --git a/src/platform/keymaps/vscode-macos.ts b/src/platform/keymaps/vscode-macos.ts index b4e097a18c..3d1801d7c5 100644 --- a/src/platform/keymaps/vscode-macos.ts +++ b/src/platform/keymaps/vscode-macos.ts @@ -458,6 +458,8 @@ export const vscodeMacos: Record = { "code.expressionTypeInfo": [], "code.implementMethods": [], "code.overrideMethods": [], + "markdown.preview.increaseFontSize": [], + "markdown.preview.decreaseFontSize": [], "image.zoomIn": [], "image.zoomOut": [], "image.actualSize": [], diff --git a/src/platform/keymaps/vscode-windows.ts b/src/platform/keymaps/vscode-windows.ts index 0b9c49e505..7a9a4b6432 100644 --- a/src/platform/keymaps/vscode-windows.ts +++ b/src/platform/keymaps/vscode-windows.ts @@ -57,6 +57,8 @@ export const vscodeWindows: Record = { "code.expressionTypeInfo": [], "code.implementMethods": [], "code.overrideMethods": [], + "markdown.preview.increaseFontSize": [], + "markdown.preview.decreaseFontSize": [], "image.zoomIn": [], "image.zoomOut": [], "image.actualSize": [], diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index fccf932622..956ed7a5af 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -5154,5 +5154,7 @@ "Until an Xcode project's first build, sourcekit-lsp can't resolve types from its other targets. Builds what Run builds, at low priority, once.": "Until an Xcode project's first build, sourcekit-lsp can't resolve types from its other targets. Builds what Run builds, at low priority, once.", "Code insight for {container} reads its last build. Without one yet, it is built in the background.": "Code insight for {container} reads its last build. Without one yet, it is built in the background.", "Background build for code insight failed": "Background build for code insight failed", - "Code insight reads what it built. Run shows the whole build log.": "Code insight reads what it built. Run shows the whole build log." + "Code insight reads what it built. Run shows the whole build log.": "Code insight reads what it built. Run shows the whole build log.", + "Increase Preview Font Size": "Increase Preview Font Size", + "Decrease Preview Font Size": "Decrease Preview Font Size" } diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index da58038e77..72c7005dfe 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -12034,5 +12034,7 @@ "Language server updates are available": "语言服务器有可用更新", "Vue Language Server version": "Vue 语言服务器版本", "Auto uses 3.0.x for Vue 2 projects and the latest release for Vue 3.": "自动选择时,Vue 2 项目使用 3.0.x,Vue 3 项目使用最新版本。", - "Enter latest, 3.0.x, or an exact Vue Language Server version.": "请输入 latest、3.0.x 或 Vue 语言服务器的具体版本。" + "Enter latest, 3.0.x, or an exact Vue Language Server version.": "请输入 latest、3.0.x 或 Vue 语言服务器的具体版本。", + "Increase Preview Font Size": "增大预览字体", + "Decrease Preview Font Size": "减小预览字体" }