瀏覽代碼

feat: queries support dynamic variables too

Resolved #1340
Tienson Qin 4 年之前
父節點
當前提交
c1e7b4e5a5

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

@@ -47,7 +47,8 @@
             [reitit.frontend.easy :as rfe]
             [frontend.commands :as commands]
             [lambdaisland.glogi :as log]
-            [frontend.context.i18n :as i18n]))
+            [frontend.context.i18n :as i18n]
+            [frontend.template :as template]))
 
 ;; TODO: remove rum/with-context because it'll make reactive queries not working
 
@@ -841,12 +842,12 @@
                                 (block/macro-subs macro-content arguments)
                                 macro-content)
                 macro-content (when macro-content
-                                (editor-handler/resolve-dynamic-template! macro-content))]
+                                (template/resolve-dynamic-template! macro-content))]
             (render-macro config name arguments macro-content format))
 
           (when-let [macro-txt (macro->text name arguments)]
             (let [macro-txt (when macro-txt
-                              (editor-handler/resolve-dynamic-template! macro-txt))
+                              (template/resolve-dynamic-template! macro-txt))
                   format (get-in config [:block :block/format] :markdown)]
               (render-macro config name arguments macro-txt format))))))
 

+ 2 - 1
src/main/frontend/components/editor.cljs

@@ -32,6 +32,7 @@
             [medley.core :as medley]
             [cljs-drag-n-drop.core :as dnd]
             [frontend.text :as text]
+            [frontend.template :as template]
             ["/frontend/utils" :as utils]))
 
 (rum/defc commands < rum/reactive
@@ -228,7 +229,7 @@
                                        content (if (string/includes? (string/trim edit-content) "\n")
                                                  content
                                                  (text/remove-level-spaces content format))
-                                       content (editor-handler/resolve-dynamic-template! content)]
+                                       content (template/resolve-dynamic-template! content)]
                                    (state/set-editor-show-template-search! false)
                                    (editor-handler/insert-command! id
                                                                    content

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

@@ -14,7 +14,8 @@
             [medley.core :as medley]
             [clojure.walk :as walk]
             [clojure.core]
-            [clojure.set :as set]))
+            [clojure.set :as set]
+            [frontend.template :as template]))
 
 ;; Query fields:
 
@@ -377,8 +378,9 @@
 
 (defn query
   [repo query-string]
-  (when query-string
-    (let [{:keys [query sort-by blocks?]} (parse repo query-string)]
+  (when (string? query-string)
+    (let [query-string (template/resolve-dynamic-template! query-string)
+          {:keys [query sort-by blocks?]} (parse repo query-string)]
       (when query
         (let [query (query-wrapper query blocks?)]
           (query-custom/react-query repo

+ 0 - 28
src/main/frontend/handler/editor.cljs

@@ -2257,31 +2257,3 @@
         value (:block/content block)
         new-value (string/replace value full_text new-full-text)]
     (save-block-aux! block new-value (:block/format block) {})))
-
-(defn variable-rules
-  []
-  {"today" (util/format "[[%s]]" (date/today))
-   "yesterday" (util/format "[[%s]]" (date/yesterday))
-   "tomorrow" (util/format "[[%s]]" (date/tomorrow))
-   "time" (date/get-current-time)
-   "current page" (util/format "[[%s]]"
-                               (or (state/get-current-page)
-                                   (date/today)))})
-
-;; TODO: programmable
-;; context information, date, current page
-(defn resolve-dynamic-template!
-  [content]
-  (string/replace content #"<%([^%].*?)%>"
-                  (fn [[_ match]]
-                    (let [match (string/trim match)]
-                      (cond
-                       (string/blank? match)
-                       ""
-                       (get (variable-rules) (string/lower-case match))
-                       (get (variable-rules) (string/lower-case match))
-                       :else
-                       (if-let [nld (date/nld-parse match)]
-                         (let [date (tc/to-local-date-time nld)]
-                           (util/format "[[%s]]" (date/journal-name date)))
-                         match))))))

+ 34 - 0
src/main/frontend/template.cljs

@@ -0,0 +1,34 @@
+(ns frontend.template
+  (:require [frontend.util :as util :refer-macros [profile]]
+            [frontend.date :as date]
+            [clojure.string :as string]
+            [cljs-time.coerce :as tc]
+            [frontend.state :as state]))
+
+(defn- variable-rules
+  []
+  {"today" (util/format "[[%s]]" (date/today))
+   "yesterday" (util/format "[[%s]]" (date/yesterday))
+   "tomorrow" (util/format "[[%s]]" (date/tomorrow))
+   "time" (date/get-current-time)
+   "current page" (util/format "[[%s]]"
+                               (or (state/get-current-page)
+                                   (date/today)))})
+
+;; TODO: programmable
+;; context information, date, current page
+(defn resolve-dynamic-template!
+  [content]
+  (string/replace content #"<%([^%].*?)%>"
+                  (fn [[_ match]]
+                    (let [match (string/trim match)]
+                      (cond
+                       (string/blank? match)
+                       ""
+                       (get (variable-rules) (string/lower-case match))
+                       (get (variable-rules) (string/lower-case match))
+                       :else
+                       (if-let [nld (date/nld-parse match)]
+                         (let [date (tc/to-local-date-time nld)]
+                           (util/format "[[%s]]" (date/journal-name date)))
+                         match))))))