Файли
openidea/src/features/web/index.ts
T
arookieofcтаarookieofc 68a322edad feat(web): add built-in HTTP /api/file/<path>:<line> server (#7360)
The built-in server now listens on a configurable loopback port (Settings > Build, Execution, Deployment > Debugger > Built-in Server, default 63342, falling back to the next free ports) and opens /api/file/<path>[:line[:column]] in the project window that holds the file. Paths resolve root-relative, under common source roots, or through the project's file-name index with a suffix match, and never leave a project root.

Requests from a web page (Origin, Referer or Sec-Fetch-Site) ask for confirmation in the project window first; an allowed origin is remembered for the session. Requests with a non-loopback Host are refused.

Browser previews keep a random port per root, so a fixed port never hands one project's browser storage to another.

Co-authored-by: arookieofc <[email protected]>
2026-12-04 04:26:18 +08:00

162 рядки
5.6 KiB
TypeScript
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
/**
* Web browsers (IDEA's Open In › Browser and the editor's browser bar): HTML
* and SVG files open in an installed browser through the built-in server, and
* HTML files get a built-in preview beside the editor. ⌃⇧R in an HTML editor
* opens the default browser, like IDEA's Run for HTML files. HTML files with
* Django template tags offer the Django Template language.
*/
import { registerActions } from "@/platform/actions";
import { getContext } from "@/platform/context";
import { onFileTypesChange } from "@/platform/fileTypes";
import { onIdeEvent } from "@/platform/ipc";
import { basename } from "@/platform/project";
import { lazyComponent, type MenuItem } from "@/ui";
import {
maybeEditorService,
registerEditorBanner,
registerMenuGroup,
registerOverlay,
registerTextEditorWrapper,
useTextEditorWrappers,
} from "@/workbench/contributions";
import { FileApiConfirmHost } from "./FileApiConfirmDialog";
import { dropFileApiConfirm, enqueueFileApiConfirm, useFileApiConfirm } from "./fileApiConfirm";
import { activeEditor, registerEditorContribution } from "@/editor/api";
import { djangoContentWatcher, djangoTemplateBanner } from "./djangoTemplates";
import {
browserIcon,
isHtmlPath,
isWebPagePath,
isWebPath,
loadBrowsers,
openInBrowser,
togglePreview,
useBrowsers,
useWebPreview,
} from "./browsers";
import { tx } from "@/platform/i18n";
interface OpenArg {
path?: string;
browser?: string;
}
function targetPath(arg: unknown): string | null {
if (typeof arg === "string") return arg;
const path = (arg as OpenArg | undefined)?.path ?? getContext().editorPath;
return typeof path === "string" ? path : null;
}
async function showPreview(path: string) {
await maybeEditorService()?.openFile(path, { focus: 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 {
const html = isHtmlPath(path);
return {
label: "Browser",
icon: "web",
submenu: [
{ kind: "action", action: "web.openInBrowser", arg: { path }, label: "Default Browser" },
{ kind: "separator" },
...(html ? [{ label: "Built-in Preview", icon: "eye", run: () => void showPreview(path) }] : []),
...useBrowsers.getState().list.map((b): MenuItem => ({
label: b.name,
verbatim: true,
icon: browserIcon(b.id),
run: () => void openInBrowser(path, b.id),
})),
],
};
}
export function activate(): () => void {
void loadBrowsers();
const disposers = [
registerOverlay({ id: "web.fileApiConfirm", component: FileApiConfirmHost }),
onIdeEvent("fileApiConfirm", (e) =>
enqueueFileApiConfirm({ id: e.id, source: e.source, path: e.path, line: e.line }),
),
onIdeEvent("fileApiConfirmDone", (e) => dropFileApiConfirm(e.id)),
// Open tabs gain or lose the browser bar and preview when File Types change.
onFileTypesChange(() => useTextEditorWrappers.setState((w) => ({ wrappers: new Map(w.wrappers) }))),
registerEditorBanner(djangoTemplateBanner),
registerEditorContribution({ id: "web.djangoContent", order: 57, extensions: djangoContentWatcher }),
registerTextEditorWrapper({
id: "web.html",
matches: isHtmlPath,
component: lazyComponent(() => import("./WebEditor").then((m) => m.WebEditor)),
}),
registerActions([
{
id: "web.openInBrowser",
title: "Open in Browser",
description: "Open the HTML or SVG file in the default browser through the built-in server",
category: "View",
icon: "web",
when: "projectOpen",
keyWhen: "textEditorActive && editorLangId == html",
keywords: ["browser", "html", "preview", "chrome", "firefox", "safari", "run"],
run: ({ arg }) => {
const path = targetPath(arg);
if (path && isWebPath(path)) return openInBrowser(path, (arg as OpenArg | undefined)?.browser ?? null);
},
},
{
id: "web.togglePreview",
title: "Built-in Preview",
description: "Show the HTML file rendered beside the editor",
category: "View",
icon: "eye",
when: "textEditorActive && editorLangId == html",
keywords: ["browser", "html", "preview", "live"],
toggled: () => {
const tabId = previewTab(undefined);
return !!tabId && !!useWebPreview.getState()[tabId];
},
run: ({ arg }) => {
const tabId = previewTab(arg);
if (tabId) togglePreview(tabId);
},
},
]),
registerMenuGroup({
id: "web.open",
menus: ["editor", "editorTab", "projectView"],
order: 5,
items: (_menu, { paths, dirs }) => {
const path = paths.length === 1 && !dirs?.[0] ? paths[0]! : null;
if (!path || !isWebPagePath(path)) return [];
return [
{
kind: "action",
action: "web.openInBrowser",
arg: { path },
label: tx`Open '${basename(path)}' in Browser`,
icon: "web",
},
];
},
}),
registerMenuGroup({
id: "web.openIn",
menus: ["openIn"],
items: (_menu, { paths, dirs }) =>
paths.length === 1 && !dirs?.[0] && isWebPath(paths[0]!) ? [browserMenu(paths[0]!)] : [],
}),
];
return () => {
disposers.reverse().forEach((d) => d());
useFileApiConfirm.setState({ queue: [] });
};
}