Skip to content

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

:bug: Reveal folded flashcard answers https://github.com/siyuan-note/siyuan/issues/18362

Danielf584e0ca6321
diff --git a/app/src/card/openCard.ts b/app/src/card/openCard.ts
index 383ac9b552..a2c655cd54 100644
--- a/app/src/card/openCard.ts
+++ b/app/src/card/openCard.ts
@@ -164,7 +164,8 @@ const getEditor = (id: string, protyle: IProtyle, element: Element, currentCard:
fetchPost("/api/filetree/getDoc", {
id,
mode: 0,
- size: Constants.SIZE_GET_MAX
+ size: Constants.SIZE_GET_MAX,
+ unfold: true
}, (response) => {
onGet({
updateReadonly: true,
diff --git a/kernel/api/filetree.go b/kernel/api/filetree.go
index ef8111b6da..c6ac602ecc 100644
--- a/kernel/api/filetree.go
+++ b/kernel/api/filetree.go
@@ -1415,6 +1415,10 @@ func getDoc(c *gin.Context) {
if nil != highlightArg {
highlight = highlightArg.(bool)
}
+ unfold := false
+ if unfoldArg := arg["unfold"]; nil != unfoldArg {
+ unfold = unfoldArg.(bool)
+ }
var blockCount int
var content, parentID, parent2ID, rootID, typ string
@@ -1427,10 +1431,10 @@ func getDoc(c *gin.Context) {
// 加密笔记本的打开文档走 InBox 版(查加密 blocktree + content db)
if notebook, ok := arg["notebook"].(string); ok && notebook != "" && model.IsEncryptedBox(notebook) {
blockCount, content, parentID, parent2ID, rootID, typ, eof, scroll, boxID, docPath, isBacklinkExpand, keywords, headingNumbers, err =
- model.GetDocInBox(startID, endID, id, index, query, queryTypes, querySubTypes, queryMethod, mode, size, isBacklink, originalRefBlockIDs, highlight, notebook)
+ model.GetDocInBox(startID, endID, id, index, query, queryTypes, querySubTypes, queryMethod, mode, size, isBacklink, originalRefBlockIDs, highlight, unfold, notebook)
} else {
blockCount, content, parentID, parent2ID, rootID, typ, eof, scroll, boxID, docPath, isBacklinkExpand, keywords, headingNumbers, err =
- model.GetDoc(startID, endID, id, index, query, queryTypes, querySubTypes, queryMethod, mode, size, isBacklink, originalRefBlockIDs, highlight)
+ model.GetDoc(startID, endID, id, index, query, queryTypes, querySubTypes, queryMethod, mode, size, isBacklink, originalRefBlockIDs, highlight, unfold)
}
if errors.Is(err, model.ErrBlockNotFound) {
ret.Code = 3
diff --git a/kernel/model/file.go b/kernel/model/file.go
index 8e0c11da34..dfb2596303 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -582,14 +582,33 @@ func ListDocTree(boxID, listPath string, sortMode int, flashcard, showHidden boo
return
}
-func GetDoc(startID, endID, id string, index int, query string, queryTypes, querySubTypes map[string]bool, queryMethod, mode int, size int, isBacklink bool, originalRefBlockIDs map[string]string, highlight bool) (
+func unfoldBlockForRender(node *ast.Node) {
+ removeFold := func(root *ast.Node) {
+ ast.Walk(root, func(n *ast.Node, entering bool) ast.WalkStatus {
+ if entering {
+ n.RemoveIALAttr("fold")
+ n.RemoveIALAttr("heading-fold")
+ }
+ return ast.WalkContinue
+ })
+ }
+
+ removeFold(node)
+ if ast.NodeHeading == node.Type {
+ for _, child := range treenode.HeadingChildren(node) {
+ removeFold(child)
+ }
+ }
+}
+
+func GetDoc(startID, endID, id string, index int, query string, queryTypes, querySubTypes map[string]bool, queryMethod, mode int, size int, isBacklink bool, originalRefBlockIDs map[string]string, highlight, unfold bool) (
blockCount int, dom, parentID, parent2ID, rootID, typ string, eof, scroll bool, boxID, docPath string, isBacklinkExpand bool, keywords []string, headingNumbers map[string]string, err error) {
- return GetDocInBox(startID, endID, id, index, query, queryTypes, querySubTypes, queryMethod, mode, size, isBacklink, originalRefBlockIDs, highlight, "")
+ return GetDocInBox(startID, endID, id, index, query, queryTypes, querySubTypes, queryMethod, mode, size, isBacklink, originalRefBlockIDs, highlight, unfold, "")
}
// GetDocInBox 与 GetDoc 一致,但按 boxID 路由到加密 db 或全局 db。
// 加密笔记本打开文档时传入 boxID,blocktree/content 查询走加密 db;boxID 为空时 fall-through 全局 db。
-func GetDocInBox(startID, endID, id string, index int, query string, queryTypes, querySubTypes map[string]bool, queryMethod, mode int, size int, isBacklink bool, originalRefBlockIDs map[string]string, highlight bool, boxID string) (
+func GetDocInBox(startID, endID, id string, index int, query string, queryTypes, querySubTypes map[string]bool, queryMethod, mode int, size int, isBacklink bool, originalRefBlockIDs map[string]string, highlight, unfold bool, boxID string) (
blockCount int, dom, parentID, parent2ID, rootID, typ string, eof, scroll bool, boxIDOut, docPath string, isBacklinkExpand bool, keywords []string, headingNumbers map[string]string, err error) {
//os.MkdirAll("pprof", 0755)
//cpuProfile, _ := os.Create("pprof/GetDoc")
@@ -623,6 +642,9 @@ func GetDocInBox(startID, endID, id string, index int, query string, queryTypes,
return
}
}
+ if unfold {
+ unfoldBlockForRender(node)
+ }
located := false
isDoc := ast.NodeDocument == node.Type
diff --git a/kernel/model/file_unfold_test.go b/kernel/model/file_unfold_test.go
new file mode 100644
index 0000000000..83c93f986c
--- /dev/null
+++ b/kernel/model/file_unfold_test.go
@@ -0,0 +1,88 @@
+// 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 <https://www.gnu.org/licenses/>.
+
+package model
+
+import (
+ "testing"
+
+ "github.com/88250/lute/ast"
+)
+
+func TestUnfoldHeadingForRender(t *testing.T) {
+ root := &ast.Node{Type: ast.NodeDocument}
+ heading := &ast.Node{Type: ast.NodeHeading, HeadingLevel: 2, ID: "heading"}
+ paragraph := &ast.Node{Type: ast.NodeParagraph, ID: "paragraph"}
+ list := &ast.Node{Type: ast.NodeList, ID: "list"}
+ listItem := &ast.Node{Type: ast.NodeListItem, ID: "list-item"}
+ nextHeading := &ast.Node{Type: ast.NodeHeading, HeadingLevel: 2, ID: "next-heading"}
+ for _, node := range []*ast.Node{heading, paragraph, list, listItem, nextHeading} {
+ node.SetIALAttr("fold", "1")
+ node.SetIALAttr("heading-fold", "1")
+ }
+ list.AppendChild(listItem)
+ root.AppendChild(heading)
+ root.AppendChild(paragraph)
+ root.AppendChild(list)
+ root.AppendChild(nextHeading)
+
+ nodes, _ := loadNodesByMode(heading, 0, 0, 100, false, true)
+ if 1 != len(nodes) {
+ t.Fatalf("folded heading returned %d nodes, want 1", len(nodes))
+ }
+
+ unfoldBlockForRender(heading)
+
+ nodes, _ = loadNodesByMode(heading, 0, 0, 100, false, true)
+ if 3 != len(nodes) {
+ t.Fatalf("unfolded heading returned %d nodes, want 3", len(nodes))
+ }
+ for _, node := range []*ast.Node{heading, paragraph, list, listItem} {
+ if "" != node.IALAttr("fold") || "" != node.IALAttr("heading-fold") {
+ t.Fatalf("render node [%s] remains folded", node.ID)
+ }
+ }
+ if "1" != nextHeading.IALAttr("fold") || "1" != nextHeading.IALAttr("heading-fold") {
+ t.Fatal("the following heading fold state was changed")
+ }
+}
+
+func TestUnfoldContainerForRender(t *testing.T) {
+ list := &ast.Node{Type: ast.NodeList, ID: "list"}
+ listItem := &ast.Node{Type: ast.NodeListItem, ID: "list-item"}
+ childList := &ast.Node{Type: ast.NodeList, ID: "child-list"}
+ childItem := &ast.Node{Type: ast.NodeListItem, ID: "child-item"}
+ siblingItem := &ast.Node{Type: ast.NodeListItem, ID: "sibling-item"}
+ for _, node := range []*ast.Node{listItem, childList, childItem, siblingItem} {
+ node.SetIALAttr("fold", "1")
+ node.SetIALAttr("heading-fold", "1")
+ }
+ childList.AppendChild(childItem)
+ listItem.AppendChild(childList)
+ list.AppendChild(listItem)
+ list.AppendChild(siblingItem)
+
+ unfoldBlockForRender(listItem)
+
+ for _, node := range []*ast.Node{listItem, childList, childItem} {
+ if "" != node.IALAttr("fold") || "" != node.IALAttr("heading-fold") {
+ t.Fatalf("render node [%s] remains folded", node.ID)
+ }
+ }
+ if "1" != siblingItem.IALAttr("fold") || "1" != siblingItem.IALAttr("heading-fold") {
+ t.Fatal("the sibling list item fold state was changed")
+ }
+}