Skip to content

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

:bug: Correct folded subheading levels after transformation https://github.com/siyuan-note/siyuan/issues/18625

Danielbe1013d84e60
diff --git a/app/src/layout/dock/Outline.ts b/app/src/layout/dock/Outline.ts
index a265d7c115..97eeff2cad 100644
--- a/app/src/layout/dock/Outline.ts
+++ b/app/src/layout/dock/Outline.ts
@@ -35,6 +35,7 @@ import {
operationsMayChangeOutline,
transactionsMayChangeRootHeadingNumberSetting
} from "../../protyle/util/headingNumberCore";
+import {applyHeadingLevelUpdates, getHeadingLevelUpdateOperations} from "../../protyle/util/headingTransform";
export class Outline extends Model {
public tree: Tree;
@@ -1424,22 +1425,22 @@ export class Outline extends Model {
id,
level
}, (response) => {
- response.data.doOperations.forEach((operation: any, index: number) => {
- protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach((itemElement: HTMLElement) => {
- itemElement.outerHTML = operation.data;
- });
- // 使用 outer 后元素需要重新查询
- protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach((itemElement: HTMLElement) => {
- mathRender(itemElement);
- });
- if (index === 0) {
- const focusElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${operation.id}"]`);
- if (focusElement) {
- focusElement.scrollIntoView({behavior: "smooth", block: "center"});
- }
+ if (!response.data?.doOperations?.length) {
+ return;
+ }
+ applyHeadingLevelUpdates(protyle, response.data.doOperations, mathRender);
+ const focusElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`);
+ if (focusElement) {
+ focusElement.scrollIntoView({behavior: "smooth", block: "center"});
+ }
+ const childUpdateOperations = getHeadingLevelUpdateOperations(response.data.doOperations, new Set([id]));
+ const hasUnfoldOperation = response.data.doOperations.some((operation: IOperation) =>
+ operation.action === "unfoldHeading");
+ transaction(protyle, response.data.doOperations, response.data.undoOperations, hasUnfoldOperation ? {
+ callback() {
+ applyHeadingLevelUpdates(protyle, childUpdateOperations, mathRender);
}
- });
- transaction(protyle, response.data.doOperations, response.data.undoOperations);
+ } : undefined);
});
}
};
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index cb31a61b33..e998a57302 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -93,6 +93,7 @@ import {checkFold} from "../../util/noRelyPCFunction";
import {clearSelect} from "../util/clear";
import {chartRender} from "../render/chartRender";
import {appendListItem, prependListItem} from "../wysiwyg/list";
+import {applyHeadingLevelUpdates, getHeadingLevelUpdateOperations} from "../util/headingTransform";
// 块类型 data-type 到本地化名称键的映射,用于块标提示中的 ${x}
const BLOCK_TYPE_LANG_KEYS: { [key: string]: string } = {
@@ -2698,20 +2699,19 @@ export class Gutter {
if (!response.data?.doOperations?.length) {
return;
}
- response.data.doOperations.forEach((operation: IOperation) => {
- protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach((itemElement: HTMLElement) => {
- itemElement.outerHTML = operation.data;
- });
- // 使用 outer 后元素需要重新查询
- protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach((itemElement: HTMLElement) => {
- mathRender(itemElement);
- });
- });
+ applyHeadingLevelUpdates(protyle, response.data.doOperations, mathRender);
const focusElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${ids[0]}"]`);
if (focusElement) {
focusBlock(focusElement, protyle.wysiwyg.element, true);
}
- transaction(protyle, response.data.doOperations, response.data.undoOperations);
+ const childUpdateOperations = getHeadingLevelUpdateOperations(response.data.doOperations, new Set(ids));
+ const hasUnfoldOperation = response.data.doOperations.some((operation: IOperation) =>
+ operation.action === "unfoldHeading");
+ transaction(protyle, response.data.doOperations, response.data.undoOperations, hasUnfoldOperation ? {
+ callback() {
+ applyHeadingLevelUpdates(protyle, childUpdateOperations, mathRender);
+ }
+ } : undefined);
});
}
};
diff --git a/app/src/protyle/util/headingTransform.test.ts b/app/src/protyle/util/headingTransform.test.ts
new file mode 100644
index 0000000000..37e7c16729
--- /dev/null
+++ b/app/src/protyle/util/headingTransform.test.ts
@@ -0,0 +1,19 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {getHeadingLevelUpdateOperations} from "./headingTransform";
+
+describe("getHeadingLevelUpdateOperations", () => {
+ it("keeps heading updates out of a compound unfold transaction", () => {
+ const operations = [
+ {action: "unfoldHeading", id: "heading-1"},
+ {action: "update", id: "heading-1", data: "parent"},
+ {action: "update", id: "heading-2", data: "child"},
+ {action: "foldHeading", id: "heading-1"},
+ ] as IOperation[];
+
+ assert.deepEqual(getHeadingLevelUpdateOperations(operations).map(operation => operation.id),
+ ["heading-1", "heading-2"]);
+ assert.deepEqual(getHeadingLevelUpdateOperations(operations, new Set(["heading-1"])).map(operation => operation.id),
+ ["heading-2"]);
+ });
+});
diff --git a/app/src/protyle/util/headingTransform.ts b/app/src/protyle/util/headingTransform.ts
new file mode 100644
index 0000000000..1ceaae3d3e
--- /dev/null
+++ b/app/src/protyle/util/headingTransform.ts
@@ -0,0 +1,15 @@
+export const getHeadingLevelUpdateOperations = (operations: IOperation[], excludedIDs = new Set<string>()) => {
+ return operations.filter(operation => operation.action === "update" && !excludedIDs.has(operation.id));
+};
+
+export const applyHeadingLevelUpdates = (protyle: IProtyle, operations: IOperation[], render: (element: HTMLElement) => unknown) => {
+ getHeadingLevelUpdateOperations(operations).forEach(operation => {
+ protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach((itemElement: HTMLElement) => {
+ itemElement.outerHTML = operation.data;
+ });
+ // 使用 outer 后元素需要重新查询
+ protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach((itemElement: HTMLElement) => {
+ render(itemElement);
+ });
+ });
+};
diff --git a/kernel/model/block.go b/kernel/model/block.go
index 7f05a4fd6e..eeb98b1edd 100644
--- a/kernel/model/block.go
+++ b/kernel/model/block.go
@@ -1048,13 +1048,14 @@ func GetHeadingLevelBatchTransaction(ids []string, level int) (transaction *Tran
childrenHeadings := collectHeadingLevelNodes(tree.Root, selectedHeadings)
fillBlockRefCount(childrenHeadings, tree.Box)
+ var foldedHeadings []*ast.Node
for _, heading := range selectedHeadings {
if treenode.IsSelfFolded(heading) {
- unfoldHeading(heading, heading)
+ foldedHeadings = append(foldedHeadings, heading)
}
}
- transaction = buildHeadingLevelTransaction(childrenHeadings, diff)
+ transaction = buildHeadingLevelTransaction(childrenHeadings, foldedHeadings, diff)
return
}
@@ -1114,8 +1115,13 @@ func collectHeadingLevelNodes(root *ast.Node, selectedHeadings []*ast.Node) (ret
return
}
-func buildHeadingLevelTransaction(headings []*ast.Node, diff int) (transaction *Transaction) {
+func buildHeadingLevelTransaction(headings, foldedHeadings []*ast.Node, diff int) (transaction *Transaction) {
transaction = &Transaction{}
+ for _, heading := range foldedHeadings {
+ treenode.SetSelfFolded(heading, false)
+ transaction.DoOperations = append(transaction.DoOperations, &Operation{Action: "unfoldHeading", ID: heading.ID})
+ }
+
luteEngine := util.NewLute()
for _, c := range headings {
op := &Operation{}
@@ -1137,6 +1143,9 @@ func buildHeadingLevelTransaction(headings []*ast.Node, diff int) (transaction *
op.Data = luteEngine.RenderNodeBlockDOM(cleanRenderNode(c, false))
transaction.DoOperations = append(transaction.DoOperations, op)
}
+ for _, heading := range foldedHeadings {
+ transaction.UndoOperations = append(transaction.UndoOperations, &Operation{Action: "foldHeading", ID: heading.ID})
+ }
return
}
diff --git a/kernel/model/heading_level_test.go b/kernel/model/heading_level_test.go
index 095e991e7a..55154fe061 100644
--- a/kernel/model/heading_level_test.go
+++ b/kernel/model/heading_level_test.go
@@ -17,10 +17,12 @@
package model
import (
+ "strings"
"testing"
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
)
func TestHeadingLevelSelectionUsesDirectContainerAndLevel(t *testing.T) {
@@ -92,7 +94,7 @@ func TestBuildHeadingLevelTransactionClampsLevels(t *testing.T) {
h2 := newHeadingLevelTestNode("h2", 2)
h6 := newHeadingLevelTestNode("h6", 6)
- transaction := buildHeadingLevelTransaction([]*ast.Node{h1, h2, h6}, 1)
+ transaction := buildHeadingLevelTransaction([]*ast.Node{h1, h2, h6}, nil, 1)
if len(transaction.DoOperations) != 3 || len(transaction.UndoOperations) != 3 {
t.Fatalf("expected three do and undo operations")
}
@@ -107,6 +109,39 @@ func TestBuildHeadingLevelTransactionClampsLevels(t *testing.T) {
}
}
+func TestBuildHeadingLevelTransactionUnfoldsBeforeUpdates(t *testing.T) {
+ root := &ast.Node{Type: ast.NodeDocument}
+ h1First := newHeadingLevelTestNode("h1-first", 1)
+ h2First := newHeadingLevelTestNode("h2-first", 2)
+ h1Second := newHeadingLevelTestNode("h1-second", 1)
+ h4Second := newHeadingLevelTestNode("h4-second", 4)
+ root.AppendChild(h1First)
+ root.AppendChild(h2First)
+ root.AppendChild(h1Second)
+ root.AppendChild(h4Second)
+ treenode.SetSelfFolded(h1First, true)
+ treenode.SetSelfFolded(h1Second, true)
+
+ headings := collectHeadingLevelNodes(root, []*ast.Node{h1First, h1Second})
+ transaction := buildHeadingLevelTransaction(headings, []*ast.Node{h1First, h1Second}, 5)
+ assertHeadingLevelOperations(t, transaction.DoOperations,
+ []string{"unfoldHeading", "unfoldHeading", "update", "update", "update", "update"},
+ []string{h1First.ID, h1Second.ID, h1First.ID, h2First.ID, h1Second.ID, h4Second.ID})
+ assertHeadingLevelOperations(t, transaction.UndoOperations,
+ []string{"update", "update", "update", "update", "foldHeading", "foldHeading"},
+ []string{h1First.ID, h2First.ID, h1Second.ID, h4Second.ID, h1First.ID, h1Second.ID})
+
+ if treenode.IsSelfFolded(h1First) || treenode.IsSelfFolded(h1Second) {
+ t.Fatalf("expected transaction update data to render unfolded headings")
+ }
+ for _, operation := range transaction.DoOperations[2:] {
+ data, ok := operation.Data.(string)
+ if !ok || !strings.Contains(data, `data-subtype="h6"`) {
+ t.Fatalf("expected heading [%s] to be transformed to level 6", operation.ID)
+ }
+ }
+}
+
func newHeadingLevelTestNode(id string, level int) *ast.Node {
heading := &ast.Node{Type: ast.NodeHeading, ID: id, HeadingLevel: level}
heading.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(id)})
@@ -124,3 +159,16 @@ func assertHeadingLevelNodeIDs(t *testing.T, nodes []*ast.Node, expected ...stri
}
}
}
+
+func assertHeadingLevelOperations(t *testing.T, operations []*Operation, actions, ids []string) {
+ t.Helper()
+ if len(operations) != len(actions) || len(operations) != len(ids) {
+ t.Fatalf("expected %d operations, got %d", len(actions), len(operations))
+ }
+ for i, operation := range operations {
+ if operation.Action != actions[i] || operation.ID != ids[i] {
+ t.Fatalf("expected operation [%s %s] at index %d, got [%s %s]",
+ actions[i], ids[i], i, operation.Action, operation.ID)
+ }
+ }
+}