Skip to content

Instantly share code, notes, and snippets.

@felixrieseberg
Created May 21, 2024 15:50
Show Gist options
  • Save felixrieseberg/6c9e46a3c2e3211a84197c64dbc9fb7f to your computer and use it in GitHub Desktop.
Save felixrieseberg/6c9e46a3c2e3211a84197c64dbc9fb7f to your computer and use it in GitHub Desktop.
Electron Fiddle Gist
<!DOCTYPE html>
<html>
<head>
<title>Programmatic Drag Event</title>
<style>
#drag-source {
width: 200px;
height: 200px;
background-color: lightgreen;
margin-bottom: 20px;
}
#drop-target {
width: 200px;
height: 200px;
background-color: lightcoral;
}
</style>
</head>
<body>
<div id="drag-source" draggable="true">Drag Source</div>
<div id="drop-target">Drop Target</div>
<script src="./renderer.js"></script>
</body>
</html>
// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron')
const path = require('node:path')
let mainWindow
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
mainWindow.webContents.openDevTools({ mode: 'detach' })
}
// 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.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS 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 (BrowserWindow.getAllWindows().length === 0) createWindow()
mainWindow.webContents.send('trigger-drag-event', { x: 100, y: 100 });
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
// 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.
{
"name": "acidic-math-sneak-h0uql",
"productName": "acidic-math-sneak-h0uql",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "felix",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "31.0.0-beta.5"
}
}
const { ipcRenderer } = require('electron');
ipcRenderer.on('trigger-drag-event', (event, position) => {
const dragSource = document.getElementById('drag-source');
const dropTarget = document.getElementById('drop-target');
// Create and configure the DragEvent
const dragStartEvent = new DragEvent('dragstart', {
bubbles: true,
cancelable: true,
clientX: position.x,
clientY: position.y,
dataTransfer: new DataTransfer()
});
// Add some data to the dataTransfer object
dragStartEvent.dataTransfer.setData('text/plain', 'This is a drag data');
// Dispatch the dragstart event on the drag source
dragSource.dispatchEvent(dragStartEvent);
// Simulate the dragover event on the drop target
const dragOverEvent = new DragEvent('dragover', {
bubbles: true,
cancelable: true,
clientX: position.x,
clientY: position.y,
dataTransfer: dragStartEvent.dataTransfer
});
dropTarget.dispatchEvent(dragOverEvent);
// Simulate the drop event on the drop target
const dropEvent = new DragEvent('drop', {
bubbles: true,
cancelable: true,
clientX: position.x,
clientY: position.y,
dataTransfer: dragStartEvent.dataTransfer
});
dropTarget.dispatchEvent(dropEvent);
});
// Listen for drag and drop events on the drop target
document.getElementById('drop-target').addEventListener('dragover', (event) => {
event.preventDefault();
});
document.getElementById('drop-target').addEventListener('drop', (event) => {
event.preventDefault();
const data = event.dataTransfer.getData('text/plain');
alert(`Dropped data: ${data}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment