You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

83 regels
2.6 KiB

  1. // Modules to control application life and create native browser window
  2. const { app, BrowserWindow, protocol, ipcMain, dialog } = require('electron')
  3. const { request } = require('http')
  4. const path = require('path')
  5. const url = require('url')
  6. function createWindow() {
  7. // Create the browser window.
  8. const mainWindow = new BrowserWindow({
  9. width: 1080,
  10. height: 600,
  11. webPreferences: {
  12. nodeIntegration: true,
  13. // javascript: true,
  14. // plugins: true,
  15. preload: path.join(__dirname, 'electron-main', 'preload.js'),
  16. }
  17. })
  18. // const startUrl = url.format({
  19. // pathname: path.join(__dirname, './dist/index.html'),
  20. // protocol: 'file:',
  21. // slashes: true
  22. // });
  23. // console.log('startUrl:', startUrl)
  24. // and load the index.html of the app.
  25. mainWindow.loadFile('./dist/index.html')
  26. // Open the DevTools.
  27. mainWindow.webContents.openDevTools()
  28. }
  29. // This method will be called when Electron has finished
  30. // initialization and is ready to create browser windows.
  31. // Some APIs can only be used after this event occurs.
  32. app.whenReady().then(() => {
  33. createWindow()
  34. protocol.interceptFileProtocol('file', (request, callback) => {
  35. const reqUrl = request.url.replace(/file:[/\\]*/, '');
  36. //
  37. let url = reqUrl;
  38. const urls = reqUrl.split(/\/static\//);
  39. if (urls.length > 1) {
  40. url = path.resolve(__dirname, 'dist/static', urls.slice(1).join('/static/'));
  41. }
  42. // console.log('incomming url: ', reqUrl, ' resolved url:', url);
  43. callback({ path: url });
  44. })
  45. app.on('activate', function () {
  46. // On macOS it's common to re-create a window in the app when the
  47. // dock icon is clicked and there are no other windows open.
  48. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  49. })
  50. })
  51. // Quit when all windows are closed, except on macOS. There, it's common
  52. // for applications and their menu bar to stay active until the user quits
  53. // explicitly with Cmd + Q.
  54. app.on('window-all-closed', function () {
  55. if (process.platform !== 'darwin') app.quit()
  56. })
  57. // In this file you can include the rest of your app's specific main process
  58. // code. You can also put them in separate files and require them here.
  59. // 监听必要的自定义事件
  60. /**
  61. * 项目中的文件上传
  62. */
  63. ipcMain.handle('project-upload-file', async (event, args) => {
  64. const res = await dialog.showOpenDialog({
  65. properties: ['openFile'],
  66. });
  67. return res;
  68. // dialog.showOpenDialog({
  69. // properties: ['openFile'],
  70. // }).then((files) => {
  71. // if(files){
  72. // event.sender.send('project-selected-upload-file', files);
  73. // }
  74. // })
  75. });