From 4a420dd5c537e93ca3c723ac9139bbcdfd3943af Mon Sep 17 00:00:00 2001
From: Molunerfinn <marksz@teamsz.xyz>
Date: Thu, 20 Sep 2018 14:49:20 +0800
Subject: [PATCH] Added: plugin config save

---
 src/main/utils/picgoCoreIPC.js                |  2 +-
 src/main/utils/uploader.js                    |  6 +--
 src/renderer/components/ConfigForm.vue        | 36 +++++++++++------
 .../components/SettingView/Plugin.vue         | 39 ++++++++++++++-----
 4 files changed, 57 insertions(+), 26 deletions(-)

diff --git a/src/main/utils/picgoCoreIPC.js b/src/main/utils/picgoCoreIPC.js
index 76960ce..0cf5fb2 100644
--- a/src/main/utils/picgoCoreIPC.js
+++ b/src/main/utils/picgoCoreIPC.js
@@ -23,9 +23,9 @@ const getConfig = (name, type, ctx) => {
 export default (app, ipcMain) => {
   const STORE_PATH = app.getPath('userData')
   const CONFIG_PATH = path.join(STORE_PATH, '/data.json')
-  const picgo = new PicGo(CONFIG_PATH)
 
   ipcMain.on('getPluginList', event => {
+    const picgo = new PicGo(CONFIG_PATH)
     const pluginList = picgo.pluginLoader.getList()
     const list = []
     for (let i in pluginList) {
diff --git a/src/main/utils/uploader.js b/src/main/utils/uploader.js
index 024179d..f75403a 100644
--- a/src/main/utils/uploader.js
+++ b/src/main/utils/uploader.js
@@ -10,7 +10,8 @@ import fecha from 'fecha'
 // eslint-disable-next-line
 const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require
 const PicGo = requireFunc('picgo')
-
+const STORE_PATH = app.getPath('userData')
+const CONFIG_PATH = path.join(STORE_PATH, '/data.json')
 const renameURL = process.env.NODE_ENV === 'development' ? `http://localhost:9080/#rename-page` : `file://${__dirname}/index.html#rename-page`
 
 const createRenameWindow = () => {
@@ -38,9 +39,6 @@ const createRenameWindow = () => {
   return window
 }
 
-const STORE_PATH = app.getPath('userData')
-const CONFIG_PATH = path.join(STORE_PATH, '/data.json')
-
 const waitForShow = (webcontent) => {
   return new Promise((resolve, reject) => {
     webcontent.on('dom-ready', () => {
diff --git a/src/renderer/components/ConfigForm.vue b/src/renderer/components/ConfigForm.vue
index 42a5086..bfd9afc 100644
--- a/src/renderer/components/ConfigForm.vue
+++ b/src/renderer/components/ConfigForm.vue
@@ -9,10 +9,10 @@
     >
       <el-form-item
         v-for="(item, index) in configList"
-        :key="index"
         :label="item.name"
         :required="item.required"
         :prop="item.name"
+        :key="item.name + index"
       >
         <el-input
           v-if="item.type === 'input' || item.type === 'password'"
@@ -44,6 +44,7 @@
   </div>
 </template>
 <script>
+import { cloneDeep } from 'lodash'
 export default {
   name: 'config-form',
   props: {
@@ -58,23 +59,36 @@ export default {
     }
   },
   created () {
-    this.configList = JSON.parse(JSON.stringify(this.config)).map(item => {
+    this.configList = cloneDeep(this.config).map(item => {
       const defaultValue = item.default !== undefined ? item.default : null
       this.$set(this.ruleForm, item.name, defaultValue)
       return item
     })
   },
-  mounted () {
-    console.log(this.$refs.form)
+  watch: {
+    config: {
+      deep: true,
+      handler (val) {
+        this.ruleForm = Object.assign({}, {})
+        this.configList = cloneDeep(val).map(item => {
+          const defaultValue = item.default !== undefined ? item.default : null
+          this.$set(this.ruleForm, item.name, defaultValue)
+          return item
+        })
+      }
+    }
   },
   methods: {
-    validate () {
-      this.$refs.form.validate(valid => {
-        if (valid) {
-          console.log(this.ruleForm)
-        } else {
-          return false
-        }
+    async validate () {
+      return new Promise((resolve, reject) => {
+        this.$refs.form.validate(valid => {
+          if (valid) {
+            resolve(this.ruleForm)
+          } else {
+            resolve(false)
+            return false
+          }
+        })
       })
     }
   }
diff --git a/src/renderer/components/SettingView/Plugin.vue b/src/renderer/components/SettingView/Plugin.vue
index 5689a7a..c33f0e4 100644
--- a/src/renderer/components/SettingView/Plugin.vue
+++ b/src/renderer/components/SettingView/Plugin.vue
@@ -26,9 +26,9 @@
                 {{ item.author }}
               </span>
               <span class="plugin-item__config" >
-                <span class="reload-button" v-if="item.reload" @click="reloadApp">
+                <!-- <span class="reload-button" v-if="item.reload" @click="reloadApp">
                   重启
-                </span>
+                </span> -->
                 <i
                   class="el-icon-setting"
                   @click="buildContextMenu(item)"
@@ -80,7 +80,7 @@ export default {
   created () {
     this.$electron.ipcRenderer.on('pluginList', (evt, list) => {
       this.pluginList = list.map(item => {
-        item.reload = false
+        // item.reload = false
         return item
       })
     })
@@ -100,7 +100,7 @@ export default {
         click () {
           _this.$db.read().set(`plugins.picgo-plugin-${plugin.name}`, true).write()
           plugin.enabled = true
-          plugin.reload = true
+          // plugin.reload = true
         }
       }, {
         label: '禁用插件',
@@ -108,7 +108,7 @@ export default {
         click () {
           _this.$db.read().set(`plugins.picgo-plugin-${plugin.name}`, false).write()
           plugin.enabled = false
-          plugin.reload = true
+          // plugin.reload = true
         }
       }]
       for (let i in plugin.config) {
@@ -116,11 +116,10 @@ export default {
           const obj = {
             label: `配置${i} - ${plugin.config[i].name}`,
             click () {
-              _this.configType = i
+              _this.currentType = i
               _this.configName = plugin.config[i].name
               _this.dialogVisible = true
               _this.config = plugin.config[i].config
-              console.log(plugin.config[i].config)
             }
           }
           menu.push(obj)
@@ -136,9 +135,29 @@ export default {
       this.$electron.remote.app.relaunch()
       this.$electron.remote.app.exit(0)
     },
-    handleConfirmConfig () {
-      console.log(this.$refs.configForm)
-      this.$refs.configForm.validate()
+    async handleConfirmConfig () {
+      const result = await this.$refs.configForm.validate()
+      if (result !== false) {
+        switch (this.currentType) {
+          case 'plugin':
+            this.$db.read().set(`picgo-plugin-${this.configName}`, result).write()
+            break
+          case 'uploader':
+            this.$db.read().set(`picBed.${this.configName}`, result).write()
+            break
+          case 'transformer':
+            this.$db.read().set(`transformer.${this.configName}`, result).write()
+            break
+        }
+        const successNotification = new window.Notification('设置结果', {
+          body: '设置成功'
+        })
+        successNotification.onclick = () => {
+          return true
+        }
+        this.dialogVisible = false
+        this.getPluginList()
+      }
     }
   }
 }