filetree.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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 getIDsByHPath(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. if nil == arg["path"] {
  225. return
  226. }
  227. if nil == arg["notebook"] {
  228. return
  229. }
  230. notebook := arg["notebook"].(string)
  231. if util.InvalidIDPattern(notebook, ret) {
  232. return
  233. }
  234. p := arg["path"].(string)
  235. ids, err := model.GetIDsByHPath(p, notebook)
  236. if nil != err {
  237. ret.Code = -1
  238. ret.Msg = err.Error()
  239. return
  240. }
  241. ret.Data = ids
  242. }
  243. func moveDocs(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. var fromPaths []string
  251. fromPathsArg := arg["fromPaths"].([]interface{})
  252. for _, fromPath := range fromPathsArg {
  253. fromPaths = append(fromPaths, fromPath.(string))
  254. }
  255. toPath := arg["toPath"].(string)
  256. toNotebook := arg["toNotebook"].(string)
  257. if util.InvalidIDPattern(toNotebook, ret) {
  258. return
  259. }
  260. callback := arg["callback"]
  261. err := model.MoveDocs(fromPaths, toNotebook, toPath, callback)
  262. if nil != err {
  263. ret.Code = -1
  264. ret.Msg = err.Error()
  265. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  266. return
  267. }
  268. }
  269. func removeDoc(c *gin.Context) {
  270. ret := gulu.Ret.NewResult()
  271. defer c.JSON(http.StatusOK, ret)
  272. arg, ok := util.JsonArg(c, ret)
  273. if !ok {
  274. return
  275. }
  276. notebook := arg["notebook"].(string)
  277. if util.InvalidIDPattern(notebook, ret) {
  278. return
  279. }
  280. p := arg["path"].(string)
  281. model.RemoveDoc(notebook, p)
  282. }
  283. func removeDocs(c *gin.Context) {
  284. ret := gulu.Ret.NewResult()
  285. defer c.JSON(http.StatusOK, ret)
  286. arg, ok := util.JsonArg(c, ret)
  287. if !ok {
  288. return
  289. }
  290. pathsArg := arg["paths"].([]interface{})
  291. var paths []string
  292. for _, path := range pathsArg {
  293. paths = append(paths, path.(string))
  294. }
  295. model.RemoveDocs(paths)
  296. }
  297. func renameDoc(c *gin.Context) {
  298. ret := gulu.Ret.NewResult()
  299. defer c.JSON(http.StatusOK, ret)
  300. arg, ok := util.JsonArg(c, ret)
  301. if !ok {
  302. return
  303. }
  304. notebook := arg["notebook"].(string)
  305. if util.InvalidIDPattern(notebook, ret) {
  306. return
  307. }
  308. p := arg["path"].(string)
  309. title := arg["title"].(string)
  310. err := model.RenameDoc(notebook, p, title)
  311. if nil != err {
  312. ret.Code = -1
  313. ret.Msg = err.Error()
  314. return
  315. }
  316. return
  317. }
  318. func duplicateDoc(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. id := arg["id"].(string)
  326. tree, err := model.LoadTreeByID(id)
  327. if nil != err {
  328. ret.Code = -1
  329. ret.Msg = err.Error()
  330. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  331. return
  332. }
  333. p := tree.Path
  334. notebook := tree.Box
  335. box := model.Conf.Box(notebook)
  336. model.DuplicateDoc(tree)
  337. pushCreate(box, p, tree.Root.ID, arg)
  338. ret.Data = map[string]interface{}{
  339. "id": tree.Root.ID,
  340. "notebook": notebook,
  341. "path": tree.Path,
  342. "hPath": tree.HPath,
  343. }
  344. }
  345. func createDoc(c *gin.Context) {
  346. ret := gulu.Ret.NewResult()
  347. defer c.JSON(http.StatusOK, ret)
  348. arg, ok := util.JsonArg(c, ret)
  349. if !ok {
  350. return
  351. }
  352. notebook := arg["notebook"].(string)
  353. p := arg["path"].(string)
  354. title := arg["title"].(string)
  355. md := arg["md"].(string)
  356. sortsArg := arg["sorts"]
  357. var sorts []string
  358. if nil != sortsArg {
  359. for _, sort := range sortsArg.([]interface{}) {
  360. sorts = append(sorts, sort.(string))
  361. }
  362. }
  363. tree, err := model.CreateDocByMd(notebook, p, title, md, sorts)
  364. if nil != err {
  365. ret.Code = -1
  366. ret.Msg = err.Error()
  367. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  368. return
  369. }
  370. model.WaitForWritingFiles()
  371. box := model.Conf.Box(notebook)
  372. pushCreate(box, p, tree.Root.ID, arg)
  373. ret.Data = map[string]interface{}{
  374. "id": tree.Root.ID,
  375. }
  376. }
  377. func createDailyNote(c *gin.Context) {
  378. ret := gulu.Ret.NewResult()
  379. defer c.JSON(http.StatusOK, ret)
  380. arg, ok := util.JsonArg(c, ret)
  381. if !ok {
  382. return
  383. }
  384. notebook := arg["notebook"].(string)
  385. p, existed, err := model.CreateDailyNote(notebook)
  386. if nil != err {
  387. if model.ErrBoxNotFound == err {
  388. ret.Code = 1
  389. } else {
  390. ret.Code = -1
  391. }
  392. ret.Msg = err.Error()
  393. return
  394. }
  395. model.WaitForWritingFiles()
  396. box := model.Conf.Box(notebook)
  397. luteEngine := util.NewLute()
  398. tree, err := filesys.LoadTree(box.ID, p, luteEngine)
  399. if nil != err {
  400. ret.Code = -1
  401. ret.Msg = err.Error()
  402. return
  403. }
  404. if !existed {
  405. // 只有创建的情况才推送,已经存在的情况不推送
  406. // Creating a dailynote existed no longer expands the doc tree https://github.com/siyuan-note/siyuan/issues/9959
  407. appArg := arg["app"]
  408. app := ""
  409. if nil != appArg {
  410. app = appArg.(string)
  411. }
  412. evt := util.NewCmdResult("createdailynote", 0, util.PushModeBroadcast)
  413. evt.AppId = app
  414. name := path.Base(p)
  415. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  416. evt.Data = map[string]interface{}{
  417. "box": box,
  418. "path": p,
  419. "files": files,
  420. "name": name,
  421. "id": tree.Root.ID,
  422. }
  423. evt.Callback = arg["callback"]
  424. util.PushEvent(evt)
  425. }
  426. ret.Data = map[string]interface{}{
  427. "id": tree.Root.ID,
  428. }
  429. }
  430. func createDocWithMd(c *gin.Context) {
  431. ret := gulu.Ret.NewResult()
  432. defer c.JSON(http.StatusOK, ret)
  433. arg, ok := util.JsonArg(c, ret)
  434. if !ok {
  435. return
  436. }
  437. notebook := arg["notebook"].(string)
  438. if util.InvalidIDPattern(notebook, ret) {
  439. return
  440. }
  441. var parentID string
  442. parentIDArg := arg["parentID"]
  443. if nil != parentIDArg {
  444. parentID = parentIDArg.(string)
  445. }
  446. id := ast.NewNodeID()
  447. idArg := arg["id"]
  448. if nil != idArg {
  449. id = idArg.(string)
  450. }
  451. hPath := arg["path"].(string)
  452. markdown := arg["markdown"].(string)
  453. baseName := path.Base(hPath)
  454. dir := path.Dir(hPath)
  455. r, _ := regexp.Compile("\r\n|\r|\n|\u2028|\u2029|\t|/")
  456. baseName = r.ReplaceAllString(baseName, "")
  457. if 512 < utf8.RuneCountInString(baseName) {
  458. baseName = gulu.Str.SubStr(baseName, 512)
  459. }
  460. hPath = path.Join(dir, baseName)
  461. if !strings.HasPrefix(hPath, "/") {
  462. hPath = "/" + hPath
  463. }
  464. id, err := model.CreateWithMarkdown(notebook, hPath, markdown, parentID, id)
  465. if nil != err {
  466. ret.Code = -1
  467. ret.Msg = err.Error()
  468. return
  469. }
  470. ret.Data = id
  471. model.WaitForWritingFiles()
  472. box := model.Conf.Box(notebook)
  473. b, _ := model.GetBlock(id, nil)
  474. p := b.Path
  475. pushCreate(box, p, id, arg)
  476. }
  477. func getDocCreateSavePath(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. docCreateSavePathTpl := model.Conf.FileTree.DocCreateSavePath
  487. if nil != box {
  488. docCreateSavePathTpl = box.GetConf().DocCreateSavePath
  489. }
  490. if "" == docCreateSavePathTpl {
  491. docCreateSavePathTpl = model.Conf.FileTree.DocCreateSavePath
  492. }
  493. docCreateSavePathTpl = strings.TrimSpace(docCreateSavePathTpl)
  494. if "../" == docCreateSavePathTpl {
  495. docCreateSavePathTpl = "../Untitled"
  496. }
  497. for strings.HasSuffix(docCreateSavePathTpl, "/") {
  498. docCreateSavePathTpl = strings.TrimSuffix(docCreateSavePathTpl, "/")
  499. docCreateSavePathTpl = strings.TrimSpace(docCreateSavePathTpl)
  500. }
  501. p, err := model.RenderGoTemplate(docCreateSavePathTpl)
  502. if nil != err {
  503. ret.Code = -1
  504. ret.Msg = err.Error()
  505. return
  506. }
  507. ret.Data = map[string]interface{}{
  508. "path": p,
  509. }
  510. }
  511. func getRefCreateSavePath(c *gin.Context) {
  512. ret := gulu.Ret.NewResult()
  513. defer c.JSON(http.StatusOK, ret)
  514. arg, ok := util.JsonArg(c, ret)
  515. if !ok {
  516. return
  517. }
  518. notebook := arg["notebook"].(string)
  519. box := model.Conf.Box(notebook)
  520. refCreateSavePath := model.Conf.FileTree.RefCreateSavePath
  521. if nil != box {
  522. refCreateSavePath = box.GetConf().RefCreateSavePath
  523. }
  524. if "" == refCreateSavePath {
  525. refCreateSavePath = model.Conf.FileTree.RefCreateSavePath
  526. }
  527. p, err := model.RenderGoTemplate(refCreateSavePath)
  528. if nil != err {
  529. ret.Code = -1
  530. ret.Msg = err.Error()
  531. return
  532. }
  533. ret.Data = map[string]interface{}{
  534. "path": p,
  535. }
  536. }
  537. func changeSort(c *gin.Context) {
  538. ret := gulu.Ret.NewResult()
  539. defer c.JSON(http.StatusOK, ret)
  540. arg, ok := util.JsonArg(c, ret)
  541. if !ok {
  542. return
  543. }
  544. notebook := arg["notebook"].(string)
  545. pathsArg := arg["paths"].([]interface{})
  546. var paths []string
  547. for _, p := range pathsArg {
  548. paths = append(paths, p.(string))
  549. }
  550. model.ChangeFileTreeSort(notebook, paths)
  551. }
  552. func searchDocs(c *gin.Context) {
  553. ret := gulu.Ret.NewResult()
  554. defer c.JSON(http.StatusOK, ret)
  555. arg, ok := util.JsonArg(c, ret)
  556. if !ok {
  557. return
  558. }
  559. flashcard := false
  560. if arg["flashcard"] != nil {
  561. flashcard = arg["flashcard"].(bool)
  562. }
  563. k := arg["k"].(string)
  564. ret.Data = model.SearchDocsByKeyword(k, flashcard)
  565. }
  566. func listDocsByPath(c *gin.Context) {
  567. ret := gulu.Ret.NewResult()
  568. defer c.JSON(http.StatusOK, ret)
  569. arg, ok := util.JsonArg(c, ret)
  570. if !ok {
  571. return
  572. }
  573. notebook := arg["notebook"].(string)
  574. p := arg["path"].(string)
  575. sortParam := arg["sort"]
  576. sortMode := util.SortModeUnassigned
  577. if nil != sortParam {
  578. sortMode = int(sortParam.(float64))
  579. }
  580. flashcard := false
  581. if arg["flashcard"] != nil {
  582. flashcard = arg["flashcard"].(bool)
  583. }
  584. maxListCount := model.Conf.FileTree.MaxListCount
  585. if arg["maxListCount"] != nil {
  586. // API `listDocsByPath` add an optional parameter `maxListCount` https://github.com/siyuan-note/siyuan/issues/7993
  587. maxListCount = int(arg["maxListCount"].(float64))
  588. if 0 >= maxListCount {
  589. maxListCount = math.MaxInt
  590. }
  591. }
  592. showHidden := false
  593. if arg["showHidden"] != nil {
  594. showHidden = arg["showHidden"].(bool)
  595. }
  596. files, totals, err := model.ListDocTree(notebook, p, sortMode, flashcard, showHidden, maxListCount)
  597. if nil != err {
  598. ret.Code = -1
  599. ret.Msg = err.Error()
  600. return
  601. }
  602. if maxListCount < totals {
  603. util.PushMsg(fmt.Sprintf(model.Conf.Language(48), len(files)), 7000)
  604. }
  605. ret.Data = map[string]interface{}{
  606. "box": notebook,
  607. "path": p,
  608. "files": files,
  609. }
  610. }
  611. func getDoc(c *gin.Context) {
  612. ret := gulu.Ret.NewResult()
  613. defer c.JSON(http.StatusOK, ret)
  614. arg, ok := util.JsonArg(c, ret)
  615. if !ok {
  616. return
  617. }
  618. id := arg["id"].(string)
  619. idx := arg["index"]
  620. index := 0
  621. if nil != idx {
  622. index = int(idx.(float64))
  623. }
  624. var query string
  625. if queryArg := arg["query"]; nil != queryArg {
  626. query = queryArg.(string)
  627. }
  628. var queryMethod int
  629. if queryMethodArg := arg["queryMethod"]; nil != queryMethodArg {
  630. queryMethod = int(queryMethodArg.(float64))
  631. }
  632. var queryTypes map[string]bool
  633. if queryTypesArg := arg["queryTypes"]; nil != queryTypesArg {
  634. typesArg := queryTypesArg.(map[string]interface{})
  635. queryTypes = map[string]bool{}
  636. for t, b := range typesArg {
  637. queryTypes[t] = b.(bool)
  638. }
  639. }
  640. m := arg["mode"] // 0: 仅当前 ID,1:向上 2:向下,3:上下都加载,4:加载末尾
  641. mode := 0
  642. if nil != m {
  643. mode = int(m.(float64))
  644. }
  645. s := arg["size"]
  646. size := 102400 // 默认最大加载块数
  647. if nil != s {
  648. size = int(s.(float64))
  649. }
  650. startID := ""
  651. endID := ""
  652. startIDArg := arg["startID"]
  653. endIDArg := arg["endID"]
  654. if nil != startIDArg && nil != endIDArg {
  655. startID = startIDArg.(string)
  656. endID = endIDArg.(string)
  657. size = model.Conf.Editor.DynamicLoadBlocks
  658. }
  659. isBacklinkArg := arg["isBacklink"]
  660. isBacklink := false
  661. if nil != isBacklinkArg {
  662. isBacklink = isBacklinkArg.(bool)
  663. }
  664. blockCount, content, parentID, parent2ID, rootID, typ, eof, scroll, boxID, docPath, isBacklinkExpand, err := model.GetDoc(startID, endID, id, index, query, queryTypes, queryMethod, mode, size, isBacklink)
  665. if model.ErrBlockNotFound == err {
  666. ret.Code = 3
  667. return
  668. }
  669. if nil != err {
  670. ret.Code = 1
  671. ret.Msg = err.Error()
  672. return
  673. }
  674. // 判断是否正在同步中 https://github.com/siyuan-note/siyuan/issues/6290
  675. isSyncing := model.IsSyncingFile(rootID)
  676. ret.Data = map[string]interface{}{
  677. "id": id,
  678. "mode": mode,
  679. "parentID": parentID,
  680. "parent2ID": parent2ID,
  681. "rootID": rootID,
  682. "type": typ,
  683. "content": content,
  684. "blockCount": blockCount,
  685. "eof": eof,
  686. "scroll": scroll,
  687. "box": boxID,
  688. "path": docPath,
  689. "isSyncing": isSyncing,
  690. "isBacklinkExpand": isBacklinkExpand,
  691. }
  692. }
  693. func pushCreate(box *model.Box, p, treeID string, arg map[string]interface{}) {
  694. evt := util.NewCmdResult("create", 0, util.PushModeBroadcast)
  695. name := path.Base(p)
  696. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  697. evt.Data = map[string]interface{}{
  698. "box": box,
  699. "path": p,
  700. "files": files,
  701. "name": name,
  702. "id": treeID,
  703. }
  704. evt.Callback = arg["callback"]
  705. util.PushEvent(evt)
  706. }