8000 [GSoD] docs: revised the drag and drop feature page by bandantonio · Pull Request #25939 · electron/electron · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[GSoD] docs: revised the drag and drop feature page #25939

8000
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added docs/images/drag-and-drop.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 33 additions & 12 deletions docs/tutorial/native-file-drag-drop.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,44 @@
# Native File Drag & Drop

## Overview

Certain kinds of applications that manipulate files might want to support
the operating system's native file drag & drop feature. Dragging files into
web content is common and supported by many websites. Electron additionally
supports dragging files and content out from web content into the operating
system's world.

To implement this feature in your app, you need to call `webContents.startDrag(item)`
To implement this feature in your app, you need to call the
[`webContents.startDrag(item)`](../api/web-contents.md#contentsstartdragitem)
API in response to the `ondragstart` event.

In your renderer process, handle the `ondragstart` event and forward the
information to your main process.
## Example

Starting with a working application from the
[Quick Start Guide](quick-start.md), add the following lines to the
`index.html` file:

```html
<a href="#" id="drag">item</a>
<script type="text/javascript" charset="utf-8">
document.getElementById('drag'). => {
event.preventDefault()
ipcRenderer.send('ondragstart', '/path/to/item')
}
</script>
<a href="#" id="drag">Drag me</a>
<script src="renderer.js"></script>
```

Then, in the main process, augment the event with a path to the file that is
being dragged and an icon.
and add the following lines to the `renderer.js` file:

```js
const { ipcRenderer } = require('electron')

document.getElementById('drag'). => {
event.preventDefault()
ipcRenderer.send('ondragstart', '/absolute/path/to/the/item')
}
```

The code above instructs the Renderer process to handle the `ondragstart` event
and forward the information to the Main process.

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
const { ipcMain } = require('electron')
Expand All @@ -35,3 +50,9 @@ ipcMain.on('ondragstart', (event, filePath) => {
})
})
```

After launching the Electron application, try to dragging and dropping
the item from the BroswerWindow onto your desktop. In this guide,
the item is a Markdown file located in the root of the project:

![Drag and drop](../images/drag-and-drop.gif)
0