diff --git a/API.md b/API.md index 741f9ef11..d812967e6 100644 --- a/API.md +++ b/API.md @@ -27,6 +27,7 @@ * [Append blocks](#Append-blocks) * [Update a block](#Update-a-block) * [Delete a block](#Delete-a-block) + * [Move a block](#Move-a-block) * [Get a block kramdown](#Get-a-block-kramdown) * [Attributes](#Attributes) * [Set block attributes](#Set-block-attributes) @@ -696,6 +697,50 @@ View API token in Settings - About, request header: `Authorization: T } ``` +### Move a block + +* `/api/block/moveBlock` +* Parameters + + ```json + { + "id": "20230406180530-3o1rqkc", + "previousID": "20230406152734-if5kyx6", + "parentID": "20230404183855-woe52ko" + } + ``` + + * `id`: Block ID to move + * `previousID`: The ID of the previous block, used to anchor the insertion position + * `parentID`: The ID of the parent block, used to anchor the insertion position, `previousID` and `parentID` cannot be empty at the same time, if they exist at the same time, `previousID` will be used first +* Return value + + ```json + { + "code": 0, + "msg": "", + "data": [ + { + "doOperations": [ + { + "action": "move", + "data": null, + "id": "20230406180530-3o1rqkc", + "parentID": "20230404183855-woe52ko", + "previousID": "20230406152734-if5kyx6", + "nextID": "", + "retData": null, + "srcIDs": null, + "name": "", + "type": "" + } + ], + "undoOperations": null + } + ] + } + ``` + ### Get a block kramdown * `/api/block/getBlockKramdown` diff --git a/API_zh_CN.md b/API_zh_CN.md index d159064be..340f155ad 100644 --- a/API_zh_CN.md +++ b/API_zh_CN.md @@ -27,6 +27,7 @@ * [插入后置子块](#插入后置子块) * [更新块](#更新块) * [删除块](#删除块) + * [移动块](#移动块) * [获取块 kramdown 源码](#获取块-kramdown-源码) * [属性](#属性) * [设置块属性](#设置块属性) @@ -690,6 +691,50 @@ } ``` +### 移动块 + +* `/api/block/moveBlock` +* 参数 + + ```json + { + "id": "20230406180530-3o1rqkc", + "previousID": "20230406152734-if5kyx6", + "parentID": "20230404183855-woe52ko" + } + ``` + + * `id`:待移动块 ID + * `previousID`:前一个块的 ID,用于锚定插入位置 + * `parentID`:父块的 ID,用于锚定插入位置,`previousID` 和 `parentID` 不能同时为空,同时存在的话优先使用 `previousID` +* 返回值 + + ```json + { + "code": 0, + "msg": "", + "data": [ + { + "doOperations": [ + { + "action": "move", + "data": null, + "id": "20230406180530-3o1rqkc", + "parentID": "20230404183855-woe52ko", + "previousID": "20230406152734-if5kyx6", + "nextID": "", + "retData": null, + "srcIDs": null, + "name": "", + "type": "" + } + ], + "undoOperations": null + } + ] + } + ``` + ### 获取块 kramdown 源码 * `/api/block/getBlockKramdown` diff --git a/CHANGELOG.md b/changelogs/v0.1.0-v2.8.3.md similarity index 100% rename from CHANGELOG.md rename to changelogs/v0.1.0-v2.8.3.md diff --git a/changelogs/v2.8.4.md b/changelogs/v2.8.4.md new file mode 100644 index 000000000..e69de29bb diff --git a/changelogs/v2.8.4_zh_CN.md b/changelogs/v2.8.4_zh_CN.md new file mode 100644 index 000000000..e69de29bb diff --git a/kernel/api/block_op.go b/kernel/api/block_op.go index 364ccd295..f5df3a91d 100644 --- a/kernel/api/block_op.go +++ b/kernel/api/block_op.go @@ -29,6 +29,54 @@ import ( "github.com/siyuan-note/siyuan/kernel/util" ) +func moveBlock(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 util.InvalidIDPattern(parentID, ret) { + return + } + } + if nil != arg["previousID"] { + previousID = arg["previousID"].(string) + if util.InvalidIDPattern(previousID, ret) { + return + } + } + + transactions := []*model.Transaction{ + { + DoOperations: []*model.Operation{ + { + Action: "move", + ID: id, + PreviousID: previousID, + ParentID: parentID, + }, + }, + }, + } + + model.PerformTransactions(&transactions) + model.WaitForWritingFiles() + + ret.Data = transactions + broadcastTransactions(transactions) +} + func appendBlock(c *gin.Context) { ret := gulu.Ret.NewResult() defer c.JSON(http.StatusOK, ret) diff --git a/kernel/api/router.go b/kernel/api/router.go index e8e14c4a1..089efaf4a 100644 --- a/kernel/api/router.go +++ b/kernel/api/router.go @@ -164,6 +164,7 @@ func ServeAPI(ginServer *gin.Engine) { ginServer.Handle("POST", "/api/block/appendBlock", model.CheckAuth, model.CheckReadonly, appendBlock) 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/setBlockReminder", model.CheckAuth, model.CheckReadonly, setBlockReminder) ginServer.Handle("POST", "/api/block/getHeadingLevelTransaction", model.CheckAuth, getHeadingLevelTransaction) ginServer.Handle("POST", "/api/block/getHeadingDeleteTransaction", model.CheckAuth, getHeadingDeleteTransaction)