🎨 Improve priority of folding processing when headings and super blocks are mixed https://github.com/siyuan-note/siyuan/issues/9488

This commit is contained in:
Daniel 2023-10-24 00:56:10 +08:00
parent 256e64e8b5
commit 5e6d94783b
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
5 changed files with 19 additions and 112 deletions

View file

@ -2,30 +2,14 @@ export const removeFoldHeading = (nodeElement: Element) => {
const nodeH = parseInt(nodeElement.getAttribute("data-subtype").substr(1)); const nodeH = parseInt(nodeElement.getAttribute("data-subtype").substr(1));
let nextElement = nodeElement.nextElementSibling; let nextElement = nodeElement.nextElementSibling;
while (nextElement) { while (nextElement) {
if (nextElement.classList.contains("sb")) { const currentH = parseInt(nextElement.getAttribute("data-subtype")?.substr(1));
let nextFirstElement = nextElement.firstElementChild; if (!nextElement.classList.contains("protyle-attr") && // 超级块末尾为属性
while (nextFirstElement && nextFirstElement.classList.contains("sb")) { (isNaN(currentH) || currentH > nodeH)) {
nextFirstElement = nextFirstElement.firstElementChild; const tempElement = nextElement;
} nextElement = nextElement.nextElementSibling;
if ((nextFirstElement.getAttribute("data-type") === "NodeHeading" && tempElement.remove();
parseInt(nextFirstElement.getAttribute("data-subtype").substr(1)) > nodeH) ||
nextFirstElement.getAttribute("data-type") !== "NodeHeading") {
const tempElement = nextElement;
nextElement = nextElement.nextElementSibling;
tempElement.remove();
} else {
break;
}
} else { } else {
const currentH = parseInt(nextElement.getAttribute("data-subtype")?.substr(1)); break;
if (!nextElement.classList.contains("protyle-attr") && // 超级块末尾为属性
(isNaN(currentH) || currentH > nodeH)) {
const tempElement = nextElement;
nextElement = nextElement.nextElementSibling;
tempElement.remove();
} else {
break;
}
} }
} }
}; };

View file

@ -848,12 +848,6 @@ func loadNodesByMode(node *ast.Node, inputIndex, mode, size int, isDoc, isHeadin
if n.HeadingLevel <= level { if n.HeadingLevel <= level {
break break
} }
} else if ast.NodeSuperBlock == n.Type {
if h := treenode.SuperBlockHeading(n); nil != h {
if level >= h.HeadingLevel {
break
}
}
} }
nodes = append(nodes, n) nodes = append(nodes, n)
count++ count++

View file

@ -46,11 +46,14 @@ func (tx *Transaction) doFoldHeading(operation *Operation) (ret *TxErr) {
return &TxErr{code: TxErrCodeBlockNotFound, id: headingID} return &TxErr{code: TxErrCodeBlockNotFound, id: headingID}
} }
children := treenode.HeadingChildren4Folding(heading) children := treenode.HeadingChildren(heading)
for _, child := range children { for _, child := range children {
childrenIDs = append(childrenIDs, child.ID) childrenIDs = append(childrenIDs, child.ID)
child.SetIALAttr("fold", "1") ast.Walk(child, func(n *ast.Node, entering bool) ast.WalkStatus {
child.SetIALAttr("heading-fold", "1") n.SetIALAttr("fold", "1")
n.SetIALAttr("heading-fold", "1")
return ast.WalkContinue
})
} }
heading.SetIALAttr("fold", "1") heading.SetIALAttr("fold", "1")
if err = tx.writeTree(tree); nil != err { if err = tx.writeTree(tree); nil != err {
@ -80,10 +83,13 @@ func (tx *Transaction) doUnfoldHeading(operation *Operation) (ret *TxErr) {
return &TxErr{code: TxErrCodeBlockNotFound, id: headingID} return &TxErr{code: TxErrCodeBlockNotFound, id: headingID}
} }
children := treenode.HeadingChildren4Folding(heading) children := treenode.HeadingChildren(heading)
for _, child := range children { for _, child := range children {
child.RemoveIALAttr("heading-fold") ast.Walk(child, func(n *ast.Node, entering bool) ast.WalkStatus {
child.RemoveIALAttr("fold") n.RemoveIALAttr("heading-fold")
n.RemoveIALAttr("fold")
return ast.WalkContinue
})
} }
heading.RemoveIALAttr("fold") heading.RemoveIALAttr("fold")
heading.RemoveIALAttr("heading-fold") heading.RemoveIALAttr("heading-fold")

View file

@ -881,12 +881,6 @@ func heading(node *ast.Node) *ast.Node {
currentLevel := 16 currentLevel := 16
if ast.NodeHeading == node.Type { if ast.NodeHeading == node.Type {
currentLevel = node.HeadingLevel currentLevel = node.HeadingLevel
} else if ast.NodeSuperBlock == node.Type {
superBlockHeading := treenode.SuperBlockHeading(node)
if nil != superBlockHeading {
node = superBlockHeading
currentLevel = node.HeadingLevel
}
} }
for prev := node.Previous; nil != prev; prev = prev.Previous { for prev := node.Previous; nil != prev; prev = prev.Previous {

View file

@ -62,20 +62,6 @@ func IsInFoldedHeading(node, currentHeading *ast.Node) bool {
return false return false
} }
if ast.NodeSuperBlock == node.Type {
// The super block below the folded heading contains headings of the same level and cannot be loaded https://github.com/siyuan-note/siyuan/issues/9162
if nil == currentHeading {
return false
}
sbChildHeading := SuperBlockHeading(node)
if nil != sbChildHeading {
if sbChildHeading.HeadingLevel <= currentHeading.HeadingLevel {
return false
}
}
}
heading := HeadingParent(node) heading := HeadingParent(node)
if nil == heading { if nil == heading {
return false return false
@ -99,39 +85,6 @@ func GetHeadingFold(nodes []*ast.Node) (ret []*ast.Node) {
return return
} }
// HeadingChildren4Folding 获取标题下方所属该标题层级的所有节点,如果遇到超级块的话则递归获取超级块下方的所有节点。
// 折叠和取消折叠要用这个函数单独处理 https://github.com/siyuan-note/siyuan/issues/9435
func HeadingChildren4Folding(heading *ast.Node) (ret []*ast.Node) {
start := heading.Next
if nil == start {
return
}
if ast.NodeKramdownBlockIAL == start.Type {
start = start.Next // 跳过 heading 的 IAL
}
currentLevel := heading.HeadingLevel
for n := start; nil != n; n = n.Next {
if ast.NodeHeading == n.Type {
if currentLevel >= n.HeadingLevel {
break
}
} else if ast.NodeSuperBlock == n.Type {
ast.Walk(n, func(child *ast.Node, entering bool) ast.WalkStatus {
if !entering || !child.IsBlock() {
return ast.WalkContinue
}
ret = append(ret, child)
return ast.WalkContinue
})
continue
}
ret = append(ret, n)
}
return
}
func HeadingChildren(heading *ast.Node) (ret []*ast.Node) { func HeadingChildren(heading *ast.Node) (ret []*ast.Node) {
start := heading.Next start := heading.Next
if nil == start { if nil == start {
@ -147,36 +100,12 @@ func HeadingChildren(heading *ast.Node) (ret []*ast.Node) {
if currentLevel >= n.HeadingLevel { if currentLevel >= n.HeadingLevel {
break break
} }
} else if ast.NodeSuperBlock == n.Type {
if h := SuperBlockHeading(n); nil != h {
if currentLevel >= h.HeadingLevel {
break
}
}
} else if ast.NodeSuperBlockCloseMarker == n.Type {
continue
} }
ret = append(ret, n) ret = append(ret, n)
} }
return return
} }
func SuperBlockHeading(sb *ast.Node) *ast.Node {
c := sb.FirstChild.Next.Next
if nil == c {
return nil
}
if ast.NodeHeading == c.Type {
return c
}
if ast.NodeSuperBlock == c.Type {
return SuperBlockHeading(c)
}
return nil
}
func SuperBlockLastHeading(sb *ast.Node) *ast.Node { func SuperBlockLastHeading(sb *ast.Node) *ast.Node {
headings := sb.ChildrenByType(ast.NodeHeading) headings := sb.ChildrenByType(ast.NodeHeading)
if 0 < len(headings) { if 0 < len(headings) {