|
1 | 1 | (ns frontend.components.property.value
|
2 | 2 | (:require [cljs-time.coerce :as tc]
|
3 | 3 | [cljs-time.core :as t]
|
| 4 | + [cljs-time.local :as local] |
4 | 5 | [clojure.set :as set]
|
5 | 6 | [clojure.string :as string]
|
6 | 7 | [datascript.impl.entity :as de]
|
|
298 | 299 | (when done-choice
|
299 | 300 | (db-property/property-value-content done-choice))]])]))
|
300 | 301 |
|
| 302 | +(defn- get-local-journal-date-time |
| 303 | + [year month day] |
| 304 | + (let [[op h m] (:offset (t/default-time-zone)) |
| 305 | + f (if (= op :-) t/plus t/minus)] |
| 306 | + (-> (t/date-time year month day) |
| 307 | + (f (t/hours h)) |
| 308 | + (f (t/minutes m))))) |
| 309 | + |
301 | 310 | (rum/defcs calendar-inner < rum/reactive db-mixins/query
|
302 | 311 | (rum/local (str "calendar-inner-" (js/Date.now)) ::identity)
|
303 | 312 | {:init (fn [state]
|
|
320 | 329 | value (get block (:db/ident property))
|
321 | 330 | value (cond
|
322 | 331 | (map? value)
|
323 |
| - (js/Date. (date/journal-title->long (:block/title value))) |
| 332 | + (when-let [day (:block/journal-day value)] |
| 333 | + (let [t (tc/to-date-time (date/journal-day->ts day))] |
| 334 | + (js/Date. |
| 335 | + (get-local-journal-date-time (t/year t) (t/month t) (t/day t))))) |
324 | 336 |
|
325 | 337 | (number? value)
|
326 | 338 | (js/Date. value)
|
|
330 | 342 | (.setHours d 0 0 0)
|
331 | 343 | d))
|
332 | 344 | *ident (::identity state)
|
333 |
| - initial-day (or (some-> value (.getTime) (js/Date.)) (js/Date.)) |
| 345 | + initial-day value |
334 | 346 | initial-month (when value
|
335 |
| - (js/Date. (.getFullYear value) (.getMonth value))) |
| 347 | + (let [d (tc/to-date-time value)] |
| 348 | + (js/Date. (t/last-day-of-the-month (t/date-time (t/year d) (t/month d)))))) |
336 | 349 | select-handler!
|
337 | 350 | (fn [^js d]
|
338 |
| - ;; force local to UTC |
339 | 351 | (when d
|
340 |
| - (let [gd (goog.date.Date. (.getFullYear d) (.getMonth d) (.getDate d))] |
341 |
| - (let [journal (date/js-date->journal-title gd)] |
342 |
| - (p/do! |
343 |
| - (when-not (db/get-page journal) |
344 |
| - (page-handler/<create! journal {:redirect? false |
345 |
| - :create-first-block? false})) |
346 |
| - (when (fn? on-change) |
347 |
| - (let [value (if datetime? (tc/to-long d) (db/get-page journal))] |
348 |
| - (on-change value))) |
349 |
| - (when-not datetime? |
350 |
| - (shui/popup-hide! id) |
351 |
| - (ui/hide-popups-until-preview-popup!) |
352 |
| - (shui/dialog-close!)))))))] |
| 352 | + (let [journal (date/js-date->journal-title d)] |
| 353 | + (p/do! |
| 354 | + (when-not (db/get-page journal) |
| 355 | + (page-handler/<create! journal {:redirect? false |
| 356 | + :create-first-block? false})) |
| 357 | + (when (fn? on-change) |
| 358 | + (let [value (if datetime? (tc/to-long d) (db/get-page journal))] |
| 359 | + (on-change value))) |
| 360 | + (when-not datetime? |
| 361 | + (shui/popup-hide! id) |
| 362 | + (ui/hide-popups-until-preview-popup!) |
| 363 | + (shui/dialog-close!))))))] |
353 | 364 | [:div.flex.flex-row.gap-2
|
354 | 365 | [:div.flex.flex-col
|
355 | 366 | (ui/nlp-calendar
|
|
380 | 391 | :title "Overdue"))
|
381 | 392 | content])))
|
382 | 393 |
|
383 |
| -(defn- human-date-label |
384 |
| - [date] |
385 |
| - (let [today (t/today) |
386 |
| - today-y (t/year today) |
387 |
| - today-m (t/month today) |
388 |
| - today-d (t/day today) |
389 |
| - same-day? (fn [date] |
390 |
| - (and (= today-y (t/year date)) (= today-m (t/month date)) (= today-d (t/day date))))] |
| 394 | +(defn- human-date-label [date] |
| 395 | + (let [given-date (date/start-of-day date) |
| 396 | + now (local/local-now) |
| 397 | + today (date/start-of-day now) |
| 398 | + tomorrow (t/plus today (t/days 1)) |
| 399 | + yesterday (t/minus today (t/days 1))] |
391 | 400 | (cond
|
392 |
| - (same-day? date) |
| 401 | + (and (t/before? given-date today) (not (t/before? given-date yesterday))) |
| 402 | + "Yesterday" |
| 403 | + |
| 404 | + (and (not (t/before? given-date today)) (t/before? given-date tomorrow)) |
393 | 405 | "Today"
|
394 |
| - (let [tomorrow (t/minus date (t/days 1))] |
395 |
| - (same-day? tomorrow)) |
| 406 | + |
| 407 | + (and (not (t/before? given-date tomorrow)) (t/before? given-date (t/plus tomorrow (t/days 1)))) |
396 | 408 | "Tomorrow"
|
397 |
| - (let [yesterday (t/plus date (t/days 1))] |
398 |
| - (same-day? yesterday)) |
399 |
| - "Yesterday" |
400 |
| - :else |
401 |
| - nil))) |
| 409 | + |
| 410 | + :else nil))) |
402 | 411 |
|
403 | 412 | (rum/defc datetime-value
|
404 | 413 | [value property-id repeated-task?]
|
405 |
| - (when-let [date (tc/from-long value)] |
| 414 | + (when-let [date (t/to-default-time-zone (tc/from-long value))] |
406 | 415 | (let [content [:div.ls-datetime.flex.flex-row.gap-1.items-center
|
407 | 416 | (when-let [page-cp (state/get-component :block/page-cp)]
|
408 |
| - (let [page-title (date/journal-name (date/js-date->goog-date (js/Date. value)))] |
| 417 | + (let [page-title (date/journal-name date)] |
409 | 418 | (rum/with-key
|
410 | 419 | (page-cp {:disable-preview? true
|
411 | 420 | :show-non-exists-page? true
|
|
469 | 478 | (ui/icon "repeat" {:size 14 :class "opacity-40"}))
|
470 | 479 | (cond
|
471 | 480 | (map? value)
|
472 |
| - (let [date (tc/to-date-time (date/journal-title->long (:block/title value))) |
| 481 | + (let [date (tc/to-date-time (date/journal-day->ts (:block/journal-day value))) |
473 | 482 | compare-value (some-> date
|
474 | 483 | (t/plus (t/days 1))
|
475 | 484 | (t/minus (t/seconds 1)))
|
476 | 485 | content (when-let [page-cp (state/get-component :block/page-cp)]
|
477 | 486 | (rum/with-key
|
478 | 487 | (page-cp {:disable-preview? true
|
479 | 488 | :meta-click? other-position?
|
480 |
| - :label (human-date-label date)} value) |
| 489 | + :label (human-date-label (t/to-default-time-zone date))} value) |
481 | 490 | (:db/id value)))]
|
482 | 491 | (if (or repeated-task? (contains? #{:logseq.task/deadline :logseq.task/scheduled} (:db/id property)))
|
483 | 492 | (overdue compare-value content)
|
|
501 | 510 | :datetime? datetime?
|
502 | 511 | :multiple-values? multiple-values?
|
503 | 512 | :on-change (fn [value]
|
504 |
| - (let [journal (when (number? value) |
505 |
| - (date/journal-name (date/js-date->goog-date (js/Date. value))))] |
506 |
| - (p/do! |
507 |
| - (when-not (db/get-page journal) |
508 |
| - (page-handler/<create! journal |
509 |
| - {:redirect? false |
510 |
| - :create-first-block? false |
511 |
| - :tags #{:logseq.class/Journal}})) |
512 |
| - (property-handler/set-block-property! repo (:block/uuid block) |
513 |
| - (:db/ident property) |
514 |
| - (if datetime? |
515 |
| - value |
516 |
| - (:db/id value)))))) |
| 513 | + (property-handler/set-block-property! repo (:block/uuid block) |
| 514 | + (:db/ident property) |
| 515 | + (if datetime? |
| 516 | + value |
| 517 | + (:db/id value)))) |
517 | 518 | :del-btn? (some? value)
|
518 | 519 | :on-delete (fn []
|
519 | 520 | (property-handler/set-block-property! repo (:block/uuid block)
|
|
0 commit comments