🎨 支持通过界面设置代码片段 https://github.com/siyuan-note/siyuan/issues/6357

This commit is contained in:
Liang Ding 2022-10-27 11:30:04 +08:00
parent 41d882ddba
commit e3eff40e56
No known key found for this signature in database
GPG Key ID: 136F30F901A2231D
2 changed files with 10 additions and 7 deletions

View File

@ -126,9 +126,11 @@ func removeSnippet(c *gin.Context) {
} }
id := arg["id"].(string) id := arg["id"].(string)
if err := model.RemoveSnippet(id); nil != err { snippet, err := model.RemoveSnippet(id)
if nil != err {
ret.Code = -1 ret.Code = -1
ret.Msg = "remove snippet failed: " + err.Error() ret.Msg = "remove snippet failed: " + err.Error()
return return
} }
ret.Data = snippet
} }

View File

@ -29,7 +29,7 @@ import (
var snippetsLock = sync.Mutex{} var snippetsLock = sync.Mutex{}
func RemoveSnippet(id string) (err error) { func RemoveSnippet(id string) (ret *conf.Snippet, err error) {
snippetsLock.Lock() snippetsLock.Lock()
defer snippetsLock.Unlock() defer snippetsLock.Unlock()
@ -40,6 +40,7 @@ func RemoveSnippet(id string) (err error) {
for i, s := range snippets { for i, s := range snippets {
if s.ID == id { if s.ID == id {
ret = s
snippets = append(snippets[:i], snippets[i+1:]...) snippets = append(snippets[:i], snippets[i+1:]...)
break break
} }
@ -48,7 +49,7 @@ func RemoveSnippet(id string) (err error) {
return return
} }
func SetSnippet(id, name, typ, content string, enabled bool) (snippet *conf.Snippet, err error) { func SetSnippet(id, name, typ, content string, enabled bool) (ret *conf.Snippet, err error) {
snippetsLock.Lock() snippetsLock.Lock()
defer snippetsLock.Unlock() defer snippetsLock.Unlock()
@ -64,15 +65,15 @@ func SetSnippet(id, name, typ, content string, enabled bool) (snippet *conf.Snip
s.Type = typ s.Type = typ
s.Content = content s.Content = content
s.Enabled = enabled s.Enabled = enabled
snippet = s ret = s
isUpdate = true isUpdate = true
break break
} }
} }
if !isUpdate { if !isUpdate {
snippet = &conf.Snippet{ID: id, Name: name, Type: typ, Content: content, Enabled: enabled} ret = &conf.Snippet{ID: id, Name: name, Type: typ, Content: content, Enabled: enabled}
snippets = append(snippets, snippet) snippets = append(snippets, ret)
} }
err = writeSnippetsConf(snippets) err = writeSnippetsConf(snippets)
return return