Skip to content

Commit

Permalink
feat: filter inline refs by coref
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesilva committed Jan 28, 2022
1 parent 7aecbfe commit c80cb16
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
10 changes: 0 additions & 10 deletions src/cljc/athens/common_db.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -516,16 +516,6 @@
(throw (ex-info fail-msg position)))))


(defn page-refs
"Returns page titles referenced in eid."
[db eid]
(->> eid
(d/entity db)
:block/refs
(map :node/title)
(remove nil?)))


(defn extract-tag-values
"Extracts `tag` values from `children-fn` children with `extractor-fn` from parser AST."
[ast tag-selector children-fn extractor-fn]
Expand Down
4 changes: 3 additions & 1 deletion src/cljs/athens/db.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@


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


(def node-document-pull-vector
Expand Down
66 changes: 42 additions & 24 deletions src/cljs/athens/views/blocks/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,19 @@
:margin-block-start "0"}]]})


(defn has-coref?
[set {:block/keys [refs] :as backref}]
(when (some set refs)
backref))


(defn inline-linked-refs-el
[block state]
(let [refs (db/get-linked-block-references block)]
(let [ref-filter (-> state deref :inline-refs/filter)
block' (if (seq ref-filter)
(update block :block/_refs #(vec (keep (partial has-coref? ref-filter) %)))
block)
refs (db/get-linked-block-references block')]
(when (not-empty refs)
[:div (stylefy/use-style references-style {:key "Inline Linked References"})
[:section
Expand All @@ -235,30 +245,29 @@
(def coref-string-size-limit 3)


;; TODO: show corefs in block view
(defn block-refs-count-el
[refs click-fn]
(let [db @db/dsdb ; TODO: this isn't reactive
total (count refs)
corefs (->> refs
(map :db/id)
(mapcat (partial common-db/page-refs db))
(remove #(> (count %) coref-string-size-limit))
(frequencies))]
[backrefs click-fn]
(let [total (count backrefs)
coref-freqs (->> backrefs
(mapcat :block/refs)
(remove #(-> % :node/title not))
(remove #(> (count (:node/title %)) coref-string-size-limit))
(frequencies))] ; TODO: stable sort

[:div (stylefy/use-style {:margin-left "1em"
:grid-area "refs"
:z-index (:zindex-dropdown style/ZINDICES)
:visibility (when-not (pos? total) "hidden")})
:grid-area "refs"
:z-index (:zindex-dropdown style/ZINDICES)
:visibility (when-not (pos? total) "hidden")})
(doall
(for [[title total] corefs]
[:> Button {:on-click (fn [e]
(.. e stopPropagation)
(click-fn e))}
(for [[{:node/keys [title] :as coref} total] coref-freqs]
[:> Button {:key (str "coref-count-" title)
:on-click (fn [e]
(.. e stopPropagation)
(click-fn e #{coref}))}
[:span title " " [:sub total]]]))
[:> Button {:on-click (fn [e]
(.. e stopPropagation)
(click-fn e))}
[:> Button {:on-click (fn [e]
(.. e stopPropagation)
(click-fn e #{}))}
total]]))


Expand Down Expand Up @@ -416,6 +425,7 @@
:show-editable-dom false
:linked-ref/open (or (false? linked-ref) initial-open)
:inline-refs/open false
:inline-refs/filter #{}
:inline-refs/states {}
:block/uid uid})
save-fn #(db/transact-state-for-uid (or original-uid uid) state)
Expand Down Expand Up @@ -502,10 +512,18 @@
[presence/inline-presence-el uid]

(when (and (> (count _refs) 0) (not= :block-embed? opts))
[block-refs-count-el _refs (fn [e]
(if (.. e -shiftKey)
(rf/dispatch [:right-sidebar/open-item uid])
(swap! state update :inline-refs/open not)))])]
[block-refs-count-el _refs (fn [e new-filter]
(let [{:inline-refs/keys [open filter]} @state]
(if (.. e -shiftKey)
;; shift-click always opens the block on the sidebar
(rf/dispatch [:right-sidebar/open-item uid])
(do
;; update the filter
(swap! state assoc :inline-refs/filter new-filter)

;; toggle open unless we're already open and just switching filters
(when-not (and open (not= filter new-filter))
(swap! state update :inline-refs/open not))))))])]

[autocomplete-search/inline-search-el block state]
[autocomplete-slash/slash-menu-el block state]
Expand Down

0 comments on commit c80cb16

Please sign in to comment.