Просмотр исходного кода

enhance(srs): move db-based fns to fsrs

rcmerci 1 год назад
Родитель
Сommit
6ab025c3f1

+ 8 - 3
src/main/frontend/components/content.cljs

@@ -31,7 +31,8 @@
             [logseq.common.util.page-ref :as page-ref]
             [logseq.common.util.page-ref :as page-ref]
             [promesa.core :as p]
             [promesa.core :as p]
             [rum.core :as rum]
             [rum.core :as rum]
-            [logseq.db :as ldb]))
+            [logseq.db :as ldb]
+            [frontend.extensions.fsrs :as fsrs]))
 
 
 ;; TODO i18n support
 ;; TODO i18n support
 
 
@@ -100,7 +101,9 @@
      (when (state/enable-flashcards?)
      (when (state/enable-flashcards?)
        (shui/dropdown-menu-item
        (shui/dropdown-menu-item
         {:key "Make a Card"
         {:key "Make a Card"
-         :on-click #(srs/batch-make-cards!)}
+         :on-click #(if (config/db-based-graph? (state/get-current-repo))
+                      (fsrs/batch-make-cards!)
+                      (srs/batch-make-cards!))}
         (t :context-menu/make-a-flashcard)))
         (t :context-menu/make-a-flashcard)))
 
 
      (shui/dropdown-menu-item
      (shui/dropdown-menu-item
@@ -289,7 +292,9 @@
            (state/enable-flashcards?)
            (state/enable-flashcards?)
            (shui/dropdown-menu-item
            (shui/dropdown-menu-item
             {:key      "Make a Card"
             {:key      "Make a Card"
-             :on-click #(srs/make-block-a-card! block-id)}
+             :on-click #(if (config/db-based-graph? (state/get-current-repo))
+                          (fsrs/batch-make-cards! [block-id])
+                          (srs/batch-make-cards! [block-id]))}
             (t :context-menu/make-a-flashcard))
             (t :context-menu/make-a-flashcard))
            :else
            :else
            nil)
            nil)

+ 26 - 0
src/main/frontend/extensions/fsrs.cljs

@@ -7,9 +7,15 @@
             [frontend.db :as db]
             [frontend.db :as db]
             [frontend.db.async :as db-async]
             [frontend.db.async :as db-async]
             [frontend.extensions.srs :as srs]
             [frontend.extensions.srs :as srs]
+            [frontend.handler.block :as block-handler]
             [frontend.handler.db-based.property :as db-property-handler]
             [frontend.handler.db-based.property :as db-property-handler]
+            [frontend.handler.property :as property-handler]
+            [frontend.modules.shortcut.core :as shortcut]
             [frontend.state :as state]
             [frontend.state :as state]
+            [frontend.ui :as ui]
             [frontend.util :as util]
             [frontend.util :as util]
+            [logseq.db :as ldb]
+            [logseq.shui.ui :as shui]
             [missionary.core :as m]
             [missionary.core :as m]
             [open-spaced-repetition.cljc-fsrs.core :as fsrs.core]
             [open-spaced-repetition.cljc-fsrs.core :as fsrs.core]
             [rum.core :as rum]
             [rum.core :as rum]
@@ -252,3 +258,23 @@
   (let [canceler (c.m/run-task new-task--update-due-cards-count :update-due-cards-count)]
   (let [canceler (c.m/run-task new-task--update-due-cards-count :update-due-cards-count)]
     (reset! *last-update-due-cards-count-canceler canceler)
     (reset! *last-update-due-cards-count-canceler canceler)
     nil))
     nil))
+
+(defn- get-operating-blocks
+  [block-ids]
+  (some->> block-ids
+           (map (fn [id] (db/entity [:block/uuid id])))
+           (seq)
+           block-handler/get-top-level-blocks
+           (remove ldb/property?)))
+
+(defn batch-make-cards!
+  ([] (batch-make-cards! (state/get-selection-block-ids)))
+  ([block-ids]
+   (let [repo (state/get-current-repo)
+         blocks (get-operating-blocks block-ids)]
+     (when-let [block-ids (not-empty (map :block/uuid blocks))]
+       (property-handler/batch-set-block-property!
+        repo
+        block-ids
+        :block/tags
+        (:db/id (db/entity :logseq.class/Card)))))))

+ 31 - 73
src/main/frontend/extensions/srs.cljs

@@ -5,34 +5,32 @@
             [cljs-time.core :as t]
             [cljs-time.core :as t]
             [cljs-time.local :as tl]
             [cljs-time.local :as tl]
             [clojure.string :as string]
             [clojure.string :as string]
-            [frontend.config :as config]
             [frontend.commands :as commands]
             [frontend.commands :as commands]
             [frontend.components.block :as component-block]
             [frontend.components.block :as component-block]
             [frontend.components.editor :as editor]
             [frontend.components.editor :as editor]
             [frontend.components.macro :as component-macro]
             [frontend.components.macro :as component-macro]
             [frontend.components.select :as component-select]
             [frontend.components.select :as component-select]
             [frontend.components.svg :as svg]
             [frontend.components.svg :as svg]
+            [frontend.config :as config]
             [frontend.context.i18n :refer [t]]
             [frontend.context.i18n :refer [t]]
             [frontend.date :as date]
             [frontend.date :as date]
             [frontend.db :as db]
             [frontend.db :as db]
-            [logseq.db :as ldb]
             [frontend.db-mixins :as db-mixins]
             [frontend.db-mixins :as db-mixins]
             [frontend.db.query-dsl :as query-dsl]
             [frontend.db.query-dsl :as query-dsl]
             [frontend.db.query-react :as query-react]
             [frontend.db.query-react :as query-react]
+            [frontend.format.mldoc :as mldoc]
             [frontend.handler.editor :as editor-handler]
             [frontend.handler.editor :as editor-handler]
             [frontend.handler.property :as property-handler]
             [frontend.handler.property :as property-handler]
             [frontend.handler.property.file :as property-file]
             [frontend.handler.property.file :as property-file]
-            [frontend.handler.block :as block-handler]
             [frontend.modules.shortcut.core :as shortcut]
             [frontend.modules.shortcut.core :as shortcut]
             [frontend.state :as state]
             [frontend.state :as state]
             [frontend.template :as template]
             [frontend.template :as template]
             [frontend.ui :as ui]
             [frontend.ui :as ui]
             [frontend.util :as util]
             [frontend.util :as util]
-            [frontend.format.mldoc :as mldoc]
             [frontend.util.file-based.drawer :as drawer]
             [frontend.util.file-based.drawer :as drawer]
             [frontend.util.persist-var :as persist-var]
             [frontend.util.persist-var :as persist-var]
-            [logseq.graph-parser.property :as gp-property]
             [logseq.common.util.page-ref :as page-ref]
             [logseq.common.util.page-ref :as page-ref]
+            [logseq.graph-parser.property :as gp-property]
             [logseq.shui.ui :as shui]
             [logseq.shui.ui :as shui]
             [medley.core :as medley]
             [medley.core :as medley]
             [rum.core :as rum]))
             [rum.core :as rum]))
@@ -121,22 +119,15 @@
 
 
 (defn- reset-block-card-properties!
 (defn- reset-block-card-properties!
   [block]
   [block]
-  (let [repo (state/get-current-repo)]
-    (if (config/db-based-graph? repo)
-      (do
-        (property-handler/remove-block-property! repo (:block/uuid block) :logseq.property.fsrs/state)
-        (property-handler/remove-block-property! repo (:block/uuid block) :logseq.property.fsrs/due))
-      (save-block-card-properties! block {card-last-interval-property -1
-                                          card-repeats-property 0
-                                          card-last-easiness-factor-property 2.5
-                                          card-last-reviewed-property "nil"
-                                          card-next-schedule-property "nil"
-                                          card-last-score-property "nil"}))))
-
+  (save-block-card-properties! block {card-last-interval-property -1
+                                      card-repeats-property 0
+                                      card-last-easiness-factor-property 2.5
+                                      card-last-reviewed-property "nil"
+                                      card-next-schedule-property "nil"
+                                      card-last-score-property "nil"}))
 
 
 ;;; used by other ns
 ;;; used by other ns
 
 
-
 (defn card-block?
 (defn card-block?
   [block]
   [block]
   (let [card-entity (db/get-page card-hash-tag)
   (let [card-entity (db/get-page card-hash-tag)
@@ -199,11 +190,9 @@
       [-1 1 ef next-of-matrix]
       [-1 1 ef next-of-matrix]
       [(fix-2f next-interval) (+ 1 repeats) (fix-2f next-ef) next-of-matrix])))
       [(fix-2f next-interval) (+ 1 repeats) (fix-2f next-ef) next-of-matrix])))
 
 
-
 ;;; ================================================================
 ;;; ================================================================
 ;;; card protocol
 ;;; card protocol
 
 
-
 (defprotocol ICard
 (defprotocol ICard
   (get-root-block [this]))
   (get-root-block [this]))
 
 
@@ -213,7 +202,6 @@
 
 
   (show-cycle-config [this phase]))
   (show-cycle-config [this phase]))
 
 
-
 (defn- has-cloze?
 (defn- has-cloze?
   [blocks]
   [blocks]
   (->> (map :block/title blocks)
   (->> (map :block/title blocks)
@@ -283,8 +271,8 @@
                                          query-string)
                                          query-string)
                           {query* :query :keys [sort-by rules]} (query-dsl/parse query-string {:db-graph? (config/db-based-graph? repo)})
                           {query* :query :keys [sort-by rules]} (query-dsl/parse query-string {:db-graph? (config/db-based-graph? repo)})
                           query** (util/concat-without-nil
                           query** (util/concat-without-nil
-                                  [['?b :block/refs '?br] ['?br :block/name card-hash-tag]]
-                                  (if (coll? (first query*)) query* [query*]))]
+                                   [['?b :block/refs '?br] ['?br :block/name card-hash-tag]]
+                                   (if (coll? (first query*)) query* [query*]))]
                       (when-let [query' (query-dsl/query-wrapper query**
                       (when-let [query' (query-dsl/query-wrapper query**
                                                                  {:blocks? true
                                                                  {:blocks? true
                                                                   :block-attrs [:db/id :block/properties]})]
                                                                   :block-attrs [:db/id :block/properties]})]
@@ -318,11 +306,9 @@
     {:total (count blocks)
     {:total (count blocks)
      :result sort-by-next-schedule}))
      :result sort-by-next-schedule}))
 
 
-
 ;;; ================================================================
 ;;; ================================================================
 ;;; operations
 ;;; operations
 
 
-
 (defn- get-next-interval
 (defn- get-next-interval
   [card score]
   [card score]
   {:pre [(and (<= score 5) (>= score 0))
   {:pre [(and (<= score 5) (>= score 0))
@@ -635,8 +621,8 @@
             (fn [{:keys [toggle-fn]}]
             (fn [{:keys [toggle-fn]}]
               [:div.ml-1.text-sm.font-medium.cursor
               [:div.ml-1.text-sm.font-medium.cursor
                {:on-pointer-down (fn [e]
                {:on-pointer-down (fn [e]
-                                 (util/stop e)
-                                 (toggle-fn))}
+                                   (util/stop e)
+                                   (toggle-fn))}
                [:span.flex (if (string/blank? query-string) (t :flashcards/modal-select-all) query-string)
                [:span.flex (if (string/blank? query-string) (t :flashcards/modal-select-all) query-string)
                 [:span {:style {:margin-top 2}}
                 [:span {:style {:margin-top 2}}
                  (svg/caret-down)]]])
                  (svg/caret-down)]]])
@@ -678,8 +664,8 @@
               {:icon "letter-a"
               {:icon "letter-a"
                :intent "link"
                :intent "link"
                :on-click (fn [e]
                :on-click (fn [e]
-                          (util/stop e)
-                          (swap! *preview-mode? not)
+                           (util/stop e)
+                           (swap! *preview-mode? not)
                            (reset! *card-index 0))
                            (reset! *card-index 0))
                :button-props {:id "preview-all-cards"}
                :button-props {:id "preview-all-cards"}
                :small? true}
                :small? true}
@@ -705,8 +691,8 @@
           (when (and (not modal?) (not @*preview-mode?))
           (when (and (not modal?) (not @*preview-mode?))
             {:on-click (fn []
             {:on-click (fn []
                          (shui/dialog-open!
                          (shui/dialog-open!
-                           #(cards (assoc config :modal? true) {:query-string query-string})
-                           {:id :srs}))})
+                          #(cards (assoc config :modal? true) {:query-string query-string})
+                          {:id :srs}))})
           (let [view-fn (if modal? view-modal view)
           (let [view-fn (if modal? view-modal view)
                 blocks (if @*preview-mode? query-result review-cards)
                 blocks (if @*preview-mode? query-result review-cards)
                 blocks (if @*random-mode? (shuffle blocks) blocks)]
                 blocks (if @*random-mode? (shuffle blocks) blocks)]
@@ -793,52 +779,24 @@
   "given a block struct, adds the #card to title and returns
   "given a block struct, adds the #card to title and returns
    a seq of [original-block new-content-string]"
    a seq of [original-block new-content-string]"
   [block]
   [block]
-    (when-let [content (:block/title block)]
-      (let [format (:block/format block)
-            content (-> (property-file/remove-built-in-properties-when-file-based
-                         (state/get-current-repo) (:block/format block) content)
-                        (drawer/remove-logbook))
-            [title body] (mldoc/get-title&body content format)]
-        [block (str title " #" card-hash-tag "\n" body)])))
-
-(defn- get-operating-blocks
-  [block-ids]
-  (some->> block-ids
-           (map (fn [id] (db/entity [:block/uuid id])))
-           (seq)
-           block-handler/get-top-level-blocks
-           (remove ldb/property?)))
+  (when-let [content (:block/title block)]
+    (let [format (:block/format block)
+          content (-> (property-file/remove-built-in-properties-when-file-based
+                       (state/get-current-repo) (:block/format block) content)
+                      (drawer/remove-logbook))
+          [title body] (mldoc/get-title&body content format)]
+      [block (str title " #" card-hash-tag "\n" body)])))
 
 
 (defn batch-make-cards!
 (defn batch-make-cards!
   ([] (batch-make-cards! (state/get-selection-block-ids)))
   ([] (batch-make-cards! (state/get-selection-block-ids)))
   ([block-ids]
   ([block-ids]
-   (if (config/db-based-graph? (state/get-current-repo))
-     (let [blocks (get-operating-blocks block-ids)
-           block-ids (map :block/uuid blocks)]
-       (when (seq block-ids)
-         (property-handler/batch-set-block-property! (state/get-current-repo)
-                                                     block-ids
-                                                     :block/tags
-                                                     (:db/id (db/entity :logseq.class/Card)))))
-     (let [valid-blocks (->> block-ids
-                             (map #(db/entity [:block/uuid %]))
-                             (remove card-block?)
-                             (map #(db/pull [:block/uuid (:block/uuid %)])))
-           blocks (map add-card-tag-to-block valid-blocks)]
-       (when-not (empty? blocks)
-         (editor-handler/save-blocks! blocks))))))
-
-(defn make-block-a-card!
-  [block-id]
-  (if (config/db-based-graph? (state/get-current-repo))
-    (when-let [block-ids (when block-id [block-id])]
-      (batch-make-cards! block-ids))
-    (when-let [block (db/entity [:block/uuid block-id])]
-     (let [block-content (add-card-tag-to-block block)
-           new-content (get block-content 1)]
-       (editor-handler/save-block! (state/get-current-repo) block-id new-content)))))
-
-
+   (let [valid-blocks (->> block-ids
+                           (map #(db/entity [:block/uuid %]))
+                           (remove card-block?)
+                           (map #(db/pull [:block/uuid (:block/uuid %)])))
+         blocks (map add-card-tag-to-block valid-blocks)]
+     (when-not (empty? blocks)
+       (editor-handler/save-blocks! blocks)))))
 
 
 (defonce *due-cards-interval (atom nil))
 (defonce *due-cards-interval (atom nil))
 
 

+ 18 - 15
src/main/frontend/mobile/action_bar.cljs

@@ -1,19 +1,20 @@
 (ns frontend.mobile.action-bar
 (ns frontend.mobile.action-bar
   "Block Action bar, activated when swipe on a block"
   "Block Action bar, activated when swipe on a block"
-  (:require
-   [frontend.db :as db]
-   [frontend.extensions.srs :as srs]
-   [frontend.handler.editor :as editor-handler]
-   [frontend.mixins :as mixins]
-   [frontend.state :as state]
-   [frontend.ui :as ui]
-   [frontend.util :as util]
-   [frontend.util.url :as url-util]
-   [goog.dom :as gdom]
-   [goog.object :as gobj]
-   [rum.core :as rum]
-   [logseq.common.util.block-ref :as block-ref]
-   [frontend.mobile.util :as mobile-util]))
+  (:require [frontend.config :as config]
+            [frontend.db :as db]
+            [frontend.extensions.fsrs :as fsrs]
+            [frontend.extensions.srs :as srs]
+            [frontend.handler.editor :as editor-handler]
+            [frontend.mixins :as mixins]
+            [frontend.mobile.util :as mobile-util]
+            [frontend.state :as state]
+            [frontend.ui :as ui]
+            [frontend.util :as util]
+            [frontend.util.url :as url-util]
+            [goog.dom :as gdom]
+            [goog.object :as gobj]
+            [logseq.common.util.block-ref :as block-ref]
+            [rum.core :as rum]))
 
 
 (defn- action-command
 (defn- action-command
   [icon description command-handler]
   [icon description command-handler]
@@ -56,7 +57,9 @@
           (.scrollBy (util/app-scroll-container-node) #js {:top (- 10 delta)})))
           (.scrollBy (util/app-scroll-container-node) #js {:top (- 10 delta)})))
       [:div.action-bar
       [:div.action-bar
        [:div.action-bar-commands
        [:div.action-bar-commands
-        (action-command "infinity" "Card" #(srs/make-block-a-card! (:block/uuid block)))
+        (action-command "infinity" "Card" #(if (config/db-based-graph? (state/get-current-repo))
+                                             (fsrs/batch-make-cards! [(:block/uuid block)])
+                                             (srs/batch-make-cards! [(:block/uuid block)])))
         (action-command "copy" "Copy" #(editor-handler/copy-selection-blocks false))
         (action-command "copy" "Copy" #(editor-handler/copy-selection-blocks false))
         (action-command "cut" "Cut" #(editor-handler/cut-selection-blocks true))
         (action-command "cut" "Cut" #(editor-handler/cut-selection-blocks true))
         (action-command "trash" "Delete" #(editor-handler/delete-block-aux! block))
         (action-command "trash" "Delete" #(editor-handler/delete-block-aux! block))