mirror of https://github.com/logseq/logseq
Predefined view and transform (#5479)
* 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.ednremotes/origin/allow-share-any-files
parent
22780acbd9
commit
14dc2d1e30
|
@ -2735,6 +2735,8 @@
|
||||||
[state config {:keys [title query view collapsed? children? breadcrumb-show? table-view?] :as q}]
|
[state config {:keys [title query view collapsed? children? breadcrumb-show? table-view?] :as q}]
|
||||||
(let [dsl-query? (:dsl-query? config)
|
(let [dsl-query? (:dsl-query? config)
|
||||||
query-atom (:query-atom state)
|
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))
|
current-block-uuid (or (:block/uuid (:block config))
|
||||||
(:block/uuid config))
|
(:block/uuid config))
|
||||||
current-block (db/entity [:block/uuid current-block-uuid])
|
current-block (db/entity [:block/uuid current-block-uuid])
|
||||||
|
@ -2755,7 +2757,7 @@
|
||||||
_ (when-let [query-result (:query-result config)]
|
_ (when-let [query-result (:query-result config)]
|
||||||
(let [result (remove (fn [b] (some? (get-in b [:block/properties :template]))) result)]
|
(let [result (remove (fn [b] (some? (get-in b [:block/properties :template]))) result)]
|
||||||
(reset! query-result 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))
|
only-blocks? (:block/uuid (first result))
|
||||||
blocks-grouped-by-page? (and (seq result)
|
blocks-grouped-by-page? (and (seq result)
|
||||||
(not not-grouped-by-page?)
|
(not not-grouped-by-page?)
|
||||||
|
|
|
@ -76,8 +76,10 @@
|
||||||
remove-nested-children-blocks
|
remove-nested-children-blocks
|
||||||
(model/sort-by-left-recursive)
|
(model/sort-by-left-recursive)
|
||||||
(model/with-pages)))
|
(model/with-pages)))
|
||||||
result)]
|
result)
|
||||||
(if-let [result-transform (:result-transform q)]
|
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))]
|
(if-let [f (sci/eval-string (pr-str result-transform))]
|
||||||
(try
|
(try
|
||||||
(sci/call-fn f result)
|
(sci/call-fn f result)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
(ns frontend.extensions.sci
|
(ns frontend.extensions.sci
|
||||||
(:require [sci.core :as sci]))
|
(:require [sci.core :as sci]
|
||||||
|
[frontend.util :as util]))
|
||||||
|
|
||||||
;; Some helpers
|
;; Some helpers
|
||||||
(def sum (partial apply +))
|
(def sum (partial apply +))
|
||||||
|
@ -13,7 +14,9 @@
|
||||||
(sci/eval-string s {:bindings {'sum sum
|
(sci/eval-string s {:bindings {'sum sum
|
||||||
'average average
|
'average average
|
||||||
'parseFloat js/parseFloat
|
'parseFloat js/parseFloat
|
||||||
'isNaN js/isNaN}})
|
'isNaN js/isNaN
|
||||||
|
'log js/console.log
|
||||||
|
'pprint util/pp-str}})
|
||||||
(catch js/Error e
|
(catch js/Error e
|
||||||
(println "Query: sci eval failed:")
|
(println "Query: sci eval failed:")
|
||||||
(js/console.error e))))
|
(js/console.error e))))
|
||||||
|
|
|
@ -108,6 +108,16 @@
|
||||||
;; Whether to show empty bullets for non-document mode (the default mode)
|
;; Whether to show empty bullets for non-document mode (the default mode)
|
||||||
:ui/show-empty-bullets? false
|
: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 app will show those queries in today's journal page,
|
||||||
;; the "NOW" query asks the tasks which need to be finished "now",
|
;; the "NOW" query asks the tasks which need to be finished "now",
|
||||||
;; the "NEXT" query asks the future tasks.
|
;; the "NEXT" query asks the future tasks.
|
||||||
|
|
Loading…
Reference in New Issue