浏览代码

perfer to use get-page instead of get-first-page-by-name

Tienson Qin 1 年之前
父节点
当前提交
c018f1517d

+ 10 - 10
deps/db/src/logseq/db.cljs

@@ -172,6 +172,15 @@
   (when page-name
     (some? (get-first-page-by-name db page-name))))
 
+(defn get-page
+  "Get a page given its unsanitized name"
+  [db page-name-or-uuid]
+  (when db
+    (if-let [id (if (uuid? page-name-or-uuid) page-name-or-uuid
+                    (parse-uuid page-name-or-uuid))]
+      (d/entity db [:block/uuid id])
+      (d/entity db (get-first-page-by-name db (name page-name-or-uuid))))))
+
 (defn page-empty?
   "Whether a page is empty. Does it has a non-page block?
   `page-id` could be either a string or a db/id."
@@ -192,7 +201,7 @@
         orphaned-pages (->>
                         (map
                          (fn [page]
-                           (when-let [page (d/entity db (get-first-page-by-name db page))]
+                           (when-let [page (get-page db page)]
                              (let [name (:block/name page)]
                                (and
                                 (empty-ref-f page)
@@ -397,15 +406,6 @@
     (when (seq pages)
       (first pages))))
 
-(defn get-page
-  "Get a page given its unsanitized name"
-  [db page-name-or-uuid]
-  (when db
-    (if-let [id (if (uuid? page-name-or-uuid) page-name-or-uuid
-                    (parse-uuid page-name-or-uuid))]
-      (d/entity db [:block/uuid id])
-      (d/entity db (get-first-page-by-name db (name page-name-or-uuid))))))
-
 (defn get-page-alias
   [db page-id]
   (->>

+ 1 - 1
deps/outliner/src/logseq/outliner/tree.cljs

@@ -58,7 +58,7 @@
     (string? root-id)
     (if-let [id (parse-uuid root-id)]
       [false (d/entity db [:block/uuid id])]
-      [true (d/entity db (ldb/get-first-page-by-name db root-id))])
+      [true (ldb/get-page db root-id)])
 
     :else
     [false root-id]))

+ 2 - 2
src/main/frontend/components/container.cljs

@@ -225,7 +225,7 @@
     (let [page (:page default-home)
           page (when (and (string? page)
                           (not (string/blank? page)))
-                 (db/entity (ldb/get-first-page-by-name (db/get-db) page)))]
+                 (db/get-page page))]
       (if page
         default-home
         (dissoc default-home :page)))))
@@ -652,7 +652,7 @@
                    (let [page (util/safe-page-name-sanity-lc page)
                          [db-id block-type] (if (= page "contents")
                                               ["contents" :contents]
-                                              [(ldb/get-first-page-by-name (db/get-db) page) :page])]
+                                              [(:db/id (db/get-page page)) :page])]
                      (state/sidebar-add-block! current-repo db-id block-type)))
                  (reset! sidebar-inited? true))))
            (when (state/mobile?)

+ 2 - 2
src/main/frontend/components/editor.cljs

@@ -109,10 +109,10 @@
                         (common-util/safe-subs value (+ (count q) 4 pos)))]
         (state/set-edit-content! (.-id input) value')
         (state/clear-editor-action!)
-        (p/let [page (db/entity (ldb/get-first-page-by-name (db/get-db) chosen-item))
+        (p/let [page (db/get-page chosen-item)
                 _ (when-not page (page-handler/<create! chosen-item {:redirect? false
                                                                      :create-first-block? false}))
-                page' (db/entity (ldb/get-first-page-by-name (db/get-db) chosen-item))
+                page' (db/get-page chosen-item)
                 current-block (state/get-edit-block)]
           (editor-handler/api-insert-new-block! chosen-item
                                                 {:block-uuid (:block/uuid current-block)

+ 2 - 2
src/main/frontend/components/right_sidebar.cljs

@@ -52,7 +52,7 @@
 (rum/defc contents < rum/reactive db-mixins/query
   []
   [:div.contents.flex-col.flex.ml-3
-   (when-let [contents (db/entity (ldb/get-first-page-by-name (db/get-db) "contents"))]
+   (when-let [contents (db/get-page "contents")]
      (page/contents-page contents))])
 
 (rum/defc shortcut-settings
@@ -153,7 +153,7 @@
         (if-let [icon (get-in page [:block/properties :icon])]
           [:.text-md.mr-2 icon]
           (ui/icon (if (= "whiteboard" (:block/type page)) "whiteboard" "page") {:class "text-md mr-2"}))
-        [:span.overflow-hidden.text-ellipsis (db-model/get-page-original-name page-name)]]
+        [:span.overflow-hidden.text-ellipsis (:block/original-name page)]]
        (page-cp repo page-name)])
 
     :search

+ 1 - 1
src/main/frontend/db/async.cljs

@@ -96,7 +96,7 @@
             (util/uuid-string? name')
             (db/entity [:block/uuid (uuid name')])
             :else
-            (db/entity (ldb/get-first-page-by-name (db/get-db) name')))
+            (db/get-page name'))
         uuid-str (or (str (:block/uuid e)) name')]
     (if (:block.temp/fully-loaded? e)
       e

+ 7 - 10
src/main/frontend/db/model.cljs

@@ -236,7 +236,7 @@ independent of format as format specific heading characters are stripped"
     :markdown
     (keyword
      (or
-      (let [page (db-utils/entity (ldb/get-first-page-by-name (conn/get-db) page-name))]
+      (let [page (ldb/get-page (conn/get-db) page-name)]
         (or
          (:block/format page)
          (when-let [file (:block/file page)]
@@ -318,8 +318,7 @@ independent of format as format specific heading characters are stripped"
        :level - the level of the block, 1 for root, 2 for children of root, etc."
   [page-name]
   {:pre [(string? page-name)]}
-  (let [page-id (ldb/get-first-page-by-name (conn/get-db) page-name)
-        root (db-utils/entity page-id)]
+  (let [root (ldb/get-page (conn/get-db) page-name)]
     (loop [result []
            children (sort-by-left (:block/_parent root) root)
            ;; BFS log of walking depth
@@ -544,7 +543,7 @@ independent of format as format specific heading characters are stripped"
   ([page-name alias?]
    (when page-name
      (let [page-name' (util/page-name-sanity-lc page-name)
-           page-entity (db-utils/entity (ldb/get-first-page-by-name (conn/get-db) page-name))]
+           page-entity (ldb/get-page (conn/get-db) page-name)]
        (cond
          alias?
          page-name'
@@ -559,13 +558,11 @@ independent of format as format specific heading characters are stripped"
            (or (when source-page (:block/name source-page))
                page-name')))))))
 
-;; FIXME: should pass page's db id
 (defn get-page-original-name
   [page-name]
   (when (string? page-name)
-    (let [page (ldb/get-first-page-by-name (conn/get-db) page-name)]
-      (or (:block/original-name page)
-          (:block/name page)))))
+    (let [page (ldb/get-page (conn/get-db) page-name)]
+      (:block/original-name page))))
 
 (defn get-journals-length
   []
@@ -747,7 +744,7 @@ independent of format as format specific heading characters are stripped"
 (defn journal-page?
   "sanitized page-name only"
   [page-name]
-  (:block/journal? (db-utils/entity (ldb/get-first-page-by-name (conn/get-db) page-name))))
+  (:block/journal? (ldb/get-page (conn/get-db) page-name)))
 
 (defn get-block-property-values
   "Get blocks which have this property."
@@ -831,7 +828,7 @@ independent of format as format specific heading characters are stripped"
 ;; FIXME: use `Untitled` instead of UUID for db based graphs
 (defn untitled-page?
   [page-name]
-  (when (db-utils/entity (ldb/get-first-page-by-name (conn/get-db) page-name))
+  (when (ldb/get-page (conn/get-db) page-name)
     (some? (parse-uuid page-name))))
 
 (defn get-all-whiteboards

+ 1 - 1
src/main/frontend/db/react.cljs

@@ -162,7 +162,7 @@
     (when page
       (if (common-util/uuid-string? page)
         (db-utils/entity [:block/uuid (uuid page)])
-        (db-utils/entity (ldb/get-first-page-by-name (conn/get-db) page))))))
+        (ldb/get-page (conn/get-db) page)))))
 
 (defn- execute-query!
   [graph db k {:keys [query inputs transform-fn query-fn inputs-fn result]

+ 1 - 1
src/main/frontend/db/rtc/debug_ui.cljs

@@ -88,7 +88,7 @@
             :auto-push-updates? (:auto-push-updates? state)
             :current-page (state/get-current-page)
             :blocks-count (when-let [page (state/get-current-page)]
-                            (count (:block/_page (db/entity (ldb/get-first-page-by-name (db/get-db) page)))))}
+                            (count (:block/_page (db/get-page page))))}
            (fipp/pprint {:width 20})
            with-out-str)]]
 

+ 1 - 1
src/main/frontend/db_worker.cljs

@@ -306,7 +306,7 @@
      (let [selector (ldb/read-transit-str selector-str)
            id (ldb/read-transit-str id-str)
            eid (when (and (vector? id) (= :block/name (first id)))
-                 (ldb/get-first-page-by-name @conn (second id)))
+                 (:db/id (ldb/get-page @conn (second id))))
            result (->> (d/pull @conn selector (or eid id))
                        (sqlite-common-db/with-parent-and-left @conn))]
        (ldb/write-transit-str result))))

+ 1 - 1
src/main/frontend/extensions/tldraw.cljs

@@ -120,7 +120,7 @@
                     (assert (common-util/uuid-string? page-uuid-str))
                     (state/set-modal! #(export/export-blocks (uuid page-uuid-str) (merge (js->clj options :keywordize-keys true) {:whiteboard? true}))))
    :isWhiteboardPage (fn [page-name]
-                       (when-let [entity (ldb/get-first-page-by-name (db/get-db) page-name)]
+                       (when-let [entity (db/get-page page-name)]
                          (model/whiteboard-page? entity)))
    :isMobile util/mobile?
    :saveAsset save-asset-handler

+ 3 - 3
src/main/frontend/handler/common/page.cljs

@@ -100,8 +100,8 @@
 (defn- find-block-in-favorites-page
   [page-block-uuid]
   (let [db (conn/get-db)]
-    (when-let [page-id (ldb/get-first-page-by-name db common-config/favorites-page-name)]
-      (let [blocks (ldb/get-page-blocks db page-id {})]
+    (when-let [page (db/get-page common-config/favorites-page-name)]
+      (let [blocks (ldb/get-page-blocks db (:db/id page) {})]
         (when-let [page-block-entity (d/entity db [:block/uuid page-block-uuid])]
           (some (fn [block]
                   (when (= (:db/id (:block/link block)) (:db/id page-block-entity))
@@ -146,7 +146,7 @@
   (when page-uuid-or-name
     (assert (or (uuid? page-uuid-or-name) (string? page-uuid-or-name)))
     (when-let [page-uuid (or (and (uuid? page-uuid-or-name) page-uuid-or-name)
-                             (:block/uuid (db/entity (ldb/get-first-page-by-name (db/get-db) page-uuid-or-name))))]
+                             (:block/uuid (db/get-page page-uuid-or-name)))]
       (when-let [^Object worker @state/*db-worker]
         (-> (p/let [repo (state/get-current-repo)
                     res (.page-delete worker repo (str page-uuid))

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

@@ -3473,7 +3473,7 @@
                       (date/today))]
     (let [block-id (or root-block (parse-uuid page))
           page-id (when-not block-id
-                    (ldb/get-first-page-by-name (db/get-db) page))
+                    (:db/id (db/get-page page)))
           blocks (if block-id
                    (db/get-block-and-children (state/get-current-repo) block-id)
                    (db/get-page-blocks-no-cache page-id))

+ 1 - 1
src/main/frontend/handler/export/common.cljs

@@ -101,7 +101,7 @@
 
 (defn- page-name->ast
   [page-name]
-  (let [page (db/entity (ldb/get-first-page-by-name (db/get-db) page-name))]
+  (let [page (db/get-page page-name)]
     (when-let [content (get-page-content (:block/uuid page))]
       (when content
         (let [format :markdown]

+ 2 - 2
src/main/frontend/handler/graph.cljs

@@ -133,7 +133,7 @@
   [page theme show-journal]
   (let [dark? (= "dark" theme)]
     (when-let [repo (state/get-current-repo)]
-      (let [page-entity (db/entity (ldb/get-first-page-by-name (db/get-db) page))
+      (let [page-entity (db/entity page)
             page-id (:db/id page-entity)
             tags (if (config/db-based-graph? repo)
                    (set (map #(:block/name (db/entity repo (:db/id %)))
@@ -156,7 +156,7 @@
                              (set))
             other-pages-links (mapcat
                                (fn [page]
-                                 (let [page-id (ldb/get-first-page-by-name (db/get-db) page)
+                                 (let [page-id (:db/id (db/get-page page))
                                        ref-pages (-> (map first (db/get-page-referenced-pages repo page-id))
                                                      (set)
                                                      (set/intersection other-pages))

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

@@ -131,7 +131,7 @@
                         (common-util/uuid-string? page-uuid-or-old-name)
                         page-uuid-or-old-name
                         :else
-                        (:block/uuid (db/entity (ldb/get-first-page-by-name (db/get-db) page-uuid-or-old-name))))
+                        (:block/uuid (db/get-page page-uuid-or-old-name)))
             result (.page-rename worker repo (str page-uuid) new-name)
             result' (:result (bean/->clj result))]
       (case (if (string? result') (keyword result') result')

+ 1 - 3
src/main/frontend/handler/route.cljs

@@ -80,9 +80,7 @@
              (and (string? page-name) (not (string/blank? page-name))))
      ;; Always skip onboarding when loading an existing whiteboard
      (when-not new-whiteboard? (state/set-onboarding-whiteboard! true))
-     (when-let [db-id (if (uuid? page-name)
-                        (:db/id (db/entity [:block/uuid page-name]))
-                        (:db/id (db/entity (ldb/get-first-page-by-name (db/get-db) page-name))))]
+     (when-let [db-id (:db/id (db/get-page page-name))]
        (recent-handler/add-page-to-recent! db-id click-from-recent?))
      (if (and (= (str page-name) (state/get-current-page)) block-id)
        (state/focus-whiteboard-shape block-id)

+ 1 - 1
src/main/frontend/worker/file/page_rename.cljs

@@ -108,7 +108,7 @@
   "Unsanitized only"
   [db config page new-name]
   ;; update all pages which have references to this page
-  (let [to-page (d/entity db (ldb/get-first-page-by-name db new-name))
+  (let [to-page (ldb/get-page db new-name)
         old-original-name (:block/original-name page)
         blocks (:block/_refs (d/entity db (:db/id page)))
         tx       (->> (map (fn [{:block/keys [uuid content properties format] :as block}]

+ 1 - 1
src/main/frontend/worker/handler/page/rename.cljs

@@ -124,7 +124,7 @@
         new-name      (string/trim new-name)
         old-page-name (common-util/page-name-sanity-lc old-name)
         new-page-name (common-util/page-name-sanity-lc new-name)
-        new-page-exists? (some? (ldb/get-first-page-by-name db new-name))
+        new-page-exists? (some? (ldb/get-page db new-name))
         name-changed? (not= old-name new-name)]
     (cond
       (string/blank? new-name)

+ 1 - 1
src/main/frontend/worker/rtc/core.cljs

@@ -512,7 +512,7 @@
   (let [config (worker-state/get-config repo)]
     (doseq [{:keys [self page-name original-name] :as op-value} update-page-ops]
       (let [old-page-original-name (:block/original-name (d/entity @conn [:block/uuid self]))
-            exist-page (d/entity @conn (ldb/get-first-page-by-name @conn page-name))
+            exist-page (ldb/get-page @conn page-name)
             create-opts {:create-first-block? false
                          :uuid self :persist-op? false}]
         (cond

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

@@ -645,7 +645,7 @@
                                          [block-uuid-or-page-name nil])
                 page-name              (when page-name (util/page-name-sanity-lc page-name))
                 _                      (when (and page-name
-                                                  (nil? (ldb/get-first-page-by-name (db/get-db) page-name)))
+                                                  (nil? (ldb/get-page (db/get-db) page-name)))
                                          (page-handler/<create! block-uuid-or-page-name {:create-first-block? false}))
                 custom-uuid            (or customUUID (:id properties))
                 custom-uuid            (when custom-uuid (sdk-utils/uuid-or-throw-error custom-uuid))
@@ -819,7 +819,7 @@
 (def ^:export get_current_page_blocks_tree
   (fn []
     (when-let [page (state/get-current-page)]
-      (let [page-id (ldb/get-first-page-by-name (db/get-db) page)
+      (let [page-id (:db/id (ldb/get-page (db/get-db) page))
             blocks (db-model/get-page-blocks-no-cache page-id)
             blocks (outliner-tree/blocks->vec-tree blocks page-id)
             ;; clean key