reference.cljs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. (ns frontend.components.reference
  2. (:require [rum.core :as rum]
  3. [frontend.util :as util]
  4. [frontend.state :as state]
  5. [clojure.string :as string]
  6. [frontend.db :as db]
  7. [frontend.components.block :as block]
  8. [frontend.ui :as ui]
  9. [frontend.components.content :as content]
  10. [frontend.date :as date]
  11. [frontend.components.editor :as editor]
  12. [frontend.db-mixins :as db-mixins]
  13. [clojure.string :as string]
  14. [frontend.config :as config]
  15. [frontend.components.svg :as svg]
  16. [frontend.handler.page :as page-handler]
  17. [frontend.handler.block :as block-handler]
  18. [medley.core :as medley]))
  19. (rum/defc filter-dialog-inner < rum/reactive
  20. [close-fn references page-name]
  21. (let [filter-state (page-handler/get-filter page-name)]
  22. [:div
  23. [:div.sm:flex.sm:items-start
  24. [:div.mx-auto.flex-shrink-0.flex.items-center.justify-center.h-12.w-12.rounded-full.bg-gray-200.text-gray-500.sm:mx-0.sm:h-10.sm:w-10
  25. (svg/filter-icon)]
  26. [:div.mt-3.text-center.sm:mt-0.sm:ml-4.sm:text-left
  27. [:h3#modal-headline.text-lg.leading-6.font-medium.text-gray-900 "Filter"]
  28. [:span.text-xs
  29. "Click to include and shift-click to exclude. Click again to remove."]]]
  30. [:div.mt-5.sm:mt-4.sm:flex.sm.gap-1.flex-wrap
  31. (for [reference references]
  32. (let [filtered (get (rum/react filter-state) reference)
  33. color (condp = filtered
  34. true "text-green-500"
  35. false "text-red-500"
  36. nil)]
  37. [:button.border.rounded.px-1 {:key reference :class color :style {:border-color "currentColor"}
  38. :on-click (fn [e]
  39. (swap! filter-state #(if (nil? (get @filter-state reference))
  40. (assoc % reference (not (.-shiftKey e)))
  41. (dissoc % reference)))
  42. (page-handler/save-filter! page-name @filter-state))}
  43. reference]))]]))
  44. (defn filter-dialog
  45. [references page-name]
  46. (fn [close-fn]
  47. (filter-dialog-inner close-fn references page-name)))
  48. (rum/defc references < rum/reactive
  49. [page-name marker? priority?]
  50. (when page-name
  51. (let [block? (util/uuid-string? page-name)
  52. block-id (and block? (uuid page-name))
  53. page-name (string/lower-case page-name)
  54. journal? (date/valid-journal-title? (string/capitalize page-name))
  55. repo (state/get-current-repo)
  56. ref-blocks (cond
  57. priority?
  58. (db/get-blocks-by-priority repo page-name)
  59. marker?
  60. (db/get-marker-blocks repo page-name)
  61. block-id
  62. (db/get-block-referenced-blocks block-id)
  63. :else
  64. (db/get-page-referenced-blocks page-name))
  65. scheduled-or-deadlines (if journal?
  66. (db/get-date-scheduled-or-deadlines (string/capitalize page-name))
  67. nil)
  68. references (db/get-page-linked-refs-refed-pages repo page-name)
  69. filter-state (rum/react (page-handler/get-filter page-name))
  70. included (filter (fn [[x v]]) filter-state)
  71. filters (when (seq filter-state)
  72. (->> (group-by second filter-state)
  73. (medley/map-vals #(map first %))))
  74. filtered-ref-blocks (block-handler/filter-blocks repo ref-blocks filters true)
  75. n-ref (count filtered-ref-blocks)]
  76. (when (or (> n-ref 0)
  77. (seq scheduled-or-deadlines))
  78. [:div.references.mt-6.flex-1.flex-row
  79. [:div.content
  80. (when (seq scheduled-or-deadlines)
  81. (ui/foldable
  82. [:h2.font-bold.opacity-50 (let []
  83. "SCHEDULED AND DEADLINE")]
  84. [:div.references-blocks.mb-6
  85. (let [ref-hiccup (block/->hiccup scheduled-or-deadlines
  86. {:id (str page-name "-agenda")
  87. :start-level 2
  88. :ref? true
  89. :group-by-page? true
  90. :editor-box editor/box}
  91. {})]
  92. (content/content page-name
  93. {:hiccup ref-hiccup}))]))
  94. (ui/foldable
  95. [:div.flex.flex-row.flex-1.justify-between
  96. [:h2.font-bold.opacity-50 (let []
  97. (str n-ref " Linked Reference"
  98. (if (> n-ref 1) "s")))]
  99. [:a {:title "Filter"
  100. :on-click #(state/set-modal! (filter-dialog references page-name))}
  101. (svg/filter-icon (cond
  102. (empty? filter-state) nil
  103. (every? true? (vals filter-state)) "text-green-500"
  104. (every? false? (vals filter-state)) "text-red-500"
  105. :else "text-yellow-200"))]]
  106. [:div.references-blocks
  107. (let [ref-hiccup (block/->hiccup filtered-ref-blocks
  108. {:id page-name
  109. :start-level 2
  110. :ref? true
  111. :breadcrumb-show? true
  112. :group-by-page? true
  113. :editor-box editor/box
  114. :filters filters}
  115. {})]
  116. (content/content page-name
  117. {:hiccup ref-hiccup}))])]]))))
  118. (rum/defcs unlinked-references-aux
  119. < rum/reactive db-mixins/query
  120. {:will-mount (fn [state]
  121. (let [[page-name n-ref] (:rum/args state)
  122. ref-blocks (db/get-page-unlinked-references page-name)]
  123. (reset! n-ref (count ref-blocks))
  124. (assoc state ::ref-blocks ref-blocks)))}
  125. [state page-name n-ref]
  126. (let [ref-blocks (::ref-blocks state)]
  127. [:div.references-blocks
  128. (let [ref-hiccup (block/->hiccup ref-blocks
  129. {:id (str page-name "-unlinked-")
  130. :start-level 2
  131. :ref? true
  132. :group-by-page? true
  133. :editor-box editor/box}
  134. {})]
  135. (content/content page-name
  136. {:hiccup ref-hiccup}))]))
  137. (rum/defcs unlinked-references < rum/reactive
  138. (rum/local nil ::n-ref)
  139. [state page-name]
  140. (let [n-ref (get state ::n-ref)]
  141. (when page-name
  142. (let [page-name (string/lower-case page-name)]
  143. [:div.references.mt-6.flex-1.flex-row
  144. [:div.content.flex-1
  145. (ui/foldable
  146. [:h2.font-bold {:style {:opacity "0.3"}}
  147. (if @n-ref
  148. (str @n-ref " Unlinked Reference" (if (> @n-ref 1)
  149. "s"))
  150. "Unlinked References")]
  151. (fn [] (unlinked-references-aux page-name n-ref))
  152. true)]]))))