mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-30 21:38:48 +01:00
🎨 支持通过界面设置代码片段 https://github.com/siyuan-note/siyuan/issues/6357
This commit is contained in:
parent
f2236b2328
commit
2c608bf593
4 changed files with 75 additions and 4 deletions
|
|
@ -284,5 +284,6 @@ func ServeAPI(ginServer *gin.Engine) {
|
|||
|
||||
ginServer.Handle("POST", "/api/snippet/getSnippet", model.CheckAuth, getSnippet)
|
||||
ginServer.Handle("POST", "/api/snippet/setSnippet", model.CheckAuth, setSnippet)
|
||||
ginServer.Handle("POST", "/api/snippet/removeSnippet", model.CheckAuth, removeSnippet)
|
||||
ginServer.Handle("GET", "/snippets/*filepath", serveSnippets)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -98,14 +98,37 @@ func setSnippet(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
id := gulu.Rand.String(12)
|
||||
idArg := arg["id"]
|
||||
if nil != idArg {
|
||||
id = idArg.(string)
|
||||
}
|
||||
name := arg["name"].(string)
|
||||
typ := arg["type"].(string)
|
||||
content := arg["content"].(string)
|
||||
enabled := arg["enabled"].(bool)
|
||||
err := model.SetSnippet(name, typ, content, enabled)
|
||||
snippet, err := model.SetSnippet(id, name, typ, content, enabled)
|
||||
if nil != err {
|
||||
ret.Code = -1
|
||||
ret.Msg = "set snippet failed: " + err.Error()
|
||||
return
|
||||
}
|
||||
ret.Data = snippet
|
||||
}
|
||||
|
||||
func removeSnippet(c *gin.Context) {
|
||||
ret := gulu.Ret.NewResult()
|
||||
defer c.JSON(http.StatusOK, ret)
|
||||
|
||||
arg, ok := util.JsonArg(c, ret)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
id := arg["id"].(string)
|
||||
if err := model.RemoveSnippet(id); nil != err {
|
||||
ret.Code = -1
|
||||
ret.Msg = "remove snippet failed: " + err.Error()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -17,6 +17,7 @@
|
|||
package conf
|
||||
|
||||
type Snippet struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Memo string `json:"memo"`
|
||||
Type string `json:"type"` // js/css
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ import (
|
|||
|
||||
var snippetsLock = sync.Mutex{}
|
||||
|
||||
func SetSnippet(name, typ, content string, enabled bool) (err error) {
|
||||
func RemoveSnippet(id string) (err error) {
|
||||
snippetsLock.Lock()
|
||||
defer snippetsLock.Unlock()
|
||||
|
||||
|
|
@ -37,8 +37,43 @@ func SetSnippet(name, typ, content string, enabled bool) (err error) {
|
|||
if nil != err {
|
||||
return
|
||||
}
|
||||
snippet := &conf.Snippet{Name: name, Type: typ, Content: content, Enabled: enabled}
|
||||
|
||||
for i, s := range snippets {
|
||||
if s.ID == id {
|
||||
snippets = append(snippets[:i], snippets[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
err = writeSnippetsConf(snippets)
|
||||
return
|
||||
}
|
||||
|
||||
func SetSnippet(id, name, typ, content string, enabled bool) (snippet *conf.Snippet, err error) {
|
||||
snippetsLock.Lock()
|
||||
defer snippetsLock.Unlock()
|
||||
|
||||
snippets, err := loadSnippets()
|
||||
if nil != err {
|
||||
return
|
||||
}
|
||||
|
||||
isUpdate := false
|
||||
for _, s := range snippets {
|
||||
if s.ID == id {
|
||||
s.Name = name
|
||||
s.Type = typ
|
||||
s.Content = content
|
||||
s.Enabled = enabled
|
||||
snippet = s
|
||||
isUpdate = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !isUpdate {
|
||||
snippet = &conf.Snippet{ID: id, Name: name, Type: typ, Content: content, Enabled: enabled}
|
||||
snippets = append(snippets, snippet)
|
||||
}
|
||||
err = writeSnippetsConf(snippets)
|
||||
return
|
||||
}
|
||||
|
|
@ -66,6 +101,17 @@ func loadSnippets() (ret []*conf.Snippet, err error) {
|
|||
logging.LogErrorf("unmarshal js snippets failed: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
needRewrite := false
|
||||
for _, snippet := range ret {
|
||||
if "" == snippet.ID {
|
||||
snippet.ID = gulu.Rand.String(12)
|
||||
needRewrite = true
|
||||
}
|
||||
}
|
||||
if needRewrite {
|
||||
writeSnippetsConf(ret)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue