filetree.go 17 KB

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