property.cljs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. (ns frontend.handler.file-based.property
  2. "Property handlers for file based graphs"
  3. (:require [frontend.db :as db]
  4. [frontend.handler.block :as block-handler]
  5. [frontend.handler.file-based.property.util :as property-util]
  6. [frontend.modules.outliner.op :as outliner-op]
  7. [frontend.modules.outliner.ui :as ui-outliner-tx]
  8. [frontend.state :as state]
  9. [logseq.common.util :as common-util]
  10. [promesa.core :as p]))
  11. (defn insert-property
  12. [format content key value & args]
  13. (apply property-util/insert-property format content key value args))
  14. (defn remove-id-property
  15. [format content]
  16. (property-util/remove-id-property format content))
  17. (def hidden-properties property-util/hidden-properties)
  18. (def built-in-properties property-util/built-in-properties)
  19. (defn batch-set-block-property-aux!
  20. "col: a collection of [block-id property-key property-value]."
  21. [col]
  22. (let [col' (group-by first col)]
  23. (p/do!
  24. (ui-outliner-tx/transact!
  25. {:outliner-op :save-block}
  26. (doseq [[block-id items] col']
  27. (let [block-id (if (string? block-id) (uuid block-id) block-id)
  28. new-properties (zipmap (map second items)
  29. (map last items))]
  30. (when-let [block (db/entity [:block/uuid block-id])]
  31. (let [format (:block/format block)
  32. content (:block/content block)
  33. properties (:block/properties block)
  34. properties-text-values (:block/properties-text-values block)
  35. properties (-> (merge properties new-properties)
  36. common-util/remove-nils-non-nested)
  37. properties-text-values (-> (merge properties-text-values new-properties)
  38. common-util/remove-nils-non-nested)
  39. property-ks (->> (concat (:block/properties-order block)
  40. (map second items))
  41. (filter (set (keys properties)))
  42. distinct
  43. vec)
  44. content (property-util/remove-properties format content)
  45. kvs (for [key property-ks] [key (or (get properties-text-values key)
  46. (get properties key))])
  47. content (property-util/insert-properties format content kvs)
  48. content (property-util/remove-empty-properties content)
  49. block {:block/uuid block-id
  50. :block/properties properties
  51. :block/properties-order property-ks
  52. :block/properties-text-values properties-text-values
  53. :block/content content}]
  54. (outliner-op/save-block! block))))))
  55. (let [block-id (ffirst col)
  56. block-id (if (string? block-id) (uuid block-id) block-id)
  57. input-pos (or (state/get-edit-pos) :max)]
  58. ;; update editing input content
  59. (when-let [editing-block (state/get-edit-block)]
  60. (when (= (:block/uuid editing-block) block-id)
  61. (block-handler/edit-block! editing-block
  62. input-pos
  63. (state/get-edit-input-id))))))))
  64. (defn batch-set-block-property!
  65. [block-ids property-key property-value]
  66. (batch-set-block-property-aux! (map #(vector % property-key property-value) block-ids)))
  67. (defn batch-remove-block-property!
  68. [block-ids property-key]
  69. (batch-set-block-property! block-ids property-key nil))
  70. (defn remove-block-property!
  71. [block-id key]
  72. (let [key (keyword key)]
  73. (batch-set-block-property-aux! [[block-id key nil]])))
  74. (defn set-block-property!
  75. [block-id key value]
  76. (let [key (keyword key)]
  77. (batch-set-block-property-aux! [[block-id key value]])))