Skip to content

Commit

Permalink
Predefined view and transform (logseq#5479)
Browse files Browse the repository at this point in the history
* add pre-defined view in config.edn and use it in query :view

* expose js/console.log and pprint to use in query transform function or view function for easy debug query result.

* reusable pre-defined result-transform in cunfig.edn to use in query

* fix whitespace diff

* Add query namespace to predefined :view and :result-transform in config.edn
  • Loading branch information
darwis059 authored Jun 3, 2022
1 parent 22780ac commit 14dc2d1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/main/frontend/components/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2735,6 +2735,8 @@
[state config {:keys [title query view collapsed? children? breadcrumb-show? table-view?] :as q}]
(let [dsl-query? (:dsl-query? config)
query-atom (:query-atom state)
repo (state/get-current-repo)
view-fn (if (keyword? view) (state/sub [:config repo :query/views view]) view)
current-block-uuid (or (:block/uuid (:block config))
(:block/uuid config))
current-block (db/entity [:block/uuid current-block-uuid])
Expand All @@ -2755,7 +2757,7 @@
_ (when-let [query-result (:query-result config)]
(let [result (remove (fn [b] (some? (get-in b [:block/properties :template]))) result)]
(reset! query-result result)))
view-f (and view (sci/eval-string (pr-str view)))
view-f (and view-fn (sci/eval-string (pr-str view-fn)))
only-blocks? (:block/uuid (first result))
blocks-grouped-by-page? (and (seq result)
(not not-grouped-by-page?)
Expand Down Expand Up @@ -3262,4 +3264,4 @@
{})])))]

:else
(blocks-container blocks config))])
(blocks-container blocks config))])
8 changes: 5 additions & 3 deletions src/main/frontend/db/query_react.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@
remove-nested-children-blocks
(model/sort-by-left-recursive)
(model/with-pages)))
result)]
(if-let [result-transform (:result-transform q)]
result)
result-transform-fn (:result-transform q)
repo (state/get-current-repo)]
(if-let [result-transform (if (keyword? result-transform-fn) (state/sub [:config repo :query/result-transforms result-transform-fn]) result-transform-fn)]
(if-let [f (sci/eval-string (pr-str result-transform))]
(try
(sci/call-fn f result)
Expand Down Expand Up @@ -130,4 +132,4 @@
k [:custom query']]
(pprint "inputs (post-resolution):" resolved-inputs)
(pprint "query-opts:" query-opts)
(apply react/q repo k query-opts query inputs))))
(apply react/q repo k query-opts query inputs))))
9 changes: 6 additions & 3 deletions src/main/frontend/extensions/sci.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns frontend.extensions.sci
(:require [sci.core :as sci]))
(:require [sci.core :as sci]
[frontend.util :as util]))

;; Some helpers
(def sum (partial apply +))
Expand All @@ -13,7 +14,9 @@
(sci/eval-string s {:bindings {'sum sum
'average average
'parseFloat js/parseFloat
'isNaN js/isNaN}})
'isNaN js/isNaN
'log js/console.log
'pprint util/pp-str}})
(catch js/Error e
(println "Query: sci eval failed:")
(js/console.error e))))
Expand All @@ -29,4 +32,4 @@
[:div.results.mt-1
[:pre.code
(let [result (eval-string code)]
(str result))]]])
(str result))]]])
12 changes: 11 additions & 1 deletion templates/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@
;; Whether to show empty bullets for non-document mode (the default mode)
:ui/show-empty-bullets? false

;; Pre-defined :view function to use in Query
:query/views
{:pprint
(fn [r] [:pre.code (pprint r)])}

;; Pre-defined :result-transform function to use in Query
:query/result-transforms
{:sort-by-priority
(fn [result] (sort-by (fn [h] (get h :block/priority "Z")) result))}

;; The app will show those queries in today's journal page,
;; the "NOW" query asks the tasks which need to be finished "now",
;; the "NEXT" query asks the future tasks.
Expand Down Expand Up @@ -205,7 +215,7 @@

;; ignore #+keyword: for parsing page references in orgmode
;; :ignored-page-references-keywords #{"author" "startup"}

;; Quick capture templates on mobile for recieving contents from other apps.
;; Each template contains three elements {time}, {text} and {url}, which can be auto-expanded
;; by received contents from other apps. Note: the {} cannot be omitted.
Expand Down

0 comments on commit 14dc2d1

Please sign in to comment.