🎨 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));
let nextElement = nodeElement.nextElementSibling;
while (nextElement) {
if (nextElement.classList.contains("sb")) {
let nextFirstElement = nextElement.firstElementChild;
while (nextFirstElement && nextFirstElement.classList.contains("sb")) {
nextFirstElement = nextFirstElement.firstElementChild;
}
if ((nextFirstElement.getAttribute("data-type") === "NodeHeading" &&
parseInt(nextFirstElement.getAttribute("data-subtype").substr(1)) > nodeH) ||
nextFirstElement.getAttribute("data-type") !== "NodeHeading") {
const tempElement = nextElement;
nextElement = nextElement.nextElementSibling;
tempElement.remove();
} else {
break;
}
const currentH = parseInt(nextElement.getAttribute("data-subtype")?.substr(1));
if (!nextElement.classList.contains("protyle-attr") && // 超级块末尾为属性
(isNaN(currentH) || currentH > nodeH)) {
const tempElement = nextElement;
nextElement = nextElement.nextElementSibling;
tempElement.remove();
} else {
const currentH = parseInt(nextElement.getAttribute("data-subtype")?.substr(1));
if (!nextElement.classList.contains("protyle-attr") && // 超级块末尾为属性
(isNaN(currentH) || currentH > nodeH)) {
const tempElement = nextElement;
nextElement = nextElement.nextElementSibling;
tempElement.remove();
} else {
break;
}
break;
}
}
};

View file

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

View file

@ -46,11 +46,14 @@ func (tx *Transaction) doFoldHeading(operation *Operation) (ret *TxErr) {
return &TxErr{code: TxErrCodeBlockNotFound, id: headingID}
}
children := treenode.HeadingChildren4Folding(heading)
children := treenode.HeadingChildren(heading)
for _, child := range children {
childrenIDs = append(childrenIDs, child.ID)
child.SetIALAttr("fold", "1")
child.SetIALAttr("heading-fold", "1")
ast.Walk(child, func(n *ast.Node, entering bool) ast.WalkStatus {
n.SetIALAttr("fold", "1")
n.SetIALAttr("heading-fold", "1")
return ast.WalkContinue
})
}
heading.SetIALAttr("fold", "1")
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}
}
children := treenode.HeadingChildren4Folding(heading)
children := treenode.HeadingChildren(heading)
for _, child := range children {
child.RemoveIALAttr("heading-fold")
child.RemoveIALAttr("fold")
ast.Walk(child, func(n *ast.Node, entering bool) ast.WalkStatus {
n.RemoveIALAttr("heading-fold")
n.RemoveIALAttr("fold")
return ast.WalkContinue
})
}
heading.RemoveIALAttr("fold")
heading.RemoveIALAttr("heading-fold")

View file

@ -881,12 +881,6 @@ func heading(node *ast.Node) *ast.Node {
currentLevel := 16
if ast.NodeHeading == node.Type {
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 {

View file

@ -62,20 +62,6 @@ func IsInFoldedHeading(node, currentHeading *ast.Node) bool {
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)
if nil == heading {
return false
@ -99,39 +85,6 @@ func GetHeadingFold(nodes []*ast.Node) (ret []*ast.Node) {
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) {
start := heading.Next
if nil == start {
@ -147,36 +100,12 @@ func HeadingChildren(heading *ast.Node) (ret []*ast.Node) {
if currentLevel >= n.HeadingLevel {
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)
}
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 {
headings := sb.ChildrenByType(ast.NodeHeading)
if 0 < len(headings) {