Browse Source

fix: grouped transaction

close #5277
Tienson Qin 3 years ago
parent
commit
da5875efe8

+ 4 - 3
src/main/frontend/handler/editor.cljs

@@ -471,7 +471,7 @@
                    (not has-children?))]
     (outliner-tx/transact!
       {:outliner-op :insert-blocks}
-      (save-current-block!)
+      (save-current-block! {:current-block current-block})
       (outliner-core/insert-blocks! [new-block] current-block {:sibling? sibling?
                                                                :keep-uuid? keep-uuid?
                                                                :replace-empty-target? replace-empty-target?}))))
@@ -1274,7 +1274,7 @@
   "skip-properties? if set true, when editing block is likely be properties, skip saving"
   ([]
    (save-current-block! {}))
-  ([{:keys [force? skip-properties?] :as opts}]
+  ([{:keys [force? skip-properties? current-block] :as opts}]
    ;; non English input method
    (when-not (state/editor-in-composition?)
      (when (state/get-current-repo)
@@ -1295,7 +1295,8 @@
                  db-content (:block/content db-block)
                  db-content-without-heading (and db-content
                                                  (gp-util/safe-subs db-content (:block/level db-block)))
-                 value (and elem (gobj/get elem "value"))]
+                 value (or (:block/content current-block)
+                           (and elem (gobj/get elem "value")))]
              (cond
                force?
                (save-block-aux! db-block value opts)

+ 20 - 15
src/main/frontend/modules/outliner/transaction.cljc

@@ -18,18 +18,23 @@
     (delete-blocks! ...))"
   [opts & body]
   (assert (map? opts))
-  `(if (some? frontend.modules.outliner.core/*transaction-data*)
-     (do ~@body)
-     (binding [frontend.modules.outliner.core/*transaction-data* (transient [])]
-       ~@body
-       (let [r# (persistent! frontend.modules.outliner.core/*transaction-data*)
-             tx# (mapcat :tx-data r#)
-             ;; FIXME: should we merge all the tx-meta?
-             tx-meta# (first (map :tx-meta r#))
-             all-tx# (concat tx# (:additional-tx ~opts))
-             opts# (merge (dissoc ~opts :additional-tx) tx-meta#)]
-         (when (seq all-tx#)
-           (let [result# (frontend.modules.outliner.datascript/transact! all-tx# opts#)]
-             {:tx-report result#
-              :tx-data all-tx#
-              :tx-meta tx-meta#}))))))
+  `(let [transact-data# frontend.modules.outliner.core/*transaction-data*
+         opts# (if transact-data#
+                 (assoc ~opts :nested-transaction? true)
+                 ~opts)]
+     (if transact-data#
+       (do ~@body)
+       (binding [frontend.modules.outliner.core/*transaction-data* (transient [])]
+         ~@body
+         (let [r# (persistent! frontend.modules.outliner.core/*transaction-data*)
+               tx# (mapcat :tx-data r#)
+               ;; FIXME: should we merge all the tx-meta?
+               tx-meta# (first (map :tx-meta r#))
+               all-tx# (concat tx# (:additional-tx opts#))
+               opts## (merge (dissoc opts# :additional-tx) tx-meta#)]
+           (when (seq all-tx#)
+             (when-not (:nested-transaction? opts#) ; transact only for the whole transaction
+               (let [result# (frontend.modules.outliner.datascript/transact! all-tx# opts##)]
+                 {:tx-report result#
+                  :tx-data all-tx#
+                  :tx-meta tx-meta#}))))))))