attr.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. "net/http"
  19. "github.com/88250/gulu"
  20. "github.com/gin-gonic/gin"
  21. "github.com/siyuan-note/siyuan/kernel/model"
  22. "github.com/siyuan-note/siyuan/kernel/sql"
  23. "github.com/siyuan-note/siyuan/kernel/treenode"
  24. "github.com/siyuan-note/siyuan/kernel/util"
  25. )
  26. func getBookmarkLabels(c *gin.Context) {
  27. ret := gulu.Ret.NewResult()
  28. defer c.JSON(http.StatusOK, ret)
  29. ret.Data = model.BookmarkLabels()
  30. }
  31. func batchGetBlockAttrs(c *gin.Context) {
  32. ret := gulu.Ret.NewResult()
  33. defer c.JSON(http.StatusOK, ret)
  34. arg, ok := util.JsonArg(c, ret)
  35. if !ok {
  36. return
  37. }
  38. ids := arg["ids"].([]interface{})
  39. var idList []string
  40. for _, id := range ids {
  41. idList = append(idList, id.(string))
  42. }
  43. ret.Data = sql.BatchGetBlockAttrs(idList)
  44. }
  45. func getBlockAttrs(c *gin.Context) {
  46. ret := gulu.Ret.NewResult()
  47. defer c.JSON(http.StatusOK, ret)
  48. arg, ok := util.JsonArg(c, ret)
  49. if !ok {
  50. return
  51. }
  52. id := arg["id"].(string)
  53. if util.InvalidIDPattern(id, ret) {
  54. return
  55. }
  56. ret.Data = sql.GetBlockAttrs(id)
  57. }
  58. func setBlockAttrs(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. id := arg["id"].(string)
  66. if util.InvalidIDPattern(id, ret) {
  67. return
  68. }
  69. attrs := arg["attrs"].(map[string]interface{})
  70. if 1 == len(attrs) && "" != attrs["scroll"] {
  71. // 不记录用户指南滚动位置
  72. if b := treenode.GetBlockTree(id); nil != b && (model.IsUserGuide(b.BoxID)) {
  73. attrs["scroll"] = ""
  74. }
  75. }
  76. nameValues := map[string]string{}
  77. for name, value := range attrs {
  78. if nil == value { // API `setBlockAttrs` 中如果存在属性值设置为 `null` 时移除该属性 https://github.com/siyuan-note/siyuan/issues/5577
  79. nameValues[name] = ""
  80. } else {
  81. nameValues[name] = value.(string)
  82. }
  83. }
  84. err := model.SetBlockAttrs(id, nameValues)
  85. if err != nil {
  86. ret.Code = -1
  87. ret.Msg = err.Error()
  88. return
  89. }
  90. }
  91. func batchSetBlockAttrs(c *gin.Context) {
  92. ret := gulu.Ret.NewResult()
  93. defer c.JSON(http.StatusOK, ret)
  94. arg, ok := util.JsonArg(c, ret)
  95. if !ok {
  96. return
  97. }
  98. blockAttrsArg := arg["blockAttrs"].([]interface{})
  99. var blockAttrs []map[string]interface{}
  100. for _, blockAttrArg := range blockAttrsArg {
  101. blockAttr := blockAttrArg.(map[string]interface{})
  102. id := blockAttr["id"].(string)
  103. if util.InvalidIDPattern(id, ret) {
  104. return
  105. }
  106. attrs := blockAttr["attrs"].(map[string]interface{})
  107. nameValues := map[string]string{}
  108. for name, value := range attrs {
  109. if nil == value {
  110. nameValues[name] = ""
  111. } else {
  112. nameValues[name] = value.(string)
  113. }
  114. }
  115. blockAttrs = append(blockAttrs, map[string]interface{}{
  116. "id": id,
  117. "attrs": nameValues,
  118. })
  119. }
  120. err := model.BatchSetBlockAttrs(blockAttrs)
  121. if err != nil {
  122. ret.Code = -1
  123. ret.Msg = err.Error()
  124. return
  125. }
  126. }
  127. func resetBlockAttrs(c *gin.Context) {
  128. ret := gulu.Ret.NewResult()
  129. defer c.JSON(http.StatusOK, ret)
  130. arg, ok := util.JsonArg(c, ret)
  131. if !ok {
  132. return
  133. }
  134. id := arg["id"].(string)
  135. attrs := arg["attrs"].(map[string]interface{})
  136. nameValues := map[string]string{}
  137. for name, value := range attrs {
  138. nameValues[name] = value.(string)
  139. }
  140. err := model.ResetBlockAttrs(id, nameValues)
  141. if err != nil {
  142. ret.Code = -1
  143. ret.Msg = err.Error()
  144. return
  145. }
  146. }