page_property.cljs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. (ns frontend.handler.file-based.page-property
  2. "Page property fns for file graphs"
  3. (:require [clojure.string :as string]
  4. [frontend.db :as db]
  5. [frontend.db.file-based.model :as file-model]
  6. [frontend.modules.outliner.op :as outliner-op]
  7. [frontend.modules.outliner.ui :as ui-outliner-tx]
  8. [frontend.state :as state]
  9. [frontend.util :as util]))
  10. (defn insert-property
  11. [format content key value]
  12. (when (and (string? content) (not (string/blank? (name key))))
  13. (let [key (if (string? key) (keyword key) key)
  14. key-part (util/format (case format
  15. :org "#+%s: "
  16. "%s:: ") (string/lower-case (name key)))
  17. new-property-line (str key-part value)
  18. lines (string/split-lines content)
  19. key-exists? (atom false)
  20. lines (doall
  21. (map (fn [line]
  22. (if (and (string/starts-with?
  23. (string/lower-case line)
  24. (string/lower-case key-part))
  25. (not @key-exists?)) ; only replace the first match
  26. (do
  27. (reset! key-exists? true)
  28. new-property-line)
  29. line)) lines))
  30. lines (if (= lines [""]) nil lines)
  31. lines (if @key-exists? lines (cons new-property-line lines))]
  32. (string/join "\n" lines))))
  33. (defn insert-properties
  34. "Updates multiple page properties. Mainly just used in legacy title context"
  35. [format content kvs]
  36. (reduce
  37. (fn [content [k v]]
  38. (let [k (if (string? k)
  39. (keyword (-> (string/lower-case k)
  40. (string/replace " " "-")))
  41. k)
  42. v (if (coll? v)
  43. (some->>
  44. (seq v)
  45. (distinct)
  46. (string/join ", "))
  47. v)]
  48. (insert-property format content k v)))
  49. content kvs))
  50. (defn add-property!
  51. [page key value]
  52. (let [repo (state/get-current-repo)
  53. key (keyword key)
  54. pre-block (file-model/get-pre-block repo (:db/id page))
  55. format (state/get-preferred-format)
  56. page-id {:db/id (:db/id page)}
  57. org? (= format :org)
  58. value (if (contains? #{:filters} key) (pr-str value) value)]
  59. (if pre-block
  60. (let [properties (:block/properties pre-block)
  61. new-properties (assoc properties key value)
  62. content (:block/title pre-block)
  63. new-content (insert-property format content key value)
  64. block {:db/id (:db/id pre-block)
  65. :block/properties new-properties
  66. :block/title new-content
  67. :block/page page-id}
  68. tx [(assoc page-id :block/properties new-properties)
  69. block]]
  70. (db/transact! tx))
  71. (let [block {:block/uuid (db/new-block-id)
  72. :block/parent page-id
  73. :block/page page-id
  74. :block/title (if org?
  75. (str "#+" (string/upper-case (name key)) ": " value)
  76. (str (name key) ":: " value))
  77. :block/format format
  78. :block/properties {key value}
  79. :block/pre-block? true}
  80. page-properties-tx [(assoc page-id :block/properties {key value})]]
  81. (ui-outliner-tx/transact!
  82. {:outliner-op :insert-blocks
  83. :additional-tx page-properties-tx}
  84. (outliner-op/insert-blocks! [block] page {:sibling? false}))))))