Browse Source

refactor indent-and-outdent

Tienson Qin 1 year ago
parent
commit
4b9b825101

+ 30 - 25
deps/outliner/src/logseq/outliner/core.cljs

@@ -1067,7 +1067,7 @@
 
 (defn- ^:large-vars/cleanup-todo indent-outdent-blocks
   "Indent or outdent `blocks`."
-  [repo conn blocks indent? & {:keys [get-first-block-original logical-outdenting?]}]
+  [repo conn blocks indent? & {:keys [parent-original logical-outdenting?]}]
   {:pre [(seq blocks) (boolean? indent?)]}
   (let [db @conn
         top-level-blocks (map (fn [b] (d/entity db (:db/id b))) blocks)
@@ -1100,34 +1100,33 @@
                     (concat-tx-fn result collapsed-tx))
                   (move-blocks repo conn blocks' left (merge opts {:sibling? false
                                                                    :indent? true}))))))
-          (let [parent-original (when get-first-block-original (get-first-block-original))]
-            (if parent-original
+          (if parent-original
+            (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)]
-                (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
+                                        top-level-blocks)
+                    result (move-blocks repo conn blocks' parent (merge opts {:sibling? true}))]
+                (if logical-outdenting?
+                  result
                   ;; 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) ;;;;;;;;;;;;;;;;
 
@@ -1141,6 +1140,12 @@
   see also `logseq.outliner.transaction/transact!`"
   (atom nil))
 
+(defn clear-and-get-tx-data!
+  []
+  (let [result @*transaction-data]
+    (reset! *transaction-data nil)
+    result))
+
 (defn- op-transact!
   [fn-var & args]
   {:pre [(var? fn-var)]}

+ 0 - 1
deps/outliner/src/logseq/outliner/transaction.cljc

@@ -59,7 +59,6 @@
 
             (when (seq all-tx#) ;; If it's empty, do nothing
               (when-not (:nested-transaction? opts#) ; transact only for the whole transaction
-                (prn :debug :data @transact-data#)
                 (logseq.outliner.datascript/transact! all-tx# (dissoc opts## :transact-opts) (:transact-opts opts##))))
 
             (reset! logseq.outliner.core/*transaction-data nil)

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

@@ -24,7 +24,9 @@
             [frontend.worker.async-util :include-macros true :refer [<?]]
             [frontend.worker.util :as worker-util]
             [frontend.worker.handler.page.rename :as worker-page-rename]
-            [frontend.worker.handler.page :as worker-page]))
+            [frontend.worker.handler.page :as worker-page]
+            [logseq.outliner.core :as outliner-core]
+            [logseq.outliner.transaction :as outliner-tx]))
 
 (defonce *sqlite worker-state/*sqlite)
 (defonce *sqlite-conns worker-state/*sqlite-conns)
@@ -293,6 +295,9 @@
        (let []
          (pr-str result)))))
 
+  (get-block-neighbors
+   [_this repo id])
+
   (get-block-and-children
    [_this repo name children?]
    (assert (string? name))
@@ -446,6 +451,7 @@
    (when-let [conn (worker-state/get-datascript-conn repo)]
      (search/build-blocks-indice repo @conn)))
 
+  ;; page ops
   (page-search
    [this repo q limit]
    (when-let [conn (worker-state/get-datascript-conn repo)]
@@ -461,10 +467,40 @@
   (page-delete
    [this repo page-name]
    (when-let [conn (worker-state/get-datascript-conn repo)]
-     (let [config (worker-state/get-config repo)
-           result (worker-page/delete! repo conn page-name nil {})]
+     (let [result (worker-page/delete! repo conn page-name nil {})]
        (bean/->js {:result result}))))
 
+  ;; block ops
+
+  (delete-blocks
+   [this repo block-ids opts-str]
+   (when-let [conn (worker-state/get-datascript-conn repo)]
+     (let [block-ids (remove nil? block-ids)]
+       (when (seq block-ids)
+         (let [opts (if opts-str
+                      (edn/read-string opts-str)
+                      {})
+               blocks (map #(d/entity @conn %) block-ids)]
+           (outliner-tx/transact!
+             {:local-tx? true           ; keep this transaction in undo/redo history
+             :outliner-op :delete-blocks
+             :transact-opts {:repo repo
+                             :conn conn}}
+            (outliner-core/delete-blocks! repo conn
+                                          (worker-state/get-date-formatter repo)
+                                          blocks
+                                          opts)))))
+     nil))
+
+  (move-blocks-up-down
+   [this repo block-ids up?]
+   (when-let [conn (worker-state/get-datascript-conn repo)]
+     (let [block-ids (remove nil? block-ids)]
+       (when (seq block-ids)
+         (let [blocks (map #(d/entity @conn %) block-ids)
+               _ (outliner-core/move-blocks-up-down! repo conn blocks up?)]
+           (pr-str (outliner-core/clear-and-get-tx-data!)))))))
+
   (file-writes-finished?
    [this repo]
    (let [conn (worker-state/get-datascript-conn repo)

+ 16 - 13
src/main/frontend/handler/block.cljs

@@ -326,17 +326,20 @@
                                      last)]
        (get-original-block-by-dom last-block-node)))))
 
-(defn indent-outdent-block!
-  [block direction]
-  (ui-outliner-tx/transact!
-   {:outliner-op :move-blocks
-    :real-outliner-op :indent-outdent}
-   (outliner-core/indent-outdent-blocks! (state/get-current-repo)
-                                         (db/get-db false)
-                                         (get-top-level-blocks [block])
-                                         (= direction :right)
-                                         {:get-first-block-original get-first-block-original
-                                          :logical-outdenting? (state/logical-outdenting?)})))
+(defn indent-outdent-blocks!
+  [blocks indent? save-current-block]
+  (when (seq blocks)
+    (let [blocks (get-top-level-blocks blocks)]
+      (ui-outliner-tx/transact!
+       {:outliner-op :move-blocks
+        :real-outliner-op :indent-outdent}
+       (when save-current-block (save-current-block))
+       (outliner-core/indent-outdent-blocks! (state/get-current-repo)
+                                             (db/get-db false)
+                                             (get-top-level-blocks blocks)
+                                             indent?
+                                             {:parent-original (get-first-block-original)
+                                              :logical-outdenting? (state/logical-outdenting?)})))))
 
 (def *swipe (atom nil))
 
@@ -461,13 +464,13 @@
             (and left-menu (>= (.-clientWidth left-menu) 40))
             (when (indentable? block)
               (haptics/with-haptics-impact
-                (indent-outdent-block! block :right)
+                (indent-outdent-blocks! [block] true nil)
                 :light))
 
             (and right-menu (<= 40 (.-clientWidth right-menu) 79))
             (when (outdentable? block)
               (haptics/with-haptics-impact
-                (indent-outdent-block! block :left)
+                (indent-outdent-blocks! [block] false nil)
                 :light))
 
             (and right-menu (>= (.-clientWidth right-menu) 80))

+ 17 - 33
src/main/frontend/handler/editor.cljs

@@ -686,14 +686,15 @@
     (when block
       (let [blocks (block-handler/get-top-level-blocks [block])]
         (state/set-state! :ui/deleting-block uuid)
-        (ui-outliner-tx/transact!
-         {:outliner-op :delete-blocks}
-         (outliner-core/delete-blocks! repo (db/get-db false)
-                                       (state/get-date-formatter)
-                                       blocks
-                                       (merge
-                                        delete-opts
-                                        {:children? children?})))))))
+        (p/do!
+         (ui-outliner-tx/transact!
+          {:outliner-op :delete-blocks}
+          (outliner-core/delete-blocks! repo (db/get-db false)
+                                        (state/get-date-formatter)
+                                        blocks
+                                        (merge
+                                         delete-opts
+                                         {:children? children?}))))))))
 
 (defn- move-to-prev-block
   [repo sibling-block format _id value]
@@ -845,9 +846,8 @@
       (p/do!
        (ui-outliner-tx/transact!
         {:outliner-op :delete-blocks}
-        (outliner-core/delete-blocks! repo (db/get-db false)
-                                      (state/get-date-formatter)
-                                      blocks' {}))
+        (when-let [^Object worker @state/*db-worker]
+          (.delete-blocks worker repo (clj->js (map :db/id blocks')) nil)))
        (when sibling-block
          (move-to-prev-block repo sibling-block
                              (:block/format block)
@@ -1740,12 +1740,14 @@
   [up?]
   (fn [event]
     (util/stop event)
-    (let [edit-block-id (:block/uuid (state/get-edit-block))
+    (let [repo (state/get-current-repo)
+          edit-block-id (:block/uuid (state/get-edit-block))
           move-nodes (fn [blocks]
                        (let [blocks' (block-handler/get-top-level-blocks blocks)
                              result (ui-outliner-tx/transact!
                                      {:outliner-op :move-blocks}
-                                     (outliner-core/move-blocks-up-down! (state/get-current-repo) (db/get-db false) blocks' up?))]
+                                      (when-let [^Object worker @state/*db-worker]
+                                        (.move-blocks-up-down worker repo (clj->js (map :db/id blocks')) up?)))]
                          (when-let [block-node (util/get-first-block-by-id (:block/uuid (first blocks)))]
                            (.scrollIntoView block-node #js {:behavior "smooth" :block "nearest"}))
                          result))]
@@ -1782,16 +1784,7 @@
   "`direction` = :left | :right."
   [direction]
   (let [blocks (get-selected-ordered-blocks)]
-    (when (seq blocks)
-      (ui-outliner-tx/transact!
-       {:outliner-op :move-blocks
-        :real-outliner-op :indent-outdent}
-       (outliner-core/indent-outdent-blocks! (state/get-current-repo)
-                                             (db/get-db false)
-                                             (block-handler/get-top-level-blocks blocks)
-                                             (= direction :right)
-                                             {:get-first-block-original block-handler/get-first-block-original
-                                              :logical-outdenting? (state/logical-outdenting?)})))))
+    (block-handler/indent-outdent-blocks! blocks (= direction :right) nil)))
 
 (defn- get-link [format link label]
   (let [link (or link "")
@@ -2866,16 +2859,7 @@
         {:keys [block]} (get-state)]
     (when block
       (state/set-editor-last-pos! pos)
-      (ui-outliner-tx/transact!
-       {:outliner-op :move-blocks
-        :real-outliner-op :indent-outdent}
-       (save-current-block!)
-       (outliner-core/indent-outdent-blocks! (state/get-current-repo)
-                                             (db/get-db false)
-                                             (block-handler/get-top-level-blocks [block])
-                                             indent?
-                                             {:get-first-block-original block-handler/get-first-block-original
-                                              :logical-outdenting? (state/logical-outdenting?)})))))
+      (block-handler/indent-outdent-blocks! [block] indent? save-current-block!))))
 
 (defn keydown-tab-handler
   [direction]

+ 6 - 1
src/main/frontend/worker/state.cljs

@@ -1,6 +1,7 @@
 (ns frontend.worker.state
   "State hub for worker"
-  (:require [logseq.common.util :as common-util]))
+  (:require [logseq.common.util :as common-util]
+            [logseq.common.config :as common-config]))
 
 (defonce *state (atom {:worker/object nil
 
@@ -79,3 +80,7 @@
 (defn get-worker-object
   []
   (:worker/object @*state))
+
+(defn get-date-formatter
+  [repo]
+  (common-config/get-date-formatter (get-config repo)))