From 20462f302dc8ac4b5c489bc85287730028207c57 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Mon, 18 Dec 2023 12:24:39 +0800 Subject: [PATCH 1/4] :art: Add template func `pow`, `powf`, `log` and `logf` https://github.com/siyuan-note/siyuan/issues/9911 --- kernel/model/attribute_view.go | 3 +-- kernel/model/template.go | 27 ++------------------ kernel/treenode/node.go | 3 +-- kernel/util/template.go | 45 ++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 29 deletions(-) create mode 100644 kernel/util/template.go diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index c617381c1..a4e34724f 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -412,7 +412,7 @@ func renderTemplateCol(ial map[string]string, tplContent string, rowValues []*av } goTpl := template.New("").Delims(".action{", "}") - goTpl = goTpl.Funcs(builtInTemplateFuncs()) + goTpl = goTpl.Funcs(util.BuiltInTemplateFuncs()) tpl, tplErr := goTpl.Parse(tplContent) if nil != tplErr { logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr) @@ -1801,7 +1801,6 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID, cellID string, bindBlockAv(tx, avID, val.BlockID) } else { // 之前绑定的块和现在绑定的块一样 // 直接返回,因为锚文本不允许更改 - return } } } diff --git a/kernel/model/template.go b/kernel/model/template.go index 122373555..43fa5190f 100644 --- a/kernel/model/template.go +++ b/kernel/model/template.go @@ -21,7 +21,6 @@ import ( "errors" "fmt" "io/fs" - "math" "os" "path/filepath" "sort" @@ -33,7 +32,6 @@ import ( "github.com/88250/lute/ast" "github.com/88250/lute/parse" "github.com/88250/lute/render" - "github.com/Masterminds/sprig/v3" "github.com/araddon/dateparse" "github.com/siyuan-note/filelock" "github.com/siyuan-note/logging" @@ -42,12 +40,11 @@ import ( "github.com/siyuan-note/siyuan/kernel/sql" "github.com/siyuan-note/siyuan/kernel/treenode" "github.com/siyuan-note/siyuan/kernel/util" - "github.com/spf13/cast" ) func RenderGoTemplate(templateContent string) (ret string, err error) { tmpl := template.New("") - tmpl = tmpl.Funcs(builtInTemplateFuncs()) + tmpl = tmpl.Funcs(util.BuiltInTemplateFuncs()) tpl, err := tmpl.Parse(templateContent) if nil != err { return "", errors.New(fmt.Sprintf(Conf.Language(44), err.Error())) @@ -220,7 +217,7 @@ func renderTemplate(p, id string, preview bool) (string, error) { dataModel["alias"] = block.Alias } - funcMap := builtInTemplateFuncs() + funcMap := util.BuiltInTemplateFuncs() funcMap["queryBlocks"] = func(stmt string, args ...string) (ret []*sql.Block) { for _, arg := range args { stmt = strings.Replace(stmt, "?", arg, 1) @@ -417,23 +414,3 @@ func addBlockIALNodes(tree *parse.Tree, removeUpdated bool) { block.InsertAfter(&ast.Node{Type: ast.NodeKramdownBlockIAL, Tokens: parse.IAL2Tokens(block.KramdownIAL)}) } } - -func builtInTemplateFuncs() (ret template.FuncMap) { - ret = sprig.TxtFuncMap() - ret["Weekday"] = util.Weekday - ret["WeekdayCN"] = util.WeekdayCN - ret["WeekdayCN2"] = util.WeekdayCN2 - ret["ISOWeek"] = util.ISOWeek - ret["pow"] = pow - ret["powf"] = powf - ret["log"] = log - ret["logf"] = logf - return -} - -func pow(a, b interface{}) int64 { return int64(math.Pow(cast.ToFloat64(a), cast.ToFloat64(b))) } -func powf(a, b interface{}) float64 { return math.Pow(cast.ToFloat64(a), cast.ToFloat64(b)) } -func log(a, b interface{}) int64 { - return int64(math.Log(cast.ToFloat64(a)) / math.Log(cast.ToFloat64(b))) -} -func logf(a, b interface{}) float64 { return math.Log(cast.ToFloat64(a)) / math.Log(cast.ToFloat64(b)) } diff --git a/kernel/treenode/node.go b/kernel/treenode/node.go index 747254dc1..6d413d0e6 100644 --- a/kernel/treenode/node.go +++ b/kernel/treenode/node.go @@ -18,7 +18,6 @@ package treenode import ( "bytes" - "github.com/Masterminds/sprig/v3" "github.com/siyuan-note/siyuan/kernel/av" "github.com/siyuan-note/siyuan/kernel/cache" "strings" @@ -834,7 +833,7 @@ func renderTemplateCol(ial map[string]string, tplContent string, rowValues []*av ial["updated"] = time.UnixMilli(block.Block.Updated).Format("20060102150405") } - funcMap := sprig.TxtFuncMap() + funcMap := util.BuiltInTemplateFuncs() goTpl := template.New("").Delims(".action{", "}") tpl, tplErr := goTpl.Funcs(funcMap).Parse(tplContent) if nil != tplErr { diff --git a/kernel/util/template.go b/kernel/util/template.go new file mode 100644 index 000000000..053790ade --- /dev/null +++ b/kernel/util/template.go @@ -0,0 +1,45 @@ +// SiYuan - Refactor your thinking +// Copyright (c) 2020-present, b3log.org +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package util + +import ( + "math" + "text/template" + + "github.com/Masterminds/sprig/v3" + "github.com/spf13/cast" +) + +func BuiltInTemplateFuncs() (ret template.FuncMap) { + ret = sprig.TxtFuncMap() + ret["Weekday"] = Weekday + ret["WeekdayCN"] = WeekdayCN + ret["WeekdayCN2"] = WeekdayCN2 + ret["ISOWeek"] = ISOWeek + ret["pow"] = pow + ret["powf"] = powf + ret["log"] = log + ret["logf"] = logf + return +} + +func pow(a, b interface{}) int64 { return int64(math.Pow(cast.ToFloat64(a), cast.ToFloat64(b))) } +func powf(a, b interface{}) float64 { return math.Pow(cast.ToFloat64(a), cast.ToFloat64(b)) } +func log(a, b interface{}) int64 { + return int64(math.Log(cast.ToFloat64(a)) / math.Log(cast.ToFloat64(b))) +} +func logf(a, b interface{}) float64 { return math.Log(cast.ToFloat64(a)) / math.Log(cast.ToFloat64(b)) } From 5d493e207aed03b5329e55aad326860c4b52a691 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Mon, 18 Dec 2023 12:26:38 +0800 Subject: [PATCH 2/4] :bug: Fix https://ld246.com/article/1702871791961 --- kernel/av/av.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/av/av.go b/kernel/av/av.go index 9c0025aa5..a85a3766d 100644 --- a/kernel/av/av.go +++ b/kernel/av/av.go @@ -238,7 +238,7 @@ func SaveAttributeView(av *AttributeView) (err error) { } case KeyTypeNumber: for _, v := range kv.Values { - if 0 != v.Number.Content && !v.Number.IsNotEmpty { + if nil != v.Number && 0 != v.Number.Content && !v.Number.IsNotEmpty { v.Number.IsNotEmpty = true } } From 2b201abb80a0cdeeca7e517eee83dd09b032773e Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Mon, 18 Dec 2023 12:35:48 +0800 Subject: [PATCH 3/4] :art: Support modifying the primary key value of the binding block https://github.com/siyuan-note/siyuan/issues/9892 --- kernel/model/attribute_view.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index a4e34724f..04327f2de 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -1710,12 +1710,6 @@ func replaceAttributeViewBlock(operation *Operation, tx *Transaction) (err error for _, v := range attrView.Views { switch v.LayoutType { case av.LayoutTypeTable: - for _, rowID := range v.Table.RowIDs { - if rowID == operation.NextID { - return - } - } - for i, rowID := range v.Table.RowIDs { if rowID == operation.PreviousID { v.Table.RowIDs[i] = operation.NextID @@ -1800,7 +1794,10 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID, cellID string, unbindBlockAv(tx, avID, oldBoundBlockID) bindBlockAv(tx, avID, val.BlockID) } else { // 之前绑定的块和现在绑定的块一样 - // 直接返回,因为锚文本不允许更改 + if av.KeyTypeBlock == val.Type && nil != val.Block { + // 直接返回,因为锚文本不允许更改 + return + } } } } @@ -1811,6 +1808,7 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID, cellID string, if rowID == v.Block.ID { v.Block.Updated = time.Now().UnixMilli() v.IsInitialized = true + v.IsDetached = val.IsDetached break } } From 63d07237ad5e6a190be2fa002852d5c4c7baab56 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Mon, 18 Dec 2023 12:53:53 +0800 Subject: [PATCH 4/4] :art: Support modifying the primary key value of the binding block https://github.com/siyuan-note/siyuan/issues/9892 --- kernel/model/attribute_view.go | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index 04327f2de..3046788dc 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -1747,7 +1747,21 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID, cellID string, return } + var blockVal *av.Value + for _, kv := range attrView.KeyValues { + if av.KeyTypeBlock == kv.Key.Type { + for _, v := range kv.Values { + if rowID == v.Block.ID { + blockVal = v + break + } + } + break + } + } + var val *av.Value + oldIsDetached := blockVal.IsDetached for _, keyValues := range attrView.KeyValues { if keyID != keyValues.Key.ID { continue @@ -1768,9 +1782,7 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID, cellID string, break } - oldIsDetached := val.IsDetached oldBoundBlockID := val.BlockID - data, err := gulu.JSON.MarshalJSON(valueData) if nil != err { return @@ -1802,18 +1814,10 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID, cellID string, } } - for _, kv := range attrView.KeyValues { - if av.KeyTypeBlock == kv.Key.Type { - for _, v := range kv.Values { - if rowID == v.Block.ID { - v.Block.Updated = time.Now().UnixMilli() - v.IsInitialized = true - v.IsDetached = val.IsDetached - break - } - } - break - } + if nil != blockVal { + blockVal.Block.Updated = time.Now().UnixMilli() + blockVal.IsInitialized = true + blockVal.IsDetached = val.IsDetached } if err = av.SaveAttributeView(attrView); nil != err {