Skip to content

Commit 0b83f37

Browse files
committed
chore: file drag drop working in electron
1 parent 3dead95 commit 0b83f37

1 file changed

Lines changed: 51 additions & 10 deletions

File tree

src/utils/DragAndDrop.js

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,20 @@ define(function (require, exports, module) {
186186

187187
async function _focusAndOpenDroppedFiles(droppedPaths) {
188188
try{
189-
const currentWindow = window.__TAURI__.window.getCurrent();
190-
await currentWindow.setAlwaysOnTop(true);
191-
await currentWindow.setAlwaysOnTop(false);
189+
if(window.__TAURI__) {
190+
const currentWindow = window.__TAURI__.window.getCurrent();
191+
await currentWindow.setAlwaysOnTop(true);
192+
await currentWindow.setAlwaysOnTop(false);
193+
} else if(window.__ELECTRON__) {
194+
await window.electronAPI.focusWindow();
195+
}
192196
} catch (e) {
193197
console.error("Error focusing window");
194198
}
195199
openDroppedFiles(droppedPaths);
196200
}
197201

198-
if(Phoenix.isNativeApp){
202+
if(window.__TAURI__){
199203
window.__TAURI__.event.listen('file-drop-event-phoenix', ({payload})=> {
200204
if(!payload || !payload.pathList || !payload.pathList.length || !payload.windowLabelOfListener
201205
|| payload.windowLabelOfListener !== window.__TAURI__.window.appWindow.label){
@@ -302,10 +306,24 @@ define(function (require, exports, module) {
302306
event.dataTransfer.types && event.dataTransfer.types.includes("Files")){
303307
// in linux, there is a bug in ubuntu 24 where dropping a file will cause a ghost icon which only
304308
// goes away on reboot. So we dont support drop files in linux for now.
305-
showAndResizeFileDropWindow(event);
309+
if(window.__TAURI__) {
310+
showAndResizeFileDropWindow(event);
311+
}
306312
}
307313

308-
if (files && files.length) {
314+
if(window.__ELECTRON__ &&
315+
event.dataTransfer.types && event.dataTransfer.types.includes("Files")) {
316+
// In Electron (Chromium), dataTransfer.files is empty during dragover for
317+
// security reasons - only populated on drop. We must call preventDefault()
318+
// here based on types to allow the drop event to fire.
319+
event.stopPropagation();
320+
event.preventDefault();
321+
if ($(".modal.instance").length === 0) {
322+
event.dataTransfer.dropEffect = "copy";
323+
} else {
324+
event.dataTransfer.dropEffect = "none";
325+
}
326+
} else if (files && files.length) {
309327
event.stopPropagation();
310328
event.preventDefault();
311329

@@ -331,11 +349,34 @@ define(function (require, exports, module) {
331349
event.stopPropagation();
332350
event.preventDefault();
333351

334-
brackets.app.getDroppedFiles(function (err, paths) {
335-
if (!err) {
336-
openDroppedFiles(paths);
352+
if(window.__ELECTRON__) {
353+
// With contextIsolation, file.path is not available.
354+
// Use Electron's webUtils.getPathForFile() exposed via preload.
355+
const droppedVirtualPaths = [];
356+
for (const file of files) {
357+
const filePath = window.electronAPI.getPathForFile(file);
358+
if (filePath) {
359+
try {
360+
droppedVirtualPaths.push(
361+
window.fs.getTauriVirtualPath(filePath)
362+
);
363+
} catch (e) {
364+
console.error("Error resolving dropped path: ",
365+
filePath);
366+
}
367+
}
368+
}
369+
if (droppedVirtualPaths.length) {
370+
openDroppedFiles(droppedVirtualPaths);
337371
}
338-
});
372+
} else {
373+
// Browser fallback (legacy Brackets API)
374+
brackets.app.getDroppedFiles(function (err, paths) {
375+
if (!err) {
376+
openDroppedFiles(paths);
377+
}
378+
});
379+
}
339380
}
340381
}
341382

0 commit comments

Comments
 (0)