mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-22 17:40:13 +01:00
🎨 Add plugin config items backends and frontends https://github.com/siyuan-note/siyuan/issues/8386
Improve plugin load performance https://github.com/siyuan-note/siyuan/issues/8397
This commit is contained in:
parent
52c9b5fbca
commit
f5a9c2d316
5 changed files with 78 additions and 31 deletions
|
|
@ -110,16 +110,15 @@ func uninstallBazaarPlugin(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
frontend := arg["frontend"].(string)
|
||||||
packageName := arg["packageName"].(string)
|
packageName := arg["packageName"].(string)
|
||||||
err := model.UninstallBazaarPlugin(packageName)
|
err := model.UninstallBazaarPlugin(packageName, frontend)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
ret.Code = -1
|
ret.Code = -1
|
||||||
ret.Msg = err.Error()
|
ret.Msg = err.Error()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
frontend := arg["frontend"].(string)
|
|
||||||
|
|
||||||
ret.Data = map[string]interface{}{
|
ret.Data = map[string]interface{}{
|
||||||
"packages": model.BazaarPlugins(frontend),
|
"packages": model.BazaarPlugins(frontend),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,14 @@ func loadPetals(c *gin.Context) {
|
||||||
ret := gulu.Ret.NewResult()
|
ret := gulu.Ret.NewResult()
|
||||||
defer c.JSON(http.StatusOK, ret)
|
defer c.JSON(http.StatusOK, ret)
|
||||||
|
|
||||||
petals := model.LoadPetals()
|
arg, ok := util.JsonArg(c, ret)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
frontend := arg["frontend"].(string)
|
||||||
|
|
||||||
|
petals := model.LoadPetals(frontend)
|
||||||
ret.Data = petals
|
ret.Data = petals
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -45,5 +52,12 @@ func setPetalEnabled(c *gin.Context) {
|
||||||
packageName := arg["packageName"].(string)
|
packageName := arg["packageName"].(string)
|
||||||
enabled := arg["enabled"].(bool)
|
enabled := arg["enabled"].(bool)
|
||||||
frontend := arg["frontend"].(string)
|
frontend := arg["frontend"].(string)
|
||||||
ret.Data = model.SetPetalEnabled(packageName, enabled, frontend)
|
data, err := model.SetPetalEnabled(packageName, enabled, frontend)
|
||||||
|
if nil != err {
|
||||||
|
ret.Code = -1
|
||||||
|
ret.Msg = err.Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ret.Data = data
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -108,7 +108,39 @@ func Plugins(frontend string) (plugins []*Plugin) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func InstalledPlugins(frontend string) (ret []*Plugin) {
|
func IsIncompatibleInstalledPlugin(name, frontend string) (found, incompatible bool) {
|
||||||
|
pluginsPath := filepath.Join(util.DataDir, "plugins")
|
||||||
|
if !util.IsPathRegularDirOrSymlinkDir(pluginsPath) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pluginDirs, err := os.ReadDir(pluginsPath)
|
||||||
|
if nil != err {
|
||||||
|
logging.LogWarnf("read plugins folder failed: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, pluginDir := range pluginDirs {
|
||||||
|
if !util.IsDirRegularOrSymlink(pluginDir) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
dirName := pluginDir.Name()
|
||||||
|
if name != dirName {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
plugin, parseErr := PluginJSON(dirName)
|
||||||
|
if nil != parseErr || nil == plugin {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
found = true
|
||||||
|
incompatible = isIncompatiblePlugin(plugin, frontend)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func InstalledPlugins(frontend string, checkUpdate bool) (ret []*Plugin) {
|
||||||
ret = []*Plugin{}
|
ret = []*Plugin{}
|
||||||
|
|
||||||
pluginsPath := filepath.Join(util.DataDir, "plugins")
|
pluginsPath := filepath.Join(util.DataDir, "plugins")
|
||||||
|
|
@ -122,7 +154,10 @@ func InstalledPlugins(frontend string) (ret []*Plugin) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
bazaarPlugins := Plugins(frontend)
|
var bazaarPlugins []*Plugin
|
||||||
|
if checkUpdate {
|
||||||
|
bazaarPlugins = Plugins(frontend)
|
||||||
|
}
|
||||||
|
|
||||||
for _, pluginDir := range pluginDirs {
|
for _, pluginDir := range pluginDirs {
|
||||||
if !util.IsDirRegularOrSymlink(pluginDir) {
|
if !util.IsDirRegularOrSymlink(pluginDir) {
|
||||||
|
|
|
||||||
|
|
@ -50,9 +50,9 @@ func BazaarPlugins(frontend string) (plugins []*bazaar.Plugin) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func InstalledPlugins(frontend string) (plugins []*bazaar.Plugin) {
|
func InstalledPlugins(frontend string) (plugins []*bazaar.Plugin) {
|
||||||
plugins = bazaar.InstalledPlugins(frontend)
|
plugins = bazaar.InstalledPlugins(frontend, true)
|
||||||
|
|
||||||
petals := getPetals()
|
petals := getPetals(frontend)
|
||||||
for _, plugin := range plugins {
|
for _, plugin := range plugins {
|
||||||
petal := getPetalByName(plugin.Name, petals)
|
petal := getPetalByName(plugin.Name, petals)
|
||||||
if nil != petal {
|
if nil != petal {
|
||||||
|
|
@ -71,14 +71,14 @@ func InstallBazaarPlugin(repoURL, repoHash, pluginName string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UninstallBazaarPlugin(pluginName string) error {
|
func UninstallBazaarPlugin(pluginName, frontend string) error {
|
||||||
installPath := filepath.Join(util.DataDir, "plugins", pluginName)
|
installPath := filepath.Join(util.DataDir, "plugins", pluginName)
|
||||||
err := bazaar.UninstallPlugin(installPath)
|
err := bazaar.UninstallPlugin(installPath)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
return errors.New(fmt.Sprintf(Conf.Language(47), err.Error()))
|
return errors.New(fmt.Sprintf(Conf.Language(47), err.Error()))
|
||||||
}
|
}
|
||||||
|
|
||||||
petals := getPetals()
|
petals := getPetals(frontend)
|
||||||
var tmp []*Petal
|
var tmp []*Petal
|
||||||
for i, petal := range petals {
|
for i, petal := range petals {
|
||||||
if petal.Name != pluginName {
|
if petal.Name != pluginName {
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
@ -30,52 +31,50 @@ import (
|
||||||
|
|
||||||
// Petal represents a plugin's management status.
|
// Petal represents a plugin's management status.
|
||||||
type Petal struct {
|
type Petal struct {
|
||||||
Name string `json:"name"` // Plugin name
|
Name string `json:"name"` // Plugin name
|
||||||
Enabled bool `json:"enabled"` // Whether enabled
|
Enabled bool `json:"enabled"` // Whether enabled
|
||||||
|
Incompatible bool `json:"incompatible"` // Whether incompatible
|
||||||
|
|
||||||
JS string `json:"js"` // JS code
|
JS string `json:"js"` // JS code
|
||||||
CSS string `json:"css"` // CSS code
|
CSS string `json:"css"` // CSS code
|
||||||
I18n map[string]interface{} `json:"i18n"` // i18n text
|
I18n map[string]interface{} `json:"i18n"` // i18n text
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetPetalEnabled(name string, enabled bool, frontend string) (ret *Petal) {
|
func SetPetalEnabled(name string, enabled bool, frontend string) (ret *Petal, err error) {
|
||||||
petals := getPetals()
|
petals := getPetals()
|
||||||
|
|
||||||
plugins := bazaar.InstalledPlugins(frontend)
|
found, incompatible := bazaar.IsIncompatibleInstalledPlugin(name, frontend)
|
||||||
var plugin *bazaar.Plugin
|
if !found {
|
||||||
for _, p := range plugins {
|
|
||||||
if p.Name == name {
|
|
||||||
plugin = p
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if nil == plugin {
|
|
||||||
logging.LogErrorf("plugin [%s] not found", name)
|
logging.LogErrorf("plugin [%s] not found", name)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = getPetalByName(plugin.Name, petals)
|
ret = getPetalByName(name, petals)
|
||||||
if nil == ret {
|
if nil == ret {
|
||||||
ret = &Petal{
|
ret = &Petal{
|
||||||
Name: plugin.Name,
|
Name: name,
|
||||||
Enabled: enabled,
|
|
||||||
}
|
}
|
||||||
petals = append(petals, ret)
|
petals = append(petals, ret)
|
||||||
} else {
|
}
|
||||||
ret.Enabled = enabled
|
ret.Enabled = enabled
|
||||||
|
ret.Incompatible = incompatible
|
||||||
|
|
||||||
|
if incompatible {
|
||||||
|
err = fmt.Errorf("plugin [%s] is incompatible", name)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
savePetals(petals)
|
savePetals(petals)
|
||||||
|
|
||||||
loadCode(ret)
|
loadCode(ret)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadPetals() (ret []*Petal) {
|
func LoadPetals(frontend string) (ret []*Petal) {
|
||||||
ret = []*Petal{}
|
ret = []*Petal{}
|
||||||
petals := getPetals()
|
petals := getPetals()
|
||||||
for _, petal := range petals {
|
for _, petal := range petals {
|
||||||
if !petal.Enabled {
|
_, petal.Incompatible = bazaar.IsIncompatibleInstalledPlugin(petal.Name, frontend)
|
||||||
|
if !petal.Enabled || petal.Incompatible {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue