Bläddra i källkod

enhance: ability to edit SRS cards during the review

Tienson Qin 4 år sedan
förälder
incheckning
6b98baa78f
1 ändrade filer med 32 tillägg och 33 borttagningar
  1. 32 33
      src/main/frontend/extensions/srs.cljs

+ 32 - 33
src/main/frontend/extensions/srs.cljs

@@ -14,6 +14,7 @@
             [frontend.ui :as ui]
             [frontend.date :as date]
             [frontend.commands :as commands]
+            [frontend.components.editor :as editor]
             [cljs-time.core :as t]
             [cljs-time.local :as tl]
             [cljs-time.coerce :as tc]
@@ -261,23 +262,21 @@
 
 (defn- query-scheduled
   "Return blocks scheduled to 'time' or before"
-  [repo {query-string :query-string query-result :query-result} time]
-  (when-let [*blocks (or query-result (and query-string (query repo query-string)))]
-    (when-let [blocks @*blocks]
-      (let [filtered-result (->>
-                             (flatten blocks)
-                             (filterv (fn [b]
-                                        (let [props (:block/properties b)
-                                              next-sched (get props card-next-schedule-property)
-                                              next-sched* (tc/from-string next-sched)
-                                              repeats (get props card-repeats-property)]
-                                          (or (nil? repeats)
-                                              (< repeats 1)
-                                              (nil? next-sched)
-                                              (nil? next-sched*)
-                                              (t/before? next-sched* time))))))]
-        {:total (count blocks)
-         :result filtered-result}))))
+  [repo blocks time]
+  (let [filtered-result (->>
+                         (flatten blocks)
+                         (filterv (fn [b]
+                                    (let [props (:block/properties b)
+                                          next-sched (get props card-next-schedule-property)
+                                          next-sched* (tc/from-string next-sched)
+                                          repeats (get props card-repeats-property)]
+                                      (or (nil? repeats)
+                                          (< repeats 1)
+                                          (nil? next-sched)
+                                          (nil? next-sched*)
+                                          (t/before? next-sched* time))))))]
+    {:total (count blocks)
+     :result filtered-result}))
 
 
 ;;; ================================================================
@@ -373,10 +372,10 @@
              :disabled false}
             (svg/info)))
 
-(defn- score-and-next-card [score card *card-index *cards *phase *review-records cb]
+(defn- score-and-next-card [score card *card-index cards *phase *review-records cb]
   (operation-score! card score)
   (swap! *review-records #(update % score (fn [ov] (conj ov card))))
-  (if (>= (inc @*card-index) (count @*cards))
+  (if (>= (inc @*card-index) (count cards))
     (when cb
       (swap! *card-index inc)
       (cb @*review-records))
@@ -384,10 +383,10 @@
       (swap! *card-index inc)
       (reset! *phase 1))))
 
-(defn- skip-card [card *card-index *cards *phase *review-records cb]
+(defn- skip-card [card *card-index cards *phase *review-records cb]
   (swap! *review-records #(update % "skip" (fn [ov] (conj ov card))))
   (swap! *card-index inc)
-  (if (>= (inc @*card-index) (count @*cards))
+  (if (>= (inc @*card-index) (count cards))
     (and cb (cb @*review-records))
     (reset! *phase 1)))
 
@@ -398,16 +397,14 @@
   < rum/reactive
   (rum/local 1 ::phase)
   (rum/local 0 ::card-index)
-  (rum/local nil ::cards)
   (rum/local {} ::review-records)
   [state cards {preview? :preview?
                 modal? :modal?
                 cb :callback}]
-  (let [cards* (::cards state)
-        _ (when (nil? @cards*) (reset! cards* cards))
+  (let [cards (mapv ->card cards)
         review-records (::review-records state)
         card-index (::card-index state)
-        card (util/nth-safe @cards* @card-index)]
+        card (util/nth-safe cards @card-index)]
     (if-not card
       review-finished
       (let [phase (::phase state)
@@ -420,7 +417,8 @@
          (component-block/blocks-container
           blocks
           (merge (show-cycle-config card @phase)
-                 {:id (str root-block-id)}))
+                 {:id (str root-block-id)
+                  :editor-box editor/box}))
          (if (or preview? modal?)
            [:div.flex.my-4.justify-between
             [:div.flex-1
@@ -439,7 +437,7 @@
                  :id "card-next"
                  :small? true
                  :class "mr-2"
-                 :on-click #(skip-card card card-index cards* phase review-records cb)))
+                 :on-click #(skip-card card card-index cards phase review-records cb)))
 
              (when (and (not preview?) (= 1 next-phase))
                (let [interval-days-score-3 (get (get-next-interval card 3) card-last-interval-property)
@@ -450,20 +448,20 @@
                     :id "card-forgotten"
                     :small? true
                     :on-click (fn []
-                                (score-and-next-card 1 card card-index cards* phase review-records cb)
+                                (score-and-next-card 1 card card-index cards phase review-records cb)
                                 (let [tomorrow (tc/to-string (t/plus (t/today) (t/days 1)))]
                                   (editor-handler/set-block-property! root-block-id card-next-schedule-property tomorrow))))
 
                   (ui/button "Remembered(r)"
                     :id "card-remembered"
                     :small? true
-                    :on-click #(score-and-next-card 5 card card-index cards* phase review-records cb))
+                    :on-click #(score-and-next-card 5 card card-index cards phase review-records cb))
 
                   (ui/button "Take a while to recall(t)"
                     :id "card-recall"
                     :class (util/hiccup->class "opacity-60.hover:opacity-100")
                     :small? true
-                    :on-click #(score-and-next-card 3 card card-index cards* phase review-records cb))]))]
+                    :on-click #(score-and-next-card 3 card card-index cards phase review-records cb))]))]
 
             (when preview?
               (ui/tippy {:html [:div.text-sm
@@ -519,8 +517,9 @@
   (let [repo (state/get-current-repo)
         query-string (string/join ", " (:arguments options))]
     (if-let [*query-result (query repo query-string)]
-      (let [{:keys [total result]} (query-scheduled repo {:query-result *query-result} (tl/local-now))
-            review-cards (mapv ->card result)
+      (let [blocks (rum/react *query-result)
+            {:keys [total result]} (query-scheduled repo blocks (tl/local-now))
+            review-cards result
             query-string (if (string/blank? query-string) "All" query-string)
             card-query-block (db/entity [:block/uuid (:block/uuid config)])
             filtered-total (count result)
@@ -553,7 +552,7 @@
                {:on-click (fn [_]
                             (let [all-blocks (flatten @(query (state/get-current-repo) query-string))]
                               (when (> (count all-blocks) 0)
-                                (let [review-cards (mapv ->card all-blocks)]
+                                (let [review-cards all-blocks]
                                   (state/set-modal! #(view-modal
                                                       review-cards
                                                       {:preview? true