Browse Source

Enhance: Add :remove-block-children? query option for advanced queries and an advanced query :template fix (#9229)

* Add query option :remove-block-children? to remove

There are cases where we want to see block children e.g. using the
:current-block input. Still kept the existing behavior as most users
probably still want children removed. Also fix result count when
removing children

* Remove confusing :template block filtering

Fix #3260 and fix #9045

* enhance: apply tree filter only if the query result are blocks

---------

Co-authored-by: Tienson Qin <[email protected]>
Gabriel Horner 2 years ago
parent
commit
1067ffde38

+ 8 - 2
src/main/frontend/components/query/result.cljs

@@ -9,7 +9,8 @@
             [frontend.util :as util]
             [clojure.string :as string]
             [promesa.core :as p]
-            [rum.core :as rum]))
+            [rum.core :as rum]
+            [frontend.modules.outliner.tree :as tree]))
 
 (defn trigger-custom-query!
   [state *query-error *query-triggered?]
@@ -66,7 +67,12 @@
             ;; exclude the current one, otherwise it'll loop forever
             remove-blocks (if current-block-uuid [current-block-uuid] nil)
             transformed-query-result (when query-result
-                                       (db/custom-query-result-transform query-result remove-blocks q))
+                                       (let [result (db/custom-query-result-transform query-result remove-blocks q)]
+                                         (if (and query-result (coll? result) (:block/uuid (first result)))
+                                           (cond-> result
+                                            (get q :remove-block-children? true)
+                                            tree/filter-top-level-blocks)
+                                           result)))
             group-by-page? (get-group-by-page q options)
             result (if (and group-by-page? (:block/uuid (first transformed-query-result)))
                      (let [result (db-utils/group-by-page transformed-query-result)]

+ 7 - 11
src/main/frontend/components/query_table.cljs

@@ -12,8 +12,7 @@
             [frontend.format.block :as block]
             [medley.core :as medley]
             [rum.core :as rum]
-            [logseq.graph-parser.text :as text]
-            [frontend.modules.outliner.tree :as tree]))
+            [logseq.graph-parser.text :as text]))
 
 ;; Util fns
 ;; ========
@@ -158,20 +157,17 @@
   (rum/local false ::mouse-down?)
   [state config current-block result {:keys [page?]} map-inline page-cp ->elem inline-text]
   (when current-block
-    (let [result (tree/filter-top-level-blocks result)
-          select? (get state ::select?)
+    (let [select? (get state ::select?)
           *mouse-down? (::mouse-down? state)
-          ;; remove templates
-          result (remove (fn [b] (some? (get-in b [:block/properties :template]))) result)
-          result (if page? result (attach-clock-property result))
+          result' (if page? result (attach-clock-property result))
           clock-time-total (when-not page?
-                             (->> (map #(get-in % [:block/properties :clock-time] 0) result)
+                             (->> (map #(get-in % [:block/properties :clock-time] 0) result')
                                   (apply +)))
-          columns (get-columns current-block result {:page? page?})
+          columns (get-columns current-block result' {:page? page?})
           ;; Sort state needs to be in sync between final result and sortable title
           ;; as user needs to know if there result is sorted
           sort-state (get-sort-state current-block)
-          result' (sort-result result (assoc sort-state :page? page?))
+          sort-result (sort-result result (assoc sort-state :page? page?))
           property-separated-by-commas? (partial text/separated-by-commas? (state/get-config))]
       [:div.overflow-x-auto {:on-mouse-down (fn [e] (.stopPropagation e))
                              :style {:width "100%"}
@@ -186,7 +182,7 @@
                              (name column))]
               (sortable-title title column sort-state (:block/uuid current-block))))]]
         [:tbody
-         (for [row result']
+         (for [row sort-result]
            (let [format (:block/format row)]
              [:tr.cursor
               (for [column columns]

+ 17 - 5
src/test/frontend/components/query/result_test.cljs

@@ -14,10 +14,12 @@
     (binding [rum/*reactions* (volatile! #{})]
       (#'query-result/get-query-result {} {} (atom nil) (atom nil) current-block-uuid query {:table? table?}))))
 
-(deftest get-query-result
-  (let [result [{:block/uuid (random-uuid) :block/scheduled 20230418 :block/page {:db/id 1}}
-                {:block/uuid (random-uuid) :block/scheduled 20230415 :block/page {:db/id 1}}
-                {:block/uuid (random-uuid) :block/scheduled 20230417 :block/page {:db/id 1}}]
+(deftest get-query-result-with-transforms-and-grouping
+  (let [result (mapv
+                #(assoc % :block/page {:db/id 1} :block/parent {:db/id 2})
+                [{:block/uuid (random-uuid) :block/scheduled 20230418}
+                 {:block/uuid (random-uuid) :block/scheduled 20230415}
+                 {:block/uuid (random-uuid) :block/scheduled 20230417}])
         sorted-result (sort-by :block/scheduled result)]
     (testing "For list view"
       (are [query expected]
@@ -38,7 +40,7 @@
            ; User overrides transform to return grouped result
            {:result-transform '(partial sort-by :block/scheduled) :group-by-page? true}
            {{:db/id 1} sorted-result})
-      
+
       (testing "For table view"
         (are [query expected]
              (= expected (mock-get-query-result result query {:table? true}))
@@ -63,3 +65,13 @@
                                         {:table? false
                                          :current-block-uuid (:block/uuid current-block)})))
             "Current block is not included in results")))))
+
+(deftest get-query-result-with-remove-block-children-option
+  (let [result [{:db/id 1 :block/content "parent" :block/uuid 1}
+                {:db/id 2 :block/content "child" :block/uuid 2 :block/parent {:db/id 1}}]]
+    (is (= [{:db/id 1 :block/content "parent" :block/uuid 1}]
+           (mock-get-query-result result {:remove-block-children? true} {:table? true}))
+        "Removes children when :remove-block-children? is true")
+    (is (= result
+           (mock-get-query-result result {:remove-block-children? false} {:table? true}))
+        "Doesn't remove children when :remove-block-children? is false")))