mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-16 22:50:13 +01:00
🎨 Plugin hot load
This commit is contained in:
parent
516276db49
commit
bf4b88a007
2 changed files with 82 additions and 76 deletions
|
|
@ -44,5 +44,5 @@ func setPetalEnabled(c *gin.Context) {
|
||||||
|
|
||||||
packageName := arg["packageName"].(string)
|
packageName := arg["packageName"].(string)
|
||||||
enabled := arg["enabled"].(bool)
|
enabled := arg["enabled"].(bool)
|
||||||
model.SetPetalEnabled(packageName, enabled)
|
ret.Data = model.SetPetalEnabled(packageName, enabled)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ type Petal struct {
|
||||||
I18n map[string]interface{} `json:"i18n"` // i18n text
|
I18n map[string]interface{} `json:"i18n"` // i18n text
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetPetalEnabled(name string, enabled bool) {
|
func SetPetalEnabled(name string, enabled bool) (ret *Petal) {
|
||||||
petals := getPetals()
|
petals := getPetals()
|
||||||
|
|
||||||
plugins := bazaar.InstalledPlugins()
|
plugins := bazaar.InstalledPlugins()
|
||||||
|
|
@ -54,18 +54,21 @@ func SetPetalEnabled(name string, enabled bool) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
petal := getPetalByName(plugin.Name, petals)
|
ret = getPetalByName(plugin.Name, petals)
|
||||||
if nil == petal {
|
if nil == ret {
|
||||||
petal = &Petal{
|
ret = &Petal{
|
||||||
Name: plugin.Name,
|
Name: plugin.Name,
|
||||||
Enabled: enabled,
|
Enabled: enabled,
|
||||||
}
|
}
|
||||||
petals = append(petals, petal)
|
petals = append(petals, ret)
|
||||||
} else {
|
} else {
|
||||||
petal.Enabled = enabled
|
ret.Enabled = enabled
|
||||||
}
|
}
|
||||||
|
|
||||||
savePetals(petals)
|
savePetals(petals)
|
||||||
|
|
||||||
|
loadCode(ret)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadPetals() (ret []*Petal) {
|
func LoadPetals() (ret []*Petal) {
|
||||||
|
|
@ -76,80 +79,83 @@ func LoadPetals() (ret []*Petal) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
pluginDir := filepath.Join(util.DataDir, "plugins", petal.Name)
|
loadCode(petal)
|
||||||
jsPath := filepath.Join(pluginDir, "index.js")
|
|
||||||
if !gulu.File.IsExist(jsPath) {
|
|
||||||
logging.LogErrorf("plugin [%s] js not found", petal.Name)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := filelock.ReadFile(jsPath)
|
|
||||||
if nil != err {
|
|
||||||
logging.LogErrorf("read plugin [%s] js failed: %s", petal.Name, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
petal.JS = string(data)
|
|
||||||
|
|
||||||
cssPath := filepath.Join(pluginDir, "index.css")
|
|
||||||
if gulu.File.IsExist(cssPath) {
|
|
||||||
data, err := filelock.ReadFile(cssPath)
|
|
||||||
if nil != err {
|
|
||||||
logging.LogErrorf("read plugin [%s] css failed: %s", petal.Name, err)
|
|
||||||
} else {
|
|
||||||
petal.CSS = string(data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
i18nDir := filepath.Join(pluginDir, "i18n")
|
|
||||||
if gulu.File.IsDir(i18nDir) {
|
|
||||||
langJSONs, err := os.ReadDir(i18nDir)
|
|
||||||
if nil != err {
|
|
||||||
logging.LogErrorf("read plugin [%s] i18n failed: %s", petal.Name, err)
|
|
||||||
} else {
|
|
||||||
preferredLang := Conf.Lang + ".json"
|
|
||||||
foundPreferredLang := false
|
|
||||||
foundEnUS := false
|
|
||||||
foundZhCN := false
|
|
||||||
for _, langJSON := range langJSONs {
|
|
||||||
if langJSON.Name() == preferredLang {
|
|
||||||
foundPreferredLang = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if langJSON.Name() == "en_US.json" {
|
|
||||||
foundEnUS = true
|
|
||||||
}
|
|
||||||
if langJSON.Name() == "zh_CN.json" {
|
|
||||||
foundZhCN = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !foundPreferredLang {
|
|
||||||
if foundEnUS {
|
|
||||||
preferredLang = "en_US.json"
|
|
||||||
} else if foundZhCN {
|
|
||||||
preferredLang = "zh_CN.json"
|
|
||||||
} else {
|
|
||||||
preferredLang = langJSONs[0].Name()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := filelock.ReadFile(filepath.Join(i18nDir, preferredLang))
|
|
||||||
if nil != err {
|
|
||||||
logging.LogErrorf("read plugin [%s] i18n failed: %s", petal.Name, err)
|
|
||||||
} else {
|
|
||||||
petal.I18n = map[string]interface{}{}
|
|
||||||
if err = gulu.JSON.UnmarshalJSON(data, &petal.I18n); nil != err {
|
|
||||||
logging.LogErrorf("unmarshal plugin [%s] i18n failed: %s", petal.Name, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = append(ret, petal)
|
ret = append(ret, petal)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadCode(petal *Petal) {
|
||||||
|
pluginDir := filepath.Join(util.DataDir, "plugins", petal.Name)
|
||||||
|
jsPath := filepath.Join(pluginDir, "index.js")
|
||||||
|
if !gulu.File.IsExist(jsPath) {
|
||||||
|
logging.LogErrorf("plugin [%s] js not found", petal.Name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := filelock.ReadFile(jsPath)
|
||||||
|
if nil != err {
|
||||||
|
logging.LogErrorf("read plugin [%s] js failed: %s", petal.Name, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
petal.JS = string(data)
|
||||||
|
|
||||||
|
cssPath := filepath.Join(pluginDir, "index.css")
|
||||||
|
if gulu.File.IsExist(cssPath) {
|
||||||
|
data, err = filelock.ReadFile(cssPath)
|
||||||
|
if nil != err {
|
||||||
|
logging.LogErrorf("read plugin [%s] css failed: %s", petal.Name, err)
|
||||||
|
} else {
|
||||||
|
petal.CSS = string(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
i18nDir := filepath.Join(pluginDir, "i18n")
|
||||||
|
if gulu.File.IsDir(i18nDir) {
|
||||||
|
langJSONs, readErr := os.ReadDir(i18nDir)
|
||||||
|
if nil != readErr {
|
||||||
|
logging.LogErrorf("read plugin [%s] i18n failed: %s", petal.Name, readErr)
|
||||||
|
} else {
|
||||||
|
preferredLang := Conf.Lang + ".json"
|
||||||
|
foundPreferredLang := false
|
||||||
|
foundEnUS := false
|
||||||
|
foundZhCN := false
|
||||||
|
for _, langJSON := range langJSONs {
|
||||||
|
if langJSON.Name() == preferredLang {
|
||||||
|
foundPreferredLang = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if langJSON.Name() == "en_US.json" {
|
||||||
|
foundEnUS = true
|
||||||
|
}
|
||||||
|
if langJSON.Name() == "zh_CN.json" {
|
||||||
|
foundZhCN = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !foundPreferredLang {
|
||||||
|
if foundEnUS {
|
||||||
|
preferredLang = "en_US.json"
|
||||||
|
} else if foundZhCN {
|
||||||
|
preferredLang = "zh_CN.json"
|
||||||
|
} else {
|
||||||
|
preferredLang = langJSONs[0].Name()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err = filelock.ReadFile(filepath.Join(i18nDir, preferredLang))
|
||||||
|
if nil != err {
|
||||||
|
logging.LogErrorf("read plugin [%s] i18n failed: %s", petal.Name, err)
|
||||||
|
} else {
|
||||||
|
petal.I18n = map[string]interface{}{}
|
||||||
|
if err = gulu.JSON.UnmarshalJSON(data, &petal.I18n); nil != err {
|
||||||
|
logging.LogErrorf("unmarshal plugin [%s] i18n failed: %s", petal.Name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var petalsStoreLock = sync.Mutex{}
|
var petalsStoreLock = sync.Mutex{}
|
||||||
|
|
||||||
func savePetals(petals []*Petal) {
|
func savePetals(petals []*Petal) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue