Files
openidea/src/platform/paths.ts
T
arookieofcanda-rookie-of-C-language 2754cf327f feat(editor): drag an editor tab out to a standalone window (#470)
Dragging an editor tab past the window edge, or choosing Open in New Window from its menu, now opens the file in its own window and closes the tab here. A text file is saved first and a custom document runs its Save / Do not Save / Cancel guard once; if saving or opening fails, the tab stays. The new window is forced even when the project is already open elsewhere, and a file outside the project opens as LightEdit.

Dragging out tool windows is not covered. Refs #454

Co-authored-by: a-rookie-of-C-language <[email protected]>
2026-10-31 00:57:53 +08:00

30 lines
1.2 KiB
TypeScript

/**
* Paths coming *into* the UI from a native API (`appDataDir()` from
* `@tauri-apps/api/path`, the file dialogs) are native strings: `C:\a\b` on
* Windows. Everything the UI stores and compares — what the backend returns
* (`IpcPath` in `crates/ide-core/src/ipc_path.rs`), the editor's `ctx.path`, the
* project tree — is `/`-separated on every OS, so such a path has to be
* converted before it is used.
*/
const DRIVE = /^[A-Za-z]:\\/;
const NETWORK = /^\\\\/;
/**
* `p` in the form the UI holds paths: `C:\a\b` and `\\?\C:\a\b` become
* `C:/a/b`, `\\server\share` becomes `//server/share`. A path that already is
* in that form — or one that is not a Windows path at all — is returned as is.
*/
export function ipcPath(p: string): string {
let path = p;
if (path.startsWith("\\\\?\\")) path = path.startsWith("\\\\?\\UNC\\") ? `\\\\${path.slice(8)}` : path.slice(4);
return DRIVE.test(path) || NETWORK.test(path) ? path.replace(/\\/g, "/") : path;
}
/** True when `path` equals `ancestor` or lies below it. */
export function isSameOrInside(path: string, ancestor: string): boolean {
path = ipcPath(path);
ancestor = ipcPath(ancestor);
return path === ancestor || path.startsWith(ancestor.endsWith("/") ? ancestor : ancestor + "/");
}