diff --git a/.gitignore b/.gitignore index 6c335a8..1ac740d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ node_modules/ rpm-4.18.0 -out/ \ No newline at end of file +out/ +ueditor-1.4.3.3/ +ueditor/ \ No newline at end of file diff --git a/index.html b/index.html index 92adcb7..e919a80 100644 --- a/index.html +++ b/index.html @@ -1,17 +1,19 @@ - + - - Hello World! - - + + +

Hello World!

- We are using Node.js , - Chromium , - and Electron . - - - +

Drag the boxes below to somewhere in your OS (Finder/Explorer, Desktop, etc.) to copy an example markdown file.

+
Drag + me - File 1 +
+
Drag + me - File 2 +
+ + \ No newline at end of file diff --git a/main.js b/main.js index 3cf6fcc..de4add1 100644 --- a/main.js +++ b/main.js @@ -1,6 +1,8 @@ // npm start -const { app, BrowserWindow } = require('electron') +const { app, BrowserWindow , ipcMain } = require('electron') const path = require('path') +const fs = require('fs') +const https = require('https') // electorn const createWindow = () => { @@ -14,14 +16,26 @@ const createWindow = () => { }) // 加载 index.html win.loadFile('index.html') + // win.loadURL('http://localhost/show/sync?current=1&pageSize=5') // 打开开发工具 - // mainWindow.webContents.openDevTools() + win.webContents.openDevTools() } +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) +// }) // 这段程序将会在 Electron 结束初始化 // 和创建浏览器窗口的时候调用 // 部分 API 在 ready 事件触发后才能使用。 app.whenReady().then(() => { + // ipcMain.handle('ping', () => 'pong') createWindow() // 在 macOS 系统内, 如果没有已开启的应用窗口 // 点击托盘图标时通常会重新创建一个新窗口 @@ -34,4 +48,11 @@ app.whenReady().then(() => { // 直到用户使用 Cmd + Q 明确退出 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 a7bd987..e164dbb 100644 --- a/preload.js +++ b/preload.js @@ -1,12 +1,7 @@ // 所有的 Node.js API接口 都可以在 preload 进程中被调用. // 它拥有与Chrome扩展一样的沙盒。 -window.addEventListener('DOMContentLoaded', () => { - const replaceText = (selector, text) => { - const element = document.getElementById(selector) - if (element) element.innerText = text - } - - for (const dependency of ['chrome', 'node', 'electron']) { - replaceText(`${dependency}-version`, process.versions[dependency]) - } - }) \ No newline at end of file +const { contextBridge, ipcRenderer } = require('electron/renderer') + +contextBridge.exposeInMainWorld('electron', { + startDrag: (fileName) => ipcRenderer.send('ondragstart', fileName) +}) \ No newline at end of file diff --git a/preload1.js b/preload1.js new file mode 100644 index 0000000..a7bd987 --- /dev/null +++ b/preload1.js @@ -0,0 +1,12 @@ +// 所有的 Node.js API接口 都可以在 preload 进程中被调用. +// 它拥有与Chrome扩展一样的沙盒。 +window.addEventListener('DOMContentLoaded', () => { + const replaceText = (selector, text) => { + const element = document.getElementById(selector) + if (element) element.innerText = text + } + + for (const dependency of ['chrome', 'node', 'electron']) { + replaceText(`${dependency}-version`, process.versions[dependency]) + } + }) \ No newline at end of file diff --git a/preload2.js b/preload2.js new file mode 100644 index 0000000..5fd9da5 --- /dev/null +++ b/preload2.js @@ -0,0 +1,9 @@ +const { contextBridge, ipcRenderer } = require('electron') + +contextBridge.exposeInMainWorld('versions', { + node: () => process.versions.node, + chrome: () => process.versions.chrome, + electron: () => process.versions.electron, + ping: () => ipcRenderer.invoke('ping') + // 除函数之外,我们也可以暴露变量 +}) \ No newline at end of file diff --git a/renderer.js b/renderer.js index e69de29..4be771a 100644 --- a/renderer.js +++ b/renderer.js @@ -0,0 +1,9 @@ +document.getElementById('drag1').ondragstart = (event) => { + event.preventDefault() + window.electron.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 diff --git a/renderer2.js b/renderer2.js new file mode 100644 index 0000000..004a95b --- /dev/null +++ b/renderer2.js @@ -0,0 +1,4 @@ +const func = async () => { + const response = await window.versions.ping() + console.log(response) // 打印 'pong' + }