Skip to content

Commit

Permalink
🎨 Improve priority of folding processing when headings and super bloc…
Browse files Browse the repository at this point in the history
…ks are mixed #9488
88250 committed Oct 23, 2023

Verified

This commit was signed with the committer’s verified signature.
1 parent 256e64e commit 5e6d947
Showing 5 changed files with 19 additions and 112 deletions.
30 changes: 7 additions & 23 deletions app/src/protyle/util/heading.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
};
6 changes: 0 additions & 6 deletions kernel/model/file.go
Original file line number Diff line number Diff line change
@@ -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++
18 changes: 12 additions & 6 deletions kernel/model/heading.go
Original file line number Diff line number Diff line change
@@ -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")
6 changes: 0 additions & 6 deletions kernel/sql/database.go
Original file line number Diff line number Diff line change
@@ -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 {
71 changes: 0 additions & 71 deletions kernel/treenode/heading.go
Original file line number Diff line number Diff line change
@@ -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) {

0 comments on commit 5e6d947

Please sign in to comment.