|
@@ -14,30 +14,46 @@ API in response to the `ondragstart` event.
|
|
|
|
|
|
## Example
|
|
|
|
|
|
-Starting with a working application from the
|
|
|
-[Quick Start Guide](quick-start.md), add the following lines to the
|
|
|
-`index.html` file:
|
|
|
+An example demonstrating how you can create a file on the fly to be dragged out of the window.
|
|
|
+
|
|
|
+### Preload.js
|
|
|
+
|
|
|
+In `preload.js` use the [`contextBridge`] to inject a method `window.electron.startDrag(...)` that will send an IPC message to the main process.
|
|
|
+
|
|
|
+```js
|
|
|
+const { contextBridge, ipcRenderer } = require('electron')
|
|
|
+const path = require('path')
|
|
|
+
|
|
|
+contextBridge.exposeInMainWorld('electron', {
|
|
|
+ startDrag: (fileName) => {
|
|
|
+ ipcRenderer.send('ondragstart', path.join(process.cwd(), fileName))
|
|
|
+ }
|
|
|
+})
|
|
|
+```
|
|
|
+
|
|
|
+### Index.html
|
|
|
+
|
|
|
+Add a draggable element to `index.html`, and reference your renderer script:
|
|
|
|
|
|
```html
|
|
|
-<a href="#" id="drag">Drag me</a>
|
|
|
+<div style="border:2px solid black;border-radius:3px;padding:5px;display:inline-block" draggable="true" id="drag">Drag me</div>
|
|
|
<script src="renderer.js"></script>
|
|
|
```
|
|
|
|
|
|
-and add the following lines to the `renderer.js` file:
|
|
|
+### Renderer.js
|
|
|
|
|
|
-```javascript
|
|
|
-const { ipcRenderer } = require('electron')
|
|
|
+In `renderer.js` set up the renderer process to handle drag events by calling the method you added via the [`contextBridge`] above.
|
|
|
|
|
|
+```javascript
|
|
|
document.getElementById('drag').ondragstart = (event) => {
|
|
|
event.preventDefault()
|
|
|
- ipcRenderer.send('ondragstart', '/absolute/path/to/the/item')
|
|
|
+ window.electron.startDrag('drag-and-drop.md')
|
|
|
}
|
|
|
```
|
|
|
|
|
|
-The code above instructs the Renderer process to handle the `ondragstart` event
|
|
|
-and forward the information to the Main process.
|
|
|
+### Main.js
|
|
|
|
|
|
-In the Main process(`main.js` file), expand the received event with a path to the file that is
|
|
|
+In the Main process (`main.js` file), expand the received event with a path to the file that is
|
|
|
being dragged and an icon:
|
|
|
|
|
|
```javascript fiddle='docs/fiddles/features/drag-and-drop'
|
|
@@ -56,3 +72,5 @@ the item from the BrowserWindow onto your desktop. In this guide,
|
|
|
the item is a Markdown file located in the root of the project:
|
|
|
|
|
|

|
|
|
+
|
|
|
+[`contextBridge`]: ../api/context-bridge.md
|