Browse Source

:art: 反链面板支持文档排序 https://github.com/siyuan-note/insider/issues/1086

Liang Ding 2 years ago
parent
commit
e34e7fe217
4 changed files with 64 additions and 3 deletions
  1. 11 1
      kernel/api/ref.go
  2. 47 1
      kernel/model/backlink.go
  3. 3 0
      kernel/model/block.go
  4. 3 1
      kernel/model/path.go

+ 11 - 1
kernel/api/ref.go

@@ -89,7 +89,17 @@ func getBacklink2(c *gin.Context) {
 	id := arg["id"].(string)
 	keyword := arg["k"].(string)
 	mentionKeyword := arg["mk"].(string)
-	boxID, backlinks, backmentions, linkRefsCount, mentionsCount := model.GetBacklink2(id, keyword, mentionKeyword)
+	sortArg := arg["sort"]
+	sort := util.SortModeUpdatedDESC
+	if nil != sortArg {
+		sort = int(sortArg.(float64))
+	}
+	mentionSortArg := arg["msort"]
+	mentionSort := util.SortModeUpdatedDESC
+	if nil != mentionSortArg {
+		sort = int(mentionSortArg.(float64))
+	}
+	boxID, backlinks, backmentions, linkRefsCount, mentionsCount := model.GetBacklink2(id, keyword, mentionKeyword, sort, mentionSort)
 	ret.Data = map[string]interface{}{
 		"backlinks":     backlinks,
 		"linkRefsCount": linkRefsCount,

+ 47 - 1
kernel/model/backlink.go

@@ -30,6 +30,7 @@ import (
 	"github.com/88250/lute/ast"
 	"github.com/88250/lute/parse"
 	"github.com/emirpasic/gods/sets/hashset"
+	"github.com/facette/natsort"
 	"github.com/siyuan-note/logging"
 	"github.com/siyuan-note/siyuan/kernel/search"
 	"github.com/siyuan-note/siyuan/kernel/sql"
@@ -299,7 +300,7 @@ func buildBacklink(refID string, refTree *parse.Tree, luteEngine *lute.Lute) (re
 	return
 }
 
-func GetBacklink2(id, keyword, mentionKeyword string) (boxID string, backlinks, backmentions []*Path, linkRefsCount, mentionsCount int) {
+func GetBacklink2(id, keyword, mentionKeyword string, sortMode, mentionSortMode int) (boxID string, backlinks, backmentions []*Path, linkRefsCount, mentionsCount int) {
 	keyword = strings.TrimSpace(keyword)
 	mentionKeyword = strings.TrimSpace(mentionKeyword)
 	backlinks, backmentions = []*Path{}, []*Path{}
@@ -326,6 +327,28 @@ func GetBacklink2(id, keyword, mentionKeyword string) (boxID string, backlinks,
 		backlinks = append(backlinks, l)
 	}
 
+	sort.Slice(backlinks, func(i, j int) bool {
+		switch sortMode {
+		case util.SortModeUpdatedDESC:
+			return backlinks[i].Updated > backlinks[j].Updated
+		case util.SortModeUpdatedASC:
+			return backlinks[i].Updated < backlinks[j].Updated
+		case util.SortModeCreatedDESC:
+			return backlinks[i].Created > backlinks[j].Created
+		case util.SortModeCreatedASC:
+			return backlinks[i].Created < backlinks[j].Created
+		case util.SortModeNameDESC:
+			return util.PinYinCompare(util.RemoveEmoji(backlinks[j].Name), util.RemoveEmoji(backlinks[i].Name))
+		case util.SortModeNameASC:
+			return util.PinYinCompare(util.RemoveEmoji(backlinks[i].Name), util.RemoveEmoji(backlinks[j].Name))
+		case util.SortModeAlphanumDESC:
+			return natsort.Compare(util.RemoveEmoji(backlinks[j].Name), util.RemoveEmoji(backlinks[i].Name))
+		case util.SortModeAlphanumASC:
+			return natsort.Compare(util.RemoveEmoji(backlinks[i].Name), util.RemoveEmoji(backlinks[j].Name))
+		}
+		return backlinks[i].ID > backlinks[j].ID
+	})
+
 	mentionRefs := buildTreeBackmention(sqlBlock, linkRefs, mentionKeyword, excludeBacklinkIDs, 12)
 	tmpBackmentions := toFlatTree(mentionRefs, 0, "backlink")
 	for _, l := range tmpBackmentions {
@@ -337,6 +360,29 @@ func GetBacklink2(id, keyword, mentionKeyword string) (boxID string, backlinks,
 		}
 		backmentions = append(backmentions, l)
 	}
+
+	sort.Slice(backmentions, func(i, j int) bool {
+		switch sortMode {
+		case util.SortModeUpdatedDESC:
+			return backmentions[i].Updated > backmentions[j].Updated
+		case util.SortModeUpdatedASC:
+			return backmentions[i].Updated < backmentions[j].Updated
+		case util.SortModeCreatedDESC:
+			return backmentions[i].Created > backmentions[j].Created
+		case util.SortModeCreatedASC:
+			return backmentions[i].Created < backmentions[j].Created
+		case util.SortModeNameDESC:
+			return util.PinYinCompare(util.RemoveEmoji(backmentions[j].Name), util.RemoveEmoji(backmentions[i].Name))
+		case util.SortModeNameASC:
+			return util.PinYinCompare(util.RemoveEmoji(backmentions[i].Name), util.RemoveEmoji(backmentions[j].Name))
+		case util.SortModeAlphanumDESC:
+			return natsort.Compare(util.RemoveEmoji(backmentions[j].Name), util.RemoveEmoji(backmentions[i].Name))
+		case util.SortModeAlphanumASC:
+			return natsort.Compare(util.RemoveEmoji(backmentions[i].Name), util.RemoveEmoji(backmentions[j].Name))
+		}
+		return backmentions[i].ID > backmentions[j].ID
+	})
+
 	mentionsCount = len(backmentions)
 	return
 }

+ 3 - 0
kernel/model/block.go

@@ -76,6 +76,9 @@ type Path struct {
 	Children []*Path  `json:"children,omitempty"` // 子路径节点
 	Depth    int      `json:"depth"`              // 层级深度
 	Count    int      `json:"count"`              // 子块计数
+
+	Updated string `json:"updated"` // 更新时间
+	Created string `json:"created"` // 创建时间
 }
 
 func RecentUpdatedBlocks() (ret []*Block) {

+ 3 - 1
kernel/model/path.go

@@ -112,6 +112,9 @@ func toFlatTree(blocks []*Block, baseDepth int, typ string) (ret []*Path) {
 			SubType:  root.SubType,
 			Depth:    baseDepth,
 			Count:    len(root.Children),
+
+			Updated: root.IAL["updated"],
+			Created: root.ID[:14],
 		}
 		for _, c := range root.Children {
 			treeNode.Blocks = append(treeNode.Blocks, c)
@@ -125,7 +128,6 @@ func toFlatTree(blocks []*Block, baseDepth int, typ string) (ret []*Path) {
 	return
 }
 
-
 func toSubTree(blocks []*Block, keyword string) (ret []*Path) {
 	keyword = strings.TrimSpace(keyword)
 	var blockRoots []*Block