Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: https://github.com/siyuan-note/siyuan/issues/16206

Danielfc4cadfd5809
diff --git a/kernel/api/outline.go b/kernel/api/outline.go
index 893a278ef3..83c35c6e39 100644
--- a/kernel/api/outline.go
+++ b/kernel/api/outline.go
@@ -25,41 +25,6 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
-func getDocOutlineAndStorage(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
-
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
-
- if nil == arg["id"] {
- return
- }
- preview := false
- if previewArg := arg["preview"]; nil != previewArg {
- preview = previewArg.(bool)
- }
- rootID := arg["id"].(string)
- data, err := model.GetOutlineStorage(rootID)
- if err != nil {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- headings, err := model.Outline(rootID, preview)
- if err != nil {
- ret.Code = 1
- ret.Msg = err.Error()
- return
- }
- ret.Data = map[string]any{
- "headings": headings,
- "storage": data,
- }
-}
-
func getDocOutline(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
diff --git a/kernel/api/router.go b/kernel/api/router.go
index 1cc54bab89..495b86175f 100644
--- a/kernel/api/router.go
+++ b/kernel/api/router.go
@@ -151,7 +151,6 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/history/getHistoryItems", model.CheckAuth, model.CheckAdminRole, getHistoryItems)
ginServer.Handle("POST", "/api/outline/getDocOutline", model.CheckAuth, getDocOutline)
- ginServer.Handle("POST", "/api/outline/getDocOutlineAndStorage", model.CheckAuth, getDocOutlineAndStorage)
ginServer.Handle("POST", "/api/bookmark/getBookmark", model.CheckAuth, getBookmark)
ginServer.Handle("POST", "/api/bookmark/renameBookmark", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, renameBookmark)
diff --git a/kernel/model/block.go b/kernel/model/block.go
index 39f81da401..2648859595 100644
--- a/kernel/model/block.go
+++ b/kernel/model/block.go
@@ -103,6 +103,7 @@ type Path struct {
Children []*Path `json:"children,omitempty"` // 子路径节点
Depth int `json:"depth"` // 层级深度
Count int `json:"count"` // 子块计数
+ Folded bool `json:"folded"` // 是否折叠
Updated string `json:"updated"` // 更新时间
Created string `json:"created"` // 创建时间
diff --git a/kernel/model/outline.go b/kernel/model/outline.go
index 2b6c54dc8d..766fbf9a1c 100644
--- a/kernel/model/outline.go
+++ b/kernel/model/outline.go
@@ -17,6 +17,7 @@
package model
import (
+ "github.com/88250/gulu"
"github.com/88250/lute/ast"
"github.com/88250/lute/html"
"github.com/88250/lute/parse"
@@ -236,9 +237,43 @@ func Outline(rootID string, preview bool) (ret []*Path, err error) {
}
ret = outline(tree)
+
+ storage, _ := GetOutlineStorage(rootID)
+ if nil == storage {
+ // 默认展开顶层
+ for _, p := range ret {
+ p.Folded = false
+ }
+ }
+
+ if nil != storage["expandIds"] {
+ expandIDsArg := storage["expandIds"].([]interface{})
+ var expandIDs []string
+ for _, id := range expandIDsArg {
+ expandIDs = append(expandIDs, id.(string))
+ }
+
+ for _, p := range ret {
+ p.Folded = false // 顶层默认展开
+
+ for _, b := range p.Blocks {
+ b.Folded = !gulu.Str.Contains(b.ID, expandIDs)
+ for _, c := range b.Children {
+ walkChildren(c, expandIDs)
+ }
+ }
+ }
+ }
return
}
+func walkChildren(b *Block, expandIDs []string) {
+ b.Folded = !gulu.Str.Contains(b.ID, expandIDs)
+ for _, c := range b.Children {
+ walkChildren(c, expandIDs)
+ }
+}
+
func outline(tree *parse.Tree) (ret []*Path) {
luteEngine := NewLute()
var headings []*Block
@@ -254,6 +289,7 @@ func outline(tree *parse.Tree) (ret []*Path) {
Content: renderOutline(n, luteEngine),
Type: n.Type.String(),
SubType: treenode.SubTypeAbbr(n),
+ Folded: true,
}
headings = append(headings, block)
return ast.WalkSkipChildren
@@ -303,6 +339,7 @@ func outline(tree *parse.Tree) (ret []*Path) {
Blocks: b.Children,
Depth: 0,
Count: b.Count,
+ Folded: true,
})
}
}
diff --git a/kernel/model/path.go b/kernel/model/path.go
index d0ea6a2deb..3d188e8480 100644
--- a/kernel/model/path.go
+++ b/kernel/model/path.go
@@ -149,6 +149,11 @@ func toFlatTree(blocks []*Block, baseDepth int, typ string, tree *parse.Tree) (r
root.Children = append(root.Children, block)
}
+ folded := false
+ if "outline" == typ {
+ folded = true
+ }
+
for _, root := range blockRoots {
treeNode := &Path{
ID: root.ID,
@@ -159,6 +164,7 @@ func toFlatTree(blocks []*Block, baseDepth int, typ string, tree *parse.Tree) (r
SubType: root.SubType,
Depth: baseDepth,
Count: len(root.Children),
+ Folded: folded,
Updated: root.IAL["updated"],
Created: root.ID[:14],