mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-16 22:50:13 +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
|
|
@ -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}
|
||||
snippets = append(snippets, snippet)
|
||||
|
||||
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