瀏覽代碼

:bug: Improve outline dnd https://github.com/siyuan-note/siyuan/issues/10828

Daniel 1 年之前
父節點
當前提交
20ee605bfc
共有 1 個文件被更改,包括 24 次插入4 次删除
  1. 24 4
      kernel/model/outline.go

+ 24 - 4
kernel/model/outline.go

@@ -173,12 +173,32 @@ func (tx *Transaction) doMoveOutlineHeading(operation *Operation) (ret *TxErr) {
 	} else {
 		generateOpTypeHistory(tree, HistoryOpOutline)
 
-		// 移到最前
-		for i := len(headingChildren) - 1; i >= 0; i-- {
+		// 移到第一个标题前
+		var firstHeading *ast.Node
+		for n := tree.Root.FirstChild; nil != n; n = n.Next {
+			if ast.NodeHeading == n.Type {
+				firstHeading = n
+				break
+			}
+		}
+		if nil == firstHeading || firstHeading.ID == heading.ID {
+			return
+		}
+
+		diffLevel := heading.HeadingLevel - firstHeading.HeadingLevel
+		heading.HeadingLevel = firstHeading.HeadingLevel
+
+		firstHeading.InsertBefore(heading)
+		for i := 0; i < len(headingChildren)-1; i++ {
 			child := headingChildren[i]
-			tree.Root.PrependChild(child)
+			if ast.NodeHeading == child.Type {
+				child.HeadingLevel -= diffLevel
+				if 6 < child.HeadingLevel {
+					child.HeadingLevel = 6
+				}
+			}
+			firstHeading.InsertBefore(child)
 		}
-		tree.Root.PrependChild(heading)
 	}
 
 	if err = tx.writeTree(tree); nil != err {