Jelajahi Sumber

:art: Init plugin system https://github.com/siyuan-note/siyuan/issues/8041

Liang Ding 2 tahun lalu
induk
melakukan
109462a22f
4 mengubah file dengan 60 tambahan dan 2 penghapusan
  1. 15 0
      kernel/api/plugin.go
  2. 1 0
      kernel/api/router.go
  3. 39 2
      kernel/model/plugin.go
  4. 5 0
      kernel/server/serve.go

+ 15 - 0
kernel/api/plugin.go

@@ -22,6 +22,7 @@ import (
 	"github.com/88250/gulu"
 	"github.com/gin-gonic/gin"
 	"github.com/siyuan-note/siyuan/kernel/model"
+	"github.com/siyuan-note/siyuan/kernel/util"
 )
 
 func loadPetals(c *gin.Context) {
@@ -31,3 +32,17 @@ func loadPetals(c *gin.Context) {
 	petals := model.LoadPetals()
 	ret.Data = petals
 }
+
+func setPetalEnabled(c *gin.Context) {
+	ret := gulu.Ret.NewResult()
+	defer c.JSON(http.StatusOK, ret)
+
+	arg, ok := util.JsonArg(c, ret)
+	if !ok {
+		return
+	}
+
+	packageName := arg["packageName"].(string)
+	enabled := arg["enabled"].(bool)
+	model.SetPetalEnabled(packageName, enabled)
+}

+ 1 - 0
kernel/api/router.go

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

+ 39 - 2
kernel/model/plugin.go

@@ -40,7 +40,46 @@ type Petal struct {
 	I18n map[string]interface{} `json:"i18n"` // i18n text
 }
 
+func SetPetalEnabled(name string, enabled bool) {
+	petals := []*Petal{}
+	petalDir := filepath.Join(util.DataDir, "storage", "petal")
+	confPath := filepath.Join(petalDir, "petals.json")
+	data, err := filelock.ReadFile(confPath)
+	if nil != err {
+		logging.LogErrorf("read petal file [%s] failed: %s", confPath, err)
+		return
+	}
+
+	if err = gulu.JSON.UnmarshalJSON(data, &petals); nil != err {
+		logging.LogErrorf("unmarshal petals failed: %s", err)
+		return
+	}
+
+	plugins := bazaar.InstalledPlugins()
+	for _, plugin := range plugins {
+		id := hash(plugin.URL)
+		petal := getPetalByID(id, petals)
+		if nil == petal {
+			continue
+		}
+
+		petal.Enabled = enabled
+		break
+	}
+
+	if data, err = gulu.JSON.MarshalIndentJSON(petals, "", "\t"); nil != err {
+		logging.LogErrorf("marshal petals failed: %s", err)
+		return
+	}
+	if err = filelock.WriteFile(confPath, data); nil != err {
+		logging.LogErrorf("write petals [%s] failed: %s", confPath, err)
+		return
+	}
+}
+
 func LoadPetals() (ret []*Petal) {
+	ret = []*Petal{}
+
 	petalDir := filepath.Join(util.DataDir, "storage", "petal")
 	if err := os.MkdirAll(petalDir, 0755); nil != err {
 		logging.LogErrorf("create petal dir [%s] failed: %s", petalDir, err)
@@ -48,8 +87,6 @@ func LoadPetals() (ret []*Petal) {
 	}
 
 	confPath := filepath.Join(petalDir, "petals.json")
-
-	ret = []*Petal{}
 	if !gulu.File.IsExist(confPath) {
 		data, err := gulu.JSON.MarshalIndentJSON(ret, "", "\t")
 		if nil != err {

+ 5 - 0
kernel/server/serve.go

@@ -72,6 +72,7 @@ func Serve(fastMode bool) {
 	serveWebSocket(ginServer)
 	serveExport(ginServer)
 	serveWidgets(ginServer)
+	servePlugins(ginServer)
 	serveEmojis(ginServer)
 	serveTemplates(ginServer)
 	api.ServeAPI(ginServer)
@@ -174,6 +175,10 @@ func serveWidgets(ginServer *gin.Engine) {
 	ginServer.Static("/widgets/", filepath.Join(util.DataDir, "widgets"))
 }
 
+func servePlugins(ginServer *gin.Engine) {
+	ginServer.Static("/plugins/", filepath.Join(util.DataDir, "plugins"))
+}
+
 func serveEmojis(ginServer *gin.Engine) {
 	ginServer.Static("/emojis/", filepath.Join(util.DataDir, "emojis"))
 }