Parcourir la source

Merge pull request #5982 from sawhney17/improve-references-filter

enhance(filters): Improve reference filters
Gabriel Horner il y a 3 ans
Parent
commit
538b2aaab2

+ 57 - 41
src/main/frontend/components/reference.cljs

@@ -3,44 +3,60 @@
             [frontend.components.block :as block]
             [frontend.components.block :as block]
             [frontend.components.content :as content]
             [frontend.components.content :as content]
             [frontend.components.editor :as editor]
             [frontend.components.editor :as editor]
+            [frontend.context.i18n :refer [t]]
             [frontend.date :as date]
             [frontend.date :as date]
             [frontend.db :as db]
             [frontend.db :as db]
-            [frontend.db.model :as model-db]
             [frontend.db-mixins :as db-mixins]
             [frontend.db-mixins :as db-mixins]
+            [frontend.db.model :as model-db]
             [frontend.handler.block :as block-handler]
             [frontend.handler.block :as block-handler]
             [frontend.handler.page :as page-handler]
             [frontend.handler.page :as page-handler]
+            [frontend.search :as search]
             [frontend.state :as state]
             [frontend.state :as state]
             [frontend.ui :as ui]
             [frontend.ui :as ui]
             [frontend.util :as util]
             [frontend.util :as util]
             [rum.core :as rum]))
             [rum.core :as rum]))
 
 
-(rum/defc filter-dialog-inner < rum/reactive
-  [filters-atom _close-fn references page-name]
-  [:div.filters
-   [:div.sm:flex.sm:items-start
-    [: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
-     (ui/icon "filter" {:style {:fontSize 20}})]
-    [:div.mt-3.text-center.sm:mt-0.sm:ml-4.sm:text-left
-     [:h3#modal-headline.text-lg.leading-6.font-medium "Filter"]
-     [:span.text-xs
-      "Click to include and shift-click to exclude. Click again to remove."]]]
-   (when (seq references)
-     (let [filters (rum/react filters-atom)]
-       [:div.mt-5.sm:mt-4.sm:flex.sm.gap-1.flex-wrap
-        (for [reference references]
-          (let [lc-reference (string/lower-case reference)
-                filtered (get filters lc-reference)
-                color (condp = filtered
-                        true "text-green-400"
-                        false "text-red-400"
-                        nil)]
-            [:button.border.rounded.px-1.mb-1.mr-1 {:key reference :class color :style {:border-color "currentColor"}
-                                                    :on-click (fn [e]
-                                                                (swap! filters-atom #(if (nil? (get filters lc-reference))
-                                                                                       (assoc % lc-reference (not (.-shiftKey e)))
-                                                                                       (dissoc % lc-reference)))
-                                                                (page-handler/save-filter! page-name @filters-atom))}
-             reference]))]))])
+(defn- frequencies-sort
+  [references]
+  (sort-by second #(> %1 %2) references))
+
+(rum/defcs filter-dialog-inner < rum/reactive (rum/local "" ::filterSearch)
+  [state filters-atom _close-fn references page-name]
+  (let [filter-search (get state ::filterSearch)
+        filtered-references  (frequencies-sort
+                              (if (= @filter-search "")
+                                references
+                                (search/fuzzy-search references @filter-search :limit 500 :extract-fn first)))]
+    [:div.filters
+     [:div.sm:flex.sm:items-start
+      [: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
+       (ui/icon "filter" {:style {:fontSize 20}})]
+      [:div.mt-3.text-center.sm:mt-0.sm:ml-4.sm:text-left.pb-2
+       [:h3#modal-headline.text-lg.leading-6.font-medium "Filter"]
+       [:span.text-xs
+        "Click to include and shift-click to exclude. Click again to remove."]]]
+     [:div.cp__filters-input-panel.flex (ui/icon "search") [:input.cp__filters-input.w-full
+                                                            {:placeholder (t :linked-references/filter-search)
+                                                             :auto-focus true
+                                                             :on-change (fn [e]
+                                                                          (reset! filter-search (util/evalue e)))}]]
+     (when (seq filtered-references)
+       (let [filters (rum/react filters-atom)]
+         [:div.mt-5.sm:mt-4.sm:flex.sm.gap-1.flex-wrap
+          (for [[ref-name ref-count] filtered-references]
+            (let [lc-reference (string/lower-case ref-name)
+                  filtered (get filters lc-reference)
+                  color (condp = filtered
+                          true "text-green-400"
+                          false "text-red-400"
+                          nil)]
+              [:button.border.rounded.px-1.mb-1.mr-1.select-none {:key ref-name :class color :style {:border-color "currentColor"}
+                                                                  :on-click (fn [e]
+                                                                              (swap! filters-atom #(if (nil? (get filters lc-reference))
+                                                                                                     (assoc % lc-reference (not (.-shiftKey e)))
+                                                                                                     (dissoc % lc-reference)))
+                                                                              (page-handler/save-filter! page-name @filters-atom))}
+               ref-name [:sub " " ref-count]]))]))]))
 
 
 (defn filter-dialog
 (defn filter-dialog
   [filters-atom references page-name]
   [filters-atom references page-name]
@@ -118,13 +134,13 @@
                 :on-mouse-down (fn [e] (util/stop-propagation e))
                 :on-mouse-down (fn [e] (util/stop-propagation e))
                 :on-click (fn []
                 :on-click (fn []
                             (let [ref-blocks (if block-id
                             (let [ref-blocks (if block-id
-                                               (db/get-block-referenced-blocks block-id)
-                                               (db/get-page-referenced-blocks page-name))
-                                  ref-pages (map (comp :block/original-name first) ref-blocks)
+                                               (db/get-block-referenced-blocks block-id {:filter? true})
+                                               (db/get-page-referenced-blocks page-name {:filter? true}))
                                   references (db/get-page-linked-refs-refed-pages repo page-name)
                                   references (db/get-page-linked-refs-refed-pages repo page-name)
-                                  references (->> (concat ref-pages references)
-                                                  (remove nil?)
-                                                  (distinct))]
+                                  references (->>
+                                              (concat ref-blocks references)
+                                              (remove nil?)
+                                              (frequencies))]
                               (state/set-modal! (filter-dialog filters-atom references page-name)
                               (state/set-modal! (filter-dialog filters-atom references page-name)
                                                 {:center? true})))}
                                                 {:center? true})))}
                (ui/icon "filter" {:class (cond
                (ui/icon "filter" {:class (cond
@@ -147,8 +163,8 @@
                                    (update-vals #(map first %))))
                                    (update-vals #(map first %))))
                      filtered-ref-blocks (block-handler/filter-blocks repo ref-blocks filters true)
                      filtered-ref-blocks (block-handler/filter-blocks repo ref-blocks filters true)
                      n-ref (apply +
                      n-ref (apply +
-                             (for [[_ rfs] filtered-ref-blocks]
-                               (count rfs)))]
+                                  (for [[_ rfs] filtered-ref-blocks]
+                                    (count rfs)))]
                  (reset! *n-ref n-ref)
                  (reset! *n-ref n-ref)
                  [:div.references-blocks
                  [:div.references-blocks
                   (let [ref-hiccup (block/->hiccup filtered-ref-blocks
                   (let [ref-hiccup (block/->hiccup filtered-ref-blocks
@@ -184,10 +200,10 @@
      (fn [state]
      (fn [state]
        (reset! (second (:rum/args state))
        (reset! (second (:rum/args state))
                (apply +
                (apply +
-                 (for [[_ rfs]
-                       (db/get-page-unlinked-references
-                        (first (:rum/args state)))]
-                   (count rfs))))
+                      (for [[_ rfs]
+                            (db/get-page-unlinked-references
+                             (first (:rum/args state)))]
+                        (count rfs))))
        (render-fn state)))}
        (render-fn state)))}
   [state page-name _n-ref]
   [state page-name _n-ref]
   (let [ref-blocks (db/get-page-unlinked-references page-name)]
   (let [ref-blocks (db/get-page-unlinked-references page-name)]
@@ -217,4 +233,4 @@
               "Unlinked References")]
               "Unlinked References")]
            (fn [] (unlinked-references-aux page-name n-ref))
            (fn [] (unlinked-references-aux page-name n-ref))
            {:default-collapsed? true
            {:default-collapsed? true
-            :title-trigger? true})]]))))
+            :title-trigger? true})]]))))

+ 12 - 0
src/main/frontend/components/reference.css

@@ -0,0 +1,12 @@
+.cp__filters-input {
+  background-color: var(--ls-primary-background-color);
+  padding: 0.5rem;
+  outline: none;
+}
+
+.cp__filters-input-panel {
+  background-color: var(--ls-primary-background-color);
+  border-radius: 0.25rem;
+  padding-left: 0.5rem;
+  align-items: center;
+}

+ 242 - 233
src/main/frontend/db/model.cljs

@@ -62,12 +62,12 @@
     (when (conn/get-db repo)
     (when (conn/get-db repo)
       (->
       (->
        (react/q repo [:frontend.db.react/block id] {}
        (react/q repo [:frontend.db.react/block id] {}
-         '[:find [(pull ?block ?block-attrs) ...]
-           :in $ ?id ?block-attrs
-           :where
-           [?block :block/uuid ?id]]
-         id
-         block-attrs)
+                '[:find [(pull ?block ?block-attrs) ...]
+                  :in $ ?id ?block-attrs
+                  :where
+                  [?block :block/uuid ?id]]
+                id
+                block-attrs)
        react
        react
        first))))
        first))))
 
 
@@ -96,7 +96,7 @@
          [?page :block/tags ?e]
          [?page :block/tags ?e]
          [?e :block/name ?tag]
          [?e :block/name ?tag]
          [?page :block/name ?page-name]]
          [?page :block/name ?page-name]]
-    (conn/get-db repo)))
+       (conn/get-db repo)))
 
 
 (defn get-all-namespace-relation
 (defn get-all-namespace-relation
   [repo]
   [repo]
@@ -105,7 +105,7 @@
          [?page :block/name ?page-name]
          [?page :block/name ?page-name]
          [?page :block/namespace ?e]
          [?page :block/namespace ?e]
          [?e :block/name ?parent]]
          [?e :block/name ?parent]]
-    (conn/get-db repo)))
+       (conn/get-db repo)))
 
 
 (defn get-pages
 (defn get-pages
   [repo]
   [repo]
@@ -120,10 +120,10 @@
 (defn get-all-pages
 (defn get-all-pages
   [repo]
   [repo]
   (d/q
   (d/q
-    '[:find [(pull ?page [*]) ...]
-      :where
-      [?page :block/name]]
-    (conn/get-db repo)))
+   '[:find [(pull ?page [*]) ...]
+     :where
+     [?page :block/name]]
+   (conn/get-db repo)))
 
 
 (defn get-page-alias
 (defn get-page-alias
   [repo page-name]
   [repo page-name]
@@ -164,13 +164,13 @@
   [repo]
   [repo]
   (when-let [db (conn/get-db repo)]
   (when-let [db (conn/get-db repo)]
     (->> (d/q
     (->> (d/q
-           '[:find ?path
+          '[:find ?path
              ;; ?modified-at
              ;; ?modified-at
-             :where
-             [?file :file/path ?path]
+            :where
+            [?file :file/path ?path]
              ;; [?file :file/last-modified-at ?modified-at]
              ;; [?file :file/last-modified-at ?modified-at]
-             ]
-           db)
+            ]
+          db)
          (seq)
          (seq)
          ;; (sort-by last)
          ;; (sort-by last)
          (reverse))))
          (reverse))))
@@ -221,9 +221,9 @@
   (when (and repo path last-modified-at)
   (when (and repo path last-modified-at)
     (when-let [conn (conn/get-db repo false)]
     (when-let [conn (conn/get-db repo false)]
       (d/transact! conn
       (d/transact! conn
-        [{:file/path path
-          :file/last-modified-at last-modified-at}]
-        {:skip-refresh? true}))))
+                   [{:file/path path
+                     :file/last-modified-at last-modified-at}]
+                   {:skip-refresh? true}))))
 
 
 (defn get-file-last-modified-at
 (defn get-file-last-modified-at
   [repo path]
   [repo path]
@@ -243,10 +243,10 @@
   (when-let [db (conn/get-db repo)]
   (when-let [db (conn/get-db repo)]
     (->>
     (->>
      (d/q
      (d/q
-       '[:find (pull ?file [*])
-         :where
-         [?file :file/path]]
-       db)
+      '[:find (pull ?file [*])
+        :where
+        [?file :file/path]]
+      db)
      (flatten))))
      (flatten))))
 
 
 (defn get-file
 (defn get-file
@@ -450,8 +450,8 @@
       (if-let [parent-sibling (get-by-parent-&-left db
       (if-let [parent-sibling (get-by-parent-&-left db
                                                     (:db/id (:block/parent parent))
                                                     (:db/id (:block/parent parent))
                                                     (:db/id parent))]
                                                     (:db/id parent))]
-       parent-sibling
-       (get-next-outdented-block db (:db/id parent))))))
+        parent-sibling
+        (get-next-outdented-block db (:db/id parent))))))
 
 
 (defn get-block-parent
 (defn get-block-parent
   ([block-id]
   ([block-id]
@@ -722,52 +722,52 @@
                             scoped-block-id nil}}]
                             scoped-block-id nil}}]
    (when block-id
    (when block-id
      (assert (integer? block-id) (str "wrong block-id: " block-id))
      (assert (integer? block-id) (str "wrong block-id: " block-id))
-   (let [entity (db-utils/entity repo-url block-id)
-         page? (some? (:block/name entity))
-         page-entity (if page? entity (:block/page entity))
-         page-id (:db/id page-entity)
-         bare-page-map {:db/id page-id
-                        :block/name (:block/name page-entity)
-                        :block/original-name (:block/original-name page-entity)
-                        :block/journal-day (:block/journal-day page-entity)}
-         query-key (if page?
-                     :frontend.db.react/page-blocks
-                     :frontend.db.react/block-and-children)]
-     (some->
-      (react/q repo-url [query-key block-id]
-        {:use-cache? use-cache?
-         :query-fn (fn [db tx-report result]
-                     (let [tx-data (:tx-data tx-report)
-                           refs (some->> (filter #(= :block/refs (:a %)) tx-data)
-                                         (map :v))
-                           tx-block-ids (distinct (-> (map :e tx-data)
-                                                      (concat refs)))
-                           [tx-id->block cached-id->block] (when (and tx-report result)
-                                                             (let [blocks (->> (db-utils/pull-many repo-url pull-keys tx-block-ids)
-                                                                               (remove nil?))]
-                                                               [(zipmap (mapv :db/id blocks) blocks)
-                                                                (zipmap (mapv :db/id @result) @result)]))
-                           limit (if (and result @result)
-                                   (max (+ (count @result) 5) limit)
-                                   limit)
-                           outliner-op (get-in tx-report [:tx-meta :outliner-op])
-                           blocks (build-paginated-blocks-from-cache repo-url tx-report result outliner-op page-id block-id tx-block-ids scoped-block-id)
-                           blocks (or blocks
-                                      (get-paginated-blocks-no-cache (conn/get-db repo-url) block-id {:limit limit
-                                                                                                      :include-start? (not page?)
-                                                                                                      :scoped-block-id scoped-block-id}))
-                           block-eids (map :db/id blocks)
-                           blocks (if (and (seq tx-id->block)
-                                           (not (contains? #{:move-blocks} outliner-op)))
-                                    (map (fn [id]
-                                           (or (get tx-id->block id)
-                                               (get cached-id->block id)
-                                               (db-utils/pull repo-url pull-keys id))) block-eids)
-                                    (db-utils/pull-many repo-url pull-keys block-eids))
-                           blocks (remove (fn [b] (nil? (:block/content b))) blocks)]
-                       (map (fn [b] (assoc b :block/page bare-page-map)) blocks)))}
-        nil)
-      react)))))
+     (let [entity (db-utils/entity repo-url block-id)
+           page? (some? (:block/name entity))
+           page-entity (if page? entity (:block/page entity))
+           page-id (:db/id page-entity)
+           bare-page-map {:db/id page-id
+                          :block/name (:block/name page-entity)
+                          :block/original-name (:block/original-name page-entity)
+                          :block/journal-day (:block/journal-day page-entity)}
+           query-key (if page?
+                       :frontend.db.react/page-blocks
+                       :frontend.db.react/block-and-children)]
+       (some->
+        (react/q repo-url [query-key block-id]
+                 {:use-cache? use-cache?
+                  :query-fn (fn [db tx-report result]
+                              (let [tx-data (:tx-data tx-report)
+                                    refs (some->> (filter #(= :block/refs (:a %)) tx-data)
+                                                  (map :v))
+                                    tx-block-ids (distinct (-> (map :e tx-data)
+                                                               (concat refs)))
+                                    [tx-id->block cached-id->block] (when (and tx-report result)
+                                                                      (let [blocks (->> (db-utils/pull-many repo-url pull-keys tx-block-ids)
+                                                                                        (remove nil?))]
+                                                                        [(zipmap (mapv :db/id blocks) blocks)
+                                                                         (zipmap (mapv :db/id @result) @result)]))
+                                    limit (if (and result @result)
+                                            (max (+ (count @result) 5) limit)
+                                            limit)
+                                    outliner-op (get-in tx-report [:tx-meta :outliner-op])
+                                    blocks (build-paginated-blocks-from-cache repo-url tx-report result outliner-op page-id block-id tx-block-ids scoped-block-id)
+                                    blocks (or blocks
+                                               (get-paginated-blocks-no-cache (conn/get-db repo-url) block-id {:limit limit
+                                                                                                               :include-start? (not page?)
+                                                                                                               :scoped-block-id scoped-block-id}))
+                                    block-eids (map :db/id blocks)
+                                    blocks (if (and (seq tx-id->block)
+                                                    (not (contains? #{:move-blocks} outliner-op)))
+                                             (map (fn [id]
+                                                    (or (get tx-id->block id)
+                                                        (get cached-id->block id)
+                                                        (db-utils/pull repo-url pull-keys id))) block-eids)
+                                             (db-utils/pull-many repo-url pull-keys block-eids))
+                                    blocks (remove (fn [b] (nil? (:block/content b))) blocks)]
+                                (map (fn [b] (assoc b :block/page bare-page-map)) blocks)))}
+                 nil)
+        react)))))
 
 
 (defn get-page-blocks-no-cache
 (defn get-page-blocks-no-cache
   ([page]
   ([page]
@@ -863,13 +863,13 @@
   [repo block-uuid]
   [repo block-uuid]
   (when-let [db (conn/get-db repo)]
   (when-let [db (conn/get-db repo)]
     (-> (d/q
     (-> (d/q
-          '[:find [(pull ?b [*]) ...]
-            :in $ ?parent-id
-            :where
-            [?parent :block/uuid ?parent-id]
-            [?b :block/parent ?parent]]
-          db
-          block-uuid)
+         '[:find [(pull ?b [*]) ...]
+           :in $ ?parent-id
+           :where
+           [?parent :block/uuid ?parent-id]
+           [?b :block/parent ?parent]]
+         db
+         block-uuid)
         (sort-by-left (db-utils/entity [:block/uuid block-uuid])))))
         (sort-by-left (db-utils/entity [:block/uuid block-uuid])))))
 
 
 (defn sub-block-direct-children
 (defn sub-block-direct-children
@@ -877,12 +877,12 @@
   [repo block-uuid]
   [repo block-uuid]
   (when-let [db (conn/get-db repo)]
   (when-let [db (conn/get-db repo)]
     (-> (react/q repo [:frontend.db.react/block-direct-children block-uuid] {}
     (-> (react/q repo [:frontend.db.react/block-direct-children block-uuid] {}
-          '[:find [(pull ?b [*]) ...]
-            :in $ ?parent-id
-            :where
-            [?parent :block/uuid ?parent-id]
-            [?b :block/parent ?parent]]
-          block-uuid)
+                 '[:find [(pull ?b [*]) ...]
+                   :in $ ?parent-id
+                   :where
+                   [?parent :block/uuid ?parent-id]
+                   [?b :block/parent ?parent]]
+                 block-uuid)
         react
         react
         (sort-by-left (db-utils/entity [:block/uuid block-uuid])))))
         (sort-by-left (db-utils/entity [:block/uuid block-uuid])))))
 
 
@@ -904,13 +904,13 @@
 (defn get-block-and-children
 (defn get-block-and-children
   [repo block-uuid]
   [repo block-uuid]
   (some-> (d/q
   (some-> (d/q
-            '[:find [(pull ?block ?block-attrs) ...]
-              :in $ ?id ?block-attrs
-              :where
-              [?block :block/uuid ?id]]
-            (conn/get-db repo)
-            block-uuid
-            block-attrs)
+           '[:find [(pull ?block ?block-attrs) ...]
+             :in $ ?id ?block-attrs
+             :where
+             [?block :block/uuid ?id]]
+           (conn/get-db repo)
+           block-uuid
+           block-attrs)
           first
           first
           flatten-tree))
           flatten-tree))
 
 
@@ -1028,14 +1028,14 @@
            today (db-utils/date->int (js/Date.))]
            today (db-utils/date->int (js/Date.))]
        (->>
        (->>
         (react/q repo-url [:frontend.db.react/journals] {:use-cache? false}
         (react/q repo-url [:frontend.db.react/journals] {:use-cache? false}
-          '[:find [(pull ?page [*]) ...]
-            :in $ ?today
-            :where
-            [?page :block/name ?page-name]
-            [?page :block/journal? true]
-            [?page :block/journal-day ?journal-day]
-            [(<= ?journal-day ?today)]]
-          today)
+                 '[:find [(pull ?page [*]) ...]
+                   :in $ ?today
+                   :where
+                   [?page :block/name ?page-name]
+                   [?page :block/journal? true]
+                   [?page :block/journal-day ?journal-day]
+                   [(<= ?journal-day ?today)]]
+                 today)
         (react)
         (react)
         (sort-by :block/journal-day)
         (sort-by :block/journal-day)
         (reverse)
         (reverse)
@@ -1049,15 +1049,15 @@
           pages (page-alias-set repo page)
           pages (page-alias-set repo page)
           page-id (:db/id (db-utils/entity [:block/name page-name]))
           page-id (:db/id (db-utils/entity [:block/name page-name]))
           ref-pages (d/q
           ref-pages (d/q
-                      '[:find [?ref-page-name ...]
-                        :in $ ?pages
-                        :where
-                        [(untuple ?pages) [?page ...]]
-                        [?block :block/page ?page]
-                        [?block :block/refs ?ref-page]
-                        [?ref-page :block/name ?ref-page-name]]
-                      db
-                      pages)]
+                     '[:find [?ref-page-name ...]
+                       :in $ ?pages
+                       :where
+                       [(untuple ?pages) [?page ...]]
+                       [?block :block/page ?page]
+                       [?block :block/refs ?ref-page]
+                       [?ref-page :block/name ?ref-page-name]]
+                     db
+                     pages)]
       (mapv (fn [page] [page (get-page-alias repo page)]) ref-pages))))
       (mapv (fn [page] [page (get-page-alias repo page)]) ref-pages))))
 
 
 (defn get-page-linked-refs-refed-pages
 (defn get-page-linked-refs-refed-pages
@@ -1136,33 +1136,40 @@
 
 
 (defn get-page-referenced-blocks
 (defn get-page-referenced-blocks
   ([page]
   ([page]
-   (get-page-referenced-blocks (state/get-current-repo) page))
-  ([repo page]
+   (get-page-referenced-blocks (state/get-current-repo) page {:filter? false}))
+  ([page options]
+   (get-page-referenced-blocks (state/get-current-repo) page options))
+  ([repo page options]
    (when repo
    (when repo
      (when (conn/get-db repo)
      (when (conn/get-db repo)
        (let [page-id (:db/id (db-utils/entity [:block/name (util/safe-page-name-sanity-lc page)]))
        (let [page-id (:db/id (db-utils/entity [:block/name (util/safe-page-name-sanity-lc page)]))
              pages (page-alias-set repo page)
              pages (page-alias-set repo page)
              aliases (set/difference pages #{page-id})
              aliases (set/difference pages #{page-id})
              query-result (react/q repo
              query-result (react/q repo
-                            [:frontend.db.react/page<-blocks-or-block<-blocks page-id]
-                            {}
-                            '[:find [(pull ?block ?block-attrs) ...]
-                              :in $ [?ref-page ...] ?block-attrs
-                              :where
-                              [?block :block/refs ?ref-page]]
-                            pages
-                            (butlast block-attrs))
-             result (->> query-result
-                         react
-                         (remove (fn [block]
-                                   (= page-id (:db/id (:block/page block)))))
-                         (sort-by-left-recursive)
-                         db-utils/group-by-page
-                         (map (fn [[k blocks]]
-                                (let [k (if (contains? aliases (:db/id k))
-                                          (assoc k :block/alias? true)
-                                          k)]
-                                  [k blocks]))))]
+                                   [:frontend.db.react/page<-blocks-or-block<-blocks page-id]
+                                   {}
+                                   '[:find [(pull ?block ?block-attrs) ...]
+                                     :in $ [?ref-page ...] ?block-attrs
+                                     :where
+                                     [?block :block/refs ?ref-page]]
+                                   pages
+                                   (butlast block-attrs))
+             result (if (not (options :filter?)) (->> query-result
+                                           react
+                                           (remove (fn [block]
+                                                     (= page-id (:db/id (:block/page block)))))
+                                           (sort-by-left-recursive)
+                                           db-utils/group-by-page
+                                           (map (fn [[k blocks]]
+                                                  (let [k (if (contains? aliases (:db/id k))
+                                                            (assoc k :block/alias? true)
+                                                            k)]
+                                                    [k blocks]))))
+
+                        (map (comp :block/original-name :block/page) (->> query-result
+                                                                          react
+                                                                          (remove (fn [block]
+                                                                                    (= page-id (:db/id (:block/page block))))) (sort-by-left-recursive))))]
          result)))))
          result)))))
 
 
 (defn get-page-referenced-blocks-ids
 (defn get-page-referenced-blocks-ids
@@ -1174,12 +1181,12 @@
      (when-let [db (conn/get-db repo)]
      (when-let [db (conn/get-db repo)]
        (let [pages (page-alias-set repo page)]
        (let [pages (page-alias-set repo page)]
          (d/q
          (d/q
-           '[:find ?block
-             :in $ [?ref-page ...]
-             :where
-             [?block :block/refs ?ref-page]]
-           db
-           pages))))))
+          '[:find ?block
+            :in $ [?ref-page ...]
+            :where
+            [?block :block/refs ?ref-page]]
+          db
+          pages))))))
 
 
 (defn get-date-scheduled-or-deadlines
 (defn get-date-scheduled-or-deadlines
   [journal-title]
   [journal-title]
@@ -1187,24 +1194,24 @@
     (let [future-days (state/get-scheduled-future-days)]
     (let [future-days (state/get-scheduled-future-days)]
       (when-let [repo (state/get-current-repo)]
       (when-let [repo (state/get-current-repo)]
         (->> (react/q repo [:custom :scheduled-deadline journal-title] {}
         (->> (react/q repo [:custom :scheduled-deadline journal-title] {}
-               '[:find [(pull ?block ?block-attrs) ...]
-                 :in $ ?day ?future ?block-attrs
-                 :where
-                 (or
-                  [?block :block/scheduled ?d]
-                  [?block :block/deadline ?d])
-                 [(get-else $ ?block :block/repeated? false) ?repeated]
-                 [(get-else $ ?block :block/marker "NIL") ?marker]
-                 [(not= ?marker "DONE")]
-                 [(not= ?marker "CANCELED")]
-                 [(not= ?marker "CANCELLED")]
-                 [(<= ?d ?future)]
-                 (or-join [?repeated ?d ?day]
-                          [(true? ?repeated)]
-                          [(>= ?d ?day)])]
-               date
-               (+ date future-days)
-               block-attrs)
+                      '[:find [(pull ?block ?block-attrs) ...]
+                        :in $ ?day ?future ?block-attrs
+                        :where
+                        (or
+                         [?block :block/scheduled ?d]
+                         [?block :block/deadline ?d])
+                        [(get-else $ ?block :block/repeated? false) ?repeated]
+                        [(get-else $ ?block :block/marker "NIL") ?marker]
+                        [(not= ?marker "DONE")]
+                        [(not= ?marker "CANCELED")]
+                        [(not= ?marker "CANCELLED")]
+                        [(<= ?d ?future)]
+                        (or-join [?repeated ?d ?day]
+                                 [(true? ?repeated)]
+                                 [(>= ?d ?day)])]
+                      date
+                      (+ date future-days)
+                      block-attrs)
              react
              react
              (sort-by-left-recursive)
              (sort-by-left-recursive)
              db-utils/group-by-page)))))
              db-utils/group-by-page)))))
@@ -1244,23 +1251,25 @@
 ;; TODO: Replace recursive queries with datoms index implementation
 ;; TODO: Replace recursive queries with datoms index implementation
 ;; see https://github.com/tonsky/datascript/issues/130#issuecomment-169520434
 ;; see https://github.com/tonsky/datascript/issues/130#issuecomment-169520434
 (defn get-block-referenced-blocks
 (defn get-block-referenced-blocks
-  [block-uuid]
-  (when-let [repo (state/get-current-repo)]
-    (when (conn/get-db repo)
-      (let [block (db-utils/entity [:block/uuid block-uuid])]
-        (->> (react/q repo [:frontend.db.react/page<-blocks-or-block<-blocks
-                            (:db/id block)]
-                      {:use-cache? false}
-                      '[:find [(pull ?ref-block ?block-attrs) ...]
-                        :in $ ?block-uuid ?block-attrs
-                        :where
-                        [?block :block/uuid ?block-uuid]
-                        [?ref-block :block/refs ?block]]
-                      block-uuid
-                      block-attrs)
-             react
-            (sort-by-left-recursive)
-            db-utils/group-by-page)))))
+  ([block-uuid & options]
+   (when-let [repo (state/get-current-repo)]
+     (when (conn/get-db repo)
+       (let [block (db-utils/entity [:block/uuid block-uuid])
+             query-result (->> (react/q repo [:frontend.db.react/page<-blocks-or-block<-blocks
+                                              (:db/id block)]
+                                        {:use-cache? false}
+                                        '[:find [(pull ?ref-block ?block-attrs) ...]
+                                          :in $ ?block-uuid ?block-attrs
+                                          :where
+                                          [?block :block/uuid ?block-uuid]
+                                          [?ref-block :block/refs ?block]]
+                                        block-uuid
+                                        block-attrs)
+                               react
+                               (sort-by-left-recursive))]
+         (if (options :filter?)
+           (map (comp :block/original-name :block/page) query-result)
+           (db-utils/group-by-page query-result)))))))
 
 
 (defn get-block-referenced-blocks-ids
 (defn get-block-referenced-blocks-ids
   [block-uuid]
   [block-uuid]
@@ -1268,13 +1277,13 @@
     (let [block (db-utils/entity [:block/uuid block-uuid])]
     (let [block (db-utils/entity [:block/uuid block-uuid])]
       (->> (react/q repo [:frontend.db.react/block<-block-ids
       (->> (react/q repo [:frontend.db.react/block<-block-ids
                           (:db/id block)] {}
                           (:db/id block)] {}
-             '[:find ?ref-block
-               :in $ ?block-uuid ?block-attrs
-               :where
-               [?block :block/uuid ?block-uuid]
-               [?ref-block :block/refs ?block]]
-             block-uuid
-             block-attrs)
+                    '[:find ?ref-block
+                      :in $ ?block-uuid ?block-attrs
+                      :where
+                      [?block :block/uuid ?block-uuid]
+                      [?ref-block :block/refs ?block]]
+                    block-uuid
+                    block-attrs)
            react))))
            react))))
 
 
 (defn get-referenced-blocks-ids
 (defn get-referenced-blocks-ids
@@ -1303,26 +1312,26 @@
 (defn get-public-false-pages
 (defn get-public-false-pages
   [db]
   [db]
   (-> (d/q
   (-> (d/q
-        '[:find ?p
-          :where
-          [?p :block/name]
-          [?p :block/properties ?properties]
-          [(get ?properties :public) ?pub]
-          [(= false ?pub)]]
-        db)
+       '[:find ?p
+         :where
+         [?p :block/name]
+         [?p :block/properties ?properties]
+         [(get ?properties :public) ?pub]
+         [(= false ?pub)]]
+       db)
       (db-utils/seq-flatten)))
       (db-utils/seq-flatten)))
 
 
 (defn get-public-false-block-ids
 (defn get-public-false-block-ids
   [db]
   [db]
   (-> (d/q
   (-> (d/q
-        '[:find ?b
-          :where
-          [?p :block/name]
-          [?p :block/properties ?properties]
-          [(get ?properties :public) ?pub]
-          [(= false ?pub)]
-          [?b :block/page ?p]]
-        db)
+       '[:find ?b
+         :where
+         [?p :block/name]
+         [?p :block/properties ?properties]
+         [(get ?properties :public) ?pub]
+         [(= false ?pub)]
+         [?b :block/page ?p]]
+       db)
       (db-utils/seq-flatten)))
       (db-utils/seq-flatten)))
 
 
 (defn get-all-templates
 (defn get-all-templates
@@ -1379,14 +1388,14 @@
   [name]
   [name]
   (when (string? name)
   (when (string? name)
     (->> (d/q
     (->> (d/q
-           '[:find [(pull ?b [*]) ...]
-             :in $ ?name
-             :where
-             [?b :block/properties ?p]
-             [(get ?p :template) ?t]
-             [(= ?t ?name)]]
-           (conn/get-db)
-           name)
+          '[:find [(pull ?b [*]) ...]
+            :in $ ?name
+            :where
+            [?b :block/properties ?p]
+            [(get ?p :template) ?t]
+            [(= ?t ?name)]]
+          (conn/get-db)
+          name)
          (sort-by :block/name)
          (sort-by :block/name)
          (first))))
          (first))))
 
 
@@ -1539,16 +1548,16 @@
   (assert (string? namespace))
   (assert (string? namespace))
   (let [namespace (util/page-name-sanity-lc namespace)]
   (let [namespace (util/page-name-sanity-lc namespace)]
     (d/q
     (d/q
-      '[:find [(pull ?c [:db/id :block/name :block/original-name
-                         :block/namespace
-                         {:block/file [:db/id :file/path]}]) ...]
-        :in $ % ?namespace
-        :where
-        [?p :block/name ?namespace]
-        (namespace ?p ?c)]
-      (conn/get-db repo)
-      rules
-      namespace)))
+     '[:find [(pull ?c [:db/id :block/name :block/original-name
+                        :block/namespace
+                        {:block/file [:db/id :file/path]}]) ...]
+       :in $ % ?namespace
+       :where
+       [?p :block/name ?namespace]
+       (namespace ?p ?c)]
+     (conn/get-db repo)
+     rules
+     namespace)))
 
 
 (defn- tree [flat-col root]
 (defn- tree [flat-col root]
   (let [sort-fn #(sort-by :block/name %)
   (let [sort-fn #(sort-by :block/name %)
@@ -1557,7 +1566,7 @@
                              (map (fn [m]
                              (map (fn [m]
                                     (assoc m :namespace/children
                                     (assoc m :namespace/children
                                            (sort-fn (namespace-children {:db/id (:db/id m)}))))
                                            (sort-fn (namespace-children {:db/id (:db/id m)}))))
-                               (sort-fn (get children parent-id))))]
+                                  (sort-fn (get children parent-id))))]
     (namespace-children root)))
     (namespace-children root)))
 
 
 (defn get-namespace-hierarchy
 (defn get-namespace-hierarchy
@@ -1594,33 +1603,33 @@
 
 
 (defn get-orphaned-pages
 (defn get-orphaned-pages
   [{:keys [repo pages empty-ref-f]
   [{:keys [repo pages empty-ref-f]
-          :or {repo (state/get-current-repo)
-               empty-ref-f (fn [page] (zero? (count (:block/_refs page))))}}]
+    :or {repo (state/get-current-repo)
+         empty-ref-f (fn [page] (zero? (count (:block/_refs page))))}}]
   (let [pages (->> (or pages (get-pages repo))
   (let [pages (->> (or pages (get-pages repo))
                    (remove nil?))
                    (remove nil?))
         built-in-pages (set (map string/lower-case default-db/built-in-pages-names))
         built-in-pages (set (map string/lower-case default-db/built-in-pages-names))
         orphaned-pages (->>
         orphaned-pages (->>
                         (map
                         (map
-                          (fn [page]
-                            (let [name (util/page-name-sanity-lc page)]
-                              (when-let [page (db-utils/entity [:block/name name])]
-                                (and
-                                 (empty-ref-f page)
-                                 (or
-                                  (page-empty? repo (:db/id page))
-                                  (let [first-child (first (:block/_left page))
-                                        children (:block/_page page)]
-                                    (and
-                                     first-child
-                                     (= 1 (count children))
-                                     (contains? #{"" "-" "*"} (string/trim (:block/content first-child))))))
-                                 (not (contains? built-in-pages name))
-                                 (not (:block/_namespace page))
+                         (fn [page]
+                           (let [name (util/page-name-sanity-lc page)]
+                             (when-let [page (db-utils/entity [:block/name name])]
+                               (and
+                                (empty-ref-f page)
+                                (or
+                                 (page-empty? repo (:db/id page))
+                                 (let [first-child (first (:block/_left page))
+                                       children (:block/_page page)]
+                                   (and
+                                    first-child
+                                    (= 1 (count children))
+                                    (contains? #{"" "-" "*"} (string/trim (:block/content first-child))))))
+                                (not (contains? built-in-pages name))
+                                (not (:block/_namespace page))
                                  ;; a/b/c might be deleted but a/b/c/d still exists (for backward compatibility)
                                  ;; a/b/c might be deleted but a/b/c/d still exists (for backward compatibility)
-                                 (not (and (string/includes? name "/")
-                                           (not (:block/journal? page))))
-                                 page))))
-                          pages)
+                                (not (and (string/includes? name "/")
+                                          (not (:block/journal? page))))
+                                page))))
+                         pages)
                         (remove false?)
                         (remove false?)
                         (remove nil?))]
                         (remove nil?))]
     orphaned-pages))
     orphaned-pages))

+ 1 - 0
src/main/frontend/dicts.cljc

@@ -116,6 +116,7 @@
         :page/created-at "Created At"
         :page/created-at "Created At"
         :page/updated-at "Updated At"
         :page/updated-at "Updated At"
         :page/backlinks "Back Links"
         :page/backlinks "Back Links"
+        :linked-references/filter-search "Search in linked pages"
         :editor/block-search "Search for a block"
         :editor/block-search "Search for a block"
         :editor/image-uploading "Uploading"
         :editor/image-uploading "Uploading"
         :draw/invalid-file "Could not load this invalid excalidraw file"
         :draw/invalid-file "Could not load this invalid excalidraw file"