Просмотр исходного кода

enhance(api): add `fullTitle` and remove temp properties

Tienson Qin 5 месяцев назад
Родитель
Сommit
8039054e75
3 измененных файлов с 60 добавлено и 45 удалено
  1. 9 8
      src/main/logseq/api/block.cljs
  2. 23 9
      src/main/logseq/sdk/utils.cljs
  3. 28 28
      src/test/logseq/api_test.cljs

+ 9 - 8
src/main/logseq/api/block.cljs

@@ -17,7 +17,6 @@
             [frontend.state :as state]
             [logseq.db :as ldb]
             [logseq.db.frontend.db-ident :as db-ident]
-            [logseq.db.frontend.property :as db-property]
             [logseq.sdk.utils :as sdk-utils]
             [promesa.core :as p]))
 
@@ -71,17 +70,19 @@
           (db-pu/readable-properties
            {:original-key? true :key-fn str})))
 
+(defn- entity->map
+  [e]
+  (assoc (into {} e) :db/id (:db/id e)))
+
 (defn into-properties
   ([block] (into-properties (state/get-current-repo) block))
   ([repo block]
    (if (some-> repo (config/db-based-graph?))
-     (let [props (some->> block
-                          (filter (fn [[k _]] (db-property/property? k)))
-                          (into {})
-                          (into-readable-db-properties))
-           block (update block :block/properties merge props)
-           block (apply dissoc (concat [block] (keys props)))]
-       block)
+     (let [e (db/entity (:db/id block))
+           props (-> (:block/properties e)
+                     sdk-utils/remove-hidden-properties)]
+       (-> (entity->map block)
+           (assoc :block/properties props)))
      block)))
 
 (defn parse-property-json-value-if-need

+ 23 - 9
src/main/logseq/sdk/utils.cljs

@@ -1,16 +1,18 @@
 (ns logseq.sdk.utils
-  (:require [clojure.walk :as walk]
-            [camel-snake-kebab.core :as csk]
-            [frontend.util :as util]
+  (:require [camel-snake-kebab.core :as csk]
+            [cljs-bean.core :as bean]
+            [clojure.walk :as walk]
             [datascript.impl.entity :as de]
+            [frontend.db :as db]
+            [frontend.util :as util]
             [goog.object :as gobj]
-            [cljs-bean.core :as bean]))
+            [logseq.db.frontend.content :as db-content]))
 
 (defn- keep-json-keyword?
   [k]
   (some->> (namespace k)
-    (contains? #{"block" "db" "file"})
-    (not)))
+           (contains? #{"block" "db" "file"})
+           (not)))
 
 (defn- entity->map
   "Convert a db Entity to a map"
@@ -18,6 +20,13 @@
   (assert (de/entity? e))
   (assoc (into {} e) :db/id (:db/id e)))
 
+(defn remove-hidden-properties
+  [m]
+  (->> (remove (fn [[k _v]]
+                 (or (= "block.temp" (namespace k))
+                     (contains? #{:logseq.property.embedding/hnsw-label-updated-at} k))) m)
+       (into {})))
+
 (defn normalize-keyword-for-json
   ([input] (normalize-keyword-for-json input true))
   ([input camel-case?]
@@ -42,8 +51,13 @@
             (uuid? a) (str a)
 
             ;; @FIXME compatible layer for classic APIs
-            (and (map? a) (:block/uuid a))
-            (or (some->> (:block/title a) (assoc a :block/content)) a)
+            (and (map? a) (:block/uuid a) (:block/title a))
+            (-> a
+                (assoc :block/content (:block/title a)
+                       :block/full-title (or (when-let [e (db/entity [:block/uuid (:block/uuid a)])]
+                                               (db-content/recur-replace-uuid-in-block-title e))
+                                             (:block/title a)))
+                remove-hidden-properties)
 
             :else a)) input)))))
 
@@ -68,7 +82,7 @@
             (if (= "function" (goog/typeOf v))
               (assoc result k v)
               (assoc result k (jsx->clj v)))))
-      (reduce {} (gobj/getKeys obj)))
+        (reduce {} (gobj/getKeys obj)))
     obj))
 
 (def ^:export to-clj bean/->clj)

+ 28 - 28
src/test/logseq/api_test.cljs

@@ -1,10 +1,10 @@
 (ns logseq.api-test
-  (:require [cljs.test :refer [use-fixtures deftest is]]
-            [frontend.test.helper :as test-helper]
+  (:require [cljs-bean.core :as bean]
+            [cljs.test :refer [use-fixtures deftest is]]
             [frontend.db :as db]
-            [logseq.api.block :as api-block]
             [frontend.state :as state]
-            [cljs-bean.core :as bean]))
+            [frontend.test.helper :as test-helper]
+            [logseq.api.block :as api-block]))
 
 (use-fixtures :each {:before test-helper/start-test-db!
                      :after test-helper/destroy-test-db!})
@@ -12,32 +12,32 @@
 (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/title "1"}
-       {:db/id 10001
-        :block/uuid #uuid "d9b7b45f-267f-4794-9569-f43d1ce77172"
-        :block/title "2"}
-       {:db/id 10002
-        :block/uuid #uuid "adae3006-f03e-4814-a1f5-f17f15b86556"
-        :block/parent 10001
-        :block/title "3"}
-       {:db/id 10003
-        :block/uuid #uuid "0c3053c3-2dab-4769-badd-14ce16d8ba8d"
-        :block/parent 10002
-        :block/title "4"}])
+                  [{:db/id 10000
+                    :block/uuid #uuid "4406f839-6410-43b5-87db-25e9b8f54cc0"
+                    :block/title "1"}
+                   {:db/id 10001
+                    :block/uuid #uuid "d9b7b45f-267f-4794-9569-f43d1ce77172"
+                    :block/refs #{10000}
+                    :block/title "2 [[4406f839-6410-43b5-87db-25e9b8f54cc0]]"}
+                   {:db/id 10002
+                    :block/uuid #uuid "adae3006-f03e-4814-a1f5-f17f15b86556"
+                    :block/parent 10001
+                    :block/title "3"}
+                   {:db/id 10003
+                    :block/uuid #uuid "0c3053c3-2dab-4769-badd-14ce16d8ba8d"
+                    :block/parent 10002
+                    :block/title "4"}])
 
     (is (= (:title (bean/->clj (api-block/get_block 10000 #js {}))) "1"))
-    (is (= (:title (bean/->clj (api-block/get_block "d9b7b45f-267f-4794-9569-f43d1ce77172" #js {}))) "2"))
-    (is (= (:title (bean/->clj (api-block/get_block #uuid "d9b7b45f-267f-4794-9569-f43d1ce77172" #js {}))) "2"))
-    (is (= {:id 10001, :title "2", :uuid "d9b7b45f-267f-4794-9569-f43d1ce77172", :children [["uuid" "adae3006-f03e-4814-a1f5-f17f15b86556"]]}
-          (select-keys (js->clj (api-block/get_block 10001 #js {:includeChildren false}) :keywordize-keys true)
-            [:id :title :uuid :children])))
+    (is (= (:content (bean/->clj (api-block/get_block 10000 #js {}))) "1"))
+    (is (= (:title (bean/->clj (api-block/get_block "d9b7b45f-267f-4794-9569-f43d1ce77172" #js {}))) "2 [[4406f839-6410-43b5-87db-25e9b8f54cc0]]"))
+    (is (= (:fullTitle (bean/->clj (api-block/get_block "d9b7b45f-267f-4794-9569-f43d1ce77172" #js {}))) "2 [[1]]"))
+    (is (= (:title (bean/->clj (api-block/get_block #uuid "d9b7b45f-267f-4794-9569-f43d1ce77172" #js {}))) "2 [[4406f839-6410-43b5-87db-25e9b8f54cc0]]"))
+    (is (= {:id 10001, :title "2 [[4406f839-6410-43b5-87db-25e9b8f54cc0]]", :uuid "d9b7b45f-267f-4794-9569-f43d1ce77172", :children [["uuid" "adae3006-f03e-4814-a1f5-f17f15b86556"]]}
+           (select-keys (js->clj (api-block/get_block 10001 #js {:includeChildren false}) :keywordize-keys true)
+                        [:id :title :uuid :children])))
     ;; NOTE: `content` key is to be compatible with old APIs
-    (is (= {:id 10001, :title "2", :content "2" :uuid "d9b7b45f-267f-4794-9569-f43d1ce77172"
-            :children [{:id 10002 :title "3", :content "3"
-                        :parent {:id 10001}, :uuid "adae3006-f03e-4814-a1f5-f17f15b86556", :level 1,
-                        :children [{:id 10003, :title "4", :content "4" :parent {:id 10002}, :uuid "0c3053c3-2dab-4769-badd-14ce16d8ba8d", :level 2, :children []}]}]}
-          (js->clj (api-block/get_block 10001 #js {:includeChildren true}) :keywordize-keys true)))))
+    (is (= {:id 10001, :refs [{:id 10000}], :title "2 [[4406f839-6410-43b5-87db-25e9b8f54cc0]]", :uuid "d9b7b45f-267f-4794-9569-f43d1ce77172", :children [{:id 10002, :parent {:id 10001}, :title "3", :uuid "adae3006-f03e-4814-a1f5-f17f15b86556", :level 1, :children [{:id 10003, :parent {:id 10002}, :title "4", :uuid "0c3053c3-2dab-4769-badd-14ce16d8ba8d", :level 2, :children [], :content "4", :fullTitle "4"}], :content "3", :fullTitle "3"}], :properties {}, :content "2 [[4406f839-6410-43b5-87db-25e9b8f54cc0]]", :fullTitle "2 [[1]]"}
+           (js->clj (api-block/get_block 10001 #js {:includeChildren true}) :keywordize-keys true)))))
 
 #_(cljs.test/run-tests)