Files
openidea/src/features/web/browsers.ts
T
l0ng-ai cacddee19d feat(web): Open HTML in browsers with a built-in server; add Emmet
- ide-web: built-in static server on 127.0.0.1 that serves project files,
  plus detection and launching of installed browsers.
- Open In › Browser in the editor, project view and tab menus; browser bar
  and built-in preview beside HTML editors; ⌃⇧R opens the default browser.
- Emmet: Tab expansion in markup editors and Update Tag with Emmet.

Claude-Session: https://claude.ai/code/session_01SaPPsVPuk6ZTNsoUCNCbMv
2026-10-24 22:16:28 +08:00

52 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/** Installed browsers, the built-in server's URLs, and per-file preview state. */
import { create } from "zustand";
import { executeAction } from "@/platform/actions";
import { call, commands, isTauri, type Browser } from "@/platform/ipc";
import { hasIcon } from "@/ui";
import { notifications } from "@/workbench/contributions";
/** Files a browser can show (Open In › Browser). */
export const isWebPath = (path: string) => /\.(html?|xhtml|shtml|svg)$/i.test(path);
/** Files that get the browser bar and the built-in preview. */
export const isHtmlPath = (path: string) => /\.(html?|xhtml|shtml)$/i.test(path);
export const useBrowsers = create<{ list: Browser[] }>(() => ({ list: [] }));
export async function loadBrowsers(): Promise<void> {
if (!isTauri()) return;
try {
useBrowsers.setState({ list: await call(commands.webBrowsers()) });
} catch (e) {
console.warn("web browsers", e);
}
}
export const browserIcon = (id: string) => (hasIcon(`browser-${id}`) ? `browser-${id}` : "web");
/** Paths shown with the built-in preview beside the editor. */
export const useWebPreview = create<Record<string, boolean>>(() => ({}));
export function togglePreview(path: string, on = !useWebPreview.getState()[path]) {
useWebPreview.setState({ [path]: on });
}
/** The built-in server's URL for `path`. */
export function webUrl(path: string): Promise<string> {
return call(commands.webUrl(path));
}
/** Saves all documents (the server reads the disk), then opens `path` in `browser` or the default browser. */
export async function openInBrowser(path: string, browser: string | null = null): Promise<void> {
try {
await executeAction("file.save");
await call(commands.webOpen(await webUrl(path), browser));
} catch (e) {
notifications().notify({
level: "error",
title: "Cannot open the browser",
body: e instanceof Error ? e.message : String(e),
key: "web-open",
});
}
}