Skip to content

Commit

Permalink
docs: Added Drag and drop files Fiddle example
Browse files Browse the repository at this point in the history
  • Loading branch information
yashints committed Oct 8, 2019
1 parent c85648a commit a65b00b
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
78 changes: 78 additions & 0 deletions docs/fiddles/native-ui/drag-and-drop/index.html
@@ -0,0 +1,78 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Drag and drop files</title>
</head>

<body>
<div class="section-wrapper">
<h1>Drag and drop files</h1>

<h3>
Electron supports dragging files and content out from web content into
the operating system's world.
</h3>

<p>
Open the
<a href="https://electronjs.org/docs/tutorial/native-file-drag-drop"
>full API documentation<span class="u-visible-to-screen-reader"
>(opens in new window)</span
></a
>
in your browser.
</p>
</div>

<div class="demo">
<div class="demo-wrapper">
<h2>Dragging files</h2>
<div class="demo-box">
<div class="demo-controls">
<a href="#" id="drag-file-link">Drag Demo</a>
</div>
<p>
Click and drag the link above to copy the renderer process
javascript file on to your machine.
</p>

<p>
In this demo, the <code>webContents.startDrag()</code> API is called
in response to the <code>ondragstart</code> event.
</p>
<h5>Renderer Process</h5>
<pre><code>
const {ipcRenderer} = require('electron')

const dragFileLink = document.getElementById('drag-file-link')

dragFileLink.addEventListener('dragstart', (event) => {
event.preventDefault()
ipcRenderer.send('ondragstart', __filename)
})
</code></pre>
<h5>Main Process</h5>
<pre>
<code>
const {ipcMain} = require('electron')
const path = require('path')

ipcMain.on('ondragstart', (event, filepath) => {
const iconName = 'codeIcon.png'
event.sender.startDrag({
file: filepath,
icon: path.join(__dirname, iconName)
})
})
</code></pre>
</div>
</div>
</div>

<script>
// You can also require other files to run in this process
require("./renderer.js");
</script>
</body>
</html>
64 changes: 64 additions & 0 deletions docs/fiddles/native-ui/drag-and-drop/main.js
@@ -0,0 +1,64 @@
// Modules to control application life and create native browser window
const { app, BrowserWindow, ipcMain } = require('electron')
const path = require('path')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow

function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})

// and load the index.html of the app.
mainWindow.loadFile('index.html')

// Open the DevTools.
// mainWindow.webContents.openDevTools()

// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})

app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})

ipcMain.on('ondragstart', (event, filepath) => {
const iconName = 'codeIcon.png'
event.sender.startDrag({
file: filepath,
icon: path.join(__dirname, iconName)
})
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
8 changes: 8 additions & 0 deletions docs/fiddles/native-ui/drag-and-drop/renderer.js
@@ -0,0 +1,8 @@
const { ipcRenderer } = require('electron')

const dragFileLink = document.getElementById('drag-file-link')

dragFileLink.addEventListener('dragstart', event => {
event.preventDefault()
ipcRenderer.send('ondragstart', __filename)
})

0 comments on commit a65b00b

Please sign in to comment.