fix(plugins): stream dropped files and folders into plugin workbenches

This commit is contained in:
Jinpy authored and GitHub committed 2026-09-30 15:08:17 +08:00
1 parent ce5217bd23
commit be82fdc989
13 files changed
+1717 -671

No files matched your search

+2 -2
View File
@@ -5389,9 +5389,9 @@ export * from "@/lib/backend/mqtt-http";
// Plugin local file streaming (native dialogs / OS drops are Tauri-only)
// ---------------------------------------------------------------------------
import type { PluginLocalFileChunk, PluginLocalFileHandle, PluginLocalFileWriteResult } from "./tauri";
import type { PluginDroppedFilesResult, PluginLocalFileChunk, PluginLocalFileHandle, PluginLocalFileWriteResult } from "./tauri";
export async function openPluginLocalFile(_pluginId: string, _path: string, _write: boolean): Promise<PluginLocalFileHandle> {
export async function openDroppedPluginLocalFiles(_pluginId: string, _paths: string[]): Promise<PluginDroppedFilesResult> {
throw new Error("Plugin local file access is not available in the web backend");
}
+17 -6
View File
@@ -2736,6 +2736,8 @@ export interface PluginLocalFileHandle {
size: number;
contentType: string;
write: boolean;
/** Only for files expanded out of a dropped folder: '/'-separated path relative to the dropped folder root. */
relativePath?: string;
}
export interface PluginLocalFileChunk {
@@ -2749,14 +2751,23 @@ export interface PluginLocalFileWriteResult {
nextOffset: number;
}
export async function openPluginLocalFile(pluginId: string, path: string, write: boolean): Promise<PluginLocalFileHandle> {
return invoke("plugin_file_open", { pluginId, path, write });
}
// The native open/save dialogs run on the Rust side: the host never passes
// paths into the plugin-file registry, it only receives handles for what the
// user picked. Only the OS drop flow still goes through openPluginLocalFile,
// and the Rust command accepts exactly the paths its own drop pipeline granted.
// user picked. Only the OS drop flow goes through openDroppedPluginLocalFiles:
// the Rust command accepts exactly the paths its own drop pipeline granted to
// this webview (one open attempt per granted path); a granted folder expands
// to its contained files on the Rust side, and `truncated` flags any cap
// cutoff so partial delivery is visible to the plugin.
export interface PluginDroppedFilesResult {
dropId: string;
files: PluginLocalFileHandle[];
truncated: boolean;
}
export async function openDroppedPluginLocalFiles(pluginId: string, paths: string[]): Promise<PluginDroppedFilesResult> {
return invoke("plugin_file_open_dropped", { pluginId, paths });
}
export async function pickPluginLocalFiles(pluginId: string, multiple: boolean): Promise<PluginLocalFileHandle[]> {
return invoke("plugin_file_pick_files", { pluginId, multiple });
}
@@ -120,6 +120,8 @@ export interface PluginFileHandleMeta {
name: string;
size: number;
contentType: string;
/** Only for files expanded out of a dropped folder: '/'-separated path relative to the dropped folder root, so the plugin can rebuild the dragged tree. */
relativePath?: string;
}
export interface PluginPickFilesOptions {
@@ -250,6 +252,8 @@ export interface PluginHostBridgeApi {
finishFileSave?(pluginId: string, handleId: string): Promise<void>;
/** Close any file handle, discarding unsaved state. */
closeFileHandle?(pluginId: string, handleId: string): Promise<void>;
/** Whether this host receives OS file drops on the plugin's behalf (desktop only). */
receiveOsDrops?: boolean;
/** Persistent per-plugin key-value storage for sandboxed UIs; resolves null when the key is unset. */
storageGet?(pluginId: string, key: string): Promise<unknown>;
storageSet?(pluginId: string, key: string, value: unknown): Promise<void>;
@@ -460,6 +464,16 @@ export class PluginHostBridge {
clipboardRead: !!this.api.clipboardRead,
clipboardImageRead: !!this.api.clipboardReadImage,
mediaUrl: !!this.api.openMedia && !!this.api.closeMedia,
// The namespace exists on both hosts, but what it can DO differs:
// portable plugins gate on these flags instead of probing calls.
fileTransfer: {
pick: !!this.api.pickFiles,
beginSave: !!this.api.beginFileSave && !!this.api.writeFileChunk && !!this.api.finishFileSave,
read: !!this.api.readFileChunk,
// OS drops (and their folder expansion) are desktop-only.
drop: !!this.api.receiveOsDrops,
folderExpansion: !!this.api.receiveOsDrops,
},
},
context: snapshotPluginWorkbenchContext(this.context),
});
@@ -539,8 +553,8 @@ export class PluginHostBridge {
}
/** Hand the plugin already-opened handles for files dropped onto its workbench. */
forwardFileDrop(files: PluginFileHandleMeta[]): void {
this.post({ source: HOST_MESSAGE_SOURCE, version: BRIDGE_VERSION, type: "filedrop", files });
forwardFileDrop(files: PluginFileHandleMeta[], drop?: { dropId?: string; truncated?: boolean }): void {
this.post({ source: HOST_MESSAGE_SOURCE, version: BRIDGE_VERSION, type: "filedrop", files, dropId: drop?.dropId, truncated: drop?.truncated === true });
}
private async handleRequest(request: PluginRequestMessage, target: Window): Promise<void> {
@@ -1352,8 +1366,11 @@ export function pluginSdkSource(initialTheme?: PluginBridgeTheme): string {
document.dispatchEvent(new CustomEvent('dbx-plugin-binary', { detail: payload }));
} else if (message.type === 'filedrop') {
const files = Array.isArray(message.files) ? message.files : [];
listeners.filedrop.forEach((listener) => listener(files));
document.dispatchEvent(new CustomEvent('dbx-plugin-filedrop', { detail: files }));
// The drop metadata rides as a second listener argument so existing
// single-parameter listeners keep working unchanged.
const drop = { dropId: typeof message.dropId === 'string' ? message.dropId : undefined, truncated: message.truncated === true };
listeners.filedrop.forEach((listener) => listener(files, drop));
document.dispatchEvent(new CustomEvent('dbx-plugin-filedrop', { detail: { files, drop } }));
} else if (message.type === 'dragstate') {
const active = message.active === true;
listeners.dragstate.forEach((listener) => listener(active));