mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 15:10:12 +01:00
🎨 支持通过界面设置代码片段 https://github.com/siyuan-note/siyuan/issues/6357
This commit is contained in:
parent
8f942aa631
commit
dd30521f72
2 changed files with 19 additions and 33 deletions
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/88250/gulu"
|
"github.com/88250/gulu"
|
||||||
|
"github.com/88250/lute/ast"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/siyuan-note/logging"
|
"github.com/siyuan-note/logging"
|
||||||
"github.com/siyuan-note/siyuan/kernel/conf"
|
"github.com/siyuan-note/siyuan/kernel/conf"
|
||||||
|
|
@ -98,22 +99,29 @@ func setSnippet(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
id := gulu.Rand.String(12)
|
snippetsArg := arg["snippets"].([]interface{})
|
||||||
idArg := arg["id"]
|
var snippets []*conf.Snippet
|
||||||
if nil != idArg {
|
for _, s := range snippetsArg {
|
||||||
id = idArg.(string)
|
m := s.(map[string]interface{})
|
||||||
|
snippet := &conf.Snippet{
|
||||||
|
ID: m["id"].(string),
|
||||||
|
Name: m["name"].(string),
|
||||||
|
Type: m["type"].(string),
|
||||||
|
Content: m["content"].(string),
|
||||||
|
Enabled: m["enabled"].(bool),
|
||||||
}
|
}
|
||||||
name := arg["name"].(string)
|
if "" == snippet.ID {
|
||||||
typ := arg["type"].(string)
|
snippet.ID = ast.NewNodeID()
|
||||||
content := arg["content"].(string)
|
}
|
||||||
enabled := arg["enabled"].(bool)
|
snippets = append(snippets, snippet)
|
||||||
snippet, err := model.SetSnippet(id, name, typ, content, enabled)
|
}
|
||||||
|
|
||||||
|
err := model.SetSnippet(snippets)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
ret.Code = -1
|
ret.Code = -1
|
||||||
ret.Msg = "set snippet failed: " + err.Error()
|
ret.Msg = "set snippet failed: " + err.Error()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ret.Data = snippet
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeSnippet(c *gin.Context) {
|
func removeSnippet(c *gin.Context) {
|
||||||
|
|
|
||||||
|
|
@ -50,32 +50,10 @@ func RemoveSnippet(id string) (ret *conf.Snippet, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetSnippet(id, name, typ, content string, enabled bool) (ret *conf.Snippet, err error) {
|
func SetSnippet(snippets []*conf.Snippet) (err error) {
|
||||||
snippetsLock.Lock()
|
snippetsLock.Lock()
|
||||||
defer snippetsLock.Unlock()
|
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
|
|
||||||
ret = s
|
|
||||||
isUpdate = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !isUpdate {
|
|
||||||
ret = &conf.Snippet{ID: id, Name: name, Type: typ, Content: content, Enabled: enabled}
|
|
||||||
snippets = append(snippets, ret)
|
|
||||||
}
|
|
||||||
err = writeSnippetsConf(snippets)
|
err = writeSnippetsConf(snippets)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue