Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai 3ab381ffff fix(editor): keep preview layouts per editor tab, not per file
Split panes of one file shared their Markdown layout (Editor Only / Editor and
Preview / Preview Only) because the mode was stored per path. The HTML Built-in
Preview toggle and plugin side-by-side editors had the same per-path state. All
three are now kept per editor tab; a new Markdown tab still starts in the last
layout chosen, and shown tabs keep theirs when another tab switches.

Fixes #4377
2026-11-19 08:42:52 +08:00
17 змінених файлів з 118 додано та 70 видалено

No files matched your search

+30 -4
Переглянути файл
@@ -33,7 +33,7 @@ const originalTabs = useTabs.getState();
const originalProject = useProject.getState();
beforeEach(() => {
useMarkdownModes.setState({ byPath: {}, fallback: "editor" });
useMarkdownModes.setState({ byTab: {}, fallback: "editor" });
useTabs.setState({
layout: { kind: "pane", paneId: "p1" },
panes: { p1: { id: "p1", tabs: [first], active: first.id, mru: [first.id] } },
@@ -87,7 +87,33 @@ describe("Markdown layout controls", () => {
expect(screen.getByRole("toolbar", { name: "Markdown editor layout" }).closest('[role="tabpanel"]')).not.toBeNull();
});
it("routes each pane's layout buttons to that pane's file", () => {
it("keeps a separate layout in each split pane of the same file", () => {
useProject.setState({ settings: null });
const copy = md("t3", first.input.path!);
useTabs.setState({
layout: {
kind: "split",
id: "split",
dir: "row",
children: [
{ kind: "pane", paneId: "p1" },
{ kind: "pane", paneId: "p2" },
],
},
panes: {
p1: { id: "p1", tabs: [first], active: first.id, mru: [first.id] },
p2: { id: "p2", tabs: [copy], active: copy.id, mru: [copy.id] },
},
});
render(<EditorAreaView />);
fireEvent.click(screen.getAllByRole("button", { name: "Preview Only" })[1]!);
const [left, right] = screen.getAllByRole("button", { name: "Preview Only" });
expect(left).toHaveAttribute("aria-pressed", "false");
expect(right).toHaveAttribute("aria-pressed", "true");
expect(screen.getAllByRole("document", { name: "Markdown preview" })).toHaveLength(1);
});
it("routes each pane's layout buttons to that pane's tab", () => {
useProject.setState({ settings: null });
useTabs.setState({
layout: {
@@ -106,8 +132,8 @@ describe("Markdown layout controls", () => {
});
render(<EditorAreaView />);
fireEvent.click(screen.getAllByRole("button", { name: "Preview Only" })[1]!);
expect(useMarkdownModes.getState().byPath[first.input.path!]).toBeUndefined();
expect(useMarkdownModes.getState().byPath[second.input.path!]).toBe("preview");
expect(useMarkdownModes.getState().byTab[first.id]).toBe("editor");
expect(useMarkdownModes.getState().byTab[second.id]).toBe("preview");
act(() => useDistractionFree.setState({ active: true }));
expect(screen.getAllByRole("toolbar", { name: "Markdown editor layout" })).toHaveLength(2);
});
+17 -15
Переглянути файл
@@ -3,7 +3,8 @@
* `"preview"`): the file opens in the text editor, wrapped with the plugin's
* editor and the Editor / Editor and Preview / Preview switch (in the tab
* strip, or floating when tabs are hidden), as Markdown's preview. The
* layout is kept per file; files not switched yet start in the declared one.
* layout is kept per editor tab (split panes of one file each have their own);
* tabs not switched yet start in the declared one.
*/
import clsx from "clsx";
import { useRef, type ReactNode } from "react";
@@ -15,21 +16,21 @@ import styles from "./PluginView.module.css";
export type EditorLayout = "editor" | "split" | "preview";
/** `<plugin>/<editor>` → path → layout. */
export const useEditorLayouts = create<{ byPath: Record<string, EditorLayout> }>(() => ({ byPath: {} }));
/** `<plugin>/<editor>:<tab>` → layout. */
const useEditorLayouts = create<{ byTab: Record<string, EditorLayout> }>(() => ({ byTab: {} }));
const layoutKey = (pluginId: string, editorId: string, path: string) => `${pluginId}/${editorId}:${path}`;
const layoutKey = (pluginId: string, editorId: string, tabId: string) => `${pluginId}/${editorId}:${tabId}`;
export function layoutOf(pluginId: string, decl: CustomEditorDecl, path: string, byPath: Record<string, EditorLayout>) {
return byPath[layoutKey(pluginId, decl.id, path)] ?? (decl.layout === "preview" ? "preview" : "split");
function layoutOf(pluginId: string, decl: CustomEditorDecl, tabId: string, byTab: Record<string, EditorLayout>) {
return byTab[layoutKey(pluginId, decl.id, tabId)] ?? (decl.layout === "preview" ? "preview" : "split");
}
export function setEditorLayout(pluginId: string, editorId: string, path: string, layout: EditorLayout) {
useEditorLayouts.setState((s) => ({ byPath: { ...s.byPath, [layoutKey(pluginId, editorId, path)]: layout } }));
function setEditorLayout(pluginId: string, editorId: string, tabId: string, layout: EditorLayout) {
useEditorLayouts.setState((s) => ({ byTab: { ...s.byTab, [layoutKey(pluginId, editorId, tabId)]: layout } }));
}
function LayoutActions({ pluginId, decl, path }: { pluginId: string; decl: CustomEditorDecl; path: string }) {
const layout = useEditorLayouts((s) => layoutOf(pluginId, decl, path, s.byPath));
function LayoutActions({ pluginId, decl, tabId }: { pluginId: string; decl: CustomEditorDecl; tabId: string }) {
const layout = useEditorLayouts((s) => layoutOf(pluginId, decl, tabId, s.byTab));
const button = (value: EditorLayout, icon: string, title: string) => (
<IconButton
icon={icon}
@@ -37,7 +38,7 @@ function LayoutActions({ pluginId, decl, path }: { pluginId: string; decl: Custo
size="small"
title={title}
pressed={layout === value}
onClick={() => setEditorLayout(pluginId, decl.id, path, value)}
onClick={() => setEditorLayout(pluginId, decl.id, tabId, value)}
/>
);
return (
@@ -51,8 +52,8 @@ function LayoutActions({ pluginId, decl, path }: { pluginId: string; decl: Custo
/** The `tabActions` of a wrapper: the layout switch in the editor's tab strip. */
export function layoutActions(pluginId: string, decl: CustomEditorDecl) {
return function PluginLayoutActions({ path }: { path: string; tabId: string }) {
return <LayoutActions pluginId={pluginId} decl={decl} path={path} />;
return function PluginLayoutActions({ tabId }: { path: string; tabId: string }) {
return <LayoutActions pluginId={pluginId} decl={decl} tabId={tabId} />;
};
}
@@ -60,6 +61,7 @@ export function layoutActions(pluginId: string, decl: CustomEditorDecl) {
export function layoutWrapper(pluginId: string, decl: CustomEditorDecl) {
return function PluginEditorLayout({
path,
tabId,
children,
actionsInTabs = false,
}: {
@@ -68,7 +70,7 @@ export function layoutWrapper(pluginId: string, decl: CustomEditorDecl) {
children: ReactNode;
actionsInTabs?: boolean;
}) {
const layout = useEditorLayouts((s) => layoutOf(pluginId, decl, path, s.byPath));
const layout = useEditorLayouts((s) => layoutOf(pluginId, decl, tabId, s.byTab));
const ref = useRef<HTMLDivElement>(null);
return (
<div ref={ref} className={clsx(styles.split, styles[`layout-${layout}`])}>
@@ -81,7 +83,7 @@ export function layoutWrapper(pluginId: string, decl: CustomEditorDecl) {
)}
{!actionsInTabs && (
<FloatingToolbar targetRef={ref}>
<LayoutActions pluginId={pluginId} decl={decl} path={path} />
<LayoutActions pluginId={pluginId} decl={decl} tabId={tabId} />
</FloatingToolbar>
)}
</div>
+5 -3
Переглянути файл
@@ -6,7 +6,7 @@
*/
import { registerActions, type Action } from "@/platform/actions";
import { call, commands, type FileProbe } from "@/platform/ipc";
import { getContext } from "@/platform/context";
import { activeEditor } from "@/editor/api";
import { openWithSystemApp } from "@/platform/fileManager";
import { onFileTypesChange } from "@/platform/fileTypes";
import { basename, useProject } from "@/platform/project";
@@ -72,8 +72,10 @@ function markdownActions(): Action[] {
keywords: ["markdown", "preview", "layout"],
when: "textEditorActive && editorLangId == markdown",
run: ({ arg }) => {
const path = typeof arg === "string" ? arg : getContext().editorPath;
if (typeof path === "string" && isMarkdownPath(path)) setMarkdownMode(path, m);
// The tab's layout buttons pass their tab; the keyboard acts on the focused editor's.
const editor = activeEditor();
const tabId = typeof arg === "string" ? arg : isMarkdownPath(editor?.ctx.path ?? "") ? editor?.tabId : undefined;
if (tabId) setMarkdownMode(tabId, m);
},
});
return [
+3 -2
Переглянути файл
@@ -42,7 +42,7 @@ import { publishPreviewSelection, publishPreviewTop, stepMarkdownPreviewFontZoom
import { subscribeTheme, themeKey } from "@/platform/themeWatch";
import { hasMermaid, MERMAID_SELECTOR, renderMermaidIn } from "./mermaid";
import { renderMath } from "@/editor/lsp/markdownMath";
import { modeOf, useMarkdownModes } from "./modes";
import { modeOf, pinMarkdownMode, useMarkdownModes } from "./modes";
import { MarkdownLayoutActions } from "./MarkdownLayoutActions";
import {
ANCHOR_PREFIX,
@@ -90,7 +90,8 @@ export function MarkdownEditor({
children: ReactNode;
actionsInTabs?: boolean;
}) {
const mode = useMarkdownModes((s) => modeOf(s, path));
const mode = useMarkdownModes((s) => modeOf(s, tabId));
useEffect(() => pinMarkdownMode(tabId), [tabId]);
const ref = useRef<HTMLDivElement>(null);
const editorRef = useRef<HTMLDivElement>(null);
+5 -5
Переглянути файл
@@ -3,8 +3,8 @@ import { tx } from "@/platform/i18n";
import { modeOf, useMarkdownModes } from "./modes";
import styles from "./Markdown.module.css";
export function MarkdownLayoutActions({ path }: { path: string; tabId: string }) {
const mode = useMarkdownModes((s) => modeOf(s, path));
export function MarkdownLayoutActions({ tabId }: { path: string; tabId: string }) {
const mode = useMarkdownModes((s) => modeOf(s, tabId));
return (
<div className={styles.layoutActions} role="toolbar" aria-label={tx("Markdown editor layout")}>
<IconButton
@@ -13,7 +13,7 @@ export function MarkdownLayoutActions({ path }: { path: string; tabId: string })
size="small"
title="Editor Only"
action="markdown.editorOnly"
actionArg={path}
actionArg={tabId}
pressed={mode === "editor"}
/>
<IconButton
@@ -22,7 +22,7 @@ export function MarkdownLayoutActions({ path }: { path: string; tabId: string })
size="small"
title="Editor and Preview"
action="markdown.editorAndPreview"
actionArg={path}
actionArg={tabId}
pressed={mode === "split"}
/>
<IconButton
@@ -31,7 +31,7 @@ export function MarkdownLayoutActions({ path }: { path: string; tabId: string })
size="small"
title="Preview Only"
action="markdown.previewOnly"
actionArg={path}
actionArg={tabId}
pressed={mode === "preview"}
/>
</div>
+3 -3
Переглянути файл
@@ -49,7 +49,7 @@ describe("Markdown preview search & selection highlights", () => {
beforeEach(() => {
disposers.push(registerMarkdownLiveSource());
act(() => useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, [path]: "preview" } })));
act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "preview" } })));
});
afterEach(() => {
@@ -184,7 +184,7 @@ describe("Markdown preview search & selection highlights", () => {
it("auto-highlights all other occurrences when a word is selected in the preview", async () => {
openEditor("alpha beta alpha gamma alpha\n");
act(() => useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, [path]: "split" } })));
act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "split" } })));
render(
<MarkdownEditor path={path} tabId="t">
<div />
@@ -228,7 +228,7 @@ describe("Markdown preview search & selection highlights", () => {
it("auto-highlights occurrences in the preview when a word is selected in the editor", () => {
openEditor("beta alpha beta\n");
act(() => useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, [path]: "split" } })));
act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "split" } })));
render(
<MarkdownEditor path={path} tabId="t">
<div />
+16 -7
Переглянути файл
@@ -1,4 +1,7 @@
/** Markdown editor layout per file: Editor only / Editor and Preview / Preview only (IDEA's split editor). */
/**
* Markdown editor layout per editor tab: Editor only / Editor and Preview / Preview only. Split
* panes showing the same file each keep their own.
*/
import { create } from "zustand";
export type MarkdownMode = "editor" | "split" | "preview";
@@ -16,17 +19,23 @@ function storedDefault(): MarkdownMode {
}
interface ModesState {
byPath: Record<string, MarkdownMode>;
/** Mode of files not switched yet: the last one chosen. */
byTab: Record<string, MarkdownMode>;
/** Mode of tabs opened since: the last one chosen. */
fallback: MarkdownMode;
}
export const useMarkdownModes = create<ModesState>(() => ({ byPath: {}, fallback: storedDefault() }));
export const useMarkdownModes = create<ModesState>(() => ({ byTab: {}, fallback: storedDefault() }));
export const modeOf = (s: ModesState, path: string): MarkdownMode => s.byPath[path] ?? s.fallback;
export const modeOf = (s: ModesState, tabId: string): MarkdownMode => s.byTab[tabId] ?? s.fallback;
export function setMarkdownMode(path: string, mode: MarkdownMode) {
useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, [path]: mode }, fallback: mode }));
/** Fixes a shown tab's mode, so choosing one in another tab doesn't change it. */
export function pinMarkdownMode(tabId: string) {
if (useMarkdownModes.getState().byTab[tabId]) return;
useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, [tabId]: s.fallback } }));
}
export function setMarkdownMode(tabId: string, mode: MarkdownMode) {
useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, [tabId]: mode }, fallback: mode }));
try {
localStorage.setItem(DEFAULT_KEY, mode);
} catch {
+1 -1
Переглянути файл
@@ -25,7 +25,7 @@ describe("preview <details>", () => {
const handle = { view, ctx };
_addEditor(handle);
disposers.push(() => _removeEditor(handle));
act(() => useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, [ctx.path]: "split" } })));
act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "split" } })));
});
afterEach(() => {
cleanup();
+4 -4
Переглянути файл
@@ -39,7 +39,7 @@ describe("Markdown Preview only", () => {
disposers.push(registerMarkdownLiveSource());
const live = editorContributions().find((c) => c.id === "viewers.markdownLive")!;
view = new EditorView({ state: EditorState.create({ doc: source, extensions: [live.extensions(ctx)] }) });
act(() => useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, [path]: "preview" } })));
act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "preview" } })));
});
afterEach(() => {
cleanup();
@@ -97,7 +97,7 @@ describe("Markdown Preview only", () => {
});
it("leaves the split preview to the editor's scroll sync", () => {
act(() => useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, [path]: "split" } })));
act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "split" } })));
const el = preview();
goTo(20);
expect(el.scrollTop).toBe(0);
@@ -110,10 +110,10 @@ describe("Markdown Preview only", () => {
const el = preview();
goTo(20);
expect(shown()).toBe(20);
act(() => useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, [path]: "split" } })));
act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "split" } })));
expect(shown()).toBeNull();
act(() => useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, [path]: "preview" } })));
act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "preview" } })));
expect(shown()).not.toBeNull();
cleanup();
expect(shown()).toBeNull();
+5 -5
Переглянути файл
@@ -56,7 +56,7 @@ describe("preview-only scroll position", () => {
it("restores the preview's own scroll after a remount", async () => {
open(mdCtx);
activate(views[0]!);
act(() => useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, [mdCtx.path]: "preview" } })));
act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "preview" } })));
const first = render(
<MarkdownEditor path={mdCtx.path} tabId="t">
<div />
@@ -84,7 +84,7 @@ describe("preview-only scroll position", () => {
open(mdCtx);
activate(views[0]!);
act(() => useLiveMarkdown.setState((s) => ({ [mdCtx.path]: { ...s[mdCtx.path]!, previewTop: 300 } })));
act(() => useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, [mdCtx.path]: "preview" } })));
act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "preview" } })));
render(
<MarkdownEditor path={mdCtx.path} tabId="t">
<div />
@@ -105,8 +105,8 @@ describe("preview-only scroll position", () => {
});
try {
// Re-running the restore (mode toggle) hits the clamp…
act(() => useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, [mdCtx.path]: "split" } })));
act(() => useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, [mdCtx.path]: "preview" } })));
act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "split" } })));
act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "preview" } })));
expect(preview.scrollTop).toBe(100);
// The browser reports the clamped write as a scroll; the saved offset stays.
act(() => preview.dispatchEvent(new Event("scroll")));
@@ -132,7 +132,7 @@ describe("preview-only scroll position", () => {
open(mdCtx);
activate(views[0]!);
act(() => useLiveMarkdown.setState((s) => ({ [mdCtx.path]: { ...s[mdCtx.path]!, previewTop: 300 } })));
act(() => useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, [mdCtx.path]: "preview" } })));
act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "preview" } })));
render(
<MarkdownEditor path={mdCtx.path} tabId="t">
<div />
+4 -4
Переглянути файл
@@ -13,7 +13,7 @@ const SPLIT_KEY = "openidea.markdown.split";
describe("Markdown split resizer", () => {
beforeEach(() => {
localStorage.clear();
useMarkdownModes.setState({ byPath: {}, fallback: "split" });
useMarkdownModes.setState({ byTab: {}, fallback: "split" });
});
afterEach(() => {
@@ -38,17 +38,17 @@ describe("Markdown split resizer", () => {
expect(screen.getByRole("separator", { name: "Resize Markdown preview" })).toBeInTheDocument();
expect(mounts).toBe(1);
act(() => setMarkdownMode(PATH, "preview"));
act(() => setMarkdownMode("t", "preview"));
expect(screen.queryByRole("separator", { name: "Resize Markdown preview" })).toBeNull();
expect(screen.getByTestId("editor-probe")).toBeInTheDocument();
expect(mounts).toBe(1);
act(() => setMarkdownMode(PATH, "editor"));
act(() => setMarkdownMode("t", "editor"));
expect(screen.queryByRole("separator", { name: "Resize Markdown preview" })).toBeNull();
expect(screen.getByTestId("editor-probe")).toBeInTheDocument();
expect(mounts).toBe(1);
act(() => setMarkdownMode(PATH, "split"));
act(() => setMarkdownMode("t", "split"));
expect(screen.getByRole("separator", { name: "Resize Markdown preview" })).toBeInTheDocument();
expect(mounts).toBe(1);
});
+3 -3
Переглянути файл
@@ -128,7 +128,7 @@ describe("editor font size actions", () => {
const md = open(mdCtx);
const otherMd = open(otherMdCtx);
activate(md);
act(() => useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, [mdCtx.path]: "split" } })));
act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "split" } })));
render(
<MarkdownEditor path={mdCtx.path} tabId="t">
<div />
@@ -208,7 +208,7 @@ describe("editor font size actions", () => {
views.push(view);
activate(view);
act(() => useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, [mdCtx.path]: "preview" } })));
act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "preview" } })));
render(
<MarkdownEditor path={mdCtx.path} tabId="t">
<div />
@@ -373,7 +373,7 @@ describe("editor font size actions", () => {
describe("Markdown preview font size", () => {
const preview = () => {
act(() => useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, [mdCtx.path]: "preview" } })));
act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "preview" } })));
render(
<MarkdownEditor path={mdCtx.path} tabId="t">
<div />
+3 -3
Переглянути файл
@@ -15,8 +15,8 @@ import { tx } from "@/platform/i18n";
const RELOAD_DELAY = 250;
export function WebEditor({ path, children }: { path: string; tabId: string; children: ReactNode }) {
const preview = useWebPreview((s) => !!s[path]);
export function WebEditor({ path, tabId, children }: { path: string; tabId: string; children: ReactNode }) {
const preview = useWebPreview((s) => !!s[tabId]);
const browsers = useBrowsers((s) => s.list);
const ref = useRef<HTMLDivElement>(null);
return (
@@ -30,7 +30,7 @@ export function WebEditor({ path, children }: { path: string; tabId: string; chi
size="small"
iconSize={16}
action="web.togglePreview"
actionArg={path}
actionArg={tabId}
pressed={preview}
title="Built-in Preview"
/>
+3 -3
Переглянути файл
@@ -33,11 +33,11 @@ export async function loadBrowsers(): Promise<void> {
export const browserIcon = (id: string) => (hasIcon(`browser-${id}`) ? `browser-${id}` : "web");
/** Paths shown with the built-in preview beside the editor. */
/** Editor tabs showing the built-in preview beside the editor (each split pane has its own). */
export const useWebPreview = create<Record<string, boolean>>(() => ({}));
export function togglePreview(path: string, on = !useWebPreview.getState()[path]) {
useWebPreview.setState({ [path]: on });
export function togglePreview(tabId: string, on = !useWebPreview.getState()[tabId]) {
useWebPreview.setState({ [tabId]: on });
}
/** The built-in server's URL for `path`. */
+14 -6
Переглянути файл
@@ -17,7 +17,7 @@ import {
registerTextEditorWrapper,
useTextEditorWrappers,
} from "@/workbench/contributions";
import { registerEditorContribution } from "@/editor/api";
import { activeEditor, registerEditorContribution } from "@/editor/api";
import { djangoContentWatcher, djangoTemplateBanner } from "./djangoTemplates";
import {
browserIcon,
@@ -44,7 +44,15 @@ function targetPath(arg: unknown): string | null {
async function showPreview(path: string) {
await maybeEditorService()?.openFile(path, { focus: true });
togglePreview(path, true);
const editor = activeEditor();
if (editor?.tabId && editor.ctx.path === path) togglePreview(editor.tabId, true);
}
/** The tab a preview button passes, or the focused HTML editor's. */
function previewTab(arg: unknown): string | undefined {
if (typeof arg === "string") return arg;
const editor = activeEditor();
return editor && isHtmlPath(editor.ctx.path) ? editor.tabId : undefined;
}
function browserMenu(path: string): MenuItem {
@@ -102,12 +110,12 @@ export function activate(): () => void {
when: "textEditorActive && editorLangId == html",
keywords: ["browser", "html", "preview", "live"],
toggled: () => {
const path = getContext().editorPath;
return typeof path === "string" && !!useWebPreview.getState()[path];
const tabId = previewTab(undefined);
return !!tabId && !!useWebPreview.getState()[tabId];
},
run: ({ arg }) => {
const path = targetPath(arg);
if (path && isHtmlPath(path)) togglePreview(path);
const tabId = previewTab(arg);
if (tabId) togglePreview(tabId);
},
},
]),
+1 -1
Переглянути файл
@@ -33,7 +33,7 @@ describe("the built-in HTML preview", () => {
vi.useFakeTimers();
try {
useProject.setState({ project: { root: "/p" } as never });
useWebPreview.setState({ "/p/page.html": true });
useWebPreview.setState({ t: true });
const { container } = render(
<WebEditor path="/p/page.html" tabId="t">
<div />
+1 -1
Переглянути файл
@@ -33,7 +33,7 @@ describe("selectable read-only surfaces", () => {
},
}),
);
act(() => useMarkdownModes.setState((s) => ({ byPath: { ...s.byPath, "/p/README.md": "preview" } })));
act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "preview" } })));
render(
<MarkdownEditor path="/p/README.md" tabId="t">
<div />