![]() |
VOOZH | about |
ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime.
There are several differences between a Drag-and-Drop Operation in traditional web applications and Electron. One of the major differences is that Electron applications work with the native filesystem in the OS environment. Hence we need to obtain the absolute file path for any files dragged onto the Electron application from the native file dialog on the user's machine. Once we have obtained the file path, we can perform file operations using the NodeJS fs module or upload the file to a server. Electron makes use of the HTML5 File API to work with files in the native filesystem. This tutorial will demonstrate how to implement Drag-and-Drop functionality for native files in an Electron application.
We assume that you are familiar with the prerequisites as covered in the above-mentioned link. For Electron to work, node and npm need to be pre-installed in the system.
Example: We will start by building the basic Electron Application by following the given steps.
npm initnpm install electron --save{
"name": "electron-drag",
"version": "1.0.0",
"description": "File Drag and Drop in Electron",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"keywords": [
"electron"
],
"author": "Radhesh Khanna",
"license": "ISC",
"dependencies": {
"electron": "^8.3.0"
}
}
Output: At this point, our basic Electron Application is set up. To launch the Electron Application, run the Command:
npm start👁 GUI OutputUsing the HTML5 File API, users can directly work with the native files in the system OS environment. This is possible because the DOM's File Interface provides an abstraction for the underlying native filesystem. Electron enhances the DOM's File Interface by adding a path attribute to it. This path attribute exposes the absolute file path of the files on the filesystem. We will be making use of this functionality to get the absolute file path of a dragged-and-dropped file onto the Electron application. For more detailed Information, Refer this link.
All the Instance events of the Drag-and-Drop Operation belong to the DragEvent Interface. This event is a DOM Event that represents a drag-and-drop operation from start to finish. This Interface also inherits properties from the MouseEvent and the global Event Interface. It has specific Instance Properties for data transfer, GlobalEventHandlers and Instance Events which we have used in our code. For more detailed Information, Refer this link.
index.js: Add the following snippet in that file.
A Detailed explanation of all the Instance Events and Properties of the HTML5 File API used in the code are explained below. All the Instance Events of DragEvent Interface will be fired upon the global document object and cannot be directly fired on a Specific DOM element.
The dragStart, drag, dragend and dragexit Instance events will not be fired in this particular code example and hence have been excluded from the same. All of these Instance Events are fired on the Drag Target and in this case, drag Target does not exist in the application. The Drag Operation for files is initiated from outside the application from within the native filesystem dialog. All of the Instance events used in the above code are triggered on the Drop Target which lies within the application.
Note: The event.stopPropagation() method prevents propagation of the same event from being called. Propagation means transferring up to the Parent DOM elements or transferring down to the Child DOM elements.
Output: