Skip to content

Commit c80cb16

Browse files
committed
feat: filter inline refs by coref
1 parent 7aecbfe commit c80cb16

File tree

3 files changed

+45
-35
lines changed

3 files changed

+45
-35
lines changed

src/cljc/athens/common_db.cljc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -516,16 +516,6 @@
516516
(throw (ex-info fail-msg position)))))
517517

518518

519-
(defn page-refs
520-
"Returns page titles referenced in eid."
521-
[db eid]
522-
(->> eid
523-
(d/entity db)
524-
:block/refs
525-
(map :node/title)
526-
(remove nil?)))
527-
528-
529519
(defn extract-tag-values
530520
"Extracts `tag` values from `children-fn` children with `extractor-fn` from parser AST."
531521
[ast tag-selector children-fn extractor-fn]

src/cljs/athens/db.cljs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,9 @@
295295

296296

297297
(def block-document-pull-vector
298-
'[:db/id :block/uid :block/string :block/open :block/order :block/header {:block/children ...} :block/refs :block/_refs])
298+
'[:db/id :block/uid :block/string :block/open :block/order :block/header {:block/children ...} :block/refs
299+
;; for backrefs, also return their refs and matching titles, if any, for coref analysis.
300+
{:block/_refs [:db/id {:block/refs [:db/id :node/title]}]}])
299301

300302

301303
(def node-document-pull-vector

src/cljs/athens/views/blocks/core.cljs

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,19 @@
214214
:margin-block-start "0"}]]})
215215

216216

217+
(defn has-coref?
218+
[set {:block/keys [refs] :as backref}]
219+
(when (some set refs)
220+
backref))
221+
222+
217223
(defn inline-linked-refs-el
218224
[block state]
219-
(let [refs (db/get-linked-block-references block)]
225+
(let [ref-filter (-> state deref :inline-refs/filter)
226+
block' (if (seq ref-filter)
227+
(update block :block/_refs #(vec (keep (partial has-coref? ref-filter) %)))
228+
block)
229+
refs (db/get-linked-block-references block')]
220230
(when (not-empty refs)
221231
[:div (stylefy/use-style references-style {:key "Inline Linked References"})
222232
[:section
@@ -235,30 +245,29 @@
235245
(def coref-string-size-limit 3)
236246

237247

238-
;; TODO: show corefs in block view
239248
(defn block-refs-count-el
240-
[refs click-fn]
241-
(let [db @db/dsdb ; TODO: this isn't reactive
242-
total (count refs)
243-
corefs (->> refs
244-
(map :db/id)
245-
(mapcat (partial common-db/page-refs db))
246-
(remove #(> (count %) coref-string-size-limit))
247-
(frequencies))]
249+
[backrefs click-fn]
250+
(let [total (count backrefs)
251+
coref-freqs (->> backrefs
252+
(mapcat :block/refs)
253+
(remove #(-> % :node/title not))
254+
(remove #(> (count (:node/title %)) coref-string-size-limit))
255+
(frequencies))] ; TODO: stable sort
248256

249257
[:div (stylefy/use-style {:margin-left "1em"
250-
:grid-area "refs"
251-
:z-index (:zindex-dropdown style/ZINDICES)
252-
:visibility (when-not (pos? total) "hidden")})
258+
:grid-area "refs"
259+
:z-index (:zindex-dropdown style/ZINDICES)
260+
:visibility (when-not (pos? total) "hidden")})
253261
(doall
254-
(for [[title total] corefs]
255-
[:> Button {:on-click (fn [e]
256-
(.. e stopPropagation)
257-
(click-fn e))}
262+
(for [[{:node/keys [title] :as coref} total] coref-freqs]
263+
[:> Button {:key (str "coref-count-" title)
264+
:on-click (fn [e]
265+
(.. e stopPropagation)
266+
(click-fn e #{coref}))}
258267
[:span title " " [:sub total]]]))
259-
[:> Button {:on-click (fn [e]
260-
(.. e stopPropagation)
261-
(click-fn e))}
268+
[:> Button {:on-click (fn [e]
269+
(.. e stopPropagation)
270+
(click-fn e #{}))}
262271
total]]))
263272

264273

@@ -416,6 +425,7 @@
416425
:show-editable-dom false
417426
:linked-ref/open (or (false? linked-ref) initial-open)
418427
:inline-refs/open false
428+
:inline-refs/filter #{}
419429
:inline-refs/states {}
420430
:block/uid uid})
421431
save-fn #(db/transact-state-for-uid (or original-uid uid) state)
@@ -502,10 +512,18 @@
502512
[presence/inline-presence-el uid]
503513

504514
(when (and (> (count _refs) 0) (not= :block-embed? opts))
505-
[block-refs-count-el _refs (fn [e]
506-
(if (.. e -shiftKey)
507-
(rf/dispatch [:right-sidebar/open-item uid])
508-
(swap! state update :inline-refs/open not)))])]
515+
[block-refs-count-el _refs (fn [e new-filter]
516+
(let [{:inline-refs/keys [open filter]} @state]
517+
(if (.. e -shiftKey)
518+
;; shift-click always opens the block on the sidebar
519+
(rf/dispatch [:right-sidebar/open-item uid])
520+
(do
521+
;; update the filter
522+
(swap! state assoc :inline-refs/filter new-filter)
523+
524+
;; toggle open unless we're already open and just switching filters
525+
(when-not (and open (not= filter new-filter))
526+
(swap! state update :inline-refs/open not))))))])]
509527

510528
[autocomplete-search/inline-search-el block state]
511529
[autocomplete-slash/slash-menu-el block state]

0 commit comments

Comments
 (0)