Bladeren bron

fix: editor unit tests

Tienson Qin 1 jaar geleden
bovenliggende
commit
0a028ba235

+ 87 - 0
deps/outliner/src/logseq/outliner/op.cljs

@@ -0,0 +1,87 @@
+(ns logseq.outliner.op
+  "Transact outliner ops"
+  (:require [logseq.outliner.transaction :as outliner-tx]
+            [logseq.outliner.core :as outliner-core]
+            [datascript.core :as d]
+            [malli.core :as m]))
+
+(def op-schema
+  [:multi {:dispatch first}
+   [:save-block
+    [:catn
+     [:op :keyword]
+     [:args [:tuple ::block]]]]
+   [:insert-blocks
+    [:catn
+     [:op :keyword]
+     [:args [:tuple ::blocks ::id ::option]]]]
+   [:delete-blocks
+    [:catn
+     [:op :keyword]
+     [:args [:tuple ::ids ::option]]]]
+   [:move-blocks
+    [:catn
+     [:op :keyword]
+     [:args [:tuple ::ids ::id :boolean]]]]
+   [:move-blocks-up-down
+    [:catn
+     [:op :keyword]
+     [:args [:tuple ::ids :boolean]]]]
+   [:indent-outdent-blocks
+    [:catn
+     [:op :keyword]
+     [:args [:tuple ::ids :boolean ::option]]]]])
+
+(def ops-schema [:schema {:registry {::id int?
+                                     ::block map?
+                                     ::option [:maybe map?]
+                                     ::blocks [:sequential ::block]
+                                     ::ids [:sequential ::id]}}
+                 [:sequential op-schema]])
+
+(def ops-validator (m/validator ops-schema))
+
+(defn apply-ops!
+  [repo conn ops date-formatter opts]
+  (assert (ops-validator ops) ops)
+  (let [opts' (assoc opts
+                     :transact-opts {:repo repo :conn conn}
+                     :local-tx? true)
+        *insert-result (atom nil)]
+    (outliner-tx/transact!
+     opts'
+     (doseq [[op args] ops]
+       (case op
+         :save-block
+         (apply outliner-core/save-block! repo conn date-formatter args)
+
+         :insert-blocks
+         (let [[blocks target-block-id opts] args]
+           (when-let [target-block (d/entity @conn target-block-id)]
+             (let [result (outliner-core/insert-blocks! repo conn blocks target-block opts)]
+               (reset! *insert-result result))))
+
+         :delete-blocks
+         (let [[block-ids opts] args
+               blocks (keep #(d/entity @conn %) block-ids)]
+           (outliner-core/delete-blocks! repo conn date-formatter blocks opts))
+
+         :move-blocks
+         (let [[block-ids target-block-id sibling?] args
+               blocks (keep #(d/entity @conn %) block-ids)
+               target-block (d/entity @conn target-block-id)]
+           (when (and target-block (seq blocks))
+             (outliner-core/move-blocks! repo conn blocks target-block sibling?)))
+
+         :move-blocks-up-down
+         (let [[block-ids up?] args
+               blocks (keep #(d/entity @conn %) block-ids)]
+           (when (seq blocks)
+             (outliner-core/move-blocks-up-down! repo conn blocks up?)))
+
+         :indent-outdent-blocks
+         (let [[block-ids indent? opts] args
+               blocks (keep #(d/entity @conn %) block-ids)]
+           (when (seq blocks)
+             (outliner-core/indent-outdent-blocks! repo conn blocks indent? opts))))))
+    @*insert-result))

+ 13 - 3
src/main/frontend/db_worker.cljs

@@ -25,7 +25,7 @@
             [frontend.worker.util :as worker-util]
             [frontend.worker.handler.page.rename :as worker-page-rename]
             [frontend.worker.handler.page :as worker-page]
-            [frontend.worker.outliner-op :as worker-outliner-op]))
+            [logseq.outliner.op :as outliner-op]))
 
 (defonce *sqlite worker-state/*sqlite)
 (defonce *sqlite-conns worker-state/*sqlite-conns)
@@ -482,8 +482,18 @@
    [this repo ops-str opts-str]
    (when-let [conn (worker-state/get-datascript-conn repo)]
      (let [ops (edn/read-string ops-str)
-           opts (edn/read-string opts-str)]
-       (worker-outliner-op/apply-ops! repo conn ops opts))))
+           opts (edn/read-string opts-str)
+           start-tx (:max-tx @conn)
+           result (outliner-op/apply-ops! repo conn ops (worker-state/get-date-formatter repo) opts)
+           end-tx (:max-tx @conn)]
+       (when (= start-tx end-tx)        ; nothing changes
+         ;; remove task from ldb/*request-id->response
+         (worker-util/post-message :sync-db-changes (pr-str
+                                                     {:request-id (:request-id opts)
+                                                      :repo repo
+                                                      :tx-data []
+                                                      :tx-meta nil})))
+       (pr-str result))))
 
   (file-writes-finished?
    [this repo]

+ 26 - 18
src/main/frontend/modules/outliner/ui.cljc

@@ -1,24 +1,32 @@
 (ns frontend.modules.outliner.ui
   #?(:cljs (:require-macros [frontend.modules.outliner.ui]))
   #?(:cljs (:require [frontend.state :as state]
-                     [frontend.db :as db])))
+                     [frontend.db :as db]
+                     [logseq.outliner.op])))
 
 (defmacro transact!
   [opts & body]
-  `(when (db/request-finished?)
-     (when (nil? @(:history/tx-before-editor-cursor @state/state))
-       (state/set-state! :history/tx-before-editor-cursor (state/get-current-edit-block-and-position)))
-     (let [ops# frontend.modules.outliner.op/*outliner-ops*]
-       (if ops#
-         (do ~@body)                    ; nested transact!
-         (binding [frontend.modules.outliner.op/*outliner-ops* (transient [])]
-           ~@body
-           (let [r# (persistent! frontend.modules.outliner.op/*outliner-ops*)
-                 worker# @state/*db-worker]
-             (when (and worker# (seq r#))
-               (let [request-id# (state/get-worker-next-request-id)
-                     response# (.apply-outliner-ops ^Object worker# (state/get-current-repo)
-                                                    (pr-str r#)
-                                                    (pr-str (assoc ~opts :request-id request-id#)))]
-                 (state/add-worker-request! request-id# :outliner-tx)
-                 response#))))))))
+  `(let [test?# frontend.util/node-test?]
+     (when (or test?# (db/request-finished?))
+       (when (nil? @(:history/tx-before-editor-cursor @state/state))
+         (state/set-state! :history/tx-before-editor-cursor (state/get-current-edit-block-and-position)))
+       (let [ops# frontend.modules.outliner.op/*outliner-ops*]
+         (if ops#
+           (do ~@body)                    ; nested transact!
+           (binding [frontend.modules.outliner.op/*outliner-ops* (transient [])]
+             ~@body
+             (let [r# (persistent! frontend.modules.outliner.op/*outliner-ops*)
+                   worker# @state/*db-worker]
+               (if (and test?# (seq r#))
+                 (logseq.outliner.op/apply-ops! (state/get-current-repo)
+                                                (db/get-db false)
+                                                r#
+                                                (state/get-date-formatter)
+                                                ~opts)
+                 (when (and worker# (seq r#))
+                   (let [request-id# (state/get-worker-next-request-id)
+                         response# (.apply-outliner-ops ^Object worker# (state/get-current-repo)
+                                                        (pr-str r#)
+                                                        (pr-str (assoc ~opts :request-id request-id#)))]
+                     (state/add-worker-request! request-id# :outliner-tx)
+                     response#))))))))))

+ 0 - 99
src/main/frontend/worker/outliner_op.cljs

@@ -1,99 +0,0 @@
-(ns frontend.worker.outliner-op
-  "Transact outliner ops from UI"
-  (:require [logseq.outliner.transaction :as outliner-tx]
-            [logseq.outliner.core :as outliner-core]
-            [frontend.worker.state :as worker-state]
-            [datascript.core :as d]
-            [malli.core :as m]
-            [frontend.worker.util :as worker-util]))
-
-(def op-schema
-  [:multi {:dispatch first}
-   [:save-block
-    [:catn
-     [:op :keyword]
-     [:args [:tuple ::block]]]]
-   [:insert-blocks
-    [:catn
-     [:op :keyword]
-     [:args [:tuple ::blocks ::id ::option]]]]
-   [:delete-blocks
-    [:catn
-     [:op :keyword]
-     [:args [:tuple ::ids ::option]]]]
-   [:move-blocks
-    [:catn
-     [:op :keyword]
-     [:args [:tuple ::ids ::id :boolean]]]]
-   [:move-blocks-up-down
-    [:catn
-     [:op :keyword]
-     [:args [:tuple ::ids :boolean]]]]
-   [:indent-outdent-blocks
-    [:catn
-     [:op :keyword]
-     [:args [:tuple ::ids :boolean ::option]]]]])
-
-(def ops-schema [:schema {:registry {::id int?
-                                     ::block map?
-                                     ::option [:maybe map?]
-                                     ::blocks [:sequential ::block]
-                                     ::ids [:sequential ::id]}}
-                 [:sequential op-schema]])
-
-(def ops-validator (m/validator ops-schema))
-
-(defn apply-ops!
-  [repo conn ops opts]
-  (assert (ops-validator ops) ops)
-  (let [opts' (assoc opts
-                     :transact-opts {:repo repo :conn conn}
-                     :local-tx? true)
-        date-formatter (worker-state/get-date-formatter repo)
-        *insert-result (atom nil)
-        max-tx (:max-tx @conn)
-        _ (outliner-tx/transact!
-           opts'
-           (doseq [[op args] ops]
-             (case op
-               :save-block
-               (apply outliner-core/save-block! repo conn date-formatter args)
-
-               :insert-blocks
-               (let [[blocks target-block-id opts] args]
-                 (when-let [target-block (d/entity @conn target-block-id)]
-                   (let [result (outliner-core/insert-blocks! repo conn blocks target-block opts)]
-                     (reset! *insert-result result))))
-
-               :delete-blocks
-               (let [[block-ids opts] args
-                     blocks (keep #(d/entity @conn %) block-ids)]
-                 (outliner-core/delete-blocks! repo conn date-formatter blocks opts))
-
-               :move-blocks
-               (let [[block-ids target-block-id sibling?] args
-                     blocks (keep #(d/entity @conn %) block-ids)
-                     target-block (d/entity @conn target-block-id)]
-                 (when (and target-block (seq blocks))
-                   (outliner-core/move-blocks! repo conn blocks target-block sibling?)))
-
-               :move-blocks-up-down
-               (let [[block-ids up?] args
-                     blocks (keep #(d/entity @conn %) block-ids)]
-                 (when (seq blocks)
-                   (outliner-core/move-blocks-up-down! repo conn blocks up?)))
-
-               :indent-outdent-blocks
-               (let [[block-ids indent? opts] args
-                     blocks (keep #(d/entity @conn %) block-ids)]
-                 (when (seq blocks)
-                   (outliner-core/indent-outdent-blocks! repo conn blocks indent? opts))))))
-        after-max-tx (:max-tx @conn)]
-    (if (= after-max-tx max-tx)
-      ;; remove task from ldb/*request-id->response
-      (worker-util/post-message :sync-db-changes (pr-str
-                                                  {:request-id (:request-id opts)
-                                                   :repo repo
-                                                   :tx-data []
-                                                   :tx-meta nil}))
-      (pr-str @*insert-result))))