Skip to content

Commit

Permalink
🎨 Supports searching database view content #9419
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Oct 13, 2023
1 parent 2304921 commit 3de7781
Show file tree
Hide file tree
Showing 3 changed files with 371 additions and 81 deletions.
16 changes: 8 additions & 8 deletions kernel/av/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,6 @@ const (
CalcOperatorLatest CalcOperator = "Latest"
)

type TableCell struct {
ID string `json:"id"`
Value *Value `json:"value"`
ValueType KeyType `json:"valueType"`
Color string `json:"color"`
BgColor string `json:"bgColor"`
}

func (value *Value) Compare(other *Value) int {
if nil == value {
return -1
Expand Down Expand Up @@ -566,6 +558,14 @@ type TableColumn struct {
Template string `json:"template"` // 模板内容
}

type TableCell struct {
ID string `json:"id"`
Value *Value `json:"value"`
ValueType KeyType `json:"valueType"`
Color string `json:"color"`
BgColor string `json:"bgColor"`
}

type TableRow struct {
ID string `json:"id"`
Cells []*TableCell `json:"cells"`
Expand Down
118 changes: 59 additions & 59 deletions kernel/model/attribute_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,65 +39,6 @@ type BlockAttributeViewKeys struct {
KeyValues []*av.KeyValues `json:"keyValues"`
}

func renderTemplateCol(ial map[string]string, tplContent string, rowValues []*av.KeyValues) string {
if "" == ial["id"] {
block := getRowBlockValue(rowValues)
ial["id"] = block.Block.ID
}
if "" == ial["updated"] {
block := getRowBlockValue(rowValues)
ial["updated"] = time.UnixMilli(block.Block.Updated).Format("20060102150405")
}

funcMap := sprig.TxtFuncMap()
goTpl := template.New("").Delims(".action{", "}")
tpl, tplErr := goTpl.Funcs(funcMap).Parse(tplContent)
if nil != tplErr {
logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr)
return ""
}

buf := &bytes.Buffer{}
dataModel := map[string]interface{}{} // 复制一份 IAL 以避免修改原始数据
for k, v := range ial {
dataModel[k] = v

// Database template column supports `created` and `updated` built-in variables https://github.com/siyuan-note/siyuan/issues/9364
createdStr := ial["id"]
if "" != createdStr {
createdStr = createdStr[:len("20060102150405")]
}
created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
if nil == parseErr {
dataModel["created"] = created
} else {
logging.LogWarnf("parse created [%s] failed: %s", createdStr, parseErr)
dataModel["created"] = time.Now()
}
updatedStr := ial["updated"]
updated, parseErr := time.ParseInLocation("20060102150405", updatedStr, time.Local)
if nil == parseErr {
dataModel["updated"] = updated
} else {
dataModel["updated"] = time.Now()
}
}
for _, rowValue := range rowValues {
if 0 < len(rowValue.Values) {
v := rowValue.Values[0]
if av.KeyTypeNumber == v.Type {
dataModel[rowValue.Key.Name] = v.Number.Content
} else {
dataModel[rowValue.Key.Name] = v.String()
}
}
}
if err := tpl.Execute(buf, dataModel); nil != err {
logging.LogWarnf("execute template [%s] failed: %s", tplContent, err)
}
return buf.String()
}

func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
waitForSyncingStorages()

Expand Down Expand Up @@ -292,6 +233,65 @@ func RenderAttributeView(avID string) (viewable av.Viewable, attrView *av.Attrib
return
}

func renderTemplateCol(ial map[string]string, tplContent string, rowValues []*av.KeyValues) string {
if "" == ial["id"] {
block := getRowBlockValue(rowValues)
ial["id"] = block.Block.ID
}
if "" == ial["updated"] {
block := getRowBlockValue(rowValues)
ial["updated"] = time.UnixMilli(block.Block.Updated).Format("20060102150405")
}

funcMap := sprig.TxtFuncMap()
goTpl := template.New("").Delims(".action{", "}")
tpl, tplErr := goTpl.Funcs(funcMap).Parse(tplContent)
if nil != tplErr {
logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr)
return ""
}

buf := &bytes.Buffer{}
dataModel := map[string]interface{}{} // 复制一份 IAL 以避免修改原始数据
for k, v := range ial {
dataModel[k] = v

// Database template column supports `created` and `updated` built-in variables https://github.com/siyuan-note/siyuan/issues/9364
createdStr := ial["id"]
if "" != createdStr {
createdStr = createdStr[:len("20060102150405")]
}
created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
if nil == parseErr {
dataModel["created"] = created
} else {
logging.LogWarnf("parse created [%s] failed: %s", createdStr, parseErr)
dataModel["created"] = time.Now()
}
updatedStr := ial["updated"]
updated, parseErr := time.ParseInLocation("20060102150405", updatedStr, time.Local)
if nil == parseErr {
dataModel["updated"] = updated
} else {
dataModel["updated"] = time.Now()
}
}
for _, rowValue := range rowValues {
if 0 < len(rowValue.Values) {
v := rowValue.Values[0]
if av.KeyTypeNumber == v.Type {
dataModel[rowValue.Key.Name] = v.Number.Content
} else {
dataModel[rowValue.Key.Name] = v.String()
}
}
}
if err := tpl.Execute(buf, dataModel); nil != err {
logging.LogWarnf("execute template [%s] failed: %s", tplContent, err)
}
return buf.String()
}

func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *av.Table, err error) {
ret = &av.Table{
ID: view.ID,
Expand Down
Loading

0 comments on commit 3de7781

Please sign in to comment.