Browse Source

修复内容导出后,hosts 的选中状态全部变成未选中的问题。 #138

oldj 8 years ago
parent
commit
599f3c84bf
7 changed files with 67 additions and 47 deletions
  1. 50 0
      app/bg/events.js
  2. 4 3
      app/build/bundle.js
  3. 2 37
      app/main.js
  4. 1 1
      app/package.json
  5. 4 3
      app/src/components/panel/list.js
  6. 5 2
      app/src/modules/mainMenu.js
  7. 1 1
      app/version.js

+ 50 - 0
app/bg/events.js

@@ -0,0 +1,50 @@
+/**
+ * @author oldj
+ * @blog http://oldj.net
+ */
+
+'use strict';
+
+const fs = require('fs');
+const {ipcMain} = require('electron');
+
+exports.init = (app, contents) => {
+
+    ipcMain.on('show_app', () => {
+        app.emit('show');
+    });
+
+    ipcMain.on('to_add_host', () => {
+        if (contents && contents.send) {
+            contents.send('to_add_host');
+        }
+    });
+
+    ipcMain.on('to_export', (fn) => {
+        if (contents && contents.send) {
+            contents.send('get_export_data', fn);
+        }
+    });
+
+    ipcMain.on('export_data', (e, fn, data) => {
+        console.log(fn);
+        console.log(data);
+        fs.writeFile(fn, data, 'utf-8', (err) => {
+            if (err) {
+                electron.dialog.showErrorBox('error', err.message || 'Fail to export!');
+            }
+        });
+    });
+
+    ipcMain.on('to_import', (fn) => {
+        if (contents && contents.send) {
+            contents.send('to_import', fn);
+        }
+    });
+
+    ipcMain.on('relaunch', (fn) => {
+        app.relaunch({args: process.argv.slice(1) + ['--relaunch']});
+        app.exit(0);
+    });
+
+};

+ 4 - 3
app/build/bundle.js

@@ -22655,8 +22655,9 @@
 	            var data = Object.assign({}, {
 	                version: __webpack_require__(200).version,
 	                list: _this.state.list.map(function (item) {
-	                    item.on = false;
-	                    return item;
+	                    var new_item = Object.assign({}, item);
+	                    new_item.on = false;
+	                    return new_item;
 	                })
 	            });
 	            ipcRenderer.send('export_data', fn, JSON.stringify(data));
@@ -23396,7 +23397,7 @@
 
 	"use strict";
 	
-	exports.version = [3, 2, 2, 4210];
+	exports.version = [3, 2, 2, 4212];
 
 /***/ },
 /* 202 */

+ 2 - 37
app/main.js

@@ -72,6 +72,8 @@ function createWindow() {
             is_tray_initialized = true;
         }
     });
+
+    require('./bg/events').init(app, contents);
 }
 
 // This method will be called when Electron has finished
@@ -113,40 +115,3 @@ app.on('activate', function () {
 });
 
 app.on('before-quit', () => willQuitApp = true);
-
-electron.ipcMain.on('show_app', () => {
-    app.emit('show');
-});
-
-electron.ipcMain.on('to_add_host', () => {
-    if (contents && contents.send) {
-        contents.send('to_add_host');
-    }
-});
-
-electron.ipcMain.on('to_export', (fn) => {
-    if (contents && contents.send) {
-        contents.send('get_export_data', fn);
-    }
-});
-
-electron.ipcMain.on('export_data', (e, fn, data) => {
-    console.log(fn);
-    console.log(data);
-    fs.writeFile(fn, data, 'utf-8', (err) => {
-        if (err) {
-            electron.dialog.showErrorBox('error', err.message || 'Fail to export!');
-        }
-    });
-});
-
-electron.ipcMain.on('to_import', (fn) => {
-    if (contents && contents.send) {
-        contents.send('to_import', fn);
-    }
-});
-
-electron.ipcMain.on('relaunch', (fn) => {
-    app.relaunch({args: process.argv.slice(1) + ['--relaunch']});
-    app.exit(0);
-});

+ 1 - 1
app/package.json

@@ -27,4 +27,4 @@
     "yargs": "^6.6.0"
   },
   "devDependencies": {}
-}
+}

+ 4 - 3
app/src/components/panel/list.js

@@ -122,9 +122,10 @@ class List extends React.Component {
         ipcRenderer.on('get_export_data', (e, fn) => {
             let data = Object.assign({}, {
                 version: require('../../configs').version,
-                list: this.state.list.map((item) => {
-                    item.on = false;
-                    return item;
+                list: this.state.list.map(item => {
+                    let new_item = Object.assign({}, item);
+                    new_item.on = false;
+                    return new_item;
                 })
             });
             ipcRenderer.send('export_data', fn, JSON.stringify(data));

+ 5 - 2
app/src/modules/mainMenu.js

@@ -14,6 +14,7 @@ const version = require('../../version').version;
 
 exports.init = function (app, sys_lang = 'en') {
     let lang = m_lang.getLang(pref.get('user_language', sys_lang));
+    let last_path = null;
 
     const template = [
         {
@@ -33,7 +34,7 @@ exports.init = function (app, sys_lang = 'en') {
                     click: () => {
                         dialog.showOpenDialog({
                             title: lang.import,
-                            defaultPath: path.join(paths.home_path, 'sh.json'),
+                            defaultPath: path.join(last_path || paths.home_path, 'sh.json'),
                             filters: [
                                 {name: 'JSON', extensions: ['json']},
                                 {name: 'All Files', extensions: ['*']}
@@ -41,6 +42,7 @@ exports.init = function (app, sys_lang = 'en') {
                         }, (fns) => {
                             if (fns && fns.length > 0) {
                                 ipcMain.emit('to_import', fns[0]);
+                                last_path = path.dirname(fns[0]);
                             }
                         });
                     }
@@ -50,7 +52,7 @@ exports.init = function (app, sys_lang = 'en') {
                     click: () => {
                         dialog.showSaveDialog({
                             title: lang.export,
-                            defaultPath: path.join(paths.home_path, 'sh.json'),
+                            defaultPath: path.join(last_path || paths.home_path, 'sh.json'),
                             filters: [
                                 {name: 'JSON', extensions: ['json']},
                                 {name: 'All Files', extensions: ['*']}
@@ -58,6 +60,7 @@ exports.init = function (app, sys_lang = 'en') {
                         }, (fn) => {
                             if (fn) {
                                 ipcMain.emit('to_export', fn);
+                                last_path = path.dirname(fn);
                             }
                         });
                     }

+ 1 - 1
app/version.js

@@ -1 +1 @@
-exports.version = [3,2,2,4210];
+exports.version = [3,2,2,4212];