Sfoglia il codice sorgente

Make resolve-input nbb compatible

- Brought back Xd-{before,after}-ms and tested it
- Move it to its own ns in graph-parser
- Updated nbb-logseq in deps/
- Made db-utils alias consistent
- Organized resolve-input
Gabriel Horner 2 anni fa
parent
commit
17f51c7b77

+ 2 - 0
.clj-kondo/config.edn

@@ -42,6 +42,7 @@
              frontend.db.query-dsl query-dsl
              frontend.db.react react
              frontend.db.query-react query-react
+             frontend.db.util db-utils
              frontend.diff diff
              frontend.encrypt encrypt
              frontend.extensions.sci sci
@@ -94,6 +95,7 @@
              logseq.graph-parser.config gp-config
              logseq.graph-parser.util.page-ref page-ref
              logseq.graph-parser.util.block-ref block-ref
+             logseq.graph-parser.util.db db-util
              logseq.graph-parser.date-time-util date-time-util
              medley.core medley
              promesa.core p}}

+ 1 - 1
deps/db/package.json

@@ -3,6 +3,6 @@
   "version": "1.0.0",
   "private": true,
   "devDependencies": {
-    "@logseq/nbb-logseq": "^0.7.133"
+    "@logseq/nbb-logseq": "^1.1.157"
   }
 }

+ 9 - 37
deps/db/yarn.lock

@@ -2,42 +2,14 @@
 # yarn lockfile v1
 
 
-"@logseq/nbb-logseq@^0.7.133":
-  version "0.7.133"
-  resolved "https://registry.yarnpkg.com/@logseq/nbb-logseq/-/nbb-logseq-0.7.133.tgz#793492c6f0bc3089f394795052ca0b8503018161"
-  integrity sha512-eraxs2j1HT4RjxYCB51Rlb3KBx5oihIKoFueB1QHZYnMOwPMLIn6iMzHvyGyEGweqp422PcdDXfK3Nl4iTw/wA==
+"@logseq/nbb-logseq@^1.1.157":
+  version "1.1.157"
+  resolved "https://registry.yarnpkg.com/@logseq/nbb-logseq/-/nbb-logseq-1.1.157.tgz#337be95156e5b22caf5533663ae8a5a79cc43fbd"
+  integrity sha512-cuutsKZDdg850qa6HquOTKKZ9WpWUjSozRdrfvI/2WIbAv2MVQKPQYtB03K55OW9i3D1K0jAwDM0xzGI2lWyFQ==
   dependencies:
-    import-meta-resolve "^1.1.1"
+    import-meta-resolve "^2.1.0"
 
-builtins@^4.0.0:
-  version "4.1.0"
-  resolved "https://registry.yarnpkg.com/builtins/-/builtins-4.1.0.tgz#1edd016dd91ce771a1ed6fc3b2b71fb918953250"
-  integrity sha512-1bPRZQtmKaO6h7qV1YHXNtr6nCK28k0Zo95KM4dXfILcZZwoHJBN1m3lfLv9LPkcOZlrSr+J1bzMaZFO98Yq0w==
-  dependencies:
-    semver "^7.0.0"
-
-import-meta-resolve@^1.1.1:
-  version "1.1.1"
-  resolved "https://registry.yarnpkg.com/import-meta-resolve/-/import-meta-resolve-1.1.1.tgz#244fd542fd1fae73550d4f8b3cde3bba1d7b2b18"
-  integrity sha512-JiTuIvVyPaUg11eTrNDx5bgQ/yMKMZffc7YSjvQeSMXy58DO2SQ8BtAf3xteZvmzvjYh14wnqNjL8XVeDy2o9A==
-  dependencies:
-    builtins "^4.0.0"
-
-lru-cache@^6.0.0:
-  version "6.0.0"
-  resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"
-  integrity sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==
-  dependencies:
-    yallist "^4.0.0"
-
-semver@^7.0.0:
-  version "7.3.7"
-  resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.7.tgz#12c5b649afdbf9049707796e22a4028814ce523f"
-  integrity sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==
-  dependencies:
-    lru-cache "^6.0.0"
-
-yallist@^4.0.0:
-  version "4.0.0"
-  resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
-  integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==
+import-meta-resolve@^2.1.0:
+  version "2.2.1"
+  resolved "https://registry.yarnpkg.com/import-meta-resolve/-/import-meta-resolve-2.2.1.tgz#80fdeddbc15d7f3992c37425023ffb4aca7cb583"
+  integrity sha512-C6lLL7EJPY44kBvA80gq4uMsVFw5x3oSKfuMl1cuZ2RkI5+UJqQXgn+6hlUew0y4ig7Ypt4CObAAIzU53Nfpuw==

+ 2 - 0
deps/graph-parser/.carve/ignore

@@ -36,3 +36,5 @@ logseq.graph-parser.whiteboard/page-block->tldr-page
 logseq.graph-parser/get-blocks-to-delete
 ;; Future use & be unified with markdown colon
 logseq.graph-parser.property/colons-org
+;; API
+logseq.graph-parser.util.db/resolve-input

+ 2 - 2
deps/graph-parser/package.json

@@ -3,10 +3,10 @@
   "version": "1.0.0",
   "private": true,
   "devDependencies": {
-    "@logseq/nbb-logseq": "^0.7.133"
+    "@logseq/nbb-logseq": "^1.1.157"
   },
   "dependencies": {
-    "mldoc": "^1.4.9"
+    "mldoc": "^1.5.1"
   },
   "scripts": {
     "test": "nbb-logseq -cp src:test:../db/src -m logseq.graph-parser.nbb-test-runner/run-tests"

+ 31 - 0
deps/graph-parser/src/logseq/graph_parser/date_time_util.cljs

@@ -49,3 +49,34 @@
   [day date-formatter]
   (when day
     (format (tf/parse (tf/formatter "yyyyMMdd") (str day)) date-formatter)))
+
+(defn- get-weekday
+  [date]
+  (.toLocaleString date "en-us" (clj->js {:weekday "long"})))
+
+(defn- get-date
+  ([]
+   (get-date (js/Date.)))
+  ([date]
+   {:year (.getFullYear date)
+    :month (inc (.getMonth date))
+    :day (.getDate date)
+    :weekday (get-weekday date)}))
+
+(defn year-month-day-padded
+  ([]
+   (year-month-day-padded (get-date)))
+  ([date]
+   (let [{:keys [year month day]} date]
+     {:year year
+      :month (gp-util/zero-pad month)
+      :day (gp-util/zero-pad day)})))
+
+(defn ymd
+  ([]
+   (ymd (js/Date.)))
+  ([date]
+   (ymd date "/"))
+  ([date sep]
+   (let [{:keys [year month day]} (year-month-day-padded (get-date date))]
+     (str year sep month sep day))))

+ 80 - 0
deps/graph-parser/src/logseq/graph_parser/util/db.cljs

@@ -0,0 +1,80 @@
+(ns logseq.graph-parser.util.db
+  "Db util fns that are useful for the frontend and nbb-logseq. This may be used
+  by the graph-parser soon but if not, it should be in its own library"
+  (:require [cljs-time.core :as t]
+            [logseq.graph-parser.date-time-util :as date-time-util]
+            [logseq.graph-parser.util.page-ref :as page-ref]
+            [datascript.core :as d]
+            [clojure.string :as string]))
+
+(defn date-at-local-ms
+  "Returns the milliseconds representation of the provided time, in the local timezone.
+For example, if you run this function at 10pm EDT in the EDT timezone on May 31st,
+it will return 1622433600000, which is equivalent to Mon May 31 2021 00 :00:00."
+  ([hours mins secs millisecs]
+   (date-at-local-ms (.now js/Date) hours mins secs millisecs))
+  ([date hours mins secs millisecs]
+   (.setHours (js/Date. date) hours mins secs millisecs)))
+
+(defn date->int
+  "Given a date object, returns its journal page integer"
+  [date]
+  (parse-long
+   (string/replace (date-time-util/ymd date) "/" "")))
+
+(defn resolve-input
+  "Main fn for resolving advanced query :inputs"
+  [db input {:keys [current-block-uuid current-page-fn]
+             :or {current-page-fn (constantly nil)}}]
+  (cond
+    ;; page and block inputs
+    (= :current-page input)
+    (some-> (current-page-fn) string/lower-case)
+    (and current-block-uuid (= :current-block input))
+    (:db/id (d/entity db [:block/uuid current-block-uuid]))
+    (and current-block-uuid (= :parent-block input))
+    (:db/id (:block/parent (d/entity db [:block/uuid current-block-uuid])))
+
+    ;; journal date inputs
+    (= :today input)
+    (date->int (t/today))
+    (= :yesterday input)
+    (date->int (t/minus (t/today) (t/days 1)))
+    (= :tomorrow input)
+    (date->int (t/plus (t/today) (t/days 1)))
+    ;; e.g. :3d-before
+    (and (keyword? input)
+         (re-find #"^\d+d(-before)?$" (name input)))
+    (let [input (name input)
+          days (parse-long (re-find #"^\d+" input))]
+      (date->int (t/minus (t/today) (t/days days))))
+    ;; e.g. :3d-after
+    (and (keyword? input)
+         (re-find #"^\d+d(-after)?$" (name input)))
+    (let [input (name input)
+          days (parse-long (re-find #"^\d+" input))]
+      (date->int (t/plus (t/today) (t/days days))))
+
+    ;; timestamp inputs
+    (= :right-now-ms input) (date-time-util/time-ms)
+    (= :start-of-today-ms input) (date-at-local-ms 0 0 0 0)
+    (= :end-of-today-ms input) (date-at-local-ms 24 0 0 0)
+    ;; e.g. :3d-before-ms
+    (and (keyword? input)
+         (re-find #"^\d+d-before-ms$" (name input)))
+    (let [input (name input)
+          days (parse-long (re-find #"^\d+" input))]
+      (date-at-local-ms (t/minus (t/today) (t/days days)) 0 0 0 0))
+    ;; e.g. :3d-after-ms
+    (and (keyword? input)
+         (re-find #"^\d+d-after-ms$" (name input)))
+    (let [input (name input)
+          days (parse-long (re-find #"^\d+" input))]
+      (date-at-local-ms (t/plus (t/today) (t/days days)) 24 0 0 0))
+
+    (and (string? input) (page-ref/page-ref? input))
+    (-> (page-ref/get-page-name input)
+        (string/lower-case))
+
+    :else
+    input))

+ 13 - 41
deps/graph-parser/yarn.lock

@@ -2,12 +2,12 @@
 # yarn lockfile v1
 
 
-"@logseq/nbb-logseq@^0.7.133":
-  version "0.7.133"
-  resolved "https://registry.yarnpkg.com/@logseq/nbb-logseq/-/nbb-logseq-0.7.133.tgz#793492c6f0bc3089f394795052ca0b8503018161"
-  integrity sha512-eraxs2j1HT4RjxYCB51Rlb3KBx5oihIKoFueB1QHZYnMOwPMLIn6iMzHvyGyEGweqp422PcdDXfK3Nl4iTw/wA==
+"@logseq/nbb-logseq@^1.1.157":
+  version "1.1.157"
+  resolved "https://registry.yarnpkg.com/@logseq/nbb-logseq/-/nbb-logseq-1.1.157.tgz#337be95156e5b22caf5533663ae8a5a79cc43fbd"
+  integrity sha512-cuutsKZDdg850qa6HquOTKKZ9WpWUjSozRdrfvI/2WIbAv2MVQKPQYtB03K55OW9i3D1K0jAwDM0xzGI2lWyFQ==
   dependencies:
-    import-meta-resolve "^1.1.1"
+    import-meta-resolve "^2.1.0"
 
 ansi-regex@^2.0.0:
   version "2.1.1"
@@ -19,13 +19,6 @@ ansi-regex@^3.0.0:
   resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-3.0.1.tgz#123d6479e92ad45ad897d4054e3c7ca7db4944e1"
   integrity sha512-+O9Jct8wf++lXxxFc4hc8LsjaSq0HFzzL7cVsw8pRDIPdjKD2mT4ytDZlLuSBZ4cLKZFXIrMGO7DbQCtMJJMKw==
 
-builtins@^4.0.0:
-  version "4.1.0"
-  resolved "https://registry.yarnpkg.com/builtins/-/builtins-4.1.0.tgz#1edd016dd91ce771a1ed6fc3b2b71fb918953250"
-  integrity sha512-1bPRZQtmKaO6h7qV1YHXNtr6nCK28k0Zo95KM4dXfILcZZwoHJBN1m3lfLv9LPkcOZlrSr+J1bzMaZFO98Yq0w==
-  dependencies:
-    semver "^7.0.0"
-
 camelcase@^5.0.0:
   version "5.3.1"
   resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-5.3.1.tgz#e3c9b31569e106811df242f715725a1f4c494320"
@@ -100,12 +93,10 @@ get-stream@^4.0.0:
   dependencies:
     pump "^3.0.0"
 
-import-meta-resolve@^1.1.1:
-  version "1.1.1"
-  resolved "https://registry.yarnpkg.com/import-meta-resolve/-/import-meta-resolve-1.1.1.tgz#244fd542fd1fae73550d4f8b3cde3bba1d7b2b18"
-  integrity sha512-JiTuIvVyPaUg11eTrNDx5bgQ/yMKMZffc7YSjvQeSMXy58DO2SQ8BtAf3xteZvmzvjYh14wnqNjL8XVeDy2o9A==
-  dependencies:
-    builtins "^4.0.0"
+import-meta-resolve@^2.1.0:
+  version "2.2.1"
+  resolved "https://registry.yarnpkg.com/import-meta-resolve/-/import-meta-resolve-2.2.1.tgz#80fdeddbc15d7f3992c37425023ffb4aca7cb583"
+  integrity sha512-C6lLL7EJPY44kBvA80gq4uMsVFw5x3oSKfuMl1cuZ2RkI5+UJqQXgn+6hlUew0y4ig7Ypt4CObAAIzU53Nfpuw==
 
 invert-kv@^2.0.0:
   version "2.0.0"
@@ -149,13 +140,6 @@ locate-path@^3.0.0:
     p-locate "^3.0.0"
     path-exists "^3.0.0"
 
-lru-cache@^6.0.0:
-  version "6.0.0"
-  resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"
-  integrity sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==
-  dependencies:
-    yallist "^4.0.0"
-
 map-age-cleaner@^0.1.1:
   version "0.1.3"
   resolved "https://registry.yarnpkg.com/map-age-cleaner/-/map-age-cleaner-0.1.3.tgz#7d583a7306434c055fe474b0f45078e6e1b4b92a"
@@ -177,10 +161,10 @@ mimic-fn@^2.0.0:
   resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-2.1.0.tgz#7ed2c2ccccaf84d3ffcb7a69b57711fc2083401b"
   integrity sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==
 
-mldoc@^1.4.9:
-  version "1.4.9"
-  resolved "https://registry.yarnpkg.com/mldoc/-/mldoc-1.4.9.tgz#a61d93e98f19bf4ec6303810cc3573c5e14eab44"
-  integrity sha512-rroc1tWJnNm2n33MYk+sWgLjGWmP93Am+6zvpBe0BGEyccHQT3AxHHv8fNpF7HVvT7YPtU/dtHWTh6nE2n7vPA==
+mldoc@^1.5.1:
+  version "1.5.3"
+  resolved "https://registry.yarnpkg.com/mldoc/-/mldoc-1.5.3.tgz#98d5bb276ac6908d72e1c58c27916e488ef9d395"
+  integrity sha512-hkI3PtjBHhbZqTr1U5/A8TIrIzg9DGZzCMLrfzePAdM+97GNeZijmPqUQXWEAyEQsDPnkipMoQZsBXxhnwzfJA==
   dependencies:
     yargs "^12.0.2"
 
@@ -284,13 +268,6 @@ semver@^5.5.0:
   resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
   integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==
 
-semver@^7.0.0:
-  version "7.3.7"
-  resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.7.tgz#12c5b649afdbf9049707796e22a4028814ce523f"
-  integrity sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==
-  dependencies:
-    lru-cache "^6.0.0"
-
 set-blocking@^2.0.0:
   version "2.0.0"
   resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"
@@ -379,11 +356,6 @@ wrappy@1:
   resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.3.tgz#b5f259c82cd6e336921efd7bfd8bf560de9eeedf"
   integrity sha512-JKhqTOwSrqNA1NY5lSztJ1GrBiUodLMmIZuLiDaMRJ+itFd+ABVE8XBjOvIWL+rSqNDC74LCSFmlb/U4UZ4hJQ==
 
-yallist@^4.0.0:
-  version "4.0.0"
-  resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
-  integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==
-
 yargs-parser@^11.1.1:
   version "11.1.1"
   resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-11.1.1.tgz#879a0865973bca9f6bab5cbdf3b1c67ec7d3bcf4"

+ 2 - 2
src/main/frontend/commands.cljs

@@ -4,7 +4,7 @@
             [frontend.config :as config]
             [frontend.date :as date]
             [frontend.db :as db]
-            [frontend.db.utils :as db-util]
+            [frontend.db.utils :as db-utils]
             [frontend.handler.draw :as draw]
             [frontend.handler.notification :as notification]
             [frontend.handler.plugin :as plugin-handler]
@@ -676,6 +676,6 @@
 
 (defn exec-plugin-simple-command!
   [pid {:keys [block-id] :as cmd} action]
-  (let [format (and block-id (:block/format (db-util/pull [:block/uuid block-id])))
+  (let [format (and block-id (:block/format (db-utils/pull [:block/uuid block-id])))
         inputs (vector (conj action (assoc cmd :pid pid)))]
     (handle-steps inputs format)))

+ 0 - 32
src/main/frontend/date.cljs

@@ -7,7 +7,6 @@
             [cljs-time.format :as tf]
             [cljs-time.local :as tl]
             [frontend.state :as state]
-            [frontend.util :as util]
             [logseq.graph-parser.util :as gp-util]
             [logseq.graph-parser.date-time-util :as date-time-util]
             [goog.object :as gobj]
@@ -83,28 +82,6 @@
 (defn get-date-time-string-4 []
   (tf/unparse custom-formatter-4 (tl/local-now)))
 
-(defn get-weekday
-  [date]
-  (.toLocaleString date "en-us" (clj->js {:weekday "long"})))
-
-(defn get-date
-  ([]
-   (get-date (js/Date.)))
-  ([date]
-   {:year (.getFullYear date)
-    :month (inc (.getMonth date))
-    :day (.getDate date)
-    :weekday (get-weekday date)}))
-
-(defn year-month-day-padded
-  ([]
-   (year-month-day-padded (get-date)))
-  ([date]
-   (let [{:keys [year month day]} date]
-     {:year year
-      :month (util/zero-pad month)
-      :day (util/zero-pad day)})))
-
 (defn journal-name
   ([]
    (journal-name (tl/local-now)))
@@ -138,15 +115,6 @@
   []
   (journal-name (t/minus (t/today) (t/days 1))))
 
-(defn ymd
-  ([]
-   (ymd (js/Date.)))
-  ([date]
-   (ymd date "/"))
-  ([date sep]
-   (let [{:keys [year month day]} (year-month-day-padded (get-date date))]
-     (str year sep month sep day))))
-
 (defn get-local-date
   []
   (let [date (js/Date.)

+ 1 - 1
src/main/frontend/db.cljs

@@ -30,7 +30,7 @@
   remove-conn!]
 
  [frontend.db.utils
-  date->int db->json db->edn-str db->string get-max-tx-id get-tx-id
+  db->json db->edn-str db->string get-max-tx-id get-tx-id
   group-by-page seq-flatten
   string->db
 

+ 3 - 2
src/main/frontend/db/model.cljs

@@ -20,6 +20,7 @@
             [logseq.db.schema :as db-schema]
             [logseq.graph-parser.config :as gp-config]
             [logseq.graph-parser.text :as text]
+            [logseq.graph-parser.util.db :as db-util]
             [logseq.graph-parser.util :as gp-util]))
 
 ;; lazy loading
@@ -1048,7 +1049,7 @@ independent of format as format specific heading characters are stripped"
 
 (defn get-journals-length
   []
-  (let [today (db-utils/date->int (js/Date.))]
+  (let [today (db-util/date->int (js/Date.))]
     (d/q '[:find (count ?page) .
            :in $ ?today
            :where
@@ -1065,7 +1066,7 @@ independent of format as format specific heading characters are stripped"
    (when (conn/get-db repo-url)
      (let [date (js/Date.)
            _ (.setDate date (- (.getDate date) (dec n)))
-           today (db-utils/date->int (js/Date.))]
+           today (db-util/date->int (js/Date.))]
        (->>
         (react/q repo-url [:frontend.db.react/journals] {:use-cache? false}
                  '[:find [(pull ?page [*]) ...]

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

@@ -9,7 +9,7 @@
             [frontend.date :as date]
             [frontend.db.model :as model]
             [frontend.db.query-react :as query-react]
-            [frontend.db.utils :as db-utils]
+            [logseq.graph-parser.util.db :as db-util]
             [logseq.db.rules :as rules]
             [frontend.template :as template]
             [logseq.graph-parser.text :as text]
@@ -48,13 +48,13 @@
   (let [input (string/lower-case (name input))]
     (cond
       (= "today" input)
-      (db-utils/date->int (t/today))
+      (db-util/date->int (t/today))
 
       (= "yesterday" input)
-      (db-utils/date->int (t/yesterday))
+      (db-util/date->int (t/yesterday))
 
       (= "tomorrow" input)
-      (db-utils/date->int (t/plus (t/today) (t/days 1)))
+      (db-util/date->int (t/plus (t/today) (t/days 1)))
 
       (page-ref/page-ref? input)
       (let [input (-> (page-ref/get-page-name input)
@@ -71,7 +71,7 @@
                  "m" t/months
                  "w" t/weeks
                  t/days)]
-        (db-utils/date->int (t/plus (t/today) (tf duration)))))))
+        (db-util/date->int (t/plus (t/today) (tf duration)))))))
 
 (defn- ->timestamp [input]
   (let [input (string/lower-case (name input))]

+ 19 - 46
src/main/frontend/db/query_react.cljs

@@ -1,62 +1,33 @@
 (ns frontend.db.query-react
   "Custom queries."
-  (:require [cljs-time.core :as t]
-            [clojure.string :as string]
+  (:require [clojure.string :as string]
             [clojure.walk :as walk]
             [frontend.config :as config]
+            [frontend.db.conn :as conn]
             [frontend.db.model :as model]
             [frontend.db.react :as react]
-            [frontend.db.utils :as db-utils :refer [date->int]]
+            [frontend.db.utils :as db-utils]
             [frontend.debug :as debug]
             [frontend.extensions.sci :as sci]
             [frontend.state :as state]
+            [logseq.graph-parser.util.db :as db-util]
             [logseq.graph-parser.util.page-ref :as page-ref]
             [frontend.util :as util]
             [frontend.date :as date]
             [lambdaisland.glogi :as log]))
 
 (defn resolve-input
-  ([input]
-   (resolve-input input nil))
-  ([input current-block-uuid]
-   (cond
-     (= :right-now-ms input) (util/time-ms)
-     (= :start-of-today-ms input) (util/today-at-local-ms 0 0 0 0)
-     (= :end-of-today-ms input) (util/today-at-local-ms 24 0 0 0)
-
-     (= :today input)
-     (date->int (t/today))
-     (= :yesterday input)
-     (date->int (t/minus (t/today) (t/days 1)))
-     (= :tomorrow input)
-     (date->int (t/plus (t/today) (t/days 1)))
-     (= :current-page input)
-     (some-> (or (state/get-current-page)
-                 (:page (state/get-default-home))
-                 (date/today)) string/lower-case)
-     (and current-block-uuid (= :current-block input))
-     (:db/id (db-utils/entity [:block/uuid current-block-uuid]))
-     (and current-block-uuid (= :parent-block input))
-     (:db/id (model/get-block-parent current-block-uuid))
-     ;; e.g. :3d-before
-     (and (keyword? input)
-          (re-find #"^\d+d(-before)?$" (name input)))
-     (let [input (name input)
-           days (parse-long (re-find #"^\d+" input))]
-       (date->int (t/minus (t/today) (t/days days))))
-     ;; e.g. :3d-after
-     (and (keyword? input)
-          (re-find #"^\d+d(-after)?$" (name input)))
-     (let [input (name input)
-           days (parse-long (re-find #"^\d+" input))]
-       (date->int (t/plus (t/today) (t/days days))))
-
-     (and (string? input) (page-ref/page-ref? input))
-     (-> (page-ref/get-page-name input)
-         (string/lower-case))
-
-     :else
-     input)))
+  "Wrapper around db-util/resolve-input which provides editor-specific state"
+  ([db input]
+   (resolve-input db input {}))
+  ([db input opts]
+   (db-util/resolve-input db
+                          input
+                          (merge {:current-page-fn (fn []
+                                                     (or (state/get-current-page)
+                                                         (:page (state/get-default-home))
+                                                         (date/today)))}
+                                 opts))))
 
 (defn custom-query-result-transform
   [query-result remove-blocks q]
@@ -127,11 +98,13 @@
     (pprint query')
     (let [query (resolve-query query)
           current-block-uuid (:current-block-uuid query-opts)
-          resolved-inputs (mapv #(resolve-input % current-block-uuid) inputs)
+          repo (or repo (state/get-current-repo))
+          db (conn/get-db repo)
+          resolved-inputs (mapv #(resolve-input db % {:current-block-uuid current-block-uuid})
+                                inputs)
           inputs (cond-> resolved-inputs
                          rules
                          (conj rules))
-          repo (or repo (state/get-current-repo))
           k [:custom (or (:query-string query') query')]]
       (pprint "inputs (post-resolution):" resolved-inputs)
       (pprint "query-opts:" query-opts)

+ 0 - 7
src/main/frontend/db/utils.cljs

@@ -2,9 +2,7 @@
   "Some utils are required by other namespace in frontend.db package."
   (:require [datascript.core :as d]
             [frontend.state :as state]
-            [clojure.string :as string]
             [datascript.transit :as dt]
-            [frontend.date :as date]
             [frontend.db.conn :as conn]
             [frontend.config :as config]
             [logseq.graph-parser.util :as gp-util]))
@@ -43,11 +41,6 @@
   [db]
   (:max-tx db))
 
-(defn date->int
-  [date]
-  (parse-long
-   (string/replace (date/ymd date) "/" "")))
-
 (defn entity
   "This function will return nil if passed `id-or-lookup-ref` is an integer and
   the entity doesn't exist in db (Datascript will return {:id id}).

+ 0 - 7
src/main/frontend/util.cljc

@@ -762,13 +762,6 @@
   []
   #?(:cljs (tc/to-long (t/now))))
 
-;; Returns the milliseconds representation of the provided time, in the local timezone.
-;; For example, if you run this function at 10pm EDT in the EDT timezone on May 31st,
-;; it will return 1622433600000, which is equivalent to Mon May 31 2021 00 :00:00.
-#?(:cljs
-   (defn today-at-local-ms [hours mins secs millisecs]
-     (.setHours (js/Date. (.now js/Date)) hours mins secs millisecs)))
-
 (defn d
   [k f]
   (let [result (atom nil)]

+ 1 - 1
src/main/logseq/api.cljs

@@ -877,7 +877,7 @@
       (let [query           (cljs.reader/read-string query)
             resolved-inputs (map #(cond
                                     (string? %)
-                                    (some-> % (cljs.reader/read-string) (query-react/resolve-input))
+                                    (some->> % (cljs.reader/read-string) (query-react/resolve-input db))
 
                                     (fn? %)
                                     (fn [& args]

+ 74 - 4
src/test/frontend/db/query_react_test.cljs

@@ -1,24 +1,28 @@
 (ns frontend.db.query-react-test
   (:require [cljs.test :refer [deftest is use-fixtures]]
+            [cljs-time.core :as t]
             [clojure.pprint]
+            [clojure.string :as string]
             [frontend.state :as state]
+            [logseq.graph-parser.util.db :as db-util]
             [frontend.test.helper :as test-helper :refer [load-test-files]]
             [frontend.db.query-custom :as query-custom]
             [frontend.db.utils :as db-utils]
-            [frontend.db.react :as react]))
+            [frontend.db.react :as react]
+            [goog.string :as gstring]))
 
 (use-fixtures :each {:before test-helper/start-test-db!
                      :after test-helper/destroy-test-db!})
 
 (defn- custom-query
-  "Use custom-query over react-query for testing since it handles rules the way users
-expect"
+  "Use custom-query over react-query for testing since it tests react-query and
+adds rules that users often use"
   [query & [opts]]
   (react/clear-query-state!)
   (when-let [result (query-custom/custom-query test-helper/test-db query opts)]
     (map first (deref result))))
 
-(deftest resolve-input
+(deftest resolve-input-for-page-and-block-inputs
   (load-test-files [{:file/path "pages/page1.md"
                      :file/content
                      "- parent
@@ -59,3 +63,69 @@ expect"
                                         :where [?parent-block :block/parent]]}
                               {:current-block-uuid block-uuid}))))
       ":parent-block input resolves to parent of current blocks's :db/id"))
+
+(deftest resolve-input-for-journal-date-inputs
+  (load-test-files [{:file/path "journals/2023_01_01.md"
+                     :file/content "- b1"}
+                    {:file/path "journals/2023_01_07.md"
+                     :file/content "- b2"}])
+
+  (is (= ["b2"]
+         (with-redefs [t/today (constantly (t/date-time 2023 1 7))]
+           (map :block/content
+                (custom-query {:inputs [:3d-before :today]
+                               :query '[:find (pull ?b [*])
+                                        :in $ ?start ?end
+                                        :where (between ?b ?start ?end)]}))))
+      ":Xd-before and :today resolve to correct journal range")
+
+  (is (= ["b1"]
+         (with-redefs [t/today (constantly (t/date-time 2022 12 31))]
+           (map :block/content
+                (custom-query {:inputs [:tomorrow :4d-after]
+                               :query '[:find (pull ?b [*])
+                                        :in $ ?start ?end
+                                        :where (between ?b ?start ?end)]}))))
+      ":tomorrow and :Xd-after resolve to correct journal range"))
+
+;; These tests rely on seeding timestamps with properties. If this ability goes
+;; away we could still test page-level timestamps
+(deftest resolve-input-for-timestamp-inputs
+  (let [today-timestamp (db-util/date-at-local-ms 0 0 0 0)
+        next-week-timestamp (db-util/date-at-local-ms (t/plus (t/today) (t/days 7))
+                                                      0 0 0 0)]
+    (load-test-files [{:file/path "pages/page1.md"
+                       :file/content (gstring/format "foo::bar
+- yesterday
+created-at:: %s
+- today
+created-at:: %s
+- next week
+created-at:: %s"
+                                                     (dec today-timestamp)
+                                                     (inc today-timestamp)
+                                                     next-week-timestamp)}])
+
+    (is (= ["today"]
+           (map #(-> % :block/content string/split-lines first)
+                (custom-query {:inputs [:start-of-today-ms :end-of-today-ms]
+                               :query '[:find (pull ?b [*])
+                                        :in $ ?start ?end
+                                        :where
+                                        [?b :block/content]
+                                        [?b :block/created-at ?timestamp]
+                                        [(>= ?timestamp ?start)]
+                                        [(<= ?timestamp ?end)]]})))
+        ":start-of-today-ms and :end-of-today-ms resolve to correct datetime range")
+
+    (is (= ["yesterday" "today"]
+           (map #(-> % :block/content string/split-lines first)
+                (custom-query {:inputs [:1d-before-ms :5d-after-ms]
+                               :query '[:find (pull ?b [*])
+                                        :in $ ?start ?end
+                                        :where
+                                        [?b :block/content]
+                                        [?b :block/created-at ?timestamp]
+                                        [(>= ?timestamp ?start)]
+                                        [(<= ?timestamp ?end)]]})))
+        ":Xd-before-ms and :Xd-after-ms resolve to correct datetime range")))