Файли
openidea/src/platform/fileManager.ts
T
Luis Felipeтаluisfelipe18 9f643af069 feat(web): openidea-server --web serves the IDE to a browser (#5134)
`openidea-server --web` serves the built frontend and the IDE backend over one HTTP + WebSocket port, directly or behind a reverse proxy that strips or keeps its path prefix.

- The rpc dispatcher gains an async `invoke` with a channel sink, so project-host commands, streaming channels and IDE events run over WebSockets as they do over `--stdio`.
- Token auth is on by default (a random token unless `--token` or `--no-auth`), binds 127.0.0.1 by default, and WebSocket upgrades must be same-origin.
- The browser serves the desktop shell commands with the trust classes the desktop declares (a test keeps them in step), and the desktop menu spec, now in `ide_host::menu`. Recent-project recording and project rename are shared ide-host ops.
- WebSocket output is queued without dropping channel data or events. A reloaded page gets back the project its window still has open; after a server restart the page reloads. One tab per window, with Take Over.
- Browser fallbacks for the title bar menu, path chooser, clipboard, dialogs, Settings and external links; a reconnecting banner and an unsaved-changes guard.
- `scripts/start-web.sh` and the guide (English and Chinese).

Co-authored-by: luisfelipe18 <[email protected]>
2026-11-26 23:00:28 +08:00

96 рядки
4.0 KiB
TypeScript

import type { RemoteTarget } from "./ipc";
import { call, commands, isTauri } from "./ipc";
import { usePlatform } from "./platformInfo";
import { useRemote } from "./remote";
import { isWebMode } from "./webBridge";
function wslSharePath(path: string, target: RemoteTarget | null): string {
if (target?.kind !== "wsl" || !path.startsWith("/")) return path;
return `\\\\wsl.localhost\\${target.distro}${path.replaceAll("/", "\\")}`;
}
function windowTarget(): RemoteTarget | null {
return useRemote.getState().session?.target ?? null;
}
/**
* A path in the form the local OS file manager's Shell APIs accept. Project
* paths use `/` separators and may be Windows verbatim paths, but Explorer's
* `ILCreateFromPathW` rejects `\\?\UNC\...`.
*/
export function fileManagerPath(path: string, os = usePlatform.getState().os, target = windowTarget()): string {
if (os !== "windows") return path;
path = wslSharePath(path, target);
if (/^\/\/\?\/unc\//i.test(path)) path = `\\\\${path.slice(8).replaceAll("/", "\\")}`;
else if (path.startsWith("//?/")) path = path.slice(4);
if (/^\\\\\?\\unc\\/i.test(path)) return `\\\\${path.slice(8)}`;
if (path.startsWith("\\\\?\\")) return path.slice(4);
if (path.startsWith("//")) return path.replaceAll("/", "\\");
if (/^[A-Za-z]:\//.test(path)) return path.replaceAll("/", "\\");
return path;
}
/** English source text; callers and the action registry translate it. */
export function fileManagerName(os = usePlatform.getState().os): string {
return os === "macos" ? "Finder" : os === "windows" ? "Explorer" : "Files";
}
export function showInFileManagerLabel(os = usePlatform.getState().os): string {
return os === "macos" ? "Show in Finder" : os === "windows" ? "Show in Explorer" : "Show in Files";
}
/** The project action has historically used "Reveal" on macOS. */
export function revealInFileManagerLabel(os = usePlatform.getState().os): string {
return os === "macos" ? "Reveal in Finder" : showInFileManagerLabel(os);
}
/**
* Whether this desktop's file manager can show paths of `target` (default: this window's; `null`: this
* desktop's own). SSH and dev-container paths exist only on the server; WSL ones are reachable on
* Windows through their `\\wsl.localhost` share.
*/
export function canRevealInFileManager(target = windowTarget(), os = usePlatform.getState().os): boolean {
if (isWebMode()) return false;
return target === null || (target.kind === "wsl" && os === "windows");
}
/**
* Reveals files in Explorer, Finder or the Linux desktop's file manager. `target` says where the paths
* live (default: this window's project host); `false` when this desktop cannot show them.
*/
export async function revealInFileManager(
paths: string | readonly string[],
target = windowTarget(),
): Promise<boolean> {
if (!isTauri() || isWebMode() || !canRevealInFileManager(target)) return false;
const list = (typeof paths === "string" ? [paths] : paths).map((path) => fileManagerPath(path, undefined, target));
if (!list.length) return false;
const { revealItemInDir } = await import("@tauri-apps/plugin-opener");
await revealItemInDir(list.length === 1 ? list[0]! : [...list]);
return true;
}
/**
* Whether this desktop can launch files of `target` (default: this window's). Only this desktop's own
* projects qualify: the backend refuses remote windows (SSH, dev container, WSL), whose files live on
* their server.
*/
export function canOpenWithSystemApp(target = windowTarget()): boolean {
return isTauri() && !isWebMode() && target === null;
}
/**
* Launches `path` with its associated application. The backend checks that the window is trusted and
* that the file lies inside its project. False when this desktop cannot, or the launch was refused.
*/
export async function openWithSystemApp(path: string, target = windowTarget()): Promise<boolean> {
if (!canOpenWithSystemApp(target)) return false;
try {
await call(commands.fsOpenWithSystem(path));
return true;
} catch (e) {
console.warn(`cannot open ${path} with its application`, e);
return false;
}
}