mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-08 08:52:12 +00:00
- 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
52 lines
1.9 KiB
TypeScript
52 lines
1.9 KiB
TypeScript
/** 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",
|
||
});
|
||
}
|
||
}
|