Ver Fonte

wip: remove dependency on frontend.db for outliner core

Tienson Qin há 1 ano atrás
pai
commit
e579f668eb

+ 42 - 1
deps/db/src/logseq/db.cljs

@@ -289,6 +289,43 @@
   (when-let [parent (d/entity db [:block/uuid block-uuid])]
     (sort-by-left (:block/_parent parent) parent)))
 
+(defn get-block-parents
+  [db block-id {:keys [depth] :or {depth 100}}]
+  (loop [block-id block-id
+         parents (list)
+         d 1]
+    (if (> d depth)
+      parents
+      (if-let [parent (:block/parent (d/entity db [:block/uuid block-id]))]
+        (recur (:block/uuid parent) (conj parents parent) (inc d))
+        parents))))
+
+(defn get-block-children-ids
+  [db block-uuid]
+  (when-let [eid (:db/id (d/entity db [:block/uuid block-uuid]))]
+    (let [seen   (volatile! [])]
+      (loop [steps          100      ;check result every 100 steps
+             eids-to-expand [eid]]
+        (when (seq eids-to-expand)
+          (let [eids-to-expand*
+                (mapcat (fn [eid] (map first (d/datoms db :avet :block/parent eid))) eids-to-expand)
+                uuids-to-add (remove nil? (map #(:block/uuid (d/entity db %)) eids-to-expand*))]
+            (when (and (zero? steps)
+                       (seq (set/intersection (set @seen) (set uuids-to-add))))
+              (throw (ex-info "bad outliner data, need to re-index to fix"
+                              {:seen @seen :eids-to-expand eids-to-expand})))
+            (vswap! seen (partial apply conj) uuids-to-add)
+            (recur (if (zero? steps) 100 (dec steps)) eids-to-expand*))))
+      @seen)))
+
+(defn get-block-children
+  "Including nested children."
+  [db block-uuid]
+  (let [ids (get-block-children-ids db block-uuid)]
+    (when (seq ids)
+      (let [ids' (map (fn [id] [:block/uuid id]) ids)]
+        (d/pull-many db '[*] ids')))))
+
 (defn- get-sorted-page-block-ids
   [db page-id]
   (let [root (d/entity db page-id)]
@@ -335,7 +372,7 @@
 
 (defn- consecutive-block?
   [db block-1 block-2]
-  (let [        aux-fn (fn [block-1 block-2]
+  (let [aux-fn (fn [block-1 block-2]
                  (and (= (:block/page block-1) (:block/page block-2))
                       (or
                        ;; sibling or child
@@ -353,3 +390,7 @@
         (when-not (consecutive-block? db (nth blocks i) (nth blocks (inc i)))
           (nth blocks i))))
     blocks)))
+
+(defn new-block-id
+  []
+  (d/squuid))

+ 4 - 33
deps/outliner/src/logseq/outliner/pipeline.cljs

@@ -1,7 +1,8 @@
 (ns logseq.outliner.pipeline
   "Core fns for use with frontend.modules.outliner.pipeline"
   (:require [datascript.core :as d]
-            [clojure.set :as set]))
+            [clojure.set :as set]
+            [logseq.db :as ldb]))
 
 (defn filter-deleted-blocks
   [datoms]
@@ -11,36 +12,6 @@
        (:v d)))
    datoms))
 
-;; non recursive query
-(defn ^:api get-block-parents
-  [db block-id {:keys [depth] :or {depth 100}}]
-  (loop [block-id block-id
-         parents (list)
-         d 1]
-    (if (> d depth)
-      parents
-      (if-let [parent (:block/parent (d/entity db [:block/uuid block-id]))]
-        (recur (:block/uuid parent) (conj parents parent) (inc d))
-        parents))))
-
-(defn ^:api get-block-children-ids
-  [db block-uuid]
-  (when-let [eid (:db/id (d/entity db [:block/uuid block-uuid]))]
-    (let [seen   (volatile! [])]
-      (loop [steps          100      ;check result every 100 steps
-             eids-to-expand [eid]]
-        (when (seq eids-to-expand)
-          (let [eids-to-expand*
-                (mapcat (fn [eid] (map first (d/datoms db :avet :block/parent eid))) eids-to-expand)
-                uuids-to-add (remove nil? (map #(:block/uuid (d/entity db %)) eids-to-expand*))]
-            (when (and (zero? steps)
-                       (seq (set/intersection (set @seen) (set uuids-to-add))))
-              (throw (ex-info "bad outliner data, need to re-index to fix"
-                              {:seen @seen :eids-to-expand eids-to-expand})))
-            (vswap! seen (partial apply conj) uuids-to-add)
-            (recur (if (zero? steps) 100 (dec steps)) eids-to-expand*))))
-      @seen)))
-
 ;; TODO: it'll be great if we can calculate the :block/path-refs before any
 ;; outliner transaction, this way we can group together the real outliner tx
 ;; and the new path-refs changes, which makes both undo/redo and
@@ -58,7 +29,7 @@
     (mapcat (fn [block]
               (when (and (not (@*computed-ids (:block/uuid block))) ; not computed yet
                          (not (:block/name block)))
-                (let [parents (get-block-parents db-after (:block/uuid block) {})
+                (let [parents (ldb/get-block-parents db-after (:block/uuid block) {})
                       parents-refs (->> (mapcat :block/path-refs parents)
                                         (map :db/id))
                       old-refs (if db-before
@@ -69,7 +40,7 @@
                                      (map :db/id (:block/refs block))
                                      parents-refs))
                       refs-changed? (not= old-refs new-refs)
-                      children (get-block-children-ids db-after (:block/uuid block))
+                      children (ldb/get-block-children-ids db-after (:block/uuid block))
                             ;; Builds map of children ids to their parent id and :block/refs ids
                       children-maps (into {}
                                           (map (fn [id]

+ 3 - 3
deps/outliner/test/logseq/outliner/pipeline_test.cljs

@@ -2,7 +2,7 @@
   (:require [cljs.test :refer [deftest is]]
             [logseq.db.frontend.schema :as db-schema]
             [datascript.core :as d]
-            [logseq.outliner.pipeline :as outliner-pipeline]))
+            [logseq.db :as ldb]))
 
 
 ;;; datoms
@@ -24,6 +24,6 @@
   (let [db (d/db-with (d/empty-db db-schema/schema)
                       broken-outliner-data-with-cycle)]
     (is (= "bad outliner data, need to re-index to fix"
-           (try (outliner-pipeline/get-block-children-ids db #uuid "e538d319-48d4-4a6d-ae70-c03bb55b6fe4")
+           (try (ldb/get-block-children-ids db #uuid "e538d319-48d4-4a6d-ae70-c03bb55b6fe4")
                 (catch :default e
-                  (ex-message e)))))))
+                  (ex-message e)))))))

+ 4 - 6
src/main/frontend/db.cljs

@@ -1,14 +1,14 @@
 (ns frontend.db
   "Main entry ns for db related fns"
-  (:require [datascript.core :as d]
-            [frontend.db.conn :as conn]
+  (:require [frontend.db.conn :as conn]
             [frontend.db.model]
             [frontend.db.query-custom]
             [frontend.db.query-react]
             [frontend.db.react :as react]
             [frontend.db.utils]
             [frontend.namespaces :refer [import-vars]]
-            [logseq.db.frontend.default :as default-db]))
+            [logseq.db.frontend.default :as default-db]
+            [logseq.db :as ldb]))
 
 (import-vars
  [frontend.db.conn
@@ -66,6 +66,4 @@
   ([repo option]
    (conn/start! repo option)))
 
-(defn new-block-id
-  []
-  (d/squuid))
+(def new-block-id ldb/new-block-id)

+ 2 - 4
src/main/frontend/db/model.cljs

@@ -13,12 +13,10 @@
             [frontend.state :as state]
             [frontend.util :as util :refer [react]]
             [frontend.util.drawer :as drawer]
-            [logseq.db.frontend.default :as default-db]
             [logseq.db.frontend.rules :as rules]
             [logseq.graph-parser.text :as text]
             [logseq.graph-parser.util.db :as db-util]
             [logseq.graph-parser.util :as gp-util]
-            [logseq.outliner.pipeline :as outliner-pipeline]
             [cljs-time.core :as t]
             [cljs-time.format :as tf]
             ;; add map ops to datascript Entity
@@ -466,7 +464,7 @@ independent of format as format specific heading characters are stripped"
 (defn get-block-parents
   [repo block-id opts]
   (when-let [db (conn/get-db repo)]
-    (outliner-pipeline/get-block-parents db block-id opts)))
+    (ldb/get-block-parents db block-id opts)))
 
 ;; Use built-in recursive
 (defn get-block-parents-v2
@@ -596,7 +594,7 @@ independent of format as format specific heading characters are stripped"
   "Including nested children."
   [repo block-uuid]
   (when-let [db (conn/get-db repo)]
-    (let [ids (outliner-pipeline/get-block-children-ids db block-uuid)]
+    (let [ids (ldb/get-block-children-ids db block-uuid)]
      (when (seq ids)
        (let [ids' (map (fn [id] [:block/uuid id]) ids)]
          (db-utils/pull-many repo '[*] ids'))))))

+ 3 - 3
src/main/frontend/db/rtc/core.cljs

@@ -88,12 +88,12 @@
 (defmethod transact-db! :delete-blocks [_ & args]
   (outliner-tx/transact!
    {:persist-op? false}
-   (apply outliner-core/delete-blocks! args)))
+   (apply outliner-core/delete-blocks! (state/get-current-repo) (db/get-db false) args)))
 
 (defmethod transact-db! :move-blocks [_ & args]
   (outliner-tx/transact!
    {:persist-op? false}
-   (apply outliner-core/move-blocks! args)))
+   (apply outliner-core/move-blocks! (db/get-db false) args)))
 
 (defmethod transact-db! :insert-blocks [_ & args]
   (outliner-tx/transact!
@@ -103,7 +103,7 @@
 (defmethod transact-db! :save-block [_ & args]
   (outliner-tx/transact!
    {:persist-op? false}
-   (apply outliner-core/save-block! args)))
+   (apply outliner-core/save-block! (state/get-current-repo) (db/get-db false) args)))
 
 (defmethod transact-db! :delete-whiteboard-blocks [_ repo block-uuids]
   (db/transact! repo

+ 1 - 1
src/main/frontend/handler/block.cljs

@@ -61,7 +61,7 @@
   [block direction]
   (ui-outliner-tx/transact!
    {:outliner-op :move-blocks}
-   (outliner-core/indent-outdent-blocks! [block] (= direction :right))))
+   (outliner-core/indent-outdent-blocks! (db/get-db false) [block] (= direction :right))))
 
 (defn select-block!
   [block-uuid]

+ 1 - 1
src/main/frontend/handler/db_based/editor.cljs

@@ -153,5 +153,5 @@
   (ui-outliner-tx/transact!
    {:outliner-op :save-block}
    (doseq [block-tx (keep #(set-heading-aux! % heading) block-ids)]
-     (outliner-core/save-block! block-tx))
+     (outliner-core/save-block! repo (db/get-db false) block-tx))
    (property-handler/batch-set-block-property! repo block-ids :heading heading)))

+ 1 - 1
src/main/frontend/handler/db_based/page.cljs

@@ -70,7 +70,7 @@
           from-page (db/entity [:block/name from-page-name])
           from-id (:db/id from-page)
           from-first-child (some->> (db/pull from-id)
-                                    (outliner-core/block)
+                                    (outliner-core/block (db/get-db))
                                     (otree/-get-down (db/get-db false))
                                     (outliner-core/get-data))
           to-last-direct-child-id (model/get-block-last-direct-child-id (db/get-db) to-id)

+ 3 - 1
src/main/frontend/handler/db_based/property.cljs

@@ -413,7 +413,9 @@
                                                           (some? (get-in property-block [:block/metadata :created-from-block]))
                                                           (some? (get-in property-block [:block/metadata :created-from-property])))
                                                  (let [txs-state (atom [])]
-                                                   (outliner-core/delete-block txs-state
+                                                   (outliner-core/delete-block repo
+                                                                               (db/get-db false)
+                                                                               txs-state
                                                                                (outliner-core/->Block property-block)
                                                                                {:children? true})
                                                    @txs-state))]

+ 4 - 4
src/main/frontend/handler/dnd.cljs

@@ -39,7 +39,7 @@
                           :clear? true}])
 
       (every? map? (conj blocks' target-block))
-      (let [target-node (outliner-core/block target-block)
+      (let [target-node (outliner-core/block (db/get-db) target-block)
             conn (db/get-db false)]
         (ui-outliner-tx/transact!
          {:outliner-op :move-blocks}
@@ -50,10 +50,10 @@
                     (otree/-get-left-id target-node conn))]
              (if first-child?
                (when-let [parent (otree/-get-parent target-node conn)]
-                 (outliner-core/move-blocks! blocks' (:data parent) false))
+                 (outliner-core/move-blocks! conn blocks' (:data parent) false))
                (when-let [before-node (otree/-get-left target-node conn)]
-                 (outliner-core/move-blocks! blocks' (:data before-node) true))))
-           (outliner-core/move-blocks! blocks' target-block (not nested?)))))
+                 (outliner-core/move-blocks! conn blocks' (:data before-node) true))))
+           (outliner-core/move-blocks! conn blocks' target-block (not nested?)))))
 
       :else
       nil)))

+ 131 - 126
src/main/frontend/handler/editor.cljs

@@ -75,6 +75,12 @@
 (def clear-selection! state/clear-selection!)
 (def edit-block! block-handler/edit-block!)
 
+(defn- outliner-save-block!
+  [block]
+  (let [repo (state/get-current-repo)
+        conn (db/get-db false)]
+    (outliner-core/save-block! repo conn block)))
+
 (defn get-block-own-order-list-type
   [block]
   (let [properties (:block/properties block)]
@@ -258,17 +264,17 @@
                :block/content value}]
     (profile
      "Save block: "
-      (let [block' (-> (wrap-parse-block block)
+     (let [block' (-> (wrap-parse-block block)
                       ;; :block/uuid might be changed when backspace/delete
                       ;; a block that has been refed
                       (assoc :block/uuid (:block/uuid block)))
-            opts' (assoc opts :outliner-op :save-block)]
-        (let [original-block (db/entity (:db/id block))
+           opts' (assoc opts :outliner-op :save-block)]
+       (let [original-block (db/entity (:db/id block))
              original-props (:block/properties original-block)
              {:keys [tx-data]}
              (ui-outliner-tx/transact!
               opts'
-              (outliner-core/save-block! block')
+              (outliner-save-block! block')
               ;; page properties changed
               (when-let [page-name (and (:block/pre-block? block')
                                         (not= original-props (:block/properties block'))
@@ -364,11 +370,11 @@
                    (not has-children?))]
     (ui-outliner-tx/transact!
      {:outliner-op :insert-blocks}
-      (save-current-block! {:current-block current-block
-                            :insert-block? true})
-     (outliner-core/insert-blocks! [new-block] current-block {:sibling? sibling?
-                                                              :keep-uuid? keep-uuid?
-                                                              :replace-empty-target? replace-empty-target?}))))
+     (save-current-block! {:current-block current-block
+                           :insert-block? true})
+     (outliner-core/insert-blocks! (db/get-db false) [new-block] current-block {:sibling? sibling?
+                                                                                :keep-uuid? keep-uuid?
+                                                                                :replace-empty-target? replace-empty-target?}))))
 
 (defn- block-self-alone-when-insert?
   [config uuid]
@@ -482,8 +488,8 @@
                           (if (and (some? (first (:block/_parent e)))
                                    (not (:block/collapsed? e)))
                           ;; object has children and not collapsed
-                          block'
-                          original-block)))
+                            block'
+                            original-block)))
                       block')
              insert-fn (cond
                          block-self?
@@ -704,9 +710,10 @@
     (when block
       (ui-outliner-tx/transact!
        {:outliner-op :delete-blocks}
-        (outliner-core/delete-blocks! [block] (merge
-                                               delete-opts
-                                               {:children? children?}))))))
+       (outliner-core/delete-blocks! repo (db/get-db false)
+                                     [block] (merge
+                                              delete-opts
+                                              {:children? children?}))))))
 
 (defn- move-to-prev-block
   [repo sibling-block format _id value]
@@ -717,7 +724,7 @@
           (do
             (edit-block! block :max (state/get-edit-block-node) {})
             {:prev-block block
-            :new-value (:block/original-name block)})
+             :new-value (:block/original-name block)})
           (let [edit-block (some-> (:db/id (state/get-edit-block)) db/entity)
                 edit-block-has-refs? (some? (:block/_refs edit-block))
                 db? (config/db-based-graph? repo)
@@ -771,7 +778,7 @@
             :else
             (let [has-children? (seq (:block/_parent block-e))
                   block (db/pull (:db/id block-e))
-                  left (otree/-get-left (outliner-core/block block) (db/get-db false))
+                  left (otree/-get-left (outliner-core/block (db/get-db) block) (db/get-db false))
                   left-has-children? (and left
                                           (when-let [block-id (:block/uuid (:data left))]
                                             (let [block (db/entity [:block/uuid block-id])]
@@ -806,10 +813,10 @@
                        (let [new-properties (merge (:block/properties (db/entity (:db/id prev-block)))
                                                    (:block/properties (db/entity (:db/id block))))]
                          (if (seq (:block/_refs (db/entity (:db/id block))))
-                           (let [block-right (outliner-core/get-right-sibling (:db/id block))]
+                           (let [block-right (outliner-core/get-right-sibling (db/get-db) (:db/id block))]
                              (delete-block-fn prev-block)
                              (save-block! repo block new-content {:editor/op :delete})
-                             (outliner-core/save-block! {:db/id (:db/id block)
+                             (outliner-save-block! {:db/id (:db/id block)
                                                          :block/parent (:db/id (:block/parent prev-block))
                                                          :block/left (or (:db/id (:block/left prev-block))
                                                                          (:db/id (:block/parent prev-block)))})
@@ -817,7 +824,7 @@
                              ;; block->right needs to point its `left` to block->left
                              (when (and block-right (not= (:db/id (:block/parent prev-block))
                                                           (:db/id (:block/parent block))))
-                               (outliner-core/save-block! {:db/id (:db/id block-right)
+                               (outliner-save-block! {:db/id (:db/id block-right)
                                                            :block/left (:db/id (:block/left block))}))
 
                              ;; update prev-block's children to point to the refed block
@@ -826,18 +833,18 @@
                                (let [children (:block/_parent prev-block)]
                                  (doseq [child children]
                                    (when-not (= (:db/id child) (:db/id block))
-                                     (outliner-core/save-block! {:db/id (:db/id child)
-                                                                :block/parent (:db/id block)
-                                                                :block/left (:db/id block)})))))
+                                     (outliner-save-block! {:db/id (:db/id child)
+                                                                 :block/parent (:db/id block)
+                                                                 :block/left (:db/id block)})))))
 
                              ;; parent will be removed
                              (when (= (:db/id prev-block) (:db/id (:block/parent block)))
-                               (when-let [parent-right (outliner-core/get-right-sibling (:db/id prev-block))]
-                                 (outliner-core/save-block! {:db/id (:db/id parent-right)
+                               (when-let [parent-right (outliner-core/get-right-sibling (db/get-db) (:db/id prev-block))]
+                                 (outliner-save-block! {:db/id (:db/id parent-right)
                                                              :block/left (:db/id block)})))
 
                              (when db-based?
-                               (outliner-core/save-block! {:db/id (:db/id block)
+                               (outliner-save-block! {:db/id (:db/id block)
                                                            :block/properties new-properties}))
                              (when pos
                                (util/schedule
@@ -849,7 +856,7 @@
                              (delete-block-fn block)
                              (save-block! repo prev-block new-content {:editor/op :delete})
                              (when db-based?
-                               (outliner-core/save-block! {:db/id (:db/id prev-block)
+                               (outliner-save-block! {:db/id (:db/id prev-block)
                                                            :block/properties new-properties})))))
 
                        :else
@@ -865,7 +872,7 @@
           sibling-block (when block-parent (util/get-prev-block-non-collapsed-non-embed block-parent))]
       (ui-outliner-tx/transact!
        {:outliner-op :delete-blocks}
-       (outliner-core/delete-blocks! blocks {}))
+       (outliner-core/delete-blocks! repo (db/get-db false) blocks {}))
       (when sibling-block
         (move-to-prev-block repo sibling-block
                             (:block/format block)
@@ -1271,7 +1278,7 @@
                     opts {:outliner-op :save-block}]
                 (ui-outliner-tx/transact!
                  opts
-                 (outliner-core/save-block! {:db/id (:db/id block)
+                 (outliner-save-block! {:db/id (:db/id block)
                                              :block/tags tag-pages})))))
           (save-block-if-changed! block value opts))))))
 
@@ -1376,7 +1383,7 @@
   (p/let [repo-dir (config/get-repo-dir repo)
           assets-dir "assets"
           _ (fs/mkdir-if-not-exists (path/path-join repo-dir assets-dir))]
-        [repo-dir assets-dir]))
+    [repo-dir assets-dir]))
 
 (defn get-asset-path
   "Get asset path from filename, ensure assets dir exists"
@@ -1478,8 +1485,8 @@
 
         (config/db-based-graph? (state/get-current-repo)) ; memory fs
         (p/let [binary (fs/read-file repo-dir path {})
-                 blob (js/Blob. (array binary) (clj->js {:type "image"}))]
-           (when blob (js/URL.createObjectURL blob)))
+                blob (js/Blob. (array binary) (clj->js {:type "image"}))]
+          (when blob (js/URL.createObjectURL blob)))
 
         :else ;; nfs
         (let [handle-path (str "handle/" full-path)
@@ -1545,13 +1552,13 @@
                  (insert-command!
                   id
                   (assets-handler/get-asset-file-link format
-                                       (if matched-alias
-                                         (str
-                                          (if image? "../assets/" "")
-                                          "@" (:name matched-alias) "/" asset-file-name)
-                                         (resolve-relative-path (or asset-file-fpath asset-file-name)))
-                                       (if file-obj (.-name file-obj) (if image? "image" "asset"))
-                                       image?)
+                                                      (if matched-alias
+                                                        (str
+                                                         (if image? "../assets/" "")
+                                                         "@" (:name matched-alias) "/" asset-file-name)
+                                                        (resolve-relative-path (or asset-file-fpath asset-file-name)))
+                                                      (if file-obj (.-name file-obj) (if image? "image" "asset"))
+                                                      image?)
                   format
                   {:last-pattern (if drop-or-paste? "" commands/command-trigger)
                    :restore?     true
@@ -1660,11 +1667,11 @@
 
 (comment
   (defn get-matched-classes
-   "Return matched class names"
-   [q]
-   (let [classes (->> (db-model/get-all-classes (state/get-current-repo))
-                      (map first))]
-     (search/fuzzy-search classes q {:limit 100}))))
+    "Return matched class names"
+    [q]
+    (let [classes (->> (db-model/get-all-classes (state/get-current-repo))
+                       (map first))]
+      (search/fuzzy-search classes q {:limit 100}))))
 
 (defn get-matched-blocks
   [q block-id]
@@ -1751,7 +1758,7 @@
           move-nodes (fn [blocks]
                        (ui-outliner-tx/transact!
                         {:outliner-op :move-blocks}
-                        (outliner-core/move-blocks-up-down! blocks up?))
+                        (outliner-core/move-blocks-up-down! (db/get-db false) blocks up?))
                        (when-let [block-node (util/get-first-block-by-id (:block/uuid (first blocks)))]
                          (.scrollIntoView block-node #js {:behavior "smooth" :block "nearest"})))]
       (if edit-block-id
@@ -1789,7 +1796,7 @@
       (ui-outliner-tx/transact!
        {:outliner-op :move-blocks
         :real-outliner-op :indent-outdent}
-       (outliner-core/indent-outdent-blocks! blocks (= direction :right))))))
+       (outliner-core/indent-outdent-blocks! (db/get-db false) blocks (= direction :right))))))
 
 (defn- get-link [format link label]
   (let [link (or link "")
@@ -2013,9 +2020,9 @@
              :block/content new-content}
              (not db-based?)
              (assoc :block/properties-text-values (apply dissoc (:block/properties-text-values block)
-                                                  (concat
-                                                   (when-not keep-uuid? [:id])
-                                                   exclude-properties)))))))
+                                                         (concat
+                                                          (when-not keep-uuid? [:id])
+                                                          exclude-properties)))))))
 
 (defn- edit-last-block-after-inserted!
   [result]
@@ -2078,23 +2085,23 @@
     (when has-unsaved-edits
       (ui-outliner-tx/transact!
        {:outliner-op :save-block}
-       (outliner-core/save-block! editing-block)))
+       (outliner-save-block! editing-block)))
 
     (ui-outliner-tx/transact!
-      {:outliner-op :insert-blocks
-       :additional-tx revert-cut-txs}
-      (when target-block'
-        (let [format (or (:block/format target-block') (state/get-preferred-format))
-              repo (state/get-current-repo)
-              blocks' (map (fn [block]
-                             (paste-block-cleanup repo block page exclude-properties format content-update-fn keep-uuid?))
-                        blocks)
-              result (outliner-core/insert-blocks! blocks' target-block' {:sibling? sibling?
-                                                                          :outliner-op :paste
-                                                                          :replace-empty-target? replace-empty-target?
-                                                                          :keep-uuid? keep-uuid?})]
-          (state/set-block-op-type! nil)
-          (edit-last-block-after-inserted! result)))))
+     {:outliner-op :insert-blocks
+      :additional-tx revert-cut-txs}
+     (when target-block'
+       (let [format (or (:block/format target-block') (state/get-preferred-format))
+             repo (state/get-current-repo)
+             blocks' (map (fn [block]
+                            (paste-block-cleanup repo block page exclude-properties format content-update-fn keep-uuid?))
+                          blocks)
+             result (outliner-core/insert-blocks! (db/get-db false) blocks' target-block' {:sibling? sibling?
+                                                                                           :outliner-op :paste
+                                                                                           :replace-empty-target? replace-empty-target?
+                                                                                           :keep-uuid? keep-uuid?})]
+         (state/set-block-op-type! nil)
+         (edit-last-block-after-inserted! result)))))
   (state/set-editor-op! nil))
 
 (defn- block-tree->blocks
@@ -2198,7 +2205,7 @@
                 {:outliner-op :insert-blocks
                  :created-from-journal-template? journal?}
                 (save-current-block!)
-                (let [result (outliner-core/insert-blocks! blocks'
+                (let [result (outliner-core/insert-blocks! (db/get-db false) blocks'
                                                            target
                                                            (assoc opts
                                                                   :sibling? sibling?'))]
@@ -2265,7 +2272,7 @@
       (ui-outliner-tx/transact!
        {:outliner-op :move-blocks
         :real-outliner-op :indent-outdent}
-       (outliner-core/move-blocks! [block] target true))
+       (outliner-core/move-blocks! (db/get-db false) [block] target true))
       (when original-block
         (util/schedule #(edit-block! block pos nil))))))
 
@@ -2471,7 +2478,7 @@
               config (assoc config :keydown-new-block true)
               content (gobj/get input "value")
               pos (cursor/pos input)
-              current-node (outliner-core/block block)
+              current-node (outliner-core/block (db/get-db) block)
               has-right? (-> (otree/-get-right current-node (db/get-db false))
                              (tree/satisfied-inode?))
               db-based? (config/db-based-graph? (state/get-current-repo))
@@ -2693,7 +2700,7 @@
         collapsed? (util/collapsed? current-block)
         next-block (when-let [e (db-model/get-next (db/get-db repo) (:db/id current-block))]
                      (db/pull (:db/id e)))
-        next-block-right (when next-block (outliner-core/get-right-sibling (:db/id next-block)))
+        next-block-right (when next-block (outliner-core/get-right-sibling (db/get-db) (:db/id next-block)))
         db-based? (config/db-based-graph? repo)]
     (cond
       (nil? next-block)
@@ -2722,19 +2729,19 @@
          (delete-block-aux! delete-block false)
          (save-block! repo keep-block new-content {:editor/op :delete})
          (when next-block-has-refs?
-           (outliner-core/save-block! {:db/id (:db/id keep-block)
+           (outliner-save-block! {:db/id (:db/id keep-block)
                                        :block/left (:db/id (:block/left delete-block))
                                        :block/parent (:db/id (:block/parent delete-block))})
            (when (and next-block-right (not= (:db/id (:block/parent next-block))
                                              (:db/id (:block/parent edit-block))))
-             (outliner-core/save-block! {:db/id (:db/id next-block-right)
+             (outliner-save-block! {:db/id (:db/id next-block-right)
                                          :block/left (:db/id (:block/left next-block))})))
          (when db-based?
            (let [new-properties (merge
                                  (:block/properties (db/entity (:db/id edit-block)))
                                  (:block/properties (db/entity (:db/id next-block))))]
              (when-not (= new-properties (:block/properties keep-block))
-               (outliner-core/save-block! {:db/id (:db/id keep-block)
+               (outliner-save-block! {:db/id (:db/id keep-block)
                                            :block/properties new-properties})))))
         (let [block (if next-block-has-refs? next-block edit-block)]
           (edit-block! block current-pos nil))))))
@@ -2761,7 +2768,7 @@
 
         :else
         (delete-and-update
-          input current-pos (util/safe-inc-current-pos-from-start (.-value input) current-pos))))))
+         input current-pos (util/safe-inc-current-pos-from-start (.-value input) current-pos))))))
 
 (defn keydown-backspace-handler
   [cut? e]
@@ -2863,7 +2870,7 @@
       (ui-outliner-tx/transact!
        {:outliner-op :move-blocks
         :real-outliner-op :indent-outdent}
-       (outliner-core/indent-outdent-blocks! [block] indent?)))
+       (outliner-core/indent-outdent-blocks! (db/get-db false) [block] indent?)))
     (state/set-editor-op! :nil)))
 
 (defn keydown-tab-handler
@@ -3006,55 +3013,55 @@
         (commands/handle-step [:editor/search-page])
         (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)}))
       (when (and (not editor-action) (not non-enter-processed?))
-       (cond
+        (cond
          ;; When you type text inside square brackets
-         (and (not (contains? #{"ArrowDown" "ArrowLeft" "ArrowRight" "ArrowUp" "Escape"} k))
-              (wrapped-by? input page-ref/left-brackets page-ref/right-brackets))
-         (let [orig-pos (cursor/get-caret-pos input)
-               value (gobj/get input "value")
-               square-pos (string/last-index-of (subs value 0 (:pos orig-pos)) page-ref/left-brackets)
-               pos (+ square-pos 2)
-               _ (state/set-editor-last-pos! pos)
-               pos (assoc orig-pos :pos pos)
-               command-step (if (= \# (util/nth-safe value (dec square-pos)))
-                              :editor/search-page-hashtag
-                              :editor/search-page)]
-           (commands/handle-step [command-step])
-           (state/set-editor-action-data! {:pos pos}))
+          (and (not (contains? #{"ArrowDown" "ArrowLeft" "ArrowRight" "ArrowUp" "Escape"} k))
+               (wrapped-by? input page-ref/left-brackets page-ref/right-brackets))
+          (let [orig-pos (cursor/get-caret-pos input)
+                value (gobj/get input "value")
+                square-pos (string/last-index-of (subs value 0 (:pos orig-pos)) page-ref/left-brackets)
+                pos (+ square-pos 2)
+                _ (state/set-editor-last-pos! pos)
+                pos (assoc orig-pos :pos pos)
+                command-step (if (= \# (util/nth-safe value (dec square-pos)))
+                               :editor/search-page-hashtag
+                               :editor/search-page)]
+            (commands/handle-step [command-step])
+            (state/set-editor-action-data! {:pos pos}))
 
          ;; Handle non-ascii square brackets
-         (and (input-page-ref? k current-pos blank-selected? last-key-code)
-              (not (wrapped-by? input page-ref/left-brackets page-ref/right-brackets)))
-         (do
-           (commands/handle-step [:editor/input page-ref/left-and-right-brackets {:backward-truncate-number 2
-                                                                                  :backward-pos 2}])
-           (commands/handle-step [:editor/search-page])
-           (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)}))
+          (and (input-page-ref? k current-pos blank-selected? last-key-code)
+               (not (wrapped-by? input page-ref/left-brackets page-ref/right-brackets)))
+          (do
+            (commands/handle-step [:editor/input page-ref/left-and-right-brackets {:backward-truncate-number 2
+                                                                                   :backward-pos 2}])
+            (commands/handle-step [:editor/search-page])
+            (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)}))
 
          ;; Handle non-ascii parentheses
-         (and blank-selected?
-              (contains? keycode/left-paren-keys k)
-              (= (:key last-key-code) k)
-              (> current-pos 0)
-              (not (wrapped-by? input block-ref/left-parens block-ref/right-parens)))
-         (do
-           (commands/handle-step [:editor/input block-ref/left-and-right-parens {:backward-truncate-number 2
-                                                                                 :backward-pos 2}])
-           (commands/handle-step [:editor/search-block :reference])
-           (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)}))
+          (and blank-selected?
+               (contains? keycode/left-paren-keys k)
+               (= (:key last-key-code) k)
+               (> current-pos 0)
+               (not (wrapped-by? input block-ref/left-parens block-ref/right-parens)))
+          (do
+            (commands/handle-step [:editor/input block-ref/left-and-right-parens {:backward-truncate-number 2
+                                                                                  :backward-pos 2}])
+            (commands/handle-step [:editor/search-block :reference])
+            (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)}))
 
          ;; Handle non-ascii angle brackets
-         (and (= "〈" c)
-              (= "《" (util/nth-safe (gobj/get input "value") (dec (dec current-pos))))
-              (> current-pos 0))
-         (do
-           (commands/handle-step [:editor/input commands/angle-bracket {:last-pattern "《〈"
-                                                                        :backward-pos 0}])
-           (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)})
-           (state/set-editor-show-block-commands!))
-
-         :else
-         nil)))))
+          (and (= "〈" c)
+               (= "《" (util/nth-safe (gobj/get input "value") (dec (dec current-pos))))
+               (> current-pos 0))
+          (do
+            (commands/handle-step [:editor/input commands/angle-bracket {:last-pattern "《〈"
+                                                                         :backward-pos 0}])
+            (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)})
+            (state/set-editor-show-block-commands!))
+
+          :else
+          nil)))))
 
 (defn keyup-handler
   [_state input input-id]
@@ -3179,8 +3186,8 @@
   (when-let [current-block (state/get-edit-block)]
     (when-let [block-id (:block/uuid current-block)]
       (if (= format "embed")
-       (copy-block-ref! block-id #(str "{{embed ((" % "))}}"))
-       (copy-block-ref! block-id block-ref/->block-ref)))))
+        (copy-block-ref! block-id #(str "{{embed ((" % "))}}"))
+        (copy-block-ref! block-id block-ref/->block-ref)))))
 
 (defn copy-current-block-embed []
   (copy-current-block-ref "embed"))
@@ -3310,9 +3317,9 @@
           ;; if the move is to cross block boundary, select the whole block
          (or (and (= direction :up) (cursor/textarea-cursor-rect-first-row? cursor-rect))
              (and (= direction :down) (cursor/textarea-cursor-rect-last-row? cursor-rect)))
-         (select-block-up-down direction)
+          (select-block-up-down direction)
           ;; simulate text selection
-         (cursor/select-up-down input direction anchor cursor-rect)))
+          (cursor/select-up-down input direction anchor cursor-rect)))
       (select-block-up-down direction))))
 
 (defn open-selected-block!
@@ -3389,8 +3396,6 @@
     (util/forward-kill-word input)
     (state/set-edit-content! (state/get-edit-input-id) (.-value input))))
 
-
-
 (defn block-with-title?
   [format content semantic?]
   (and (string/includes? content "\n")
@@ -3409,11 +3414,11 @@
                      macro-name (pu/lookup properties :logseq.macro-name)
                      macro-arguments (pu/lookup properties :logseq.macro-arguments)]
                  (when-let [query-body (and (= "query" macro-name) (not-empty (string/join " " macro-arguments)))]
-                  (seq (:query
-                        (try
-                          (query-dsl/parse-query query-body)
-                          (catch :default _e
-                            nil))))))))))
+                   (seq (:query
+                         (try
+                           (query-dsl/parse-query query-body)
+                           (catch :default _e
+                             nil))))))))))
 
 (defn- valid-custom-query-block?
   "Whether block has a valid custom query."
@@ -3535,7 +3540,7 @@
              (when-not (= current-value value)
                (let [block {:block/uuid block-id
                             :block/collapsed? value}]
-                 (outliner-core/save-block! block)))))))
+                 (outliner-save-block! block)))))))
       (doseq [block-id block-ids]
         (state/set-collapsed-block! block-id value)))))
 

+ 2 - 2
src/main/frontend/handler/file_based/editor.cljs

@@ -214,7 +214,7 @@
    {:outliner-op :save-block}
    (doseq [block-id block-ids]
      (when-let [block (set-heading-aux! block-id heading)]
-       (outliner-core/save-block! block)))))
+       (outliner-core/save-block! (state/get-current-repo) (db/get-db false) block)))))
 
 (defn set-blocks-id!
   "Persist block uuid to file if the uuid is valid, and it's not persisted in file.
@@ -227,4 +227,4 @@
                        [block-id :id (str block-id)])))
                  block-ids)
         col (remove nil? col)]
-    (file-property-handler/batch-set-block-property-aux! col)))
+    (file-property-handler/batch-set-block-property-aux! col)))

+ 4 - 3
src/main/frontend/handler/file_based/page.cljs

@@ -196,7 +196,7 @@
       (let [old-original-name   (:block/original-name page)
             file                (:block/file page)
             journal?            (:block/journal? page)
-            properties-block    (:data (otree/-get-down (outliner-core/block page) (db/get-db false)))
+            properties-block    (:data (otree/-get-down (outliner-core/block (db/get-db) page) (db/get-db false)))
             properties-content  (:block/content properties-block)
             properties-block-tx (when (and properties-block
                                            properties-content
@@ -307,12 +307,13 @@
              (db/page-exists? to-page-name)
              (not= from-page-name to-page-name))
     (let [to-page (db/entity [:block/name to-page-name])
+          conn (db/get-db false)
           to-id (:db/id to-page)
           from-page (db/entity [:block/name from-page-name])
           from-id (:db/id from-page)
           from-first-child (some->> (db/pull from-id)
-                                    (outliner-core/block)
-                                    (otree/-get-down (db/get-db false))
+                                    (outliner-core/block @conn)
+                                    (otree/-get-down conn)
                                     (outliner-core/get-data))
           to-last-direct-child-id (model/get-block-last-direct-child-id (db/get-db) to-id)
           repo (state/get-current-repo)

+ 1 - 2
src/main/frontend/handler/file_based/page_property.cljs

@@ -70,7 +70,6 @@
                        :block/page page-id}
                 tx [(assoc page-id :block/properties new-properties)
                     block]]
-              ;; (util/pprint tx)
             (db/transact! tx))
           (let [block {:block/uuid (db/new-block-id)
                        :block/left page-id
@@ -86,4 +85,4 @@
             (ui-outliner-tx/transact!
              {:outliner-op :insert-blocks
               :additional-tx page-properties-tx}
-             (outliner-core/insert-blocks! block page {:sibling? false}))))))))
+             (outliner-core/insert-blocks! (db/get-db false) block page {:sibling? false}))))))))

+ 1 - 1
src/main/frontend/handler/file_based/property.cljs

@@ -53,7 +53,7 @@
                         :block/properties-order property-ks
                         :block/properties-text-values properties-text-values
                         :block/content content}]
-             (outliner-core/save-block! block)))))))
+             (outliner-core/save-block! (state/get-current-repo) (db/get-db false) 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)]

+ 144 - 153
src/main/frontend/modules/outliner/core.cljs

@@ -10,7 +10,6 @@
             [logseq.graph-parser.util :as gp-util]
             [cljs.spec.alpha :as s]
             [goog.object :as gobj]
-            [logseq.outliner.pipeline :as outliner-pipeline]
             [logseq.db :as ldb]
             [frontend.worker.mldoc :as mldoc]
             [logseq.graph-parser.block :as gp-block]
@@ -19,7 +18,6 @@
             [logseq.db.sqlite.util :as sqlite-util]
 
             [frontend.db :as db]
-            [frontend.db.conn :as conn]
             [frontend.state :as state]
             [frontend.util :as util]
             [dommy.core :as dom]))
@@ -32,7 +30,7 @@
 (defrecord Block [data])
 
 (defn block
-  [m]
+  [db m]
   (assert (or (map? m) (de/entity? m)) (util/format "block data must be map or entity, got: %s %s" (type m) m))
   (let [e (if (de/entity? m)
             m
@@ -40,7 +38,7 @@
                         [:block/uuid (:block/uuid m)]
                         (:db/id m))]
               (assert eid "eid doesn't exist")
-              (db/entity eid)))
+              (d/entity db eid)))
         e-map {:db/id (:db/id e)
                :block/uuid (:block/uuid e)
                :block/page {:db/id (:db/id (:block/page e))
@@ -69,13 +67,13 @@
      (ldb/get-by-parent-&-left db parent-id left-id)
      :db/id
      d/entity
-     block)))
+     (partial block db))))
 
 (defn- block-with-timestamps
   [block]
   (let [updated-at (util/time-ms)
         block (cond->
-                (assoc block :block/updated-at updated-at)
+               (assoc block :block/updated-at updated-at)
                 (nil? (:block/created-at block))
                 (assoc :block/created-at updated-at))]
     block))
@@ -92,7 +90,7 @@
       (let [new-refs (set (map (fn [ref]
                                  (or (:block/name ref)
                                      (and (:db/id ref)
-                                          (:block/name (db/entity (:db/id ref)))))) new-refs))
+                                          (:block/name (d/entity db (:db/id ref)))))) new-refs))
             old-pages (->> (map :db/id old-refs)
                            (d/pull-many db '[*])
                            (remove (fn [e] (contains? new-refs (:block/name e))))
@@ -111,27 +109,27 @@
 (defn- update-page-when-save-block
   [txs-state block-entity m]
   (when-let [e (:block/page block-entity)]
-          (let [m' (cond-> {:db/id (:db/id e)
-                            :block/updated-at (util/time-ms)}
-                     (not (:block/created-at e))
-                     (assoc :block/created-at (util/time-ms)))
-                txs (if (or (:block/pre-block? block-entity)
-                            (:block/pre-block? m))
-                      (let [properties (:block/properties m)
-                            alias (set (:alias properties))
-                            tags (set (:tags properties))
-                            alias (map (fn [p] {:block/name (util/page-name-sanity-lc p)}) alias)
-                            tags (map (fn [p] {:block/name (util/page-name-sanity-lc p)}) tags)
-                            deleteable-page-attributes {:block/alias alias
-                                                        :block/tags tags
-                                                        :block/properties properties
-                                                        :block/properties-text-values (:block/properties-text-values m)}
+    (let [m' (cond-> {:db/id (:db/id e)
+                      :block/updated-at (util/time-ms)}
+               (not (:block/created-at e))
+               (assoc :block/created-at (util/time-ms)))
+          txs (if (or (:block/pre-block? block-entity)
+                      (:block/pre-block? m))
+                (let [properties (:block/properties m)
+                      alias (set (:alias properties))
+                      tags (set (:tags properties))
+                      alias (map (fn [p] {:block/name (util/page-name-sanity-lc p)}) alias)
+                      tags (map (fn [p] {:block/name (util/page-name-sanity-lc p)}) tags)
+                      deleteable-page-attributes {:block/alias alias
+                                                  :block/tags tags
+                                                  :block/properties properties
+                                                  :block/properties-text-values (:block/properties-text-values m)}
                             ;; Retract page attributes to allow for deletion of page attributes
-                            page-retractions
-                            (mapv #(vector :db/retract (:db/id e) %) (keys deleteable-page-attributes))]
-                        (conj page-retractions (merge m' deleteable-page-attributes)))
-                      [m'])]
-            (swap! txs-state into txs))))
+                      page-retractions
+                      (mapv #(vector :db/retract (:db/id e) %) (keys deleteable-page-attributes))]
+                  (conj page-retractions (merge m' deleteable-page-attributes)))
+                [m'])]
+      (swap! txs-state into txs))))
 
 (defn- remove-orphaned-refs-when-save
   [db txs-state block-entity m]
@@ -142,8 +140,8 @@
     (remove-orphaned-page-refs! db (:db/id block-entity) txs-state old-refs new-refs)))
 
 (defn- get-last-child-or-self
-  [block]
-  (let [last-child (some-> (ldb/get-block-last-direct-child-id (conn/get-db) (:db/id block) true)
+  [db block]
+  (let [last-child (some-> (ldb/get-block-last-direct-child-id db (:db/id block) true)
                            db/entity)
         target (or last-child block)]
     [target (some? last-child)]))
@@ -151,11 +149,11 @@
 (declare move-blocks)
 
 (defn- remove-macros-when-save
-  [repo txs-state block-entity]
+  [db txs-state block-entity]
   (swap! txs-state (fn [txs]
                      (vec (concat txs
                                   ;; Only delete if last reference
-                                  (keep #(when (<= (count (:block/_macros (db/entity repo (:db/id %))))
+                                  (keep #(when (<= (count (:block/_macros (d/entity db (:db/id %))))
                                                    1)
                                            (vector :db.fn/retractEntity (:db/id %)))
                                         (:block/macros block-entity)))))))
@@ -179,10 +177,10 @@
                   (d/transact! conn [page-m]))
               merge-tx (let [children (:block/_parent block-entity)
                              page (d/entity db [:block/uuid (:block/uuid page-m)])
-                             [target sibling?] (get-last-child-or-self page)]
+                             [target sibling?] (get-last-child-or-self db page)]
                          (when (seq children)
                            (:tx-data
-                            (move-blocks children target
+                            (move-blocks conn children target
                                          {:sibling? sibling?
                                           :outliner-op :move-blocks}))))]
           (swap! txs-state (fn [txs]
@@ -270,7 +268,7 @@
        (let [uuid (:block/uuid data)]
          (if uuid
            uuid
-           (let [new-id (db/new-block-id)]
+           (let [new-id (ldb/new-block-id)]
              (d/transact! conn [{:db/id (:db/id data)
                                  :block/uuid new-id}])
              new-id))))))
@@ -348,7 +346,7 @@
         ;; Update block's page attributes
         (update-page-when-save-block txs-state block-entity m)
         ;; Remove macros as they are replaced by new ones
-        (remove-macros-when-save repo txs-state block-entity)
+        (remove-macros-when-save db txs-state block-entity)
         ;; Remove orphaned refs from block
         (remove-orphaned-refs-when-save @conn txs-state block-entity m))
 
@@ -371,13 +369,13 @@
             "db should be satisfied outliner-tx-state?")
     (let [block-id (otree/-get-id this conn)
           ids (set (if children?
-                     (let [children (db/get-block-children (state/get-current-repo) block-id)
+                     (let [children (ldb/get-block-children @conn block-id)
                            children-ids (map :block/uuid children)]
                        (conj children-ids block-id))
                      [block-id]))
           txs (map (fn [id] [:db.fn/retractEntity [:block/uuid id]]) ids)
           txs (if-not children?
-                (let [immediate-children (db/get-block-immediate-children (state/get-current-repo) block-id)]
+                (let [immediate-children (ldb/get-block-immediate-children @conn block-id)]
                   (if (seq immediate-children)
                     (let [left-id (otree/-get-id (otree/-get-left this conn) conn)]
                       (concat txs
@@ -405,12 +403,12 @@
   (-get-children [this conn]
     (let [parent-id (otree/-get-id this conn)
           children (ldb/get-block-immediate-children @conn parent-id)]
-      (map block children))))
+      (map (partial block @conn) children))))
 
 (defn get-right-sibling
-  [db-id]
+  [db db-id]
   (when db-id
-    (ldb/get-right-sibling (conn/get-db) db-id)))
+    (ldb/get-right-sibling db db-id)))
 
 (defn- assoc-level-aux
   [tree-vec children-key init-level]
@@ -546,11 +544,10 @@
 
 (defn save-block
   "Save the `block`."
-  [repo block']
+  [repo conn block']
   {:pre [(map? block')]}
-  (let [conn (db/get-db false)
-        txs-state (atom [])]
-    (otree/-save (block block') txs-state conn repo)
+  (let [txs-state (atom [])]
+    (otree/-save (block @conn block') txs-state conn repo)
     {:tx-data @txs-state}))
 
 (defn blocks-with-level
@@ -623,21 +620,19 @@
 
 (defn- get-right-siblings
   "Get `node`'s right siblings."
-  [node]
+  [conn node]
   {:pre [(otree/satisfied-inode? node)]}
-  (let [conn (db/get-db false)]
-    (when-let [parent (otree/-get-parent node conn)]
-     (let [children (otree/-get-children parent conn)]
-       (->> (split-with #(not= (otree/-get-id node conn) (otree/-get-id % conn)) children)
-            last
-            rest)))))
+  (when-let [parent (otree/-get-parent node conn)]
+    (let [children (otree/-get-children parent conn)]
+      (->> (split-with #(not= (otree/-get-id node conn) (otree/-get-id % conn)) children)
+           last
+           rest))))
 
 (defn- blocks-with-ordered-list-props
-  [blocks target-block sibling?]
+  [conn blocks target-block sibling?]
   (let [repo (state/get-current-repo)
-        conn (db/get-db repo false)
-        db (db/get-db repo)
-        target-block (if sibling? target-block (some-> target-block :db/id db/entity block
+        db @conn
+        target-block (if sibling? target-block (some-> target-block :db/id db/entity (partial block db)
                                                        (#(otree/-get-down % conn))
                                                        :data))
         list-type-fn (fn [block] (db-property/get-property repo db block :logseq.order-list-type))
@@ -734,11 +729,11 @@
                  blocks)))
 
 (defn- get-target-block
-  [blocks target-block {:keys [outliner-op indent? sibling? up?]}]
+  [db blocks target-block {:keys [outliner-op indent? sibling? up?]}]
   (when-let [block (if (:db/id target-block)
-                     (db/entity (:db/id target-block))
+                     (d/entity db (:db/id target-block))
                      (when (:block/uuid target-block)
-                       (db/entity [:block/uuid (:block/uuid target-block)])))]
+                       (d/entity db [:block/uuid (:block/uuid target-block)])))]
     [block sibling?]
     (let [linked (:block/link block)
           up-down? (= outliner-op :move-blocks-up-down)
@@ -752,9 +747,9 @@
                                 (not= (:db/id (:block/parent (first blocks)))
                                       (:db/id target))
                                 (not= (:db/id (:block/parent
-                                               (db/entity (:db/id (:block/parent (first blocks))))))
+                                               (d/entity db (:db/id (:block/parent (first blocks))))))
                                       (:db/id target)))
-                         (get-last-child-or-self target)
+                         (get-last-child-or-self db target)
                          [target false])))
 
                    (and (= outliner-op :indent-outdent-blocks) (not indent?))
@@ -764,7 +759,7 @@
                    [block sibling?]
 
                    linked
-                   (get-last-child-or-self linked)
+                   (get-last-child-or-self db linked)
 
                    :else
                    [block sibling?])]
@@ -773,6 +768,7 @@
 (defn insert-blocks
   "Insert blocks as children (or siblings) of target-node.
   Args:
+    `conn`: db connection.
     `blocks`: blocks should be sorted already.
     `target-block`: where `blocks` will be inserted.
     Options:
@@ -785,12 +781,11 @@
                                to replace it, it defaults to be `false`.
       `update-timestamps?`: whether to update `blocks` timestamps.
     ``"
-  [blocks target-block {:keys [_sibling? keep-uuid? outliner-op replace-empty-target? update-timestamps?] :as opts
-                        :or {update-timestamps? true}}]
+  [conn blocks target-block {:keys [_sibling? keep-uuid? outliner-op replace-empty-target? update-timestamps?] :as opts
+                             :or {update-timestamps? true}}]
   {:pre [(seq blocks)
          (s/valid? ::block-map-or-entity target-block)]}
-  (let [conn (db/get-db false)
-        [target-block' sibling?] (get-target-block blocks target-block opts)
+  (let [[target-block' sibling?] (get-target-block @conn blocks target-block opts)
         _ (assert (some? target-block') (str "Invalid target: " target-block))
         sibling? (if (page-block? target-block') false sibling?)
         move? (contains? #{:move-blocks :move-blocks-up-down :indent-outdent-blocks} outliner-op)
@@ -807,7 +802,7 @@
         blocks' (cond->>
                  (-> blocks
                      blocks-with-level
-                     (blocks-with-ordered-list-props target-block sibling?))
+                     (#(blocks-with-ordered-list-props conn % target-block sibling?)))
                   (= outliner-op :paste)
                   fix-top-level-blocks
                   update-timestamps?
@@ -837,7 +832,7 @@
             tx (if move?
                  tx
                  (assign-temp-id tx replace-empty-target? target-block'))
-            target-node (block target-block')
+            target-node (block @conn target-block')
             next (if sibling?
                    (otree/-get-right target-node conn)
                    (otree/-get-down target-node conn))
@@ -853,36 +848,35 @@
          :blocks  tx}))))
 
 (defn- build-move-blocks-next-tx
-  [target-block blocks {:keys [sibling? _non-consecutive-blocks?]}]
+  [db target-block blocks {:keys [sibling? _non-consecutive-blocks?]}]
   (let [top-level-blocks (get-top-level-blocks blocks)
         top-level-blocks-ids (set (map :db/id top-level-blocks))
-        right-block (get-right-sibling (:db/id (last top-level-blocks)))]
+        right-block (get-right-sibling db (:db/id (last top-level-blocks)))]
     (when (and right-block
                (not (contains? top-level-blocks-ids (:db/id right-block))))
       (when-let [left (loop [block (:block/left right-block)]
                         (if (contains? top-level-blocks-ids (:db/id block))
-                          (recur (:block/left (db/entity (:db/id block))))
+                          (recur (:block/left (d/entity db (:db/id block))))
                           (:db/id block)))]
         (when-not (and (= left (:db/id target-block)) sibling?)
           {:db/id (:db/id right-block)
            :block/left left})))))
 
 (defn- find-new-left
-  [block moved-ids target-block current-block sibling? near-by?]
+  [db block moved-ids target-block current-block sibling? near-by?]
   (if (= (:db/id target-block) (:db/id (:block/left current-block)))
     (if sibling?
-      (db/entity (last moved-ids))
+      (d/entity db (last moved-ids))
       target-block)
-    (let [left (db/entity (:db/id (:block/left block)))]
+    (let [left (d/entity db (:db/id (:block/left block)))]
       (if (contains? (set moved-ids) (:db/id left))
-        (find-new-left left moved-ids target-block current-block sibling? near-by?)
+        (find-new-left db left moved-ids target-block current-block sibling? near-by?)
         left))))
 
 (defn- fix-non-consecutive-blocks
-  [blocks target-block sibling?]
+  [db blocks target-block sibling?]
   (when (> (count blocks) 1)
-    (let [db (db/get-db)
-          page-blocks (group-by :block/page blocks)
+    (let [page-blocks (group-by :block/page blocks)
           near-by? (= (:db/id target-block) (:db/id (:block/left (first blocks))))]
       (->>
        (mapcat (fn [[_page blocks]]
@@ -891,11 +885,11 @@
                                                    (util/distinct-by :db/id))]
                    (when (seq non-consecutive-blocks)
                      (map-indexed (fn [idx block]
-                                    (when-let [right (get-right-sibling (:db/id block))]
+                                    (when-let [right (get-right-sibling db (:db/id block))]
                                       (if (and (zero? idx) near-by? sibling?)
                                         {:db/id (:db/id right)
                                          :block/left (:db/id (last blocks))}
-                                        (when-let [new-left (find-new-left right (distinct (map :db/id blocks)) target-block block sibling? near-by?)]
+                                        (when-let [new-left (find-new-left db right (distinct (map :db/id blocks)) target-block block sibling? near-by?)]
                                           {:db/id      (:db/id right)
                                            :block/left (:db/id new-left)}))))
                                   non-consecutive-blocks)))) page-blocks)
@@ -903,16 +897,14 @@
 
 (defn delete-block
   "Delete block from the tree."
-  [txs-state node {:keys [children? children-check?]
-                   :or {children-check? true}}]
+  [repo conn txs-state node {:keys [children? children-check?]
+                        :or {children-check? true}}]
   (if (and children-check?
            (not children?)
-           (first (:block/_parent (db/entity [:block/uuid (:block/uuid (get-data node))]))))
+           (first (:block/_parent (d/entity @conn [:block/uuid (:block/uuid (get-data node))]))))
     (throw (ex-info "Block can't be deleted because it still has children left, you can pass `children?` equals to `true`."
                     {:block (get-data node)}))
-    (let [repo (state/get-current-repo)
-          conn (db/get-db false)
-          right-node (otree/-get-right node conn)]
+    (let [right-node (otree/-get-right node conn)]
       (otree/-del node txs-state children? conn)
       (when (otree/satisfied-inode? right-node)
         (let [left-node (otree/-get-left node conn)
@@ -924,22 +916,20 @@
   "Delete blocks from the tree.
    Args:
     `children?`: whether to replace `blocks'` children too. "
-  [blocks {:keys [children?]
-           :or {children? true}
-           :as delete-opts}]
+  [repo conn blocks {:keys [children?]
+                     :or {children? true}
+                     :as delete-opts}]
   [:pre [(seq blocks)]]
-  (let [repo (state/get-current-repo)
-        conn (db/get-db false)
-        txs-state (ds/new-outliner-txs-state)
+  (let [txs-state (ds/new-outliner-txs-state)
         blocks (get-top-level-blocks blocks)
         block-ids (map (fn [b] [:block/uuid (:block/uuid b)]) blocks)
         start-block (first blocks)
         end-block (last blocks)
-        start-node (block start-block)
-        end-node (block end-block)
+        start-node (block @conn start-block)
+        end-node (block @conn end-block)
         end-node-parents (->>
-                          (db/get-block-parents
-                           repo
+                          (ldb/get-block-parents
+                           @conn
                            (otree/-get-id end-node conn)
                            {:depth 1000})
                           (map :block/uuid)
@@ -949,7 +939,7 @@
          (= 1 (count blocks))
          (= start-node end-node)
          self-block?)
-      (delete-block txs-state start-node (assoc delete-opts :children? children?))
+      (delete-block repo conn txs-state start-node (assoc delete-opts :children? children?))
       (let [sibling? (= (otree/-get-parent-id start-node conn)
                         (otree/-get-parent-id end-node conn))
             right-node (otree/-get-right end-node conn)]
@@ -959,8 +949,8 @@
                                (otree/-get-id (otree/-get-left start-node conn) conn)
                                (let [end-node-left-nodes (get-left-nodes conn end-node (count block-ids))
                                      parents (->>
-                                              (db/get-block-parents
-                                               repo
+                                              (ldb/get-block-parents
+                                               @conn
                                                (otree/-get-id start-node conn)
                                                {:depth 1000})
                                               (map :block/uuid)
@@ -973,16 +963,16 @@
             (when (and (nil? left-node-id) (not non-consecutive?))
               (assert left-node-id
                       (str "Can't find the left-node-id: "
-                           (pr-str {:start (db/entity [:block/uuid (otree/-get-id start-node conn)])
-                                    :end (db/entity [:block/uuid (otree/-get-id end-node conn)])
-                                    :right-node (db/entity [:block/uuid (otree/-get-id right-node conn)])}))))
+                           (pr-str {:start (d/entity @conn [:block/uuid (otree/-get-id start-node conn)])
+                                    :end (d/entity @conn [:block/uuid (otree/-get-id end-node conn)])
+                                    :right-node (d/entity @conn [:block/uuid (otree/-get-id right-node conn)])}))))
             (when left-node-id
               (let [new-right-node (otree/-set-left-id right-node left-node-id conn)]
                 (otree/-save new-right-node txs-state conn repo)))))
         (doseq [id block-ids]
-          (let [node (block (db/entity id))]
+          (let [node (block @conn (d/entity @conn id))]
             (otree/-del node txs-state true conn)))
-        (let [fix-non-consecutive-tx (fix-non-consecutive-blocks blocks nil false)]
+        (let [fix-non-consecutive-tx (fix-non-consecutive-blocks @conn blocks nil false)]
           (swap! txs-state concat fix-non-consecutive-tx))))
     {:tx-data @txs-state}))
 
@@ -996,40 +986,40 @@
 
 (defn move-blocks
   "Move `blocks` to `target-block` as siblings or children."
-  [blocks target-block {:keys [_sibling? _up? outliner-op _indent?]
-                        :as opts}]
+  [conn blocks target-block {:keys [_sibling? _up? outliner-op _indent?]
+                             :as opts}]
   [:pre [(seq blocks)
          (s/valid? ::block-map-or-entity target-block)]]
-  (let [db (db/get-db)
-        blocks (map (fn [b] (db/entity [:block/uuid (:block/uuid b)])) blocks)
+  (let [db @conn
+        blocks (map (fn [b] (d/entity db [:block/uuid (:block/uuid b)])) blocks)
         blocks (get-top-level-blocks blocks)
-        [target-block sibling?] (get-target-block blocks target-block opts)
+        [target-block sibling?] (get-target-block db blocks target-block opts)
         non-consecutive-blocks? (seq (ldb/get-non-consecutive-blocks db blocks))
         original-position? (move-to-original-position? blocks target-block sibling? non-consecutive-blocks?)]
     (when (and (not (contains? (set (map :db/id blocks)) (:db/id target-block)))
                (not original-position?))
-      (let [parents (->> (db/get-block-parents (state/get-current-repo) (:block/uuid target-block) {})
+      (let [parents (->> (ldb/get-block-parents db (:block/uuid target-block) {})
                          (map :db/id)
                          (set))
             move-parents-to-child? (some parents (map :db/id blocks))]
         (when-not move-parents-to-child?
           (let [first-block (first blocks)
-                {:keys [tx-data]} (insert-blocks blocks target-block {:sibling? sibling?
-                                                                      :outliner-op (or outliner-op :move-blocks)
-                                                                      :update-timestamps? false})]
+                {:keys [tx-data]} (insert-blocks conn blocks target-block {:sibling? sibling?
+                                                                           :outliner-op (or outliner-op :move-blocks)
+                                                                           :update-timestamps? false})]
             (when (seq tx-data)
               (let [first-block-page (:db/id (:block/page first-block))
                     target-page (or (:db/id (:block/page target-block))
                                     (:db/id target-block))
                     not-same-page? (not= first-block-page target-page)
-                    move-blocks-next-tx [(build-move-blocks-next-tx target-block blocks {:sibling? sibling?
-                                                                                         :non-consecutive-blocks? non-consecutive-blocks?})]
+                    move-blocks-next-tx [(build-move-blocks-next-tx db target-block blocks {:sibling? sibling?
+                                                                                            :non-consecutive-blocks? non-consecutive-blocks?})]
                     children-page-tx (when not-same-page?
-                                       (let [children-ids (mapcat #(outliner-pipeline/get-block-children-ids (db/get-db (state/get-current-repo)) (:block/uuid %))
+                                       (let [children-ids (mapcat #(ldb/get-block-children-ids db (:block/uuid %))
                                                                   blocks)]
                                          (map (fn [id] {:block/uuid id
                                                         :block/page target-page}) children-ids)))
-                    fix-non-consecutive-tx (->> (fix-non-consecutive-blocks blocks target-block sibling?)
+                    fix-non-consecutive-tx (->> (fix-non-consecutive-blocks db blocks target-block sibling?)
                                                 (remove (fn [b]
                                                           (contains? (set (map :db/id move-blocks-next-tx)) (:db/id b)))))
                     full-tx (util/concat-without-nil tx-data move-blocks-next-tx children-page-tx fix-non-consecutive-tx)
@@ -1066,12 +1056,13 @@
 
 (defn move-blocks-up-down
   "Move blocks up/down."
-  [blocks up?]
+  [conn blocks up?]
   {:pre [(seq blocks) (boolean? up?)]}
-  (let [top-level-blocks (get-top-level-blocks blocks)
+  (let [db @conn
+        top-level-blocks (get-top-level-blocks blocks)
         opts {:outliner-op :move-blocks-up-down}]
     (if up?
-      (let [first-block (db/entity (:db/id (first top-level-blocks)))
+      (let [first-block (d/entity db (:db/id (first top-level-blocks)))
             first-block-parent (:block/parent first-block)
             left (:block/left first-block)
             left-left (or (:block/left left)
@@ -1081,34 +1072,34 @@
         (when (and left-left
                    (not= (:db/id (:block/page first-block-parent))
                          (:db/id left-left)))
-          (move-blocks top-level-blocks left-left (merge opts {:sibling? sibling?
-                                                               :up? up?}))))
+          (move-blocks conn top-level-blocks left-left (merge opts {:sibling? sibling?
+                                                                    :up? up?}))))
 
       (let [last-top-block (last top-level-blocks)
-            last-top-block-right (get-right-sibling (:db/id last-top-block))
+            last-top-block-right (get-right-sibling db (:db/id last-top-block))
             right (or
                    last-top-block-right
                    (let [parent (:block/parent last-top-block)
-                         parent-id (if (:block/page (db/entity (:db/id parent)))
+                         parent-id (if (:block/page (d/entity db (:db/id parent)))
                                      (:db/id parent)
                                      (:db/id (get-last-block-original last-top-block)))]
-                     (some-> parent-id get-right-sibling)))
+                     (some->> parent-id (get-right-sibling db))))
             sibling? (= (:db/id (:block/parent last-top-block))
                         (:db/id (:block/parent right)))]
         (when right
-          (move-blocks blocks right (merge opts {:sibling? sibling?
-                                                 :up? up?})))))))
+          (move-blocks conn blocks right (merge opts {:sibling? sibling?
+                                                      :up? up?})))))))
 
 (defn indent-outdent-blocks
   "Indent or outdent `blocks`."
-  [blocks indent?]
+  [conn blocks indent?]
   {:pre [(seq blocks) (boolean? indent?)]}
-  (let [db (db/get-db)
+  (let [db @conn
         top-level-blocks (get-top-level-blocks blocks)
         non-consecutive-blocks (ldb/get-non-consecutive-blocks db top-level-blocks)]
     (when (empty? non-consecutive-blocks)
-      (let [first-block (db/entity (:db/id (first top-level-blocks)))
-            left (db/entity (:db/id (:block/left first-block)))
+      (let [first-block (d/entity db (:db/id (first top-level-blocks)))
+            left (d/entity db (:db/id (:block/left first-block)))
             parent (:block/parent first-block)
             concat-tx-fn (fn [& results]
                            {:tx-data (->> (map :tx-data results)
@@ -1124,41 +1115,41 @@
                                       top-level-blocks)]
               (when (seq blocks')
                 (if last-direct-child-id
-                  (let [last-direct-child (db/entity last-direct-child-id)
-                        result (move-blocks blocks' last-direct-child (merge opts {:sibling? true
+                  (let [last-direct-child (d/entity db last-direct-child-id)
+                        result (move-blocks conn blocks' last-direct-child (merge opts {:sibling? true
                                                                                    :indent? true}))
                         ;; expand `left` if it's collapsed
                         collapsed-tx (when (:block/collapsed? left)
                                        {:tx-data [{:db/id (:db/id left)
                                                    :block/collapsed? false}]})]
                     (concat-tx-fn result collapsed-tx))
-                  (move-blocks blocks' left (merge opts {:sibling? false
+                  (move-blocks conn blocks' left (merge opts {:sibling? false
                                                          :indent? true}))))))
           (if-let [parent-original (get-first-block-original)]
             (let [blocks' (take-while (fn [b]
                                         (not= (:db/id (:block/parent b))
                                               (:db/id (:block/parent parent))))
                                       top-level-blocks)]
-              (move-blocks blocks' parent-original (merge opts {:outliner-op :indent-outdent-blocks
+              (move-blocks conn blocks' parent-original (merge opts {:outliner-op :indent-outdent-blocks
                                                                 :sibling? true
                                                                 :indent? false})))
 
-            (when (and parent (not (page-block? (db/entity (:db/id parent)))))
+            (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 blocks' parent (merge opts {:sibling? true}))]
+                    result (move-blocks conn blocks' parent (merge opts {:sibling? true}))]
                 (if (state/logical-outdenting?)
                   result
                   ;; direct outdenting (default behavior)
-                  (let [last-top-block (db/entity (:db/id (last blocks')))
-                        right-siblings (->> (get-right-siblings (block last-top-block))
+                  (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 right-siblings (db/entity last-direct-child-id) (merge opts {:sibling? true}))
-                                      (move-blocks right-siblings last-top-block (merge opts {:sibling? false})))]
+                                      (move-blocks conn right-siblings (d/entity db last-direct-child-id) (merge opts {:sibling? true}))
+                                      (move-blocks conn right-siblings last-top-block (merge opts {:sibling? false})))]
                         (concat-tx-fn result result2))
                       result)))))))))))
 
@@ -1188,25 +1179,25 @@
     result))
 
 (defn save-block!
-  [block]
-  (op-transact! #'save-block block))
+  [repo conn block]
+  (op-transact! #'save-block repo conn block))
 
 (defn insert-blocks!
-  [blocks target-block opts]
-  (op-transact! #'insert-blocks blocks target-block (assoc opts :outliner-op :insert-blocks)))
+  [conn blocks target-block opts]
+  (op-transact! #'insert-blocks conn blocks target-block (assoc opts :outliner-op :insert-blocks)))
 
 (defn delete-blocks!
-  [blocks opts]
-  (op-transact! #'delete-blocks blocks (assoc opts :outliner-op :delete-blocks)))
+  [repo conn blocks opts]
+  (op-transact! #'delete-blocks repo conn blocks (assoc opts :outliner-op :delete-blocks)))
 
 (defn move-blocks!
-  [blocks target-block sibling?]
-  (op-transact! #'move-blocks blocks target-block {:sibling? sibling?
-                                                   :outliner-op :move-blocks}))
+  [conn blocks target-block sibling?]
+  (op-transact! #'move-blocks conn blocks target-block {:sibling? sibling?
+                                                        :outliner-op :move-blocks}))
 (defn move-blocks-up-down!
-  [blocks up?]
-  (op-transact! #'move-blocks-up-down blocks up?))
+  [conn blocks up?]
+  (op-transact! #'move-blocks-up-down conn blocks up?))
 
 (defn indent-outdent-blocks!
-  [blocks indent?]
-  (op-transact! #'indent-outdent-blocks blocks indent?))
+  [conn blocks indent?]
+  (op-transact! #'indent-outdent-blocks conn blocks indent?))

+ 1 - 1
src/main/logseq/api.cljs

@@ -718,7 +718,7 @@
 (def ^:export get_next_sibling_block
   (fn [block-uuid]
     (when-let [block (db-model/query-block-by-uuid (sdk-utils/uuid-or-throw-error block-uuid))]
-      (when-let [right-sibling (outliner-core/get-right-sibling (:db/id block))]
+      (when-let [right-sibling (outliner-core/get-right-sibling (db/get-db) (:db/id block))]
         (let [block (db/pull (:db/id right-sibling))]
           (bean/->js (sdk-utils/normalize-keyword-for-json block)))))))
 

+ 21 - 22
src/test/frontend/modules/outliner/core_test.cljs

@@ -34,7 +34,7 @@
   ([id node?]
    (cond-> (db/pull test-db '[*] [:block/uuid id])
      node?
-     outliner-core/block)))
+     (partial outliner-core/block (db/get-db)))))
 
 (defn build-node-tree
   [col]
@@ -125,7 +125,7 @@
     (transact-tree! tree)
     (let [block (get-block 6)]
       (outliner-tx/transact! {:graph test-db}
-                             (outliner-core/delete-blocks! [block] {:children? true}))
+                             (outliner-core/delete-blocks! test-db (db/get-db test-db false) [block] {:children? true}))
       (is (= [3 9] (get-children 2))))))
 
 (deftest test-move-block-as-sibling
@@ -185,7 +185,7 @@
     (transact-tree! tree)
     (outliner-tx/transact!
       {:graph test-db}
-      (outliner-core/indent-outdent-blocks! [(get-block 6) (get-block 9)] true))
+      (outliner-core/indent-outdent-blocks! (db/get-db test-db false) [(get-block 6) (get-block 9)] true))
     (is (= [4 5 6 9] (get-children 3)))))
 
 (deftest test-indent-blocks-regression-5604
@@ -204,7 +204,7 @@
     (transact-tree! tree)
     (outliner-tx/transact!
       {:graph test-db}
-      (outliner-core/indent-outdent-blocks! [(get-block 13) (get-block 14) (get-block 15)] false))
+      (outliner-core/indent-outdent-blocks! (db/get-db test-db false) [(get-block 13) (get-block 14) (get-block 15)] false))
     (is (= [2 12 13 14 15 16] (get-children 22))))
   (testing "
   [22 [[2 [[3
@@ -221,7 +221,7 @@
     (transact-tree! tree)
     (outliner-tx/transact!
       {:graph test-db}
-      (outliner-core/indent-outdent-blocks! [(get-block 13) (get-block 14)] false))
+      (outliner-core/indent-outdent-blocks! (db/get-db test-db false) [(get-block 13) (get-block 14)] false))
     (is (= [2 12 13 14 16] (get-children 22)))))
 
 (deftest test-fix-top-level-blocks
@@ -271,7 +271,7 @@
     (transact-tree! tree)
     (outliner-tx/transact!
       {:graph test-db}
-      (outliner-core/indent-outdent-blocks! [(get-block 4) (get-block 5)] false))
+      (outliner-core/indent-outdent-blocks! (db/get-db test-db false) [(get-block 4) (get-block 5)] false))
     (is (= [3 4 5 6 9] (get-children 2)))))
 
 (deftest test-delete-blocks
@@ -290,7 +290,7 @@
     (transact-tree! tree)
     (outliner-tx/transact!
       {:graph test-db}
-      (outliner-core/delete-blocks! [(get-block 6) (get-block 9)] {}))
+      (outliner-core/delete-blocks! test-db (db/get-db test-db false) [(get-block 6) (get-block 9)] {}))
     (is (= [3] (get-children 2)))))
 
 (deftest test-delete-non-consecutive-blocks
@@ -309,7 +309,7 @@
     (transact-tree! tree)
     (outliner-tx/transact!
      {:graph test-db}
-      (outliner-core/delete-blocks! [(get-block 10) (get-block 13)] {}))
+      (outliner-core/delete-blocks! test-db (db/get-db test-db false) [(get-block 10) (get-block 13)] {}))
     (is (= [11] (get-children 9)))
     (is (= [14 15] (get-children 12)))))
 
@@ -328,7 +328,7 @@
     (transact-tree! tree)
     (outliner-tx/transact!
       {:graph test-db}
-      (outliner-core/move-blocks-up-down! [(get-block 9)] true))
+      (outliner-core/move-blocks-up-down! (db/get-db test-db false) [(get-block 9)] true))
     (is (= [3 9 6] (get-children 2)))))
 
 (deftest test-insert-blocks
@@ -397,7 +397,7 @@
           (outliner-core/insert-blocks! new-blocks target-block {:sibling? false
                                                                  :keep-uuid? true
                                                                  :replace-empty-target? false})
-          (outliner-core/delete-blocks! [(get-block 3)] {}))
+          (outliner-core/delete-blocks! test-db (db/get-db test-db false) [(get-block 3)] {}))
 
         (is (= [4] (get-children 2)))
 
@@ -468,7 +468,7 @@
 (defn- save-block!
   [block]
   (outliner-tx/transact! {:graph test-db}
-                         (outliner-core/save-block! block)))
+                         (outliner-core/save-block! test-db (db/get-db test-db false) block)))
 
 (deftest save-test
   (load-test-files [{:file/path "pages/page1.md"
@@ -586,7 +586,7 @@ tags:: tag1, tag2
     (when-let [block (get-random-block)]
       (loop [result [block]
              node block]
-        (if-let [next (outliner-core/get-right-sibling (:db/id node))]
+        (if-let [next (outliner-core/get-right-sibling (db/get-db test-db) (:db/id node))]
           (let [next (db/pull test-db '[*] (:db/id next))]
             (if (>= (count result) limit)
               result
@@ -616,7 +616,7 @@ tags:: tag1, tag2
       (let [blocks (get-random-successive-blocks)]
         (when (seq blocks)
           (outliner-tx/transact! {:graph test-db}
-            (outliner-core/delete-blocks! blocks {})))))))
+            (outliner-core/delete-blocks! test-db (db/get-db test-db false) blocks {})))))))
 
 (deftest ^:long random-moves
   (testing "Random moves"
@@ -633,7 +633,7 @@ tags:: tag1, tag2
           (when (seq blocks)
             (let [target (get-random-block)]
               (outliner-tx/transact! {:graph test-db}
-                (outliner-core/move-blocks! blocks target (gen/generate gen/boolean)))
+                (outliner-core/move-blocks! (db/get-db test-db false) blocks target (gen/generate gen/boolean)))
               (let [total (get-blocks-count)]
                 (is (= total (count @*random-blocks)))))))))))
 
@@ -651,7 +651,7 @@ tags:: tag1, tag2
         (let [blocks (get-random-successive-blocks)]
           (when (seq blocks)
             (outliner-tx/transact! {:graph test-db}
-              (outliner-core/move-blocks-up-down! blocks (gen/generate gen/boolean)))
+                                   (outliner-core/move-blocks-up-down! (db/get-db test-db false) blocks (gen/generate gen/boolean)))
             (let [total (get-blocks-count)]
               (is (= total (count @*random-blocks))))))))))
 
@@ -670,7 +670,7 @@ tags:: tag1, tag2
                 indent? (gen/generate gen/boolean)]
             (when (seq blocks)
               (outliner-tx/transact! {:graph test-db}
-                (outliner-core/indent-outdent-blocks! blocks indent?))
+                (outliner-core/indent-outdent-blocks! (db/get-db test-db false) blocks indent?))
               (let [total (get-blocks-count)]
                 (is (= total (count @*random-blocks)))))))))))
 
@@ -679,8 +679,7 @@ tags:: tag1, tag2
     (transact-random-tree!)
     (let [c1 (get-blocks-ids)
           *random-blocks (atom c1)
-          ops [
-               ;; insert
+          ops [;; insert
                (fn []
                  (let [blocks (gen-blocks)]
                    (swap! *random-blocks (fn [old]
@@ -694,28 +693,28 @@ tags:: tag1, tag2
                      (swap! *random-blocks (fn [old]
                                              (set/difference old (set (map :block/uuid blocks)))))
                      (outliner-tx/transact! {:graph test-db}
-                       (outliner-core/delete-blocks! blocks {})))))
+                                            (outliner-core/delete-blocks! test-db (db/get-db test-db false) blocks {})))))
 
                ;; move
                (fn []
                  (let [blocks (get-random-successive-blocks)]
                    (when (seq blocks)
                      (outliner-tx/transact! {:graph test-db}
-                       (outliner-core/move-blocks! blocks (get-random-block) (gen/generate gen/boolean))))))
+                                            (outliner-core/move-blocks! blocks (get-random-block) (gen/generate gen/boolean))))))
 
                ;; move up down
                (fn []
                  (let [blocks (get-random-successive-blocks)]
                    (when (seq blocks)
                      (outliner-tx/transact! {:graph test-db}
-                      (outliner-core/move-blocks-up-down! blocks (gen/generate gen/boolean))))))
+                                            (outliner-core/move-blocks-up-down! (db/get-db test-db false) blocks (gen/generate gen/boolean))))))
 
                ;; indent outdent
                (fn []
                  (let [blocks (get-random-successive-blocks)]
                    (when (seq blocks)
                      (outliner-tx/transact! {:graph test-db}
-                       (outliner-core/indent-outdent-blocks! blocks (gen/generate gen/boolean))))))]]
+                                            (outliner-core/indent-outdent-blocks! (db/get-db test-db false) blocks (gen/generate gen/boolean))))))]]
       (dotimes [_i 100]
         ((rand-nth ops)))
       (let [total (get-blocks-count)