1
0
Эх сурвалжийг харах

fix: api get current block (#9372)

Tienson Qin 2 жил өмнө
parent
commit
d4cde54510

+ 3 - 19
src/main/logseq/api.cljs

@@ -43,7 +43,8 @@
             [frontend.handler.shell :as shell]
             [frontend.modules.layout.core]
             [frontend.handler.code :as code-handler]
-            [frontend.handler.search :as search-handler]))
+            [frontend.handler.search :as search-handler]
+            [logseq.api.block :as api-block]))
 
 ;; Alert: this namespace shouldn't invoke any reactive queries
 
@@ -676,24 +677,7 @@
           target-block (db-model/query-block-by-uuid (sdk-utils/uuid-or-throw-error target-block-uuid))]
       (editor-dnd-handler/move-blocks nil [src-block] target-block move-to) nil)))
 
-(def ^:export get_block
-  (fn [id-or-uuid ^js opts]
-    (when-let [block (cond
-                       (number? id-or-uuid) (db-utils/pull id-or-uuid)
-                       (string? id-or-uuid) (db-model/query-block-by-uuid (sdk-utils/uuid-or-throw-error id-or-uuid)))]
-      (when-not (contains? block :block/name)
-        (when-let [uuid (:block/uuid block)]
-          (let [{:keys [includeChildren]} (bean/->clj opts)
-                repo  (state/get-current-repo)
-                block (if includeChildren
-                        ;; nested children results
-                        (first (outliner-tree/blocks->vec-tree
-                                (db-model/get-block-and-children repo uuid) uuid))
-                        ;; attached shallow children
-                        (assoc block :block/children
-                               (map #(list :uuid (:block/uuid %))
-                                 (db/get-block-immediate-children repo uuid))))]
-            (bean/->js (sdk-utils/normalize-keyword-for-json block))))))))
+(def ^:export get_block api-block/get_block)
 
 (def ^:export get_current_block
   (fn [^js opts]

+ 28 - 0
src/main/logseq/api/block.cljs

@@ -0,0 +1,28 @@
+(ns logseq.api.block
+  "Block related apis"
+  (:require [frontend.db.model :as db-model]
+            [frontend.db.utils :as db-utils]
+            [cljs-bean.core :as bean]
+            [frontend.state :as state]
+            [frontend.modules.outliner.tree :as outliner-tree]
+            [frontend.db :as db]
+            [logseq.sdk.utils :as sdk-utils]))
+
+(defn get_block
+  [id-or-uuid ^js opts]
+  (when-let [block (if (number? id-or-uuid)
+                     (db-utils/pull id-or-uuid)
+                     (db-model/query-block-by-uuid (sdk-utils/uuid-or-throw-error id-or-uuid)))]
+    (when-not (contains? block :block/name)
+      (when-let [uuid (:block/uuid block)]
+        (let [{:keys [includeChildren]} (bean/->clj opts)
+              repo  (state/get-current-repo)
+              block (if includeChildren
+                      ;; nested children results
+                      (first (outliner-tree/blocks->vec-tree
+                              (db-model/get-block-and-children repo uuid) uuid))
+                      ;; attached shallow children
+                      (assoc block :block/children
+                             (map #(list :uuid (:block/uuid %))
+                               (db/get-block-immediate-children repo uuid))))]
+          (bean/->js (sdk-utils/normalize-keyword-for-json block)))))))

+ 3 - 2
src/test/frontend/fs_test.cljs

@@ -2,6 +2,7 @@
   (:require [clojure.test :refer [is use-fixtures]]
             [frontend.test.fixtures :as fixtures]
             [frontend.test.helper :as test-helper :include-macros true :refer [deftest-async]]
+            [frontend.test.node-helper :as test-node-helper]
             [frontend.fs :as fs]
             [promesa.core :as p]
             ["fs" :as fs-node]
@@ -11,7 +12,7 @@
 
 (deftest-async create-if-not-exists-creates-correctly
   ;; dir needs to be an absolute path for fn to work correctly
-  (let [dir (node-path/resolve (test-helper/create-tmp-dir))
+  (let [dir (node-path/resolve (test-node-helper/create-tmp-dir))
         some-file (node-path/join dir "something.txt")]
 
     (->
@@ -29,7 +30,7 @@
         (fs-node/rmdirSync dir))))))
 
 (deftest-async create-if-not-exists-does-not-create-correctly
-  (let [dir (node-path/resolve (test-helper/create-tmp-dir))
+  (let [dir (node-path/resolve (test-node-helper/create-tmp-dir))
         some-file (node-path/join dir "something.txt")]
     (fs-node/writeFileSync some-file "OLD")
 

+ 2 - 1
src/test/frontend/handler/plugin_config_test.cljs

@@ -1,6 +1,7 @@
 (ns frontend.handler.plugin-config-test
   (:require [clojure.test :refer [is use-fixtures testing deftest]]
             [frontend.test.helper :as test-helper :include-macros true :refer [deftest-async]]
+            [frontend.test.node-helper :as test-node-helper]
             [frontend.test.fixtures :as fixtures]
             [frontend.handler.plugin-config :as plugin-config-handler]
             [frontend.handler.global-config :as global-config-handler]
@@ -17,7 +18,7 @@
 
 (defn- create-global-config-dir
   []
-  (let [dir (test-helper/create-tmp-dir "config")
+  (let [dir (test-node-helper/create-tmp-dir "config")
         root-dir (node-path/dirname dir)]
     (reset! global-config-handler/root-dir root-dir)
     dir))

+ 1 - 16
src/test/frontend/test/helper.cljs

@@ -1,9 +1,7 @@
 (ns frontend.test.helper
   "Common helper fns for tests"
   (:require [frontend.handler.repo :as repo-handler]
-            [frontend.db.conn :as conn]
-            ["path" :as node-path]
-            ["fs" :as fs-node]))
+            [frontend.db.conn :as conn]))
 
 (defonce test-db "test-db")
 
@@ -24,16 +22,3 @@ This can be called in synchronous contexts as no async fns should be invoked"
    files
    ;; Set :refresh? to avoid creating default files in after-parse
    {:re-render? false :verbose false :refresh? true}))
-
-(defn create-tmp-dir
-  "Creates a temporary directory under tmp/. If a subdir is given, creates an
-  additional subdirectory under the newly created temp directory."
-  ([] (create-tmp-dir nil))
-  ([subdir]
-   (when-not (fs-node/existsSync "tmp") (fs-node/mkdirSync "tmp"))
-   (let [dir (fs-node/mkdtempSync (node-path/join "tmp" "unit-test-"))]
-     (if subdir
-       (do
-         (fs-node/mkdirSync (node-path/join dir subdir))
-         (node-path/join dir subdir))
-       dir))))

+ 17 - 0
src/test/frontend/test/node_helper.cljs

@@ -0,0 +1,17 @@
+(ns frontend.test.node-helper
+  "Common helper fns for node tests"
+  (:require ["path" :as node-path]
+            ["fs" :as fs-node]))
+
+(defn create-tmp-dir
+  "Creates a temporary directory under tmp/. If a subdir is given, creates an
+  additional subdirectory under the newly created temp directory."
+  ([] (create-tmp-dir nil))
+  ([subdir]
+   (when-not (fs-node/existsSync "tmp") (fs-node/mkdirSync "tmp"))
+   (let [dir (fs-node/mkdtempSync (node-path/join "tmp" "unit-test-"))]
+     (if subdir
+       (do
+         (fs-node/mkdirSync (node-path/join dir subdir))
+         (node-path/join dir subdir))
+       dir))))

+ 40 - 0
src/test/logseq/api_test.cljs

@@ -0,0 +1,40 @@
+(ns logseq.api-test
+  (:require [cljs.test :refer [use-fixtures deftest is]]
+            [frontend.test.helper :as test-helper]
+            [frontend.db :as db]
+            [logseq.api.block :as api-block]
+            [frontend.state :as state]
+            [cljs-bean.core :as bean]))
+
+(use-fixtures :each {:before test-helper/start-test-db!
+                     :after test-helper/destroy-test-db!})
+
+(deftest get-block
+  (with-redefs [state/get-current-repo (constantly test-helper/test-db)]
+    (db/transact! test-helper/test-db
+      [{:db/id 10000
+        :block/uuid #uuid "4406f839-6410-43b5-87db-25e9b8f54cc0"
+        :block/content "1"}
+       {:db/id 10001
+        :block/uuid #uuid "d9b7b45f-267f-4794-9569-f43d1ce77172"
+        :block/content "2"}
+       {:db/id 10002
+        :block/uuid #uuid "adae3006-f03e-4814-a1f5-f17f15b86556"
+        :block/parent 10001
+        :block/left 10001
+        :block/content "3"}
+       {:db/id 10003
+        :block/uuid #uuid "0c3053c3-2dab-4769-badd-14ce16d8ba8d"
+        :block/parent 10002
+        :block/left 10002
+        :block/content "4"}])
+
+    (is (= (:content (bean/->clj (api-block/get_block 10000 #js {}))) "1"))
+    (is (= (:content (bean/->clj (api-block/get_block "d9b7b45f-267f-4794-9569-f43d1ce77172" #js {}))) "2"))
+    (is (= (:content (bean/->clj (api-block/get_block #uuid "d9b7b45f-267f-4794-9569-f43d1ce77172" #js {}))) "2"))
+    (is (= {:id 10001, :content "2", :uuid "d9b7b45f-267f-4794-9569-f43d1ce77172", :children [["uuid" "adae3006-f03e-4814-a1f5-f17f15b86556"]]}
+           (bean/->clj (api-block/get_block 10001 #js {:includeChildren false}))))
+    (is (= {:content "2", :uuid "d9b7b45f-267f-4794-9569-f43d1ce77172", :id 10001, :children [{:content "3", :left {:id 10001}, :parent {:id 10001}, :uuid "adae3006-f03e-4814-a1f5-f17f15b86556", :id 10002, :level 1, :children [{:content "4", :left {:id 10002}, :parent {:id 10002}, :uuid "0c3053c3-2dab-4769-badd-14ce16d8ba8d", :id 10003, :level 2, :children []}]}]}
+           (bean/->clj (api-block/get_block 10001 #js {:includeChildren true}))))))
+
+#_(cljs.test/run-tests)