This commit is contained in:
Liang Ding 2023-01-27 18:07:06 +08:00
parent 5da5ae8818
commit 2b09ea75f0
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
7 changed files with 56 additions and 63 deletions

View file

@ -27,7 +27,6 @@ import (
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/model"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/util"
)
@ -155,7 +154,19 @@ func checkBlockFold(c *gin.Context) {
}
id := arg["id"].(string)
ret.Data = sql.IsBlockFolded(id)
b, err := model.GetBlock(id, nil)
if errors.Is(err, filelock.ErrUnableAccessFile) {
ret.Code = 2
ret.Data = id
return
}
if errors.Is(err, model.ErrIndexing) {
ret.Code = 0
ret.Data = false
return
}
ret.Data = nil != b && "1" == b.IAL["fold"]
}
func checkBlockExist(c *gin.Context) {
@ -168,7 +179,7 @@ func checkBlockExist(c *gin.Context) {
}
id := arg["id"].(string)
b, err := model.GetBlock(id)
b, err := model.GetBlock(id, nil)
if errors.Is(err, filelock.ErrUnableAccessFile) {
ret.Code = 2
ret.Data = id
@ -376,7 +387,8 @@ func getBlockInfo(c *gin.Context) {
}
id := arg["id"].(string)
block, err := model.GetBlock(id)
tree, err := model.LoadTreeByID(id)
if errors.Is(err, filelock.ErrUnableAccessFile) {
ret.Code = 2
ret.Data = id
@ -387,6 +399,8 @@ func getBlockInfo(c *gin.Context) {
ret.Msg = model.Conf.Language(56)
return
}
block, _ := model.GetBlock(id, tree)
if nil == block {
ret.Code = -1
ret.Msg = fmt.Sprintf(model.Conf.Language(15), id)
@ -401,13 +415,13 @@ func getBlockInfo(c *gin.Context) {
rootChildID = b.ID
break
}
if b, _ = model.GetBlock(parentID); nil == b {
if b, _ = model.GetBlock(parentID, tree); nil == b {
logging.LogErrorf("not found parent")
break
}
}
root, err := model.GetBlock(block.RootID)
root, err := model.GetBlock(block.RootID, tree)
if errors.Is(err, filelock.ErrUnableAccessFile) {
ret.Code = 2
ret.Data = id

View file

@ -190,7 +190,7 @@ func updateBlock(c *gin.Context) {
return
}
block, err := model.GetBlock(id)
block, err := model.GetBlock(id, nil)
if nil != err {
ret.Code = -1
ret.Msg = "get block failed: " + err.Error()

View file

@ -327,7 +327,7 @@ func duplicateDoc(c *gin.Context) {
}
id := arg["id"].(string)
newTree, err := model.DuplicateDoc(id)
tree, err := model.LoadTreeByID(id)
if nil != err {
ret.Code = -1
ret.Msg = err.Error()
@ -335,24 +335,17 @@ func duplicateDoc(c *gin.Context) {
return
}
block, _ := model.GetBlock(id)
p := block.Path
notebook := block.Box
p := tree.Path
notebook := tree.Box
box := model.Conf.Box(notebook)
tree, err := model.LoadTree(box.ID, p)
if nil != err {
ret.Code = -1
ret.Msg = err.Error()
return
}
model.DuplicateDoc(tree)
pushCreate(box, p, tree.Root.ID, arg)
ret.Data = map[string]interface{}{
"id": newTree.Root.ID,
"id": tree.Root.ID,
"notebook": notebook,
"path": newTree.Path,
"hPath": newTree.HPath,
"path": tree.Path,
"hPath": tree.HPath,
}
}
@ -478,7 +471,7 @@ func createDocWithMd(c *gin.Context) {
ret.Data = id
box := model.Conf.Box(notebook)
b, _ := model.GetBlock(id)
b, _ := model.GetBlock(id, nil)
p := b.Path
pushCreate(box, p, id, arg)
}

View file

@ -25,7 +25,6 @@ import (
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/task"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
)
@ -383,31 +382,33 @@ func GetBlockKramdown(id string) (ret string) {
return
}
func GetBlock(id string) (ret *Block, err error) {
ret, err = getBlock(id)
func GetBlock(id string, tree *parse.Tree) (ret *Block, err error) {
ret, err = getBlock(id, tree)
return
}
func getBlock(id string) (ret *Block, err error) {
func getBlock(id string, tree *parse.Tree) (ret *Block, err error) {
if "" == id {
return
}
tree, err := loadTreeByBlockID(id)
if nil != err {
if task.ContainIndexTask() {
err = ErrIndexing
return
}
time.Sleep(1 * time.Second)
if nil == tree {
tree, err = loadTreeByBlockID(id)
if nil != err {
return
time.Sleep(1 * time.Second)
tree, err = loadTreeByBlockID(id)
if nil != err {
return
}
}
}
node := treenode.GetNodeInTree(tree, id)
if nil == node {
err = ErrBlockNotFound
return
}
sqlBlock := sql.BuildBlockFromNode(node, tree)
if nil == sqlBlock {
return

View file

@ -892,18 +892,12 @@ func renameWriteJSONQueue(tree *parse.Tree, oldHPath string) (err error) {
return
}
func DuplicateDoc(rootID string) (ret *parse.Tree, err error) {
func DuplicateDoc(tree *parse.Tree) {
msgId := util.PushMsg(Conf.Language(116), 30000)
defer util.PushClearMsg(msgId)
WaitForWritingFiles()
ret, err = loadTreeByBlockID(rootID)
if nil != err {
return
}
resetTree(ret, "Duplicated")
createTreeTx(ret)
resetTree(tree, "Duplicated")
createTreeTx(tree)
WaitForWritingFiles()
return
}

View file

@ -30,6 +30,7 @@ import (
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/task"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
)
@ -145,6 +146,10 @@ var (
ErrIndexing = errors.New("indexing")
)
func LoadTreeByID(id string) (ret *parse.Tree, err error) {
return loadTreeByBlockID(id)
}
func loadTreeByBlockID(id string) (ret *parse.Tree, err error) {
if "" == id {
return nil, ErrTreeNotFound
@ -152,6 +157,11 @@ func loadTreeByBlockID(id string) (ret *parse.Tree, err error) {
bt := treenode.GetBlockTree(id)
if nil == bt {
if task.ContainIndexTask() {
err = ErrIndexing
return
}
return nil, ErrBlockNotFound
}
ret, err = LoadTree(bt.BoxID, bt.Path)

View file

@ -95,25 +95,6 @@ func (block *Block) IsContainerBlock() bool {
return false
}
func IsBlockFolded(id string) (ret bool) {
sqlStmt := "SELECT parent_id, ial FROM blocks WHERE id = ? AND type != 'd'"
for i := 0; i < 64; i++ {
row := queryRow(sqlStmt, id)
var pid, ial string
if err := row.Scan(&pid, &ial); nil != err {
if sql.ErrNoRows != err {
logging.LogErrorf("query scan field failed: %s", err)
}
return
}
id = pid
if strings.Contains(ial, "fold=\"1\"") {
return true
}
}
return
}
func queryBlockChildrenIDs(id string) (ret []string) {
ret = append(ret, id)
childIDs := queryBlockIDByParentID(id)