attr.go 3.6 KB

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