Selaa lähdekoodia

fix: group transactions

Tienson Qin 2 vuotta sitten
vanhempi
sitoutus
a81229576d

+ 28 - 28
deps/outliner/src/logseq/outliner/core.cljs

@@ -986,7 +986,7 @@
                    (not= (:db/id (:block/page first-block-parent))
                    (not= (:db/id (:block/page first-block-parent))
                          (:db/id left-left)))
                          (:db/id left-left)))
           (move-blocks repo conn top-level-blocks left-left (merge opts {:sibling? sibling?
           (move-blocks repo conn top-level-blocks left-left (merge opts {:sibling? sibling?
-                                                                    :up? up?}))))
+                                                                         :up? up?}))))
 
 
       (let [last-top-block (last top-level-blocks)
       (let [last-top-block (last top-level-blocks)
             last-top-block-right (get-right-sibling db (:db/id last-top-block))
             last-top-block-right (get-right-sibling db (:db/id last-top-block))
@@ -1007,7 +1007,7 @@
   [repo conn blocks indent? & {:keys [get-first-block-original logical-outdenting?]}]
   [repo conn blocks indent? & {:keys [get-first-block-original logical-outdenting?]}]
   {:pre [(seq blocks) (boolean? indent?)]}
   {:pre [(seq blocks) (boolean? indent?)]}
   (let [db @conn
   (let [db @conn
-        top-level-blocks blocks
+        top-level-blocks (map (fn [b] (d/entity db (:db/id b))) blocks)
         non-consecutive-blocks (ldb/get-non-consecutive-blocks db top-level-blocks)]
         non-consecutive-blocks (ldb/get-non-consecutive-blocks db top-level-blocks)]
     (when (empty? non-consecutive-blocks)
     (when (empty? non-consecutive-blocks)
       (let [first-block (d/entity db (:db/id (first top-level-blocks)))
       (let [first-block (d/entity db (:db/id (first top-level-blocks)))
@@ -1029,42 +1029,42 @@
                 (if last-direct-child-id
                 (if last-direct-child-id
                   (let [last-direct-child (d/entity db last-direct-child-id)
                   (let [last-direct-child (d/entity db last-direct-child-id)
                         result (move-blocks repo conn blocks' last-direct-child (merge opts {:sibling? true
                         result (move-blocks repo conn blocks' last-direct-child (merge opts {:sibling? true
-                                                                                        :indent? true}))
+                                                                                             :indent? true}))
                         ;; expand `left` if it's collapsed
                         ;; expand `left` if it's collapsed
                         collapsed-tx (when (:block/collapsed? left)
                         collapsed-tx (when (:block/collapsed? left)
                                        {:tx-data [{:db/id (:db/id left)
                                        {:tx-data [{:db/id (:db/id left)
                                                    :block/collapsed? false}]})]
                                                    :block/collapsed? false}]})]
                     (concat-tx-fn result collapsed-tx))
                     (concat-tx-fn result collapsed-tx))
                   (move-blocks repo conn blocks' left (merge opts {:sibling? false
                   (move-blocks repo conn blocks' left (merge opts {:sibling? false
-                                                              :indent? true}))))))
+                                                                   :indent? true}))))))
           (let [parent-original (when get-first-block-original (get-first-block-original))]
           (let [parent-original (when get-first-block-original (get-first-block-original))]
             (if parent-original
             (if parent-original
               (let [blocks' (take-while (fn [b]
               (let [blocks' (take-while (fn [b]
-                                         (not= (:db/id (:block/parent b))
-                                               (:db/id (:block/parent parent))))
-                                       top-level-blocks)]
-               (move-blocks repo conn blocks' parent-original (merge opts {:outliner-op :indent-outdent-blocks
-                                                                      :sibling? true
-                                                                      :indent? false})))
-
-             (when (and parent (not (page-block? (d/entity db (:db/id parent)))))
-               (let [blocks' (take-while (fn [b]
-                                           (not= (:db/id (:block/parent b))
-                                                 (:db/id (:block/parent parent))))
-                                         top-level-blocks)
-                     result (move-blocks repo conn blocks' parent (merge opts {:sibling? true}))]
-                 (if logical-outdenting?
-                   result
+                                          (not= (:db/id (:block/parent b))
+                                                (:db/id (:block/parent parent))))
+                                        top-level-blocks)]
+                (move-blocks repo conn blocks' parent-original (merge opts {:outliner-op :indent-outdent-blocks
+                                                                            :sibling? true
+                                                                            :indent? false})))
+
+              (when (and parent (not (page-block? (d/entity db (:db/id parent)))))
+                (let [blocks' (take-while (fn [b]
+                                            (not= (:db/id (:block/parent b))
+                                                  (:db/id (:block/parent parent))))
+                                          top-level-blocks)
+                      result (move-blocks repo conn blocks' parent (merge opts {:sibling? true}))]
+                  (if logical-outdenting?
+                    result
                   ;; direct outdenting (default behavior)
                   ;; direct outdenting (default behavior)
-                   (let [last-top-block (d/entity db (:db/id (last blocks')))
-                         right-siblings (->> (get-right-siblings conn (block db last-top-block))
-                                             (map :data))]
-                     (if (seq right-siblings)
-                       (let [result2 (if-let [last-direct-child-id (ldb/get-block-last-direct-child-id db (:db/id last-top-block))]
-                                       (move-blocks repo conn right-siblings (d/entity db last-direct-child-id) (merge opts {:sibling? true}))
-                                       (move-blocks repo conn right-siblings last-top-block (merge opts {:sibling? false})))]
-                         (concat-tx-fn result result2))
-                       result))))))))))))
+                    (let [last-top-block (d/entity db (:db/id (last blocks')))
+                          right-siblings (->> (get-right-siblings conn (block db last-top-block))
+                                              (map :data))]
+                      (if (seq right-siblings)
+                        (let [result2 (if-let [last-direct-child-id (ldb/get-block-last-direct-child-id db (:db/id last-top-block))]
+                                        (move-blocks repo conn right-siblings (d/entity db last-direct-child-id) (merge opts {:sibling? true}))
+                                        (move-blocks repo conn right-siblings last-top-block (merge opts {:sibling? false})))]
+                          (concat-tx-fn result result2))
+                        result))))))))))))
 
 
 ;;; ### write-operations have side-effects (do transactions) ;;;;;;;;;;;;;;;;
 ;;; ### write-operations have side-effects (do transactions) ;;;;;;;;;;;;;;;;
 
 

+ 0 - 3
src/main/frontend/components/block.cljs

@@ -2218,9 +2218,6 @@
                                                 (property-file/remove-built-in-properties-when-file-based
                                                 (property-file/remove-built-in-properties-when-file-based
                                                  (state/get-current-repo) format)
                                                  (state/get-current-repo) format)
                                                 (drawer/remove-logbook)))]
                                                 (drawer/remove-logbook)))]
-                            ;; save current editing block
-                             (let [{:keys [value] :as state} (editor-handler/get-state)]
-                               (editor-handler/save-block! state value))
                              (state/set-editing!
                              (state/set-editing!
                               edit-input-id
                               edit-input-id
                               content
                               content

+ 0 - 1
src/main/frontend/components/editor.cljs

@@ -737,7 +737,6 @@
                   :on-click          (editor-handler/editor-on-click! id)
                   :on-click          (editor-handler/editor-on-click! id)
                   :on-change         (editor-handler/editor-on-change! block id search-timeout)
                   :on-change         (editor-handler/editor-on-change! block id search-timeout)
                   :on-paste          (paste-handler/editor-on-paste! id)
                   :on-paste          (paste-handler/editor-on-paste! id)
-                  :on-blur           (fn [] (editor-handler/save-current-block!))
                   :auto-focus        false
                   :auto-focus        false
                   :class             heading-class}
                   :class             heading-class}
                (some? parent-block)
                (some? parent-block)

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

@@ -943,8 +943,10 @@
   ([block-id]
   ([block-id]
    (copy-block-ref! block-id #(str %)))
    (copy-block-ref! block-id #(str %)))
   ([block-id tap-clipboard]
   ([block-id tap-clipboard]
-   (set-blocks-id! [block-id])
-   (util/copy-to-clipboard! (tap-clipboard block-id))))
+   (p/do!
+    (save-current-block!)
+    (set-blocks-id! [block-id])
+    (util/copy-to-clipboard! (tap-clipboard block-id)))))
 
 
 (defn select-block!
 (defn select-block!
   [block-uuid]
   [block-uuid]
@@ -1757,7 +1759,6 @@
   [up?]
   [up?]
   (fn [event]
   (fn [event]
     (util/stop event)
     (util/stop event)
-    (save-current-block!)
     (let [edit-block-id (:block/uuid (state/get-edit-block))
     (let [edit-block-id (:block/uuid (state/get-edit-block))
           move-nodes (fn [blocks]
           move-nodes (fn [blocks]
                        (let [blocks' (block-handler/get-top-level-blocks blocks)
                        (let [blocks' (block-handler/get-top-level-blocks blocks)
@@ -1769,9 +1770,10 @@
                          result))]
                          result))]
       (if edit-block-id
       (if edit-block-id
         (when-let [block (db/pull [:block/uuid edit-block-id])]
         (when-let [block (db/pull [:block/uuid edit-block-id])]
-          (let [blocks [block]
+          (let [blocks [(assoc block :block/content (state/get-edit-content))]
                 pos (state/get-edit-pos)]
                 pos (state/get-edit-pos)]
             (p/do!
             (p/do!
+             (save-current-block!)
              (move-nodes blocks)
              (move-nodes blocks)
              (when-let [input-id (state/get-edit-input-id)]
              (when-let [input-id (state/get-edit-input-id)]
                (when-let [input (gdom/getElement input-id)]
                (when-let [input (gdom/getElement input-id)]

+ 1 - 2
src/main/frontend/handler/editor/keyboards.cljs

@@ -2,8 +2,7 @@
   (:require [frontend.handler.editor :as editor-handler]
   (:require [frontend.handler.editor :as editor-handler]
             [frontend.mixins :as mixins]
             [frontend.mixins :as mixins]
             [frontend.state :as state]
             [frontend.state :as state]
-            [goog.dom :as gdom]
-            [frontend.util :as util]))
+            [goog.dom :as gdom]))
 
 
 ;; TODO: don't depend on handler.editor
 ;; TODO: don't depend on handler.editor
 
 

+ 6 - 2
src/main/frontend/handler/editor/lifecycle.cljs

@@ -4,7 +4,8 @@
             [frontend.state :as state]
             [frontend.state :as state]
             [frontend.util :as util]
             [frontend.util :as util]
             [frontend.util.cursor :as cursor]
             [frontend.util.cursor :as cursor]
-            [goog.dom :as gdom]))
+            [goog.dom :as gdom]
+            [frontend.db :as db]))
 
 
 (defn did-mount!
 (defn did-mount!
   [state]
   [state]
@@ -35,7 +36,10 @@
 
 
 (defn will-unmount
 (defn will-unmount
   [state]
   [state]
-  (editor-handler/clear-when-saved!)
+  (let [{:keys [value] :as state} (editor-handler/get-state)]
+    (editor-handler/clear-when-saved!)
+    (when (db/entity [:block/uuid (:block/uuid (:block state))]) ; block still exists
+      (editor-handler/save-block! state value)))
   state)
   state)
 
 
 (def lifecycle
 (def lifecycle

+ 44 - 42
src/main/frontend/handler/file_based/property.cljs

@@ -6,7 +6,8 @@
             [logseq.outliner.core :as outliner-core]
             [logseq.outliner.core :as outliner-core]
             [frontend.modules.outliner.ui :as ui-outliner-tx]
             [frontend.modules.outliner.ui :as ui-outliner-tx]
             [frontend.state :as state]
             [frontend.state :as state]
-            [logseq.common.util :as common-util]))
+            [logseq.common.util :as common-util]
+            [promesa.core :as p]))
 
 
 (defn insert-property
 (defn insert-property
   [format content key value & args]
   [format content key value & args]
@@ -23,48 +24,49 @@
   "col: a collection of [block-id property-key property-value]."
   "col: a collection of [block-id property-key property-value]."
   [col]
   [col]
   (let [col' (group-by first col)]
   (let [col' (group-by first col)]
-    (ui-outliner-tx/transact!
-     {:outliner-op :save-block}
-     (doseq [[block-id items] col']
-       (let [block-id (if (string? block-id) (uuid block-id) block-id)
-             new-properties (zipmap (map second items)
-                                    (map last items))]
-         (when-let [block (db/entity [:block/uuid block-id])]
-           (let [format (:block/format block)
-                 content (:block/content block)
-                 properties (:block/properties block)
-                 properties-text-values (:block/properties-text-values block)
-                 properties (-> (merge properties new-properties)
-                                common-util/remove-nils-non-nested)
-                 properties-text-values (-> (merge properties-text-values new-properties)
-                                            common-util/remove-nils-non-nested)
-                 property-ks (->> (concat (:block/properties-order block)
-                                          (map second items))
-                                  (filter (set (keys properties)))
-                                  distinct
-                                  vec)
-                 content (property-util/remove-properties format content)
-                 kvs (for [key property-ks] [key (or (get properties-text-values key)
-                                                     (get properties key))])
-                 content (property-util/insert-properties format content kvs)
-                 content (property-util/remove-empty-properties content)
-                 block {:block/uuid block-id
-                        :block/properties properties
-                        :block/properties-order property-ks
-                        :block/properties-text-values properties-text-values
-                        :block/content content}]
-             (outliner-core/save-block! (state/get-current-repo) (db/get-db false)
-                                        (state/get-date-formatter)
-                                        block)))))))
-  (let [block-id (ffirst col)
-        block-id (if (string? block-id) (uuid block-id) block-id)
-        input-pos (or (state/get-edit-pos) :max)]
+    (p/do!
+     (ui-outliner-tx/transact!
+      {:outliner-op :save-block}
+      (doseq [[block-id items] col']
+        (let [block-id (if (string? block-id) (uuid block-id) block-id)
+              new-properties (zipmap (map second items)
+                                     (map last items))]
+          (when-let [block (db/entity [:block/uuid block-id])]
+            (let [format (:block/format block)
+                  content (:block/content block)
+                  properties (:block/properties block)
+                  properties-text-values (:block/properties-text-values block)
+                  properties (-> (merge properties new-properties)
+                                 common-util/remove-nils-non-nested)
+                  properties-text-values (-> (merge properties-text-values new-properties)
+                                             common-util/remove-nils-non-nested)
+                  property-ks (->> (concat (:block/properties-order block)
+                                           (map second items))
+                                   (filter (set (keys properties)))
+                                   distinct
+                                   vec)
+                  content (property-util/remove-properties format content)
+                  kvs (for [key property-ks] [key (or (get properties-text-values key)
+                                                      (get properties key))])
+                  content (property-util/insert-properties format content kvs)
+                  content (property-util/remove-empty-properties content)
+                  block {:block/uuid block-id
+                         :block/properties properties
+                         :block/properties-order property-ks
+                         :block/properties-text-values properties-text-values
+                         :block/content content}]
+              (outliner-core/save-block! (state/get-current-repo) (db/get-db false)
+                                         (state/get-date-formatter)
+                                         block))))))
+     (let [block-id (ffirst col)
+           block-id (if (string? block-id) (uuid block-id) block-id)
+           input-pos (or (state/get-edit-pos) :max)]
     ;; update editing input content
     ;; update editing input content
-    (when-let [editing-block (state/get-edit-block)]
-      (when (= (:block/uuid editing-block) block-id)
-        (block-handler/edit-block! editing-block
-                                   input-pos
-                                   (state/get-edit-input-id))))))
+       (when-let [editing-block (state/get-edit-block)]
+         (when (= (:block/uuid editing-block) block-id)
+           (block-handler/edit-block! editing-block
+                                      input-pos
+                                      (state/get-edit-input-id))))))))
 
 
 (defn batch-set-block-property!
 (defn batch-set-block-property!
   [block-ids property-key property-value]
   [block-ids property-key property-value]