From 349a80e28b25c1a0cb82fea43791f96440d2e618 Mon Sep 17 00:00:00 2001 From: shixiaohua Date: Fri, 5 Jan 2024 18:46:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=89=93=E5=BC=80=E7=9B=AE=E5=BD=95,?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E6=96=87=E4=BB=B6=E5=8F=8A=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B9=E5=86=85=E5=AE=B9,=E5=9C=A8=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E5=B9=B6=E8=BF=94=E5=9B=9E=E6=88=90=E5=8A=9F=E5=93=8D=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 1 + main.js | 26 +++++++++++++++++--------- preload.js | 8 ++++---- src/comment/renderer.js | 13 ++++++++++--- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/index.html b/index.html index d1c9f20..2be3844 100644 --- a/index.html +++ b/index.html @@ -14,6 +14,7 @@
Drag me - File 2
+ 当前文件夹: \ No newline at end of file diff --git a/main.js b/main.js index c079524..5e7a2f8 100644 --- a/main.js +++ b/main.js @@ -4,6 +4,8 @@ const path = require('path') const fs = require('fs') const https = require('https') const {menuRebuild} = require('./src/comment/TopMenu.js') + +const iconName = path.join(__dirname, 'iconForDragAndDrop.png') // electorn const createWindow = () => { // Create the browser window. @@ -16,17 +18,29 @@ const createWindow = () => { }) // 加载 index.html win.loadFile('index.html') - // win.loadURL('http://localhost/show/sync?current=1&pageSize=5') // 打开开发工具 win.webContents.openDevTools() Menu.setApplicationMenu(Menu.buildFromTemplate(menuRebuild(win))) } -const iconName = path.join(__dirname, 'iconForDragAndDrop.png') + +const ipcMainOn = () => { + // ipcMain.handle('ping', () => 'pong') + ipcMain.on('open-dir-result', (_event, value) => { + console.log(value) + }) + ipcMain.on('ondragstart', (event, filePath) => { + console.log(filePath) + event.sender.startDrag({ + file: path.join(__dirname, filePath), + icon: iconName + }) + }) +} // 这段程序将会在 Electron 结束初始化和创建浏览器窗口的时候调用 // 部分 API 在 ready 事件触发后才能使用。 app.whenReady().then(() => { - // ipcMain.handle('ping', () => 'pong') + ipcMainOn() createWindow() // 在 macOS 系统内, 如果没有已开启的应用窗口 // 点击托盘图标时通常会重新创建一个新窗口 @@ -41,9 +55,3 @@ app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit() }) -ipcMain.on('ondragstart', (event, filePath) => { - event.sender.startDrag({ - file: path.join(__dirname, filePath), - icon: iconName - }) -}) \ No newline at end of file diff --git a/preload.js b/preload.js index e58e782..fabb4fa 100644 --- a/preload.js +++ b/preload.js @@ -2,9 +2,9 @@ // 所有的 Node.js API接口 都可以在 preload 进程中被调用. // 它拥有与Chrome扩展一样的沙盒。 const { contextBridge, ipcRenderer } = require('electron/renderer') -// 读取文件夹中文件,并传给渲染层 -// import { unlink } from 'node:fs/promises'; -contextBridge.exposeInMainWorld('electron', { +contextBridge.exposeInMainWorld('electronAPI', { startDrag : (fileName) => ipcRenderer.send('ondragstart', fileName), - listDirFile :(fileName)=>ipcRenderer.send('ondragstart', fileName) + openDirectory : (callback) => + ipcRenderer.on('openDirectory', (_event, value) => callback(value)), + openDirectoryResult:(value) => ipcRenderer.send('open-dir-result', value) }) \ No newline at end of file diff --git a/src/comment/renderer.js b/src/comment/renderer.js index b62be18..649b2ef 100644 --- a/src/comment/renderer.js +++ b/src/comment/renderer.js @@ -1,10 +1,17 @@ // html中使用的js代码 之后用react替换 document.getElementById('drag1').ondragstart = (event) => { event.preventDefault() - window.electron.startDrag('drag-and-drop-1.md') + window.electronAPI.startDrag('drag-and-drop-1.md') } document.getElementById('drag2').ondragstart = (event) => { event.preventDefault() - window.electron.startDrag('drag-and-drop-2.md') -} \ No newline at end of file + window.electronAPI.startDrag('drag-and-drop-2.md') +} + + +const openDir=document.getElementById('openDir'); +window.electronAPI.openDirectory ((value) => { + openDir.innerText = JSON.stringify(value) + window.electronAPI.openDirectoryResult('ok') +}) \ No newline at end of file