notebook.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // SiYuan - Build Your Eternal Digital Garden
  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. "net/http"
  19. "strings"
  20. "github.com/88250/gulu"
  21. "github.com/gin-gonic/gin"
  22. "github.com/siyuan-note/siyuan/kernel/model"
  23. "github.com/siyuan-note/siyuan/kernel/util"
  24. )
  25. func setNotebookIcon(c *gin.Context) {
  26. ret := gulu.Ret.NewResult()
  27. defer c.JSON(http.StatusOK, ret)
  28. arg, ok := util.JsonArg(c, ret)
  29. if !ok {
  30. return
  31. }
  32. boxID := arg["notebook"].(string)
  33. icon := arg["icon"].(string)
  34. model.SetBoxIcon(boxID, icon)
  35. }
  36. func changeSortNotebook(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. idsArg := arg["notebooks"].([]interface{})
  44. var ids []string
  45. for _, p := range idsArg {
  46. ids = append(ids, p.(string))
  47. }
  48. model.ChangeBoxSort(ids)
  49. }
  50. func renameNotebook(c *gin.Context) {
  51. ret := gulu.Ret.NewResult()
  52. defer c.JSON(http.StatusOK, ret)
  53. arg, ok := util.JsonArg(c, ret)
  54. if !ok {
  55. return
  56. }
  57. notebook := arg["notebook"].(string)
  58. name := arg["name"].(string)
  59. err := model.RenameBox(notebook, name)
  60. if nil != err {
  61. ret.Code = -1
  62. ret.Msg = err.Error()
  63. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  64. return
  65. }
  66. evt := util.NewCmdResult("renamenotebook", 0, util.PushModeBroadcast, util.PushModeNone)
  67. evt.Data = map[string]interface{}{
  68. "box": notebook,
  69. "name": name,
  70. }
  71. util.PushEvent(evt)
  72. }
  73. func removeNotebook(c *gin.Context) {
  74. ret := gulu.Ret.NewResult()
  75. defer c.JSON(http.StatusOK, ret)
  76. arg, ok := util.JsonArg(c, ret)
  77. if !ok {
  78. return
  79. }
  80. notebook := arg["notebook"].(string)
  81. err := model.RemoveBox(notebook)
  82. if nil != err {
  83. ret.Code = -1
  84. ret.Msg = err.Error()
  85. return
  86. }
  87. evt := util.NewCmdResult("unmount", 0, util.PushModeBroadcast, 0)
  88. evt.Data = map[string]interface{}{
  89. "box": notebook,
  90. }
  91. evt.Callback = arg["callback"]
  92. util.PushEvent(evt)
  93. }
  94. func createNotebook(c *gin.Context) {
  95. ret := gulu.Ret.NewResult()
  96. defer c.JSON(http.StatusOK, ret)
  97. arg, ok := util.JsonArg(c, ret)
  98. if !ok {
  99. return
  100. }
  101. name := arg["name"].(string)
  102. id, err := model.CreateBox(name)
  103. if nil != err {
  104. ret.Code = -1
  105. ret.Msg = err.Error()
  106. return
  107. }
  108. existed, err := model.Mount(id)
  109. if nil != err {
  110. ret.Code = -1
  111. ret.Msg = err.Error()
  112. return
  113. }
  114. ret.Data = map[string]interface{}{
  115. "notebook": model.Conf.Box(id),
  116. }
  117. evt := util.NewCmdResult("createnotebook", 0, util.PushModeBroadcast, util.PushModeNone)
  118. evt.Data = map[string]interface{}{
  119. "box": model.Conf.Box(id),
  120. "existed": existed,
  121. }
  122. util.PushEvent(evt)
  123. }
  124. func openNotebook(c *gin.Context) {
  125. ret := gulu.Ret.NewResult()
  126. defer c.JSON(http.StatusOK, ret)
  127. arg, ok := util.JsonArg(c, ret)
  128. if !ok {
  129. return
  130. }
  131. notebook := arg["notebook"].(string)
  132. msgId := util.PushMsg(model.Conf.Language(45), 1000*60*15)
  133. defer util.PushClearMsg(msgId)
  134. existed, err := model.Mount(notebook)
  135. if nil != err {
  136. ret.Code = -1
  137. ret.Msg = err.Error()
  138. return
  139. }
  140. evt := util.NewCmdResult("mount", 0, util.PushModeBroadcast, util.PushModeNone)
  141. evt.Data = map[string]interface{}{
  142. "box": model.Conf.Box(notebook),
  143. "existed": existed,
  144. }
  145. evt.Callback = arg["callback"]
  146. util.PushEvent(evt)
  147. }
  148. func closeNotebook(c *gin.Context) {
  149. ret := gulu.Ret.NewResult()
  150. defer c.JSON(http.StatusOK, ret)
  151. arg, ok := util.JsonArg(c, ret)
  152. if !ok {
  153. return
  154. }
  155. notebook := arg["notebook"].(string)
  156. model.Unmount(notebook)
  157. }
  158. func getNotebookConf(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. notebook := arg["notebook"].(string)
  166. box := model.Conf.Box(notebook)
  167. ret.Data = map[string]interface{}{
  168. "box": box.ID,
  169. "name": box.Name,
  170. "conf": box.GetConf(),
  171. }
  172. }
  173. func setNotebookConf(c *gin.Context) {
  174. ret := gulu.Ret.NewResult()
  175. defer c.JSON(http.StatusOK, ret)
  176. arg, ok := util.JsonArg(c, ret)
  177. if !ok {
  178. return
  179. }
  180. notebook := arg["notebook"].(string)
  181. box := model.Conf.Box(notebook)
  182. param, err := gulu.JSON.MarshalJSON(arg["conf"])
  183. if nil != err {
  184. ret.Code = -1
  185. ret.Msg = err.Error()
  186. return
  187. }
  188. boxConf := box.GetConf()
  189. if err = gulu.JSON.UnmarshalJSON(param, boxConf); nil != err {
  190. ret.Code = -1
  191. ret.Msg = err.Error()
  192. return
  193. }
  194. boxConf.RefCreateSavePath = strings.TrimSpace(boxConf.RefCreateSavePath)
  195. if "" != boxConf.RefCreateSavePath {
  196. if !strings.HasSuffix(boxConf.RefCreateSavePath, "/") {
  197. boxConf.RefCreateSavePath += "/"
  198. }
  199. }
  200. boxConf.DailyNoteSavePath = strings.TrimSpace(boxConf.DailyNoteSavePath)
  201. if "" != boxConf.DailyNoteSavePath {
  202. if !strings.HasPrefix(boxConf.DailyNoteSavePath, "/") {
  203. boxConf.DailyNoteSavePath = "/" + boxConf.DailyNoteSavePath
  204. }
  205. }
  206. if "/" == boxConf.DailyNoteSavePath {
  207. ret.Code = -1
  208. ret.Msg = model.Conf.Language(49)
  209. return
  210. }
  211. boxConf.DailyNoteTemplatePath = strings.TrimSpace(boxConf.DailyNoteTemplatePath)
  212. if "" != boxConf.DailyNoteTemplatePath {
  213. if !strings.HasSuffix(boxConf.DailyNoteTemplatePath, ".md") {
  214. boxConf.DailyNoteTemplatePath += ".md"
  215. }
  216. if !strings.HasPrefix(boxConf.DailyNoteTemplatePath, "/") {
  217. boxConf.DailyNoteTemplatePath = "/" + boxConf.DailyNoteTemplatePath
  218. }
  219. }
  220. box.SaveConf(boxConf)
  221. ret.Data = boxConf
  222. }
  223. func lsNotebooks(c *gin.Context) {
  224. ret := gulu.Ret.NewResult()
  225. defer c.JSON(http.StatusOK, ret)
  226. notebooks, err := model.ListNotebooks()
  227. if nil != err {
  228. return
  229. }
  230. ret.Data = map[string]interface{}{
  231. "notebooks": notebooks,
  232. }
  233. }