Sfoglia il codice sorgente

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
Darwis 3 anni fa
parent
commit
14dc2d1e30

+ 4 - 2
src/main/frontend/components/block.cljs

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

+ 5 - 3
src/main/frontend/db/query_react.cljs

@@ -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)
@@ -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))))

+ 6 - 3
src/main/frontend/extensions/sci.cljs

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

+ 11 - 1
templates/config.edn

@@ -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.
@@ -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.