Файли
openidea/src/workbench/fileDrop.ts
T
eclix-del 66f0bce92f fix(editor): keep a re-dropped file saveable (#6328)
A file from outside the project, dropped into the editor again, stopped saving after the first save, silently. Three paths led there:
- onLocal set the dirty flag after updating the touched ranges, so an exception there left the buffer clean and every save a no-op. Dirty is now set first, and a failure to map the ranges falls back to treating the whole document as touched.
- A reused model kept the touched ranges of the previous tab; they are cleared when a clean model is reacquired.
- A file opened under its native Windows spelling (E:\code\a.sql) and the editor's E:/code/a.sql got two models sharing one backend document, and dirty/patch/language events reached only one. Events now go to every model of the document, and dropped paths are normalized with ipcPath (extended-length paths too).
save() also writes when the dirty flag lags but edits are still pending.

Refs #5528

Co-authored-by: eclix-del <[email protected]>
2026-11-27 14:24:00 +08:00

98 рядки
4.0 KiB
TypeScript

/**
* Files dropped from the OS onto the project (or LightEdit) window open in the
* editor, one tab each; a dropped folder opens as a project (the "Open project in"
* policy applies). Regions marked with `OWNS_FILE_DROP` (Project view, prompt box)
* handle their drops themselves.
*/
import { executeAction } from "@/platform/actions";
import { call, commands, isTauri } from "@/platform/ipc";
import { isDraggingOut } from "@/platform/dragOut";
import { useProject } from "@/platform/project";
import { isRemoteWindow } from "@/platform/remote";
import { nativeDropPoint } from "@/platform/nativeDrop";
import { ipcPath } from "@/platform/paths";
import { editorService, OWNS_FILE_DROP } from "./contributions";
const OWNER_ATTR = Object.keys(OWNS_FILE_DROP)[0]!;
type FileDropHandler = (paths: readonly string[], x: number, y: number) => boolean;
const fileDropHandlers = new Set<FileDropHandler>();
export function registerFileDropHandler(handler: FileDropHandler): () => void {
fileDropHandlers.add(handler);
return () => {
fileDropHandlers.delete(handler);
};
}
export interface FileDropDeps {
isDir(path: string): Promise<boolean>;
openFile(path: string, focus: boolean): Promise<void>;
openProject(path: string): Promise<void>;
}
/** Whether a drop at CSS pixel (x, y) lands on a region that handles OS drops itself. */
export function dropOwnedAt(x: number, y: number): boolean {
return !!document.elementFromPoint(x, y)?.closest(`[${OWNER_ATTR}]`);
}
/** Opens the dropped files (the last one focused), then the first dropped folder as a project. */
export async function openDropped(paths: readonly string[], deps: FileDropDeps): Promise<void> {
// Tauri hands out native paths (`E:\code\a.sql`); the editor keys documents and tabs by the
// `/`-separated spelling the project tree, Ctrl+P and the restored session use, so the native
// spelling would open a second model (and a second tab) for an already open file.
const editorPaths = paths.map(ipcPath);
const kinds = await Promise.all(editorPaths.map((p) => deps.isDir(p).catch(() => false)));
const files = editorPaths.filter((_, i) => !kinds[i]);
const dir = editorPaths.find((_, i) => kinds[i]);
for (const [i, path] of files.entries()) {
// oxlint-disable-next-line no-await-in-loop -- tabs open in drop order so the last dropped file is focused
await deps.openFile(path, i === files.length - 1).catch((e) => console.warn(`could not open ${path}`, e));
}
if (dir) await deps.openProject(dir);
}
const defaultDeps: FileDropDeps = {
isDir: (path) => call(commands.fsReadDir(path)).then(() => true),
openFile: (path, focus) => editorService().openFile(path, { focus }),
openProject: async (path) => void (await executeAction("file.openProjectPath", path)),
};
/** Handles one drop; null when it is left to another handler (no local project or LightEdit: the Welcome screen). */
export function handleFileDrop(
paths: readonly string[],
x: number,
y: number,
deps: FileDropDeps = defaultDeps,
): Promise<void> | null {
// Files dragged out of this window and dropped back on it: nothing to open.
if (isDraggingOut()) return Promise.resolve();
for (const handler of fileDropHandlers) if (paths.length && handler(paths, x, y)) return Promise.resolve();
const { project, lightEdit } = useProject.getState();
if (!paths.length || !(project || lightEdit) || isRemoteWindow() || dropOwnedAt(x, y)) return null;
return openDropped(paths, deps);
}
export function installFileDrop(): () => void {
if (!isTauri()) return () => {};
let unlisten: (() => void) | undefined;
let disposed = false;
void import("@tauri-apps/api/webview").then(({ getCurrentWebview }) =>
getCurrentWebview()
.onDragDropEvent((event) => {
const p = event.payload;
if (p.type !== "drop") return;
const { x, y } = nativeDropPoint(p.position);
void handleFileDrop(p.paths, x, y);
})
.then((u) => {
if (disposed) u();
else unlisten = u;
}),
);
return () => {
disposed = true;
unlisten?.();
};
}