attr.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "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/util"
  23. )
  24. func getBookmarkLabels(c *gin.Context) {
  25. ret := gulu.Ret.NewResult()
  26. defer c.JSON(http.StatusOK, ret)
  27. ret.Data = model.BookmarkLabels()
  28. }
  29. func getBlockAttrs(c *gin.Context) {
  30. ret := gulu.Ret.NewResult()
  31. defer c.JSON(http.StatusOK, ret)
  32. arg, ok := util.JsonArg(c, ret)
  33. if !ok {
  34. return
  35. }
  36. id := arg["id"].(string)
  37. ret.Data = model.GetBlockAttrs(id)
  38. }
  39. func setBlockAttrs(c *gin.Context) {
  40. ret := gulu.Ret.NewResult()
  41. defer c.JSON(http.StatusOK, ret)
  42. arg, ok := util.JsonArg(c, ret)
  43. if !ok {
  44. return
  45. }
  46. id := arg["id"].(string)
  47. attrs := arg["attrs"].(map[string]interface{})
  48. nameValues := map[string]string{}
  49. for name, value := range attrs {
  50. if nil == value { // API `setBlockAttrs` 中如果存在属性值设置为 `null` 时移除该属性 https://github.com/siyuan-note/siyuan/issues/5577
  51. nameValues[name] = ""
  52. } else {
  53. nameValues[name] = value.(string)
  54. }
  55. }
  56. err := model.SetBlockAttrs(id, nameValues)
  57. if nil != err {
  58. ret.Code = -1
  59. ret.Msg = err.Error()
  60. return
  61. }
  62. }
  63. func resetBlockAttrs(c *gin.Context) {
  64. ret := gulu.Ret.NewResult()
  65. defer c.JSON(http.StatusOK, ret)
  66. arg, ok := util.JsonArg(c, ret)
  67. if !ok {
  68. return
  69. }
  70. id := arg["id"].(string)
  71. attrs := arg["attrs"].(map[string]interface{})
  72. nameValues := map[string]string{}
  73. for name, value := range attrs {
  74. nameValues[name] = value.(string)
  75. }
  76. err := model.ResetBlockAttrs(id, nameValues)
  77. if nil != err {
  78. ret.Code = -1
  79. ret.Msg = err.Error()
  80. return
  81. }
  82. }