дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-23 18:10:45 +02:00
Порівняти коміти
1
Коміти
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
5e8b0470fb |
No files matched your search
@@ -70,8 +70,7 @@ import { copyReferenceToClipboard, declaredName, qualifiedReference, referenceOf
|
||||
import { declarationChain } from "./view/outline";
|
||||
import { recordClipboard } from "./clipboardHistory";
|
||||
import { useDocMeta, useOutline } from "./model/document";
|
||||
import { fontZoom, setFontZoom, stepFontZoom } from "./cm/prefs";
|
||||
import { useEditorRegistry } from "@/editor/api";
|
||||
import { resetAllEditorsFontZoom, stepAllEditorsFontZoom } from "./cm/prefs";
|
||||
import { useProject } from "@/platform/project";
|
||||
import { call, commands } from "@/platform/ipc";
|
||||
import { editorPrefs } from "@/platform/editorSettings";
|
||||
@@ -121,7 +120,7 @@ function stepSurfaceFontSize(delta: 1 | -1): void {
|
||||
if (cmEl) {
|
||||
const cmView = EditorView.findFromDOM(cmEl);
|
||||
if (cmView) {
|
||||
stepFontZoom(cmView, delta);
|
||||
stepAllEditorsFontZoom(cmView, delta);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -129,7 +128,7 @@ function stepSurfaceFontSize(delta: 1 | -1): void {
|
||||
|
||||
const slot = activeSlot();
|
||||
if (!slot?.view) return;
|
||||
stepFontZoom(slot.view, delta);
|
||||
stepAllEditorsFontZoom(slot.view, delta);
|
||||
if (!slot.view.hasFocus && !getContext().popupOpen) slot.view.focus();
|
||||
}
|
||||
|
||||
@@ -898,8 +897,7 @@ export function registerEditorActions(): () => void {
|
||||
keywords: ["zoom", "font size", "mouse wheel"],
|
||||
when: "projectOpen",
|
||||
run: () => {
|
||||
for (const ed of useEditorRegistry.getState().editors)
|
||||
if (ed.view.state.field(fontZoom, false)) ed.view.dispatch({ effects: setFontZoom.of(0) });
|
||||
resetAllEditorsFontZoom();
|
||||
if (typeof document !== "undefined") {
|
||||
resetSurfaceFontZoom();
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ import {
|
||||
import { markdownImages } from "./markdownImages";
|
||||
import { selectionMarks } from "./selectionMarks";
|
||||
import { seamlessSelection } from "./selectionLayer";
|
||||
import { useEditorRegistry } from "@/editor/api";
|
||||
import type { EditorExtras } from "@/platform/editorSettings";
|
||||
import { openideaHighlightStyle } from "./highlight";
|
||||
import {
|
||||
@@ -374,10 +375,16 @@ const richCopy = EditorView.domEventHandlers({
|
||||
|
||||
// --- caret / scrolling / mouse ----------------------------------------------
|
||||
|
||||
/**
|
||||
* Font zoom new editors start from. ⌘/Ctrl+wheel keeps every open editor on the
|
||||
* same value (#2480), so editors opened afterwards match the others.
|
||||
*/
|
||||
let sharedFontZoom = 0;
|
||||
|
||||
/** Font zoom of one editor (⌘ + wheel), in px relative to the configured size. */
|
||||
export const setFontZoom = StateEffect.define<number>();
|
||||
export const fontZoom = StateField.define<number>({
|
||||
create: () => 0,
|
||||
create: () => sharedFontZoom,
|
||||
update: (v, tr) => tr.effects.reduce((acc, e) => (e.is(setFontZoom) ? e.value : acc), v),
|
||||
// Highest precedence: its style comes after the configured font size, which it scales.
|
||||
provide: (f) =>
|
||||
@@ -400,11 +407,35 @@ export function stepFontZoom(view: EditorView, delta: number): void {
|
||||
if (next !== z) view.dispatch({ effects: setFontZoom.of(next) });
|
||||
}
|
||||
|
||||
const wheelZoom = EditorView.domEventHandlers({
|
||||
/**
|
||||
* Steps the font zoom of every open editor to the same value, so ⌘/Ctrl+wheel
|
||||
* resizes all editor areas together (#2480) instead of only the focused one.
|
||||
*/
|
||||
export function stepAllEditorsFontZoom(origin: EditorView, delta: number): void {
|
||||
const current = origin.state.field(fontZoom, false);
|
||||
if (current === undefined) return;
|
||||
const next = Math.max(-6, Math.min(36, current + delta));
|
||||
if (next === current) return;
|
||||
sharedFontZoom = next;
|
||||
for (const ed of useEditorRegistry.getState().editors) {
|
||||
if (ed.view.state.field(fontZoom, false) === undefined) continue;
|
||||
if (ed.view.state.field(fontZoom) !== next) ed.view.dispatch({ effects: setFontZoom.of(next) });
|
||||
}
|
||||
}
|
||||
|
||||
/** Resets every open editor's font zoom, and the value new editors start from. */
|
||||
export function resetAllEditorsFontZoom(): void {
|
||||
sharedFontZoom = 0;
|
||||
for (const ed of useEditorRegistry.getState().editors)
|
||||
if (ed.view.state.field(fontZoom, false)) ed.view.dispatch({ effects: setFontZoom.of(0) });
|
||||
}
|
||||
|
||||
/** Exported for tests: the ⌘/Ctrl+wheel handler of the prefs compartment. */
|
||||
export const wheelZoom = EditorView.domEventHandlers({
|
||||
wheel(e, view) {
|
||||
if (!e.metaKey && !e.ctrlKey) return false;
|
||||
e.preventDefault();
|
||||
stepFontZoom(view, e.deltaY < 0 ? 1 : -1);
|
||||
stepAllEditorsFontZoom(view, e.deltaY < 0 ? 1 : -1);
|
||||
return true;
|
||||
},
|
||||
});
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { EditorState } from "@codemirror/state";
|
||||
import { EditorView } from "@codemirror/view";
|
||||
import { useEditorRegistry, type TextEditorHandle } from "@/editor/api";
|
||||
import { fontZoom, resetAllEditorsFontZoom, setFontZoom, stepAllEditorsFontZoom, wheelZoom } from "./cm/prefs";
|
||||
|
||||
const zoomOf = (view: EditorView) => view.state.field(fontZoom, false);
|
||||
|
||||
describe("shared editor font zoom (#2480)", () => {
|
||||
const handles: TextEditorHandle[] = [];
|
||||
const makeView = (extensions = [fontZoom, wheelZoom]) => {
|
||||
const parent = document.createElement("div");
|
||||
document.body.append(parent);
|
||||
const view = new EditorView({ parent, state: EditorState.create({ extensions }) });
|
||||
const handle = { view } as TextEditorHandle;
|
||||
useEditorRegistry.getState().editors.add(handle);
|
||||
handles.push(handle);
|
||||
return view;
|
||||
};
|
||||
beforeEach(() => resetAllEditorsFontZoom());
|
||||
afterEach(() => {
|
||||
for (const handle of handles) {
|
||||
useEditorRegistry.getState().editors.delete(handle);
|
||||
handle.view.destroy();
|
||||
handle.view.dom.parentElement?.remove();
|
||||
}
|
||||
handles.length = 0;
|
||||
resetAllEditorsFontZoom();
|
||||
});
|
||||
|
||||
it("zooms every open editor from ⌘/Ctrl+wheel on one of them", () => {
|
||||
const first = makeView();
|
||||
const second = makeView();
|
||||
first.contentDOM.dispatchEvent(new WheelEvent("wheel", { bubbles: true, ctrlKey: true, deltaY: -10 }));
|
||||
expect(zoomOf(first)).toBe(1);
|
||||
expect(zoomOf(second)).toBe(1);
|
||||
second.contentDOM.dispatchEvent(new WheelEvent("wheel", { bubbles: true, ctrlKey: true, deltaY: 10 }));
|
||||
expect(zoomOf(first)).toBe(0);
|
||||
expect(zoomOf(second)).toBe(0);
|
||||
});
|
||||
|
||||
it("ignores the wheel without ⌘/Ctrl", () => {
|
||||
const first = makeView();
|
||||
makeView();
|
||||
first.contentDOM.dispatchEvent(new WheelEvent("wheel", { bubbles: true, deltaY: -10 }));
|
||||
expect(zoomOf(first)).toBe(0);
|
||||
});
|
||||
|
||||
it("keeps the shared zoom within -6..36", () => {
|
||||
const first = makeView();
|
||||
const second = makeView();
|
||||
first.dispatch({ effects: setFontZoom.of(36) });
|
||||
stepAllEditorsFontZoom(first, 1);
|
||||
expect(zoomOf(first)).toBe(36);
|
||||
expect(zoomOf(second)).toBe(0);
|
||||
stepAllEditorsFontZoom(first, -1);
|
||||
expect(zoomOf(first)).toBe(35);
|
||||
expect(zoomOf(second)).toBe(35);
|
||||
});
|
||||
|
||||
it("leaves views without the zoom field alone", () => {
|
||||
const first = makeView();
|
||||
const plain = makeView([]);
|
||||
stepAllEditorsFontZoom(first, 1);
|
||||
expect(zoomOf(first)).toBe(1);
|
||||
expect(zoomOf(plain)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("seeds newly opened editors and resets them all", () => {
|
||||
const first = makeView();
|
||||
stepAllEditorsFontZoom(first, 2);
|
||||
expect(zoomOf(makeView())).toBe(2);
|
||||
resetAllEditorsFontZoom();
|
||||
expect(zoomOf(first)).toBe(0);
|
||||
expect(zoomOf(makeView())).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -4,7 +4,7 @@ import { act, cleanup, render, screen } from "@testing-library/react";
|
||||
import { EditorState } from "@codemirror/state";
|
||||
import { EditorView } from "@codemirror/view";
|
||||
import { _addEditor, _removeEditor, editorContributions, type EditorDocContext } from "@/editor/api";
|
||||
import { fontZoom, setFontZoom } from "@/editor/core/cm/prefs";
|
||||
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";
|
||||
@@ -51,6 +51,7 @@ describe("editor font size actions", () => {
|
||||
const zoom = (view: EditorView) => view.state.field(fontZoom);
|
||||
|
||||
beforeEach(() => {
|
||||
resetAllEditorsFontZoom();
|
||||
disposers.push(registerEditorActions(), registerMarkdownLiveSource());
|
||||
useContextKeys.setState({ projectOpen: true, textEditorActive: true, editorFocus: true, popupOpen: true });
|
||||
});
|
||||
@@ -63,26 +64,30 @@ describe("editor font size actions", () => {
|
||||
useContextKeys.setState({}, true);
|
||||
});
|
||||
|
||||
it("zooms only the active editor, clamped to -6..36", async () => {
|
||||
it("zooms all open editors together, clamped to -6..36", async () => {
|
||||
const md = open(mdCtx);
|
||||
const ts = open(tsCtx);
|
||||
activate(md);
|
||||
await executeAction("editor.increaseFontSize");
|
||||
await executeAction("editor.increaseFontSize");
|
||||
expect(zoom(md)).toBe(2);
|
||||
expect(zoom(ts)).toBe(0);
|
||||
expect(zoom(ts)).toBe(2);
|
||||
|
||||
activate(ts);
|
||||
await executeAction("editor.decreaseFontSize");
|
||||
expect(zoom(ts)).toBe(-1);
|
||||
expect(zoom(md)).toBe(2);
|
||||
expect(zoom(ts)).toBe(1);
|
||||
expect(zoom(md)).toBe(1);
|
||||
|
||||
md.dispatch({ effects: setFontZoom.of(36) });
|
||||
ts.dispatch({ effects: setFontZoom.of(36) });
|
||||
await executeAction("editor.increaseFontSize");
|
||||
expect(zoom(ts)).toBe(36);
|
||||
expect(zoom(md)).toBe(36);
|
||||
md.dispatch({ effects: setFontZoom.of(-6) });
|
||||
ts.dispatch({ effects: setFontZoom.of(-6) });
|
||||
await executeAction("editor.decreaseFontSize");
|
||||
expect(zoom(ts)).toBe(-6);
|
||||
expect(zoom(md)).toBe(-6);
|
||||
});
|
||||
|
||||
it("scales the Markdown preview with its editor and resets with it", async () => {
|
||||
|
||||
Reference in new issue
Заблокувати користувача