Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/main/frontend/components/query/result.cljs
Expand Up @@ -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?]
Expand Down Expand Up @@ -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)]
Expand Down
18 changes: 7 additions & 11 deletions src/main/frontend/components/query_table.cljs
Expand Up @@ -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
;; ========
Expand Down Expand Up @@ -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%"}
Expand All @@ -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]
Expand Down
22 changes: 17 additions & 5 deletions src/test/frontend/components/query/result_test.cljs
Expand Up @@ -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]
Expand All @@ -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}))
Expand All @@ -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")))