* Working * Working * Make the native-file drag and drop documents use context bridge * Add per-file sections * Use the updated link format * Use path.join instead of string interpolation. Co-authored-by: Antón Molleda <molant@users.noreply.github.com> * Use fs.promises Co-authored-by: Antón Molleda <molant@users.noreply.github.com> * Update docs/tutorial/native-file-drag-drop.md Co-authored-by: Antón Molleda <molant@users.noreply.github.com> * fix formatting Co-authored-by: Antón Molleda <molant@users.noreply.github.com> * Update docs/tutorial/native-file-drag-drop.md Co-authored-by: Antón Molleda <molant@users.noreply.github.com> * Use more path.join instead of interpolation * Update with PR suggestions * Remove process.cwd() and add more example elements * Minor text fix * Fix typo Co-authored-by: Erick Zhao <erick@hotmail.ca> Co-authored-by: Antón Molleda <molant@users.noreply.github.com> Co-authored-by: Erick Zhao <erick@hotmail.ca>
		
			
				
	
	
		
			48 lines
		
	
	
	
		
			1.2 KiB
			
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			48 lines
		
	
	
	
		
			1.2 KiB
			
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
const { app, BrowserWindow, ipcMain, nativeImage, NativeImage } = require('electron')
 | 
						|
const path = require('path')
 | 
						|
const fs = require('fs')
 | 
						|
const https = require('https')
 | 
						|
 | 
						|
function createWindow() {
 | 
						|
  const win = new BrowserWindow({
 | 
						|
    width: 800,
 | 
						|
    height: 600,
 | 
						|
    webPreferences: {
 | 
						|
      preload: path.join(__dirname, 'preload.js')
 | 
						|
    }
 | 
						|
  })
 | 
						|
 | 
						|
  win.loadFile('index.html')
 | 
						|
}
 | 
						|
 | 
						|
const iconName = path.join(__dirname, 'iconForDragAndDrop.png');
 | 
						|
const icon = fs.createWriteStream(iconName);
 | 
						|
 | 
						|
// Create a new file to copy - you can also copy existing files.
 | 
						|
fs.writeFileSync(path.join(__dirname, 'drag-and-drop-1.md'), '# First file to test drag and drop')
 | 
						|
fs.writeFileSync(path.join(__dirname, 'drag-and-drop-2.md'), '# Second file to test drag and drop')
 | 
						|
 | 
						|
https.get('https://img.icons8.com/ios/452/drag-and-drop.png', (response) => {
 | 
						|
  response.pipe(icon);
 | 
						|
});
 | 
						|
 | 
						|
app.whenReady().then(createWindow)
 | 
						|
 | 
						|
ipcMain.on('ondragstart', (event, filePath) => {
 | 
						|
  event.sender.startDrag({
 | 
						|
    file: path.join(__dirname, filePath),
 | 
						|
    icon: iconName,
 | 
						|
  })
 | 
						|
})
 | 
						|
 | 
						|
app.on('window-all-closed', () => {
 | 
						|
  if (process.platform !== 'darwin') {
 | 
						|
    app.quit()
 | 
						|
  }
 | 
						|
})
 | 
						|
 | 
						|
app.on('activate', () => {
 | 
						|
  if (BrowserWindow.getAllWindows().length === 0) {
 | 
						|
    createWindow()
 | 
						|
  }
 | 
						|
})
 |