Browse Source

save 初步实现。

oldj 8 years ago
parent
commit
a6475cbfe1
7 changed files with 84 additions and 37 deletions
  1. 36 21
      app/bundle.js
  2. 1 1
      app/server/Server.js
  3. 27 0
      app/server/actions/saveHosts.js
  4. 2 4
      ui/Agent.js
  5. 11 6
      ui/app.js
  6. 5 1
      ui/content/content.js
  7. 2 4
      ui/content/editor.js

+ 36 - 21
app/bundle.js

@@ -9540,13 +9540,7 @@ var App = function (_React$Component) {
       lang: {} // 语言
     };
 
-    _Agent2.default.pact('getHosts').then(function (data) {
-      _this.setState({
-        list: data.list,
-        sys_hosts: data.sys_hosts,
-        current: data.sys_hosts
-      });
-    });
+    _this.loadHosts();
 
     _Agent2.default.pact('getLang').then(function (lang) {
       _this.setState({ lang: lang });
@@ -9555,13 +9549,26 @@ var App = function (_React$Component) {
   }
 
   _createClass(App, [{
+    key: 'loadHosts',
+    value: function loadHosts() {
+      var _this2 = this;
+
+      _Agent2.default.pact('getHosts').then(function (data) {
+        _this2.setState({
+          list: data.list,
+          sys_hosts: data.sys_hosts,
+          current: data.sys_hosts
+        });
+      });
+    }
+  }, {
     key: 'setCurrent',
     value: function setCurrent(hosts) {
-      var _this2 = this;
+      var _this3 = this;
 
       if (hosts.is_sys) {
         _Agent2.default.act('getSysHosts', function (e, _hosts) {
-          _this2.setState({
+          _this3.setState({
             sys_hosts: _hosts,
             current: _hosts
           });
@@ -9575,10 +9582,13 @@ var App = function (_React$Component) {
   }, {
     key: 'toSave',
     value: function toSave() {
+      var _this4 = this;
+
       clearTimeout(this._t);
 
       this._t = setTimeout(function () {
-        _Agent2.default.emit('change');
+        //Agent.emit('change')
+        _Agent2.default.pact('saveHosts', _this4.state.list);
       }, 1000);
     }
   }, {
@@ -9615,7 +9625,7 @@ var App = function (_React$Component) {
         _react2.default.createElement(_content2.default, {
           current: current,
           readonly: App.isReadOnly(current),
-          setHostContent: this.setHostsContent.bind(this),
+          setHostsContent: this.setHostsContent.bind(this),
           lang: this.state.lang
         })
       );
@@ -9660,8 +9670,6 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen
 function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
 
 var IS_DEV = process.env.ENV === 'dev';
-//const SH_event = require('./ui/event').event
-//const SH_Agent = require('./ui/agent')
 
 var _require = require('electron'),
     ipcRenderer = _require.ipcRenderer;
@@ -9715,9 +9723,13 @@ function act(action, data, callback) {
   });
 }
 
-function pact(action, data) {
+function pact(action) {
+  for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
+    args[_key - 1] = arguments[_key];
+  }
+
   return new Promise(function (resolve, reject) {
-    return act(action, data, function (err, result) {
+    return act(action, args, function (err, result) {
       return err ? reject(err) : resolve(result);
     });
   });
@@ -21715,6 +21727,11 @@ var Content = function (_React$Component) {
   }
 
   _createClass(Content, [{
+    key: 'setValue',
+    value: function setValue(v) {
+      this.props.setHostsContent(v);
+    }
+  }, {
     key: 'render',
     value: function render() {
       var _props = this.props,
@@ -21765,8 +21782,8 @@ var Content = function (_React$Component) {
         ),
         _react2.default.createElement(_editor2.default, {
           readonly: this.props.readonly,
-          code: this.props.current.content || ''
-          //setValue={this.setValue.bind(this)}
+          code: this.props.current.content || '',
+          setValue: this.setValue.bind(this)
         })
       );
     }
@@ -31265,9 +31282,7 @@ var Editor = function (_React$Component) {
     _this.marks = [];
     _this.kw = '';
 
-    _this.state = {
-      code: _this.props.code
-    };
+    _this.state = {};
 
     _Agent2.default.on('search', function (kw) {
       _this.kw = kw;
@@ -31296,7 +31311,7 @@ var Editor = function (_React$Component) {
   }, {
     key: 'setValue',
     value: function setValue(v) {
-      //this.props.setValue(v)
+      this.props.setValue(v);
     }
   }, {
     key: 'toComment',

+ 1 - 1
app/server/Server.js

@@ -12,7 +12,7 @@ ipcMain.on('x', (e, d) => {
   let sender = e.sender
   let action = d.action
   if (typeof actions[action] === 'function') {
-    actions[action](...(d.args || []))
+    actions[action](...(d.data || []))
       .then(v => {
         sender.send(d.callback, [null, v])
       })

+ 27 - 0
app/server/actions/saveHosts.js

@@ -0,0 +1,27 @@
+/**
+ * @author oldj
+ * @blog https://oldj.net
+ */
+
+'use strict';
+
+const version = require('../../version').version
+const paths = require('../paths')
+const io = require('../io')
+const jsbeautify = require('js-beautify').js_beautify
+
+module.exports = (list) => {
+  let fn = paths.data_path
+  let data = {
+    list,
+    version
+  }
+  let cnt = JSON.stringify(data)
+  cnt = jsbeautify(cnt, {
+    indent_size: 2
+  })
+
+  // todo try to update system hosts
+
+  return io.pWriteFile(fn, cnt)
+}

+ 2 - 4
ui/Agent.js

@@ -6,8 +6,6 @@
 'use strict'
 
 const IS_DEV = process.env.ENV === 'dev'
-//const SH_event = require('./ui/event').event
-//const SH_Agent = require('./ui/agent')
 const {ipcRenderer} = require('electron')
 const platform = process.platform
 
@@ -44,8 +42,8 @@ function act (action, data, callback) {
   })
 }
 
-function pact (action, data) {
-  return new Promise((resolve, reject) => act(action, data,
+function pact (action, ...args) {
+  return new Promise((resolve, reject) => act(action, args,
     (err, result) => err ? reject(err) : resolve(result)))
 }
 

+ 11 - 6
ui/app.js

@@ -25,6 +25,14 @@ export default class App extends React.Component {
       lang: {} // 语言
     }
 
+    this.loadHosts()
+
+    Agent.pact('getLang').then(lang => {
+      this.setState({lang})
+    })
+  }
+
+  loadHosts () {
     Agent.pact('getHosts').then(data => {
       this.setState({
         list: data.list,
@@ -32,10 +40,6 @@ export default class App extends React.Component {
         current: data.sys_hosts
       })
     })
-
-    Agent.pact('getLang').then(lang => {
-      this.setState({lang})
-    })
   }
 
   setCurrent (hosts) {
@@ -61,7 +65,8 @@ export default class App extends React.Component {
     clearTimeout(this._t)
 
     this._t = setTimeout(() => {
-      Agent.emit('change')
+      //Agent.emit('change')
+      Agent.pact('saveHosts', this.state.list)
     }, 1000)
   }
 
@@ -94,7 +99,7 @@ export default class App extends React.Component {
         <Content
           current={current}
           readonly={App.isReadOnly(current)}
-          setHostContent={this.setHostsContent.bind(this)}
+          setHostsContent={this.setHostsContent.bind(this)}
           lang={this.state.lang}
         />
         {/*<div className="frames">*/}

+ 5 - 1
ui/content/content.js

@@ -19,6 +19,10 @@ export default class Content extends React.Component {
     }
   }
 
+  setValue(v) {
+    this.props.setHostsContent(v)
+  }
+
   render () {
     let {current, lang} = this.props
 
@@ -55,7 +59,7 @@ export default class Content extends React.Component {
         <Editor
           readonly={this.props.readonly}
           code={this.props.current.content || ''}
-          //setValue={this.setValue.bind(this)}
+          setValue={this.setValue.bind(this)}
         />
       </div>
     )

+ 2 - 4
ui/content/editor.js

@@ -28,9 +28,7 @@ export default class Editor extends React.Component {
     this.marks = []
     this.kw = ''
 
-    this.state = {
-      code: this.props.code
-    }
+    this.state = {}
 
     Agent.on('search', (kw) => {
       this.kw = kw
@@ -53,7 +51,7 @@ export default class Editor extends React.Component {
   }
 
   setValue (v) {
-    //this.props.setValue(v)
+    this.props.setValue(v)
   }
 
   toComment () {