Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Mar 28, 2024
2 parents d2c0f57 + 58f7996 commit 5429380
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 30 deletions.
70 changes: 56 additions & 14 deletions kernel/api/block_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,54 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)

func moveOutlineHeading(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)

arg, ok := util.JsonArg(c, ret)
if !ok {
return
}

id := arg["id"].(string)
if util.InvalidIDPattern(id, ret) {
return
}

var parentID, previousID string
if nil != arg["parentID"] {
parentID = arg["parentID"].(string)
if "" != parentID && util.InvalidIDPattern(parentID, ret) {
return
}
}
if nil != arg["previousID"] {
previousID = arg["previousID"].(string)
if "" != previousID && util.InvalidIDPattern(previousID, ret) {
return
}
}

transactions := []*model.Transaction{
{
DoOperations: []*model.Operation{
{
Action: "moveOutlineHeading",
ID: id,
PreviousID: previousID,
ParentID: parentID,
},
},
},
}

model.PerformTransactions(&transactions)
model.WaitForWritingFiles()

ret.Data = transactions
broadcastTransactions(transactions)
}

func appendDailyNoteBlock(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
Expand Down Expand Up @@ -222,13 +270,13 @@ func moveBlock(c *gin.Context) {
var parentID, previousID string
if nil != arg["parentID"] {
parentID = arg["parentID"].(string)
if util.InvalidIDPattern(parentID, ret) {
if "" != parentID && util.InvalidIDPattern(parentID, ret) {
return
}
}
if nil != arg["previousID"] {
previousID = arg["previousID"].(string)
if util.InvalidIDPattern(previousID, ret) {
if "" != previousID && util.InvalidIDPattern(previousID, ret) {
return
}

Expand Down Expand Up @@ -364,26 +412,20 @@ func insertBlock(c *gin.Context) {
var parentID, previousID, nextID string
if nil != arg["parentID"] {
parentID = arg["parentID"].(string)
if "" != parentID {
if util.InvalidIDPattern(parentID, ret) {
return
}
if "" != parentID && util.InvalidIDPattern(parentID, ret) {
return
}
}
if nil != arg["previousID"] {
previousID = arg["previousID"].(string)
if "" != previousID {
if util.InvalidIDPattern(previousID, ret) {
return
}
if "" != previousID && util.InvalidIDPattern(parentID, ret) {
return
}
}
if nil != arg["nextID"] {
nextID = arg["nextID"].(string)
if "" != nextID {
if util.InvalidIDPattern(nextID, ret) {
return
}
if "" != nextID && util.InvalidIDPattern(parentID, ret) {
return
}
}

Expand Down
1 change: 1 addition & 0 deletions kernel/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/block/updateBlock", model.CheckAuth, model.CheckReadonly, updateBlock)
ginServer.Handle("POST", "/api/block/deleteBlock", model.CheckAuth, model.CheckReadonly, deleteBlock)
ginServer.Handle("POST", "/api/block/moveBlock", model.CheckAuth, model.CheckReadonly, moveBlock)
ginServer.Handle("POST", "/api/block/moveOutlineHeading", model.CheckAuth, model.CheckReadonly, moveOutlineHeading)
ginServer.Handle("POST", "/api/block/foldBlock", model.CheckAuth, model.CheckReadonly, foldBlock)
ginServer.Handle("POST", "/api/block/unfoldBlock", model.CheckAuth, model.CheckReadonly, unfoldBlock)
ginServer.Handle("POST", "/api/block/setBlockReminder", model.CheckAuth, model.CheckReadonly, setBlockReminder)
Expand Down
48 changes: 40 additions & 8 deletions kernel/av/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,21 @@ func (value *Value) Compare(other *Value, attrView *AttributeView) int {
}
case KeyTypeText:
if nil != value.Text && nil != other.Text {
if "" == value.Text.Content {
if "" == other.Text.Content {
return 0
}
return 1
} else if "" == other.Text.Content {
return -1
}
return strings.Compare(value.Text.Content, other.Text.Content)
}
case KeyTypeNumber:
if nil != value.Number && nil != other.Number {
if value.Number.IsNotEmpty {
if !other.Number.IsNotEmpty {
return 1
return -1
}

if value.Number.Content > other.Number.Content {
Expand All @@ -65,17 +73,17 @@ func (value *Value) Compare(other *Value, attrView *AttributeView) int {
}
return 0
} else {
if other.Number.IsNotEmpty {
return -1
if !other.Number.IsNotEmpty {
return 0
}
return 0
return 1
}
}
case KeyTypeDate:
if nil != value.Date && nil != other.Date {
if value.Date.IsNotEmpty {
if !other.Date.IsNotEmpty {
return 1
return -1
}
if value.Date.Content > other.Date.Content {
return 1
Expand All @@ -85,10 +93,10 @@ func (value *Value) Compare(other *Value, attrView *AttributeView) int {
}
return 0
} else {
if other.Date.IsNotEmpty {
return -1
if !other.Date.IsNotEmpty {
return 0
}
return 0
return 1
}
}
case KeyTypeCreated:
Expand Down Expand Up @@ -140,14 +148,38 @@ func (value *Value) Compare(other *Value, attrView *AttributeView) int {
}
case KeyTypeURL:
if nil != value.URL && nil != other.URL {
if "" == value.URL.Content {
if "" == other.URL.Content {
return 0
}
return 1
} else if "" == other.URL.Content {
return -1
}
return strings.Compare(value.URL.Content, other.URL.Content)
}
case KeyTypeEmail:
if nil != value.Email && nil != other.Email {
if "" == value.Email.Content {
if "" == other.Email.Content {
return 0
}
return 1
} else if "" == other.Email.Content {
return -1
}
return strings.Compare(value.Email.Content, other.Email.Content)
}
case KeyTypePhone:
if nil != value.Phone && nil != other.Phone {
if "" == value.Phone.Content {
if "" == other.Phone.Content {
return 0
}
return 1
} else if "" == other.Phone.Content {
return -1
}
return strings.Compare(value.Phone.Content, other.Phone.Content)
}
case KeyTypeMAsset:
Expand Down
16 changes: 8 additions & 8 deletions kernel/av/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,25 +173,25 @@ func (table *Table) SortRows(attrView *AttributeView) {
}
}

includeUneditedRows := map[string]bool{}
editedValRows := map[string]bool{}
for i, row := range table.Rows {
for _, colIndexSort := range colIndexSorts {
val := table.Rows[i].Cells[colIndexSort.Index].Value
if !val.IsEdited() {
// 如果该行的某个列的值是未编辑的,则该行不参与排序
includeUneditedRows[row.ID] = true
if val.IsEdited() {
// 如果该行某列的值已经编辑过,则该行可参与排序
editedValRows[row.ID] = true
break
}
}
}

// 将包含未编辑的行和全部已编辑的行分开排序
// 将未编辑的行和已编辑的行分开排序
var uneditedRows, editedRows []*TableRow
for _, row := range table.Rows {
if _, ok := includeUneditedRows[row.ID]; ok {
uneditedRows = append(uneditedRows, row)
} else {
if _, ok := editedValRows[row.ID]; ok {
editedRows = append(editedRows, row)
} else {
uneditedRows = append(uneditedRows, row)
}
}

Expand Down
115 changes: 115 additions & 0 deletions kernel/model/outline.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,121 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)

func (tx *Transaction) doMoveOutlineHeading(operation *Operation) (ret *TxErr) {
headingID := operation.ID
previousID := operation.PreviousID
parentID := operation.ParentID

if headingID == parentID || headingID == previousID {
return
}

tree, err := tx.loadTree(headingID)
if nil != err {
return &TxErr{code: TxErrCodeBlockNotFound, id: headingID}
}

heading := treenode.GetNodeInTree(tree, headingID)
if nil == heading {
return &TxErr{code: TxErrCodeBlockNotFound, id: headingID}
}

headings := []*ast.Node{}
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if entering && ast.NodeHeading == n.Type && !n.ParentIs(ast.NodeBlockquote) {
headings = append(headings, n)
}
return ast.WalkContinue
})

headingChildren := treenode.HeadingChildren(heading)
if "" != previousID {
previousHeading := treenode.GetNodeInTree(tree, previousID)
if nil == previousHeading {
return &TxErr{code: TxErrCodeBlockNotFound, id: previousID}
}

targetNode := previousHeading
previousHeadingChildren := treenode.HeadingChildren(previousHeading)
if 0 < len(previousHeadingChildren) {
for _, child := range previousHeadingChildren {
if child.ID == headingID {
break
}
targetNode = child
}
}

if targetNode == heading.Previous {
if previousHeading.HeadingLevel >= heading.HeadingLevel {
return
}
}

diffLevel := heading.HeadingLevel - previousHeading.HeadingLevel
heading.HeadingLevel = previousHeading.HeadingLevel

for i := len(headingChildren) - 1; i >= 0; i-- {
child := headingChildren[i]
if ast.NodeHeading == child.Type {
child.HeadingLevel -= diffLevel
if 6 < child.HeadingLevel {
child.HeadingLevel = 6
}
}
targetNode.InsertAfter(child)
}
targetNode.InsertAfter(heading)
} else if "" != parentID {
parentHeading := treenode.GetNodeInTree(tree, parentID)
if nil == parentHeading {
return &TxErr{code: TxErrCodeBlockNotFound, id: parentID}
}

targetNode := parentHeading
parentHeadingChildren := treenode.HeadingChildren(parentHeading)
if 0 < len(parentHeadingChildren) {
for _, child := range parentHeadingChildren {
if child.ID == headingID {
break
}
targetNode = child
}
}

if targetNode == heading.Previous {
if parentHeading.HeadingLevel < heading.HeadingLevel {
return
}
}

diffLevel := 1
heading.HeadingLevel = parentHeading.HeadingLevel + 1
if 6 < heading.HeadingLevel {
heading.HeadingLevel = 6
}

for i := len(headingChildren) - 1; i >= 0; i-- {
child := headingChildren[i]
if ast.NodeHeading == child.Type {
child.HeadingLevel += diffLevel
if 6 < child.HeadingLevel {
child.HeadingLevel = 6
}
}
targetNode.InsertAfter(child)
}
targetNode.InsertAfter(heading)
} else {
return
}

if err = tx.writeTree(tree); nil != err {
return
}
return
}

func Outline(rootID string) (ret []*Path, err error) {
time.Sleep(util.FrontendQueueInterval)
WaitForWritingFiles()
Expand Down
2 changes: 2 additions & 0 deletions kernel/model/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ func performTx(tx *Transaction) (ret *TxErr) {
ret = tx.doDelete(op)
case "move":
ret = tx.doMove(op)
case "moveOutlineHeading":
ret = tx.doMoveOutlineHeading(op)
case "append":
ret = tx.doAppend(op)
case "appendInsert":
Expand Down

0 comments on commit 5429380

Please sign in to comment.