diff --git a/src/cljc/athens/common_db.cljc b/src/cljc/athens/common_db.cljc index 2e5b2fd8ed..15977614e5 100644 --- a/src/cljc/athens/common_db.cljc +++ b/src/cljc/athens/common_db.cljc @@ -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] diff --git a/src/cljs/athens/db.cljs b/src/cljs/athens/db.cljs index 448b3038b9..9102086adb 100644 --- a/src/cljs/athens/db.cljs +++ b/src/cljs/athens/db.cljs @@ -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 diff --git a/src/cljs/athens/views/blocks/core.cljs b/src/cljs/athens/views/blocks/core.cljs index 902bf68930..88fe8719eb 100644 --- a/src/cljs/athens/views/blocks/core.cljs +++ b/src/cljs/athens/views/blocks/core.cljs @@ -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 @@ -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]])) @@ -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) @@ -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]