filetree.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "fmt"
  19. "math"
  20. "net/http"
  21. "path"
  22. "regexp"
  23. "strings"
  24. "unicode/utf8"
  25. "github.com/88250/gulu"
  26. "github.com/88250/lute/ast"
  27. "github.com/gin-gonic/gin"
  28. "github.com/siyuan-note/siyuan/kernel/filesys"
  29. "github.com/siyuan-note/siyuan/kernel/model"
  30. "github.com/siyuan-note/siyuan/kernel/util"
  31. )
  32. func refreshFiletree(c *gin.Context) {
  33. ret := gulu.Ret.NewResult()
  34. defer c.JSON(http.StatusOK, ret)
  35. model.FullReindex()
  36. }
  37. func doc2Heading(c *gin.Context) {
  38. ret := gulu.Ret.NewResult()
  39. defer c.JSON(http.StatusOK, ret)
  40. arg, ok := util.JsonArg(c, ret)
  41. if !ok {
  42. return
  43. }
  44. srcID := arg["srcID"].(string)
  45. targetID := arg["targetID"].(string)
  46. after := arg["after"].(bool)
  47. srcTreeBox, srcTreePath, err := model.Doc2Heading(srcID, targetID, after)
  48. if nil != err {
  49. ret.Code = -1
  50. ret.Msg = err.Error()
  51. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  52. return
  53. }
  54. ret.Data = map[string]interface{}{
  55. "srcTreeBox": srcTreeBox,
  56. "srcTreePath": srcTreePath,
  57. }
  58. }
  59. func heading2Doc(c *gin.Context) {
  60. ret := gulu.Ret.NewResult()
  61. defer c.JSON(http.StatusOK, ret)
  62. arg, ok := util.JsonArg(c, ret)
  63. if !ok {
  64. return
  65. }
  66. srcHeadingID := arg["srcHeadingID"].(string)
  67. targetNotebook := arg["targetNoteBook"].(string)
  68. targetPath := arg["targetPath"].(string)
  69. srcRootBlockID, targetPath, err := model.Heading2Doc(srcHeadingID, targetNotebook, targetPath)
  70. if nil != err {
  71. ret.Code = -1
  72. ret.Msg = err.Error()
  73. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  74. return
  75. }
  76. model.WaitForWritingFiles()
  77. luteEngine := util.NewLute()
  78. tree, err := filesys.LoadTree(targetNotebook, targetPath, luteEngine)
  79. if nil != err {
  80. ret.Code = -1
  81. ret.Msg = err.Error()
  82. return
  83. }
  84. name := path.Base(targetPath)
  85. box := model.Conf.Box(targetNotebook)
  86. files, _, _ := model.ListDocTree(targetNotebook, path.Dir(targetPath), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  87. evt := util.NewCmdResult("heading2doc", 0, util.PushModeBroadcast)
  88. evt.Data = map[string]interface{}{
  89. "box": box,
  90. "path": targetPath,
  91. "files": files,
  92. "name": name,
  93. "id": tree.Root.ID,
  94. "srcRootBlockID": srcRootBlockID,
  95. }
  96. evt.Callback = arg["callback"]
  97. util.PushEvent(evt)
  98. }
  99. func li2Doc(c *gin.Context) {
  100. ret := gulu.Ret.NewResult()
  101. defer c.JSON(http.StatusOK, ret)
  102. arg, ok := util.JsonArg(c, ret)
  103. if !ok {
  104. return
  105. }
  106. srcListItemID := arg["srcListItemID"].(string)
  107. targetNotebook := arg["targetNoteBook"].(string)
  108. targetPath := arg["targetPath"].(string)
  109. srcRootBlockID, targetPath, err := model.ListItem2Doc(srcListItemID, targetNotebook, targetPath)
  110. if nil != err {
  111. ret.Code = -1
  112. ret.Msg = err.Error()
  113. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  114. return
  115. }
  116. model.WaitForWritingFiles()
  117. luteEngine := util.NewLute()
  118. tree, err := filesys.LoadTree(targetNotebook, targetPath, luteEngine)
  119. if nil != err {
  120. ret.Code = -1
  121. ret.Msg = err.Error()
  122. return
  123. }
  124. name := path.Base(targetPath)
  125. box := model.Conf.Box(targetNotebook)
  126. files, _, _ := model.ListDocTree(targetNotebook, path.Dir(targetPath), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  127. evt := util.NewCmdResult("li2doc", 0, util.PushModeBroadcast)
  128. evt.Data = map[string]interface{}{
  129. "box": box,
  130. "path": targetPath,
  131. "files": files,
  132. "name": name,
  133. "id": tree.Root.ID,
  134. "srcRootBlockID": srcRootBlockID,
  135. }
  136. evt.Callback = arg["callback"]
  137. util.PushEvent(evt)
  138. }
  139. func getHPathByPath(c *gin.Context) {
  140. ret := gulu.Ret.NewResult()
  141. defer c.JSON(http.StatusOK, ret)
  142. arg, ok := util.JsonArg(c, ret)
  143. if !ok {
  144. return
  145. }
  146. notebook := arg["notebook"].(string)
  147. if util.InvalidIDPattern(notebook, ret) {
  148. return
  149. }
  150. p := arg["path"].(string)
  151. hPath, err := model.GetHPathByPath(notebook, p)
  152. if nil != err {
  153. ret.Code = -1
  154. ret.Msg = err.Error()
  155. return
  156. }
  157. ret.Data = hPath
  158. }
  159. func getHPathsByPaths(c *gin.Context) {
  160. ret := gulu.Ret.NewResult()
  161. defer c.JSON(http.StatusOK, ret)
  162. arg, ok := util.JsonArg(c, ret)
  163. if !ok {
  164. return
  165. }
  166. pathsArg := arg["paths"].([]interface{})
  167. var paths []string
  168. for _, p := range pathsArg {
  169. paths = append(paths, p.(string))
  170. }
  171. hPath, err := model.GetHPathsByPaths(paths)
  172. if nil != err {
  173. ret.Code = -1
  174. ret.Msg = err.Error()
  175. return
  176. }
  177. ret.Data = hPath
  178. }
  179. func getHPathByID(c *gin.Context) {
  180. ret := gulu.Ret.NewResult()
  181. defer c.JSON(http.StatusOK, ret)
  182. arg, ok := util.JsonArg(c, ret)
  183. if !ok {
  184. return
  185. }
  186. id := arg["id"].(string)
  187. if util.InvalidIDPattern(id, ret) {
  188. return
  189. }
  190. hPath, err := model.GetHPathByID(id)
  191. if nil != err {
  192. ret.Code = -1
  193. ret.Msg = err.Error()
  194. return
  195. }
  196. ret.Data = hPath
  197. }
  198. func getFullHPathByID(c *gin.Context) {
  199. ret := gulu.Ret.NewResult()
  200. defer c.JSON(http.StatusOK, ret)
  201. arg, ok := util.JsonArg(c, ret)
  202. if !ok {
  203. return
  204. }
  205. if nil == arg["id"] {
  206. return
  207. }
  208. id := arg["id"].(string)
  209. hPath, err := model.GetFullHPathByID(id)
  210. if nil != err {
  211. ret.Code = -1
  212. ret.Msg = err.Error()
  213. return
  214. }
  215. ret.Data = hPath
  216. }
  217. func moveDocs(c *gin.Context) {
  218. ret := gulu.Ret.NewResult()
  219. defer c.JSON(http.StatusOK, ret)
  220. arg, ok := util.JsonArg(c, ret)
  221. if !ok {
  222. return
  223. }
  224. var fromPaths []string
  225. fromPathsArg := arg["fromPaths"].([]interface{})
  226. for _, fromPath := range fromPathsArg {
  227. fromPaths = append(fromPaths, fromPath.(string))
  228. }
  229. toPath := arg["toPath"].(string)
  230. toNotebook := arg["toNotebook"].(string)
  231. if util.InvalidIDPattern(toNotebook, ret) {
  232. return
  233. }
  234. callback := arg["callback"]
  235. err := model.MoveDocs(fromPaths, toNotebook, toPath, callback)
  236. if nil != err {
  237. ret.Code = -1
  238. ret.Msg = err.Error()
  239. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  240. return
  241. }
  242. }
  243. func removeDoc(c *gin.Context) {
  244. ret := gulu.Ret.NewResult()
  245. defer c.JSON(http.StatusOK, ret)
  246. arg, ok := util.JsonArg(c, ret)
  247. if !ok {
  248. return
  249. }
  250. notebook := arg["notebook"].(string)
  251. if util.InvalidIDPattern(notebook, ret) {
  252. return
  253. }
  254. p := arg["path"].(string)
  255. model.RemoveDoc(notebook, p)
  256. }
  257. func removeDocs(c *gin.Context) {
  258. ret := gulu.Ret.NewResult()
  259. defer c.JSON(http.StatusOK, ret)
  260. arg, ok := util.JsonArg(c, ret)
  261. if !ok {
  262. return
  263. }
  264. pathsArg := arg["paths"].([]interface{})
  265. var paths []string
  266. for _, path := range pathsArg {
  267. paths = append(paths, path.(string))
  268. }
  269. model.RemoveDocs(paths)
  270. }
  271. func renameDoc(c *gin.Context) {
  272. ret := gulu.Ret.NewResult()
  273. defer c.JSON(http.StatusOK, ret)
  274. arg, ok := util.JsonArg(c, ret)
  275. if !ok {
  276. return
  277. }
  278. notebook := arg["notebook"].(string)
  279. if util.InvalidIDPattern(notebook, ret) {
  280. return
  281. }
  282. p := arg["path"].(string)
  283. title := arg["title"].(string)
  284. err := model.RenameDoc(notebook, p, title)
  285. if nil != err {
  286. ret.Code = -1
  287. ret.Msg = err.Error()
  288. return
  289. }
  290. return
  291. }
  292. func duplicateDoc(c *gin.Context) {
  293. ret := gulu.Ret.NewResult()
  294. defer c.JSON(http.StatusOK, ret)
  295. arg, ok := util.JsonArg(c, ret)
  296. if !ok {
  297. return
  298. }
  299. id := arg["id"].(string)
  300. tree, err := model.LoadTreeByID(id)
  301. if nil != err {
  302. ret.Code = -1
  303. ret.Msg = err.Error()
  304. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  305. return
  306. }
  307. p := tree.Path
  308. notebook := tree.Box
  309. box := model.Conf.Box(notebook)
  310. model.DuplicateDoc(tree)
  311. pushCreate(box, p, tree.Root.ID, arg)
  312. ret.Data = map[string]interface{}{
  313. "id": tree.Root.ID,
  314. "notebook": notebook,
  315. "path": tree.Path,
  316. "hPath": tree.HPath,
  317. }
  318. }
  319. func createDoc(c *gin.Context) {
  320. ret := gulu.Ret.NewResult()
  321. defer c.JSON(http.StatusOK, ret)
  322. arg, ok := util.JsonArg(c, ret)
  323. if !ok {
  324. return
  325. }
  326. notebook := arg["notebook"].(string)
  327. p := arg["path"].(string)
  328. title := arg["title"].(string)
  329. md := arg["md"].(string)
  330. sortsArg := arg["sorts"]
  331. var sorts []string
  332. if nil != sortsArg {
  333. for _, sort := range sortsArg.([]interface{}) {
  334. sorts = append(sorts, sort.(string))
  335. }
  336. }
  337. tree, err := model.CreateDocByMd(notebook, p, title, md, sorts)
  338. if nil != err {
  339. ret.Code = -1
  340. ret.Msg = err.Error()
  341. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  342. return
  343. }
  344. box := model.Conf.Box(notebook)
  345. pushCreate(box, p, tree.Root.ID, arg)
  346. }
  347. func createDailyNote(c *gin.Context) {
  348. ret := gulu.Ret.NewResult()
  349. defer c.JSON(http.StatusOK, ret)
  350. arg, ok := util.JsonArg(c, ret)
  351. if !ok {
  352. return
  353. }
  354. notebook := arg["notebook"].(string)
  355. p, existed, err := model.CreateDailyNote(notebook)
  356. if nil != err {
  357. if model.ErrBoxNotFound == err {
  358. ret.Code = 1
  359. } else {
  360. ret.Code = -1
  361. }
  362. ret.Msg = err.Error()
  363. return
  364. }
  365. box := model.Conf.Box(notebook)
  366. model.WaitForWritingFiles()
  367. luteEngine := util.NewLute()
  368. tree, err := filesys.LoadTree(box.ID, p, luteEngine)
  369. if nil != err {
  370. ret.Code = -1
  371. ret.Msg = err.Error()
  372. return
  373. }
  374. appArg := arg["app"]
  375. app := ""
  376. if nil != appArg {
  377. app = appArg.(string)
  378. }
  379. pushMode := util.PushModeBroadcast
  380. if existed && "" != app {
  381. pushMode = util.PushModeBroadcastApp
  382. }
  383. evt := util.NewCmdResult("createdailynote", 0, pushMode)
  384. evt.AppId = app
  385. name := path.Base(p)
  386. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  387. evt.Data = map[string]interface{}{
  388. "box": box,
  389. "path": p,
  390. "files": files,
  391. "name": name,
  392. "id": tree.Root.ID,
  393. }
  394. evt.Callback = arg["callback"]
  395. util.PushEvent(evt)
  396. }
  397. func createDocWithMd(c *gin.Context) {
  398. ret := gulu.Ret.NewResult()
  399. defer c.JSON(http.StatusOK, ret)
  400. arg, ok := util.JsonArg(c, ret)
  401. if !ok {
  402. return
  403. }
  404. notebook := arg["notebook"].(string)
  405. if util.InvalidIDPattern(notebook, ret) {
  406. return
  407. }
  408. var parentID string
  409. parentIDArg := arg["parentID"]
  410. if nil != parentIDArg {
  411. parentID = parentIDArg.(string)
  412. }
  413. id := ast.NewNodeID()
  414. idArg := arg["id"]
  415. if nil != idArg {
  416. id = idArg.(string)
  417. }
  418. hPath := arg["path"].(string)
  419. markdown := arg["markdown"].(string)
  420. baseName := path.Base(hPath)
  421. dir := path.Dir(hPath)
  422. r, _ := regexp.Compile("\r\n|\r|\n|\u2028|\u2029|\t|/")
  423. baseName = r.ReplaceAllString(baseName, "")
  424. if 512 < utf8.RuneCountInString(baseName) {
  425. baseName = gulu.Str.SubStr(baseName, 512)
  426. }
  427. hPath = path.Join(dir, baseName)
  428. if !strings.HasPrefix(hPath, "/") {
  429. hPath = "/" + hPath
  430. }
  431. err := model.CreateWithMarkdown(notebook, hPath, markdown, parentID, id)
  432. if nil != err {
  433. ret.Code = -1
  434. ret.Msg = err.Error()
  435. return
  436. }
  437. ret.Data = id
  438. box := model.Conf.Box(notebook)
  439. b, _ := model.GetBlock(id, nil)
  440. p := b.Path
  441. pushCreate(box, p, id, arg)
  442. }
  443. func getDocCreateSavePath(c *gin.Context) {
  444. ret := gulu.Ret.NewResult()
  445. defer c.JSON(http.StatusOK, ret)
  446. arg, ok := util.JsonArg(c, ret)
  447. if !ok {
  448. return
  449. }
  450. notebook := arg["notebook"].(string)
  451. box := model.Conf.Box(notebook)
  452. docCreateSavePathTpl := model.Conf.FileTree.DocCreateSavePath
  453. if nil != box {
  454. docCreateSavePathTpl = box.GetConf().DocCreateSavePath
  455. }
  456. if "" == docCreateSavePathTpl {
  457. docCreateSavePathTpl = model.Conf.FileTree.DocCreateSavePath
  458. }
  459. docCreateSavePathTpl = strings.TrimSpace(docCreateSavePathTpl)
  460. if "../" == docCreateSavePathTpl {
  461. docCreateSavePathTpl = "../Untitled"
  462. }
  463. for strings.HasSuffix(docCreateSavePathTpl, "/") {
  464. docCreateSavePathTpl = strings.TrimSuffix(docCreateSavePathTpl, "/")
  465. docCreateSavePathTpl = strings.TrimSpace(docCreateSavePathTpl)
  466. }
  467. p, err := model.RenderGoTemplate(docCreateSavePathTpl)
  468. if nil != err {
  469. ret.Code = -1
  470. ret.Msg = err.Error()
  471. return
  472. }
  473. ret.Data = map[string]interface{}{
  474. "path": p,
  475. }
  476. }
  477. func getRefCreateSavePath(c *gin.Context) {
  478. ret := gulu.Ret.NewResult()
  479. defer c.JSON(http.StatusOK, ret)
  480. arg, ok := util.JsonArg(c, ret)
  481. if !ok {
  482. return
  483. }
  484. notebook := arg["notebook"].(string)
  485. box := model.Conf.Box(notebook)
  486. refCreateSavePath := model.Conf.FileTree.RefCreateSavePath
  487. if nil != box {
  488. refCreateSavePath = box.GetConf().RefCreateSavePath
  489. }
  490. if "" == refCreateSavePath {
  491. refCreateSavePath = model.Conf.FileTree.RefCreateSavePath
  492. }
  493. p, err := model.RenderGoTemplate(refCreateSavePath)
  494. if nil != err {
  495. ret.Code = -1
  496. ret.Msg = err.Error()
  497. return
  498. }
  499. ret.Data = map[string]interface{}{
  500. "path": p,
  501. }
  502. }
  503. func changeSort(c *gin.Context) {
  504. ret := gulu.Ret.NewResult()
  505. defer c.JSON(http.StatusOK, ret)
  506. arg, ok := util.JsonArg(c, ret)
  507. if !ok {
  508. return
  509. }
  510. notebook := arg["notebook"].(string)
  511. pathsArg := arg["paths"].([]interface{})
  512. var paths []string
  513. for _, p := range pathsArg {
  514. paths = append(paths, p.(string))
  515. }
  516. model.ChangeFileTreeSort(notebook, paths)
  517. }
  518. func searchDocs(c *gin.Context) {
  519. ret := gulu.Ret.NewResult()
  520. defer c.JSON(http.StatusOK, ret)
  521. arg, ok := util.JsonArg(c, ret)
  522. if !ok {
  523. return
  524. }
  525. flashcard := false
  526. if arg["flashcard"] != nil {
  527. flashcard = arg["flashcard"].(bool)
  528. }
  529. k := arg["k"].(string)
  530. ret.Data = model.SearchDocsByKeyword(k, flashcard)
  531. }
  532. func listDocsByPath(c *gin.Context) {
  533. ret := gulu.Ret.NewResult()
  534. defer c.JSON(http.StatusOK, ret)
  535. arg, ok := util.JsonArg(c, ret)
  536. if !ok {
  537. return
  538. }
  539. notebook := arg["notebook"].(string)
  540. p := arg["path"].(string)
  541. sortParam := arg["sort"]
  542. sortMode := util.SortModeUnassigned
  543. if nil != sortParam {
  544. sortMode = int(sortParam.(float64))
  545. }
  546. flashcard := false
  547. if arg["flashcard"] != nil {
  548. flashcard = arg["flashcard"].(bool)
  549. }
  550. maxListCount := model.Conf.FileTree.MaxListCount
  551. if arg["maxListCount"] != nil {
  552. // API `listDocsByPath` add an optional parameter `maxListCount` https://github.com/siyuan-note/siyuan/issues/7993
  553. maxListCount = int(arg["maxListCount"].(float64))
  554. if 0 >= maxListCount {
  555. maxListCount = math.MaxInt
  556. }
  557. }
  558. showHidden := false
  559. if arg["showHidden"] != nil {
  560. showHidden = arg["showHidden"].(bool)
  561. }
  562. files, totals, err := model.ListDocTree(notebook, p, sortMode, flashcard, showHidden, maxListCount)
  563. if nil != err {
  564. ret.Code = -1
  565. ret.Msg = err.Error()
  566. return
  567. }
  568. if maxListCount < totals {
  569. util.PushMsg(fmt.Sprintf(model.Conf.Language(48), len(files)), 7000)
  570. }
  571. ret.Data = map[string]interface{}{
  572. "box": notebook,
  573. "path": p,
  574. "files": files,
  575. }
  576. }
  577. func getDoc(c *gin.Context) {
  578. ret := gulu.Ret.NewResult()
  579. defer c.JSON(http.StatusOK, ret)
  580. arg, ok := util.JsonArg(c, ret)
  581. if !ok {
  582. return
  583. }
  584. id := arg["id"].(string)
  585. idx := arg["index"]
  586. index := 0
  587. if nil != idx {
  588. index = int(idx.(float64))
  589. }
  590. var query string
  591. if queryArg := arg["query"]; nil != queryArg {
  592. query = queryArg.(string)
  593. }
  594. var queryMethod int
  595. if queryMethodArg := arg["queryMethod"]; nil != queryMethodArg {
  596. queryMethod = int(queryMethodArg.(float64))
  597. }
  598. var queryTypes map[string]bool
  599. if queryTypesArg := arg["queryTypes"]; nil != queryTypesArg {
  600. typesArg := queryTypesArg.(map[string]interface{})
  601. queryTypes = map[string]bool{}
  602. for t, b := range typesArg {
  603. queryTypes[t] = b.(bool)
  604. }
  605. }
  606. m := arg["mode"] // 0: 仅当前 ID,1:向上 2:向下,3:上下都加载,4:加载末尾
  607. mode := 0
  608. if nil != m {
  609. mode = int(m.(float64))
  610. }
  611. s := arg["size"]
  612. size := 102400 // 默认最大加载块数
  613. if nil != s {
  614. size = int(s.(float64))
  615. }
  616. startID := ""
  617. endID := ""
  618. startIDArg := arg["startID"]
  619. endIDArg := arg["endID"]
  620. if nil != startIDArg && nil != endIDArg {
  621. startID = startIDArg.(string)
  622. endID = endIDArg.(string)
  623. size = model.Conf.Editor.DynamicLoadBlocks
  624. }
  625. isBacklinkArg := arg["isBacklink"]
  626. isBacklink := false
  627. if nil != isBacklinkArg {
  628. isBacklink = isBacklinkArg.(bool)
  629. }
  630. blockCount, content, parentID, parent2ID, rootID, typ, eof, scroll, boxID, docPath, isBacklinkExpand, err := model.GetDoc(startID, endID, id, index, query, queryTypes, queryMethod, mode, size, isBacklink)
  631. if model.ErrBlockNotFound == err {
  632. ret.Code = 3
  633. return
  634. }
  635. if nil != err {
  636. ret.Code = 1
  637. ret.Msg = err.Error()
  638. return
  639. }
  640. // 判断是否正在同步中 https://github.com/siyuan-note/siyuan/issues/6290
  641. isSyncing := model.IsSyncingFile(rootID)
  642. ret.Data = map[string]interface{}{
  643. "id": id,
  644. "mode": mode,
  645. "parentID": parentID,
  646. "parent2ID": parent2ID,
  647. "rootID": rootID,
  648. "type": typ,
  649. "content": content,
  650. "blockCount": blockCount,
  651. "eof": eof,
  652. "scroll": scroll,
  653. "box": boxID,
  654. "path": docPath,
  655. "isSyncing": isSyncing,
  656. "isBacklinkExpand": isBacklinkExpand,
  657. }
  658. }
  659. func pushCreate(box *model.Box, p, treeID string, arg map[string]interface{}) {
  660. evt := util.NewCmdResult("create", 0, util.PushModeBroadcast)
  661. name := path.Base(p)
  662. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  663. evt.Data = map[string]interface{}{
  664. "box": box,
  665. "path": p,
  666. "files": files,
  667. "name": name,
  668. "id": treeID,
  669. }
  670. evt.Callback = arg["callback"]
  671. util.PushEvent(evt)
  672. }