From dd30521f722bc94181ce350658ef07e3af1beb22 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Thu, 27 Oct 2022 20:03:02 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=94=AF=E6=8C=81=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=E8=AE=BE=E7=BD=AE=E4=BB=A3=E7=A0=81=E7=89=87?= =?UTF-8?q?=E6=AE=B5=20https://github.com/siyuan-note/siyuan/issues/6357?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/api/snippet.go | 28 ++++++++++++++++++---------- kernel/model/snippet.go | 24 +----------------------- 2 files changed, 19 insertions(+), 33 deletions(-) diff --git a/kernel/api/snippet.go b/kernel/api/snippet.go index 6a67b3ddb..5f186f1b2 100644 --- a/kernel/api/snippet.go +++ b/kernel/api/snippet.go @@ -23,6 +23,7 @@ import ( "strings" "github.com/88250/gulu" + "github.com/88250/lute/ast" "github.com/gin-gonic/gin" "github.com/siyuan-note/logging" "github.com/siyuan-note/siyuan/kernel/conf" @@ -98,22 +99,29 @@ func setSnippet(c *gin.Context) { return } - id := gulu.Rand.String(12) - idArg := arg["id"] - if nil != idArg { - id = idArg.(string) + snippetsArg := arg["snippets"].([]interface{}) + var snippets []*conf.Snippet + for _, s := range snippetsArg { + 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), + } + if "" == snippet.ID { + snippet.ID = ast.NewNodeID() + } + snippets = append(snippets, snippet) } - name := arg["name"].(string) - typ := arg["type"].(string) - content := arg["content"].(string) - enabled := arg["enabled"].(bool) - snippet, err := model.SetSnippet(id, name, typ, content, enabled) + + err := model.SetSnippet(snippets) if nil != err { ret.Code = -1 ret.Msg = "set snippet failed: " + err.Error() return } - ret.Data = snippet } func removeSnippet(c *gin.Context) { diff --git a/kernel/model/snippet.go b/kernel/model/snippet.go index e822cd7cc..416d261a9 100644 --- a/kernel/model/snippet.go +++ b/kernel/model/snippet.go @@ -50,32 +50,10 @@ func RemoveSnippet(id string) (ret *conf.Snippet, err error) { return } -func SetSnippet(id, name, typ, content string, enabled bool) (ret *conf.Snippet, err error) { +func SetSnippet(snippets []*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 - 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) return }