瀏覽代碼

:art: Support one-click enable/disable of all downloaded plugins https://github.com/siyuan-note/siyuan/issues/8523

Daniel 2 年之前
父節點
當前提交
d54195a943
共有 3 個文件被更改,包括 74 次插入0 次删除
  1. 26 0
      kernel/api/petal.go
  2. 1 0
      kernel/api/router.go
  3. 47 0
      kernel/model/plugin.go

+ 26 - 0
kernel/api/petal.go

@@ -61,3 +61,29 @@ func setPetalEnabled(c *gin.Context) {
 
 	ret.Data = data
 }
+
+func setPetalConf(c *gin.Context) {
+	ret := gulu.Ret.NewResult()
+	defer c.JSON(http.StatusOK, ret)
+
+	arg, ok := util.JsonArg(c, ret)
+	if !ok {
+		return
+	}
+
+	param, err := gulu.JSON.MarshalJSON(arg)
+	if nil != err {
+		ret.Code = -1
+		ret.Msg = err.Error()
+		return
+	}
+
+	conf := &model.PetalConf{}
+	if err = gulu.JSON.UnmarshalJSON(param, conf); nil != err {
+		ret.Code = -1
+		ret.Msg = err.Error()
+		return
+	}
+
+	conf.Save()
+}

+ 1 - 0
kernel/api/router.go

@@ -363,4 +363,5 @@ func ServeAPI(ginServer *gin.Engine) {
 
 	ginServer.Handle("POST", "/api/petal/loadPetals", model.CheckAuth, model.CheckReadonly, loadPetals)
 	ginServer.Handle("POST", "/api/petal/setPetalEnabled", model.CheckAuth, model.CheckReadonly, setPetalEnabled)
+	ginServer.Handle("POST", "/api/petal/setPetalConf", model.CheckAuth, model.CheckReadonly, setPetalConf)
 }

+ 47 - 0
kernel/model/plugin.go

@@ -40,6 +40,34 @@ type Petal struct {
 	I18n map[string]interface{} `json:"i18n"` // i18n text
 }
 
+type PetalConf struct {
+	Enabled bool `json:"enabled"`
+}
+
+func (conf *PetalConf) Save() {
+	if util.ReadOnly {
+		return
+	}
+
+	petalsStoreLock.Lock()
+	defer petalsStoreLock.Unlock()
+
+	data, _ := gulu.JSON.MarshalIndentJSON(Conf, "", "  ")
+	petalDir := filepath.Join(util.DataDir, "storage", "petal")
+	if err := os.MkdirAll(petalDir, 0777); nil != err {
+		logging.LogErrorf("create petal dir [%s] failed: %s", petalDir, err)
+		util.ReportFileSysFatalError(err)
+		return
+	}
+
+	confPath := filepath.Join(petalDir, "conf.json")
+	if err := filelock.WriteFile(confPath, data); nil != err {
+		logging.LogErrorf("write petal conf [%s] failed: %s", confPath, err)
+		util.ReportFileSysFatalError(err)
+		return
+	}
+}
+
 func SetPetalEnabled(name string, enabled bool, frontend string) (ret *Petal, err error) {
 	petals := getPetals()
 
@@ -71,6 +99,25 @@ func SetPetalEnabled(name string, enabled bool, frontend string) (ret *Petal, er
 
 func LoadPetals(frontend string) (ret []*Petal) {
 	ret = []*Petal{}
+
+	petalDir := filepath.Join(util.DataDir, "storage", "petal")
+	confPath := filepath.Join(petalDir, "conf.json")
+	if gulu.File.IsExist(confPath) {
+		data, err := filelock.ReadFile(confPath)
+		if nil != err {
+			logging.LogErrorf("read petal conf [%s] failed: %s", confPath, err)
+		} else {
+			petalConf := &PetalConf{}
+			if err = gulu.JSON.UnmarshalJSON(data, petalConf); nil != err {
+				logging.LogErrorf("unmarshal petal conf [%s] failed: %s", confPath, err)
+			} else {
+				if !petalConf.Enabled {
+					return
+				}
+			}
+		}
+	}
+
 	petals := getPetals()
 	for _, petal := range petals {
 		_, petal.Incompatible = bazaar.IsIncompatibleInstalledPlugin(petal.Name, frontend)