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.edn
remotes/origin/allow-share-any-files
Darwis 2022-06-03 22:00:15 +08:00 committed by GitHub
parent 22780acbd9
commit 14dc2d1e30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 9 deletions

View File

@ -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?)
@ -3262,4 +3264,4 @@
{})])))] {})])))]
:else :else
(blocks-container blocks config))]) (blocks-container blocks config))])

View File

@ -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)
@ -130,4 +132,4 @@
k [:custom query']] k [:custom query']]
(pprint "inputs (post-resolution):" resolved-inputs) (pprint "inputs (post-resolution):" resolved-inputs)
(pprint "query-opts:" query-opts) (pprint "query-opts:" query-opts)
(apply react/q repo k query-opts query inputs)))) (apply react/q repo k query-opts query inputs))))

View File

@ -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))))
@ -29,4 +32,4 @@
[:div.results.mt-1 [:div.results.mt-1
[:pre.code [:pre.code
(let [result (eval-string code)] (let [result (eval-string code)]
(str result))]]]) (str result))]]])

View File

@ -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.
@ -205,7 +215,7 @@
;; ignore #+keyword: for parsing page references in orgmode ;; ignore #+keyword: for parsing page references in orgmode
;; :ignored-page-references-keywords #{"author" "startup"} ;; :ignored-page-references-keywords #{"author" "startup"}
;; Quick capture templates on mobile for recieving contents from other apps. ;; 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 ;; 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. ;; by received contents from other apps. Note: the {} cannot be omitted.