Selaa lähdekoodia

全部模块化

gsx 2 vuotta sitten
vanhempi
commit
6011bcb47e

+ 4 - 6
electron/addon/autoUpdater/index.js

@@ -3,7 +3,7 @@ const { autoUpdater } = require("electron-updater");
 const is = require('ee-core/utils/is');
 const Log = require('ee-core/log');
 const Conf = require('ee-core/config');
-const Electron = require('ee-core/electron');
+const CoreWindow = require('ee-core/electron/window');
 
 /**
  * 自动升级插件
@@ -11,8 +11,7 @@ const Electron = require('ee-core/electron');
  */
 class AutoUpdaterAddon {
 
-  constructor(app) {
-    this.app = app;
+  constructor() {
   }
 
   /**
@@ -20,8 +19,6 @@ class AutoUpdaterAddon {
    */
   create () {
     Log.info('[addon:autoUpdater] load');
-
-    // const app = this.app;
     const cfg = Conf.getValue('addons.autoUpdater');
     if ((is.windows() && cfg.windows)
         || (is.macOS() && cfg.macOS)
@@ -131,7 +128,8 @@ class AutoUpdaterAddon {
   sendStatusToWindow(content = {}) {
     const textJson = JSON.stringify(content);
     const channel = 'app.updater';
-    Electron.mainWindow.webContents.send(channel, textJson);
+    const win = CoreWindow.getMainWindow();
+    win.webContents.send(channel, textJson);
   }
   
   /**

+ 3 - 5
electron/addon/awaken/index.js

@@ -8,8 +8,7 @@ const Conf = require('ee-core/config');
  */
 class AwakenAddon {
 
-  constructor(app) {
-    this.app = app;
+  constructor() {
     this.protocol = '';
   }
 
@@ -24,17 +23,16 @@ class AwakenAddon {
   
     electronApp.setAsDefaultProtocolClient(this.protocol);
   
-    const self = this;
     this.handleArgv(process.argv);
     electronApp.on('second-instance', (event, argv) => {
       if (process.platform === 'win32') {
-        self.handleArgv(argv)
+        this.handleArgv(argv)
       }
     })
   
     // 仅用于macOS
     electronApp.on('open-url', (event, urlStr) => {
-      self.handleUrl(urlStr)
+      this.handleUrl(urlStr)
     })
   }
 

+ 2 - 3
electron/addon/chromeExtension/index.js

@@ -5,13 +5,12 @@ const path = require('path');
 const Log = require('ee-core/log');
 
 /**
- * 安全插件
+ * 扩展插件 (electron自身对该功能并不完全支持,官方也不建议使用)
  * @class
  */
 class ChromeExtensionAddon {
 
-  constructor(app) {
-    this.app = app;
+  constructor() {
   }
 
   /**

+ 1 - 2
electron/addon/javaServer/index.js

@@ -10,8 +10,7 @@ const GetPort = require('ee-core/utils/get-port');
  */
 class JavaServerAddon {
 
-  constructor(app) {
-    this.app = app;
+  constructor() {
     this.cfg;
     this.javaServer;
   }

+ 4 - 3
electron/addon/security/index.js

@@ -1,4 +1,5 @@
 const Log = require('ee-core/log');
+const EE = require('ee-core/ee');
 
 /**
  * 安全插件
@@ -6,8 +7,7 @@ const Log = require('ee-core/log');
  */
 class SecurityAddon {
 
-  constructor(app) {
-    this.app = app;
+  constructor() {
   }
 
   /**
@@ -15,6 +15,7 @@ class SecurityAddon {
    */
   create () {
     Log.info('[addon:security] load');
+    const { CoreApp } = EE;
     const runWithDebug = process.argv.find(function(e){
       let isHasDebug = e.includes("--inspect") || e.includes("--inspect-brk") || e.includes("--remote-debugging-port");
       return isHasDebug;
@@ -23,7 +24,7 @@ class SecurityAddon {
     // 不允许远程调试
     if (runWithDebug) {
       Log.error('[error] Remote debugging is not allowed,  runWithDebug:', runWithDebug);
-      this.app.appQuit();
+      CoreApp.appQuit();
     }
   }
 }

+ 8 - 15
electron/addon/tray/index.js

@@ -1,9 +1,11 @@
-const { Tray, Menu, shell } = require('electron');
+const { Tray, Menu } = require('electron');
 const path = require('path');
 const Ps = require('ee-core/ps');
 const Log = require('ee-core/log');
 const Electron = require('ee-core/electron');
+const CoreWindow = require('ee-core/electron/window');
 const Conf = require('ee-core/config');
+const EE = require('ee-core/ee');
 
 /**
  * 托盘插件
@@ -11,8 +13,7 @@ const Conf = require('ee-core/config');
  */
 class TrayAddon {
 
-  constructor(app) {
-    this.app = app;
+  constructor() {
     this.tray = null;
   }
 
@@ -24,10 +25,9 @@ class TrayAddon {
     if (Ps.isDev() && Ps.isHotReload()) return;
     
     Log.info('[addon:tray] load');
-
-    const app = this.app;
+    const { CoreApp } = EE;
     const cfg = Conf.getValue('addons.tray');
-    const { mainWindow } = Electron;
+    const mainWindow = CoreWindow.getMainWindow();
 
     // 托盘图标
     let iconPath = path.join(Ps.getHomeDir(), cfg.icon);
@@ -43,15 +43,14 @@ class TrayAddon {
       {
         label: '退出',
         click: function () {
-          app.appQuit();
+          CoreApp.appQuit();
         }
       }
     ]
   
     // 点击关闭,最小化到托盘
     mainWindow.on('close', (event) => {
-      const extraObj = Electron.extra;
-      if (extraObj.closeWindow == true) {
+      if (Electron.extra.closeWindow == true) {
         return;
       }
       mainWindow.hide();
@@ -63,12 +62,6 @@ class TrayAddon {
     this.tray.setToolTip(cfg.title);
     const contextMenu = Menu.buildFromTemplate(trayMenuTemplate);
     this.tray.setContextMenu(contextMenu);
-
-    // 使用默认浏览器打开链接
-    mainWindow.webContents.setWindowOpenHandler(({ url }) => {
-      shell.openExternal(url);
-      return { action: 'deny' }
-    })
   }
 }
 

+ 4 - 5
electron/controller/example.js

@@ -2,6 +2,7 @@
 
 const { Controller } = require('ee-core');
 const Log = require('ee-core/log');
+const Services = require('ee-core/services');
 
 /**
  * example
@@ -24,12 +25,10 @@ class ExampleController extends Controller {
    * test
    */
   async test () {
-    const result = await this.service.example.test('electron');
+    const result = await Services.get('example').test('electron');
+    Log.info('service result:', result);
 
-    let tmpDir = Ps.getLogDir();
-    Log.info('tmpDir:', tmpDir);
-
-    return result;
+    return 'hello electron-egg';
   }
 }
 

+ 49 - 0
electron/index.js

@@ -0,0 +1,49 @@
+const { Application } = require('ee-core');
+
+class Index extends Application {
+
+  constructor() {
+    super();
+    // this === eeApp;
+  }
+
+  /**
+   * core app have been loaded
+   */
+  async ready () {
+    // do some things
+  }
+
+  /**
+   * electron app ready
+   */
+  async electronAppReady () {
+    // do some things
+  }
+
+  /**
+   * main window have been loaded
+   */
+  async windowReady () {
+    // do some things
+    // 延迟加载,无白屏
+    const winOpt = this.config.windowsOption;
+    if (winOpt.show == false) {
+      const win = this.electron.mainWindow;
+      win.once('ready-to-show', () => {
+        win.show();
+      })
+    }
+  }
+
+  /**
+   * before app close
+   */  
+  async beforeClose () {
+    // do some things
+
+  }
+}
+
+Index.toString = () => '[class Index]';
+module.exports = Index;

+ 12 - 16
electron/preload/index.js

@@ -1,20 +1,16 @@
 /*************************************************
  ** preload为预加载模块,该文件将会在程序启动时加载 **
  *************************************************/
+ const Addon = require('ee-core/addon');
 
-/**
- * 预加载模块入口
- */
-module.exports = async (app) => {
-
-  //已实现的功能模块,可选择性使用和修改
-  const trayAddon = app.addon.tray;
-  const securityAddon = app.addon.security;
-  const awakenAddon = app.addon.awaken;
-  const autoUpdaterAddon = app.addon.autoUpdater;
-  
-  trayAddon.create();
-  securityAddon.create();
-  awakenAddon.create();
-  autoUpdaterAddon.create();
-}
+ /**
+  * 预加载模块入口
+  */
+ module.exports = async () => {
+ 
+   // 示例功能模块,可选择性使用和修改
+   Addon.get('tray').create();
+   Addon.get('security').create();
+   Addon.get('awaken').create();
+   Addon.get('autoUpdater').create();
+ }

+ 2 - 50
main.js

@@ -1,50 +1,2 @@
-const { Application } = require('ee-core');
-const EE = require('ee-core/ee');
-
-class Main extends Application {
-
-  constructor() {
-    super();
-    // this === eeApp;
-  }
-
-  /**
-   * core app have been loaded
-   */
-  async ready () {
-    // do some things
-  }
-
-  /**
-   * electron app ready
-   */
-  async electronAppReady () {
-    // do some things
-  }
-
-  /**
-   * main window have been loaded
-   */
-  async windowReady () {
-    // do some things
-    // 延迟加载,无白屏
-    const winOpt = this.config.windowsOption;
-    if (winOpt.show == false) {
-      const win = this.electron.mainWindow;
-      win.once('ready-to-show', () => {
-        win.show();
-      })
-    }
-  }
-
-  /**
-   * before app close
-   */  
-  async beforeClose () {
-    // do some things
-
-  }
-}
-
-// Instantiate an app object
-EE.app = new Main();
+const { ElectronEgg } = require('ee-core');
+new ElectronEgg();

+ 6 - 2
package.json

@@ -1,6 +1,6 @@
 {
   "name": "ee",
-  "version": "3.2.0",
+  "version": "3.3.0",
   "description": "A fast, desktop software development framework",
   "main": "main.js",
   "scripts": {
@@ -107,10 +107,14 @@
     "electron": "^13.6.9",
     "electron-builder": "^23.6.0",
     "electron-rebuild": "^3.2.8",
+    "eslint": "^5.13.0",
+    "eslint-plugin-prettier": "^3.0.1",
     "nodemon": "^2.0.16"
   },
   "dependencies": {
-    "ee-core": "^2.1.1",
+    "better-sqlite3": "^7.6.2",
+    "dayjs": "^1.10.7",
+    "ee-core": "^2.2.1",
     "electron-updater": "^5.3.0",
     "lodash": "^4.17.21"
   }