page_property.cljs 3.6 KB

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