block.cljs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. (ns frontend.handler.block
  2. (:require [frontend.util :as util]
  3. [clojure.walk :as walk]
  4. [frontend.db :as db]
  5. [frontend.state :as state]
  6. [frontend.format.mldoc :as mldoc]))
  7. (defn blocks->vec-tree [col]
  8. (let [col (map (fn [h] (cond->
  9. h
  10. (not (:block/dummy? h))
  11. (dissoc h :block/meta))) col)
  12. parent? (fn [item children]
  13. (and (seq children)
  14. (every? #(< (:block/level item) (:block/level %)) children)))]
  15. (loop [col (reverse col)
  16. children (list)]
  17. (if (empty? col)
  18. children
  19. (let [[item & others] col
  20. cur-level (:block/level item)
  21. bottom-level (:block/level (first children))
  22. pre-block? (:block/pre-block? item)]
  23. (cond
  24. (empty? children)
  25. (recur others (list item))
  26. (<= bottom-level cur-level)
  27. (recur others (conj children item))
  28. pre-block?
  29. (recur others (cons item children))
  30. (> bottom-level cur-level) ; parent
  31. (let [[children other-children] (split-with (fn [h]
  32. (> (:block/level h) cur-level))
  33. children)
  34. children (cons
  35. (assoc item :block/children children)
  36. other-children)]
  37. (recur others children))))))))
  38. ;; recursively with children content for tree
  39. (defn get-block-content-rec
  40. ([block]
  41. (get-block-content-rec block (fn [block] (:block/content block))))
  42. ([block transform-fn]
  43. (let [contents (atom [])
  44. _ (walk/prewalk
  45. (fn [form]
  46. (when (map? form)
  47. (when-let [content (:block/content form)]
  48. (swap! contents conj (transform-fn form))))
  49. form)
  50. block)]
  51. (apply util/join-newline @contents))))
  52. ;; with children content
  53. (defn get-block-full-content
  54. ([repo block-id]
  55. (get-block-full-content repo block-id (fn [block] (:block/content block))))
  56. ([repo block-id transform-fn]
  57. (let [blocks (db/get-block-and-children-no-cache repo block-id)]
  58. (->> blocks
  59. (map transform-fn)
  60. (apply util/join-newline)))))
  61. (defn get-block-end-pos-rec
  62. [repo block]
  63. (let [children (:block/children block)]
  64. (if (seq children)
  65. (get-block-end-pos-rec repo (last children))
  66. (if-let [end-pos (get-in block [:block/meta :end-pos])]
  67. end-pos
  68. (when-let [block (db/entity repo [:block/uuid (:block/uuid block)])]
  69. (get-in block [:block/meta :end-pos]))))))
  70. (defn get-block-ids
  71. [block]
  72. (let [ids (atom [])
  73. _ (walk/prewalk
  74. (fn [form]
  75. (when (map? form)
  76. (when-let [id (:block/uuid form)]
  77. (swap! ids conj id)))
  78. form)
  79. block)]
  80. @ids))
  81. (defn collapse-block!
  82. [block]
  83. (let [repo (:block/repo block)]
  84. (db/transact! repo
  85. [{:block/uuid (:block/uuid block)
  86. :block/collapsed? true}])))
  87. (defn collapse-blocks!
  88. [block-ids]
  89. (let [repo (state/get-current-repo)]
  90. (db/transact! repo
  91. (map
  92. (fn [id]
  93. {:block/uuid id
  94. :block/collapsed? true})
  95. block-ids))))
  96. (defn expand-block!
  97. [block]
  98. (let [repo (:block/repo block)]
  99. (db/transact! repo
  100. [{:block/uuid (:block/uuid block)
  101. :block/collapsed? false}])))
  102. (defn expand-blocks!
  103. [block-ids]
  104. (let [repo (state/get-current-repo)]
  105. (db/transact! repo
  106. (map
  107. (fn [id]
  108. {:block/uuid id
  109. :block/collapsed? false})
  110. block-ids))))
  111. (defn pre-block-with-only-title?
  112. [repo block-id]
  113. (when-let [block (db/entity repo [:block/uuid block-id])]
  114. (let [properties (:page/properties (:block/page block))]
  115. (and (:title properties)
  116. (= 1 (count properties))
  117. (let [ast (mldoc/->edn (:block/content block) (mldoc/default-config (:block/format block)))]
  118. (or
  119. (empty? (rest ast))
  120. (every? (fn [[[typ break-lines]] _]
  121. (and (= typ "Paragraph")
  122. (every? #(= % ["Break_Line"]) break-lines))) (rest ast))))))))