Explorar o código

Remove db/state dep on page handler

Tienson Qin %!s(int64=2) %!d(string=hai) anos
pai
achega
9597330d9f

+ 12 - 0
deps/common/src/logseq/common/util.cljs

@@ -335,3 +335,15 @@
 (defn time-ms
   []
   (tc/to-long (t/now)))
+
+(defn get-page-original-name
+  [page]
+  (or (:block/original-name page)
+      (:block/name page)))
+
+(defn string-join-path
+  #_:clj-kondo/ignore
+  "Replace all `strings/join` used to construct paths with this function to reduce lint output.
+  https://github.com/logseq/logseq/pull/8679"
+  [parts]
+  (string/join "/" parts))

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

@@ -12,7 +12,8 @@
             [logseq.common.util :as common-util]
             [logseq.common.config :as common-config]
             [logseq.db.frontend.content :as db-content]
-            [clojure.set :as set]))
+            [clojure.set :as set]
+            [logseq.db.frontend.rules :as rules]))
 
 ;; Use it as an input argument for datalog queries
 (def block-attrs
@@ -226,6 +227,12 @@
        (map first)
        (remove hidden-page?)))
 
+(defn page-exists?
+  "Whether a page exists."
+  [db page-name]
+  (when page-name
+    (d/entity db [:block/name (common-util/page-name-sanity-lc page-name)])))
+
 (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."
@@ -463,3 +470,34 @@
   (some-> (or (d/entity db [:block/name page-name])
               (d/entity db [:block/original-name page-name]))
           :block/file))
+
+(defn get-namespace-pages
+  "Accepts both sanitized and unsanitized namespaces"
+  [db namespace]
+  (assert (string? namespace))
+  (let [namespace (common-util/page-name-sanity-lc namespace)
+        pull-attrs [:db/id :block/name :block/original-name :block/namespace
+                    {:block/file [:db/id :file/path]}]]
+    (d/q
+     [:find [(list 'pull '?c pull-attrs) '...]
+      :in '$ '% '?namespace
+      :where
+      ['?p :block/name '?namespace]
+      (list 'namespace '?p '?c)]
+     db
+     (:namespace rules/rules)
+     namespace)))
+
+(defn get-pages-by-name-partition
+  [db partition]
+  (when-not (string/blank? partition)
+    (let [partition (common-util/page-name-sanity-lc (string/trim partition))
+          ids (->> (d/datoms db :aevt :block/name)
+                   (filter (fn [datom]
+                             (let [page (:v datom)]
+                               (string/includes? page partition))))
+                   (map :e))]
+      (when (seq ids)
+        (d/pull-many db
+                     '[:db/id :block/name :block/original-name]
+                     ids)))))

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

@@ -43,7 +43,7 @@
   get-all-pages get-pages-relation get-pages-that-mentioned-page get-tag-pages
   journal-page? page-alias-set sub-block
   set-file-last-modified-at! page-empty? page-exists? page-empty-or-dummy? get-alias-source-page
-  set-file-content! has-children? get-namespace-pages get-all-namespace-relation get-pages-by-name-partition
+  set-file-content! has-children? get-namespace-pages get-all-namespace-relation
   get-original-name]
 
  [frontend.db.react

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

@@ -496,8 +496,9 @@ independent of format as format specific heading characters are stripped"
 (defn page-exists?
   "Whether a page exists."
   [page-name]
-  (when page-name
-    (db-utils/entity [:block/name (util/page-name-sanity-lc page-name)])))
+  (let [repo (state/get-current-repo)]
+    (when-let [db (conn/get-db repo)]
+     (ldb/page-exists? db page-name))))
 
 (defn page-empty?
   "Whether a page is empty. Does it has a non-page block?
@@ -527,21 +528,6 @@ independent of format as format specific heading characters are stripped"
   (when-let [block (db-utils/entity repo [:block/uuid block-uuid])]
     (db-utils/entity repo (:db/id (:block/page block)))))
 
-(defn get-pages-by-name-partition
-  [repo partition]
-  (when-let [db (conn/get-db repo)]
-    (when-not (string/blank? partition)
-      (let [partition (util/page-name-sanity-lc (string/trim partition))
-            ids (->> (d/datoms db :aevt :block/name)
-                     (filter (fn [datom]
-                               (let [page (:v datom)]
-                                 (string/includes? page partition))))
-                     (map :e))]
-        (when (seq ids)
-          (db-utils/pull-many repo
-                              '[:db/id :block/name :block/original-name]
-                              ids))))))
-
 (defn get-block-immediate-children
   "Doesn't include nested children."
   [repo block-uuid]
@@ -1027,20 +1013,7 @@ independent of format as format specific heading characters are stripped"
 (defn get-namespace-pages
   "Accepts both sanitized and unsanitized namespaces"
   [repo namespace]
-  (assert (string? namespace))
-  (let [namespace (util/page-name-sanity-lc namespace)
-        pull-attrs (cond-> [:db/id :block/name :block/original-name :block/namespace]
-                     (not (config/db-based-graph? repo))
-                     (conj {:block/file [:db/id :file/path]}))]
-    (d/q
-     [:find [(list 'pull '?c pull-attrs) '...]
-       :in '$ '% '?namespace
-       :where
-       ['?p :block/name '?namespace]
-       (list 'namespace '?p '?c)]
-     (conn/get-db repo)
-     (:namespace rules/rules)
-     namespace)))
+  (ldb/get-namespace-pages (conn/get-db repo) namespace))
 
 (defn- tree [flat-col root]
   (let [sort-fn #(sort-by :block/name %)

+ 70 - 89
src/main/frontend/handler/db_based/page.cljs

@@ -1,14 +1,6 @@
 (ns frontend.handler.db-based.page
   "Page handlers for DB graphs"
-  (:require [frontend.state :as state]
-            [frontend.db :as db]
-            [frontend.db.model :as model]
-            [frontend.db.conn :as conn]
-            [frontend.db.utils :as db-utils]
-            [frontend.util :as util]
-            [frontend.handler.notification :as notification]
-            [frontend.handler.route :as route-handler]
-            [logseq.outliner.core :as outliner-core]
+  (:require [logseq.outliner.core :as outliner-core]
             [logseq.outliner.tree :as otree]
             [frontend.handler.common.page :as page-common-handler]
             [datascript.core :as d]
@@ -18,7 +10,9 @@
             [frontend.worker.file.util :as wfu]
             [frontend.worker.file.page-rename :as page-rename]
             [logseq.db.sqlite.util :as sqlite-util]
-            [logseq.db :as ldb]))
+            [logseq.db :as ldb]
+            [logseq.common.util :as common-util]
+            [frontend.handler.notification :as notification]))
 
 (defn- replace-page-ref
   "Replace from-page refs with to-page"
@@ -51,7 +45,7 @@
 
                                                                   :else
                                                                   v)) properties)
-                                             (util/remove-nils-non-nested))
+                                             (common-util/remove-nils-non-nested))
                              tx (merge
                                  content-tx
                                  (when (not= (seq properties) (seq properties'))
@@ -73,26 +67,24 @@
        (vec)))
 
 (defn- based-merge-pages!
-  [db config from-page-name to-page-name persist-op? redirect?]
-  (when (and (db/page-exists? from-page-name)
-             (db/page-exists? to-page-name)
+  [repo conn config from-page-name to-page-name persist-op?]
+  (when (and (ldb/page-exists? @conn from-page-name)
+             (ldb/page-exists? @conn to-page-name)
              (not= from-page-name to-page-name))
-    (let [conn (db/get-db false)
-          to-page (db/entity [:block/name to-page-name])
+    (let [db @conn
+          to-page (d/entity db [:block/name to-page-name])
           to-id (:db/id to-page)
-          from-page (db/entity [:block/name from-page-name])
+          from-page (d/entity db [:block/name from-page-name])
           from-id (:db/id from-page)
-          from-first-child (some->> (db/pull from-id)
+          from-first-child (some->> (d/pull db from-id)
                                     (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)
+          to-last-direct-child-id (ldb/get-block-last-direct-child-id db to-id)
           db-based? (sqlite-util/db-based-graph? repo)
-          conn (conn/get-db repo false)
           datoms (d/datoms @conn :avet :block/page from-id)
           block-eids (mapv :e datoms)
-          blocks (db-utils/pull-many repo '[:db/id :block/page :block/refs :block/path-refs :block/left :block/parent] block-eids)
+          blocks (d/pull-many db '[:db/id :block/page :block/refs :block/path-refs :block/left :block/parent] block-eids)
           blocks-tx-data (map (fn [block]
                                 (let [id (:db/id block)]
                                   (cond->
@@ -109,17 +101,12 @@
                                 (replace-page-ref from-page to-page)
                                 (page-rename/replace-page-ref db config from-page-name to-page-name))
           tx-data (concat blocks-tx-data replace-ref-tx-data)]
-      (db/transact! repo tx-data {:persist-op? persist-op?})
+      (d/transact! conn tx-data {:persist-op? persist-op?})
       (page-common-handler/rename-update-namespace! from-page
-                                                    (util/get-page-original-name from-page)
-                                                    (util/get-page-original-name to-page)))
+                                                    (common-util/get-page-original-name from-page)
+                                                    (common-util/get-page-original-name to-page)))
 
-    (page-common-handler/delete! from-page-name nil :redirect-to-home? false :persist-op? persist-op?)
-
-    (when redirect?
-      (route-handler/redirect! {:to          :page
-                                :push        false
-                                :path-params {:name to-page-name}}))))
+    (page-common-handler/delete! from-page-name nil :redirect-to-home? false :persist-op? persist-op?)))
 
 (defn- compute-new-file-path
   "Construct the full path given old full path and the file sanitized body.
@@ -129,7 +116,7 @@
         ext (last (string/split (last result) "."))
         new-file (str new-file-name-body "." ext)
         parts (concat (butlast result) [new-file])]
-    (util/string-join-path parts)))
+    (common-util/string-join-path parts)))
 
 (defn- update-file-tx
   [db old-page-name new-page-name]
@@ -146,13 +133,12 @@
 
 (defn- rename-page-aux
   "Only accepts unsanitized page names"
-  [conn config old-name new-name]
+  [repo conn config old-name new-name]
   (let [db                  @conn
-        old-page-name       (util/page-name-sanity-lc old-name)
-        new-page-name       (util/page-name-sanity-lc new-name)
-        repo                (state/get-current-repo)
+        old-page-name       (common-util/page-name-sanity-lc old-name)
+        new-page-name       (common-util/page-name-sanity-lc new-name)
         db-based?           (sqlite-util/db-based-graph? repo)
-        page                (db/pull [:block/name old-page-name])]
+        page                (d/pull @conn [:block/name old-page-name])]
     (when (and repo page)
       (let [old-original-name   (:block/original-name page)
             page-txs            [{:db/id               (:db/id page)
@@ -181,9 +167,9 @@
 
 (defn- rename-namespace-pages!
   "Original names (unsanitized only)"
-  [conn config repo old-name new-name]
-  (let [pages (db/get-namespace-pages repo old-name)
-        page (db/pull [:block/name (util/page-name-sanity-lc old-name)])
+  [repo conn config old-name new-name]
+  (let [pages (ldb/get-namespace-pages @conn old-name)
+        page (d/pull @conn [:block/name (common-util/page-name-sanity-lc old-name)])
         pages (cons page pages)]
     (doseq [{:block/keys [name original-name]} pages]
       (let [old-page-title (or original-name name)
@@ -193,18 +179,17 @@
             ;; but don't rename [[work/worklog]] to [[work1/work1log]]
             new-page-title (string/replace-first old-page-title old-name new-name)]
         (when (and old-page-title new-page-title)
-          (rename-page-aux conn config old-page-title new-page-title)
+          (rename-page-aux repo conn config old-page-title new-page-title)
           (println "Renamed " old-page-title " to " new-page-title))))))
 
 (defn- rename-nested-pages
   "Unsanitized names only"
-  [conn config old-ns-name new-ns-name]
-  (let [repo            (state/get-current-repo)
-        nested-page-str (page-ref/->page-ref (util/page-name-sanity-lc old-ns-name))
+  [repo conn config old-ns-name new-ns-name]
+  (let [nested-page-str (page-ref/->page-ref (common-util/page-name-sanity-lc old-ns-name))
         ns-prefix-format-str (str page-ref/left-brackets "%s/")
-        ns-prefix       (util/format ns-prefix-format-str (util/page-name-sanity-lc old-ns-name))
-        nested-pages    (db/get-pages-by-name-partition repo nested-page-str)
-        nested-pages-ns (db/get-pages-by-name-partition repo ns-prefix)]
+        ns-prefix       (common-util/format ns-prefix-format-str (common-util/page-name-sanity-lc old-ns-name))
+        nested-pages    (ldb/get-pages-by-name-partition @conn nested-page-str)
+        nested-pages-ns (ldb/get-pages-by-name-partition @conn ns-prefix)]
     (when nested-pages
       ;; rename page "[[obsidian]] is a tool" to "[[logseq]] is a tool"
       (doseq [{:block/keys [name original-name]} nested-pages]
@@ -214,7 +199,7 @@
                               (page-ref/->page-ref old-ns-name)
                               (page-ref/->page-ref new-ns-name))]
           (when (and old-page-title new-page-title)
-            (rename-page-aux conn config old-page-title new-page-title)
+            (rename-page-aux repo conn config old-page-title new-page-title)
             (println "Renamed " old-page-title " to " new-page-title)))))
     (when nested-pages-ns
       ;; rename page "[[obsidian/page1]] is a tool" to "[[logseq/page1]] is a tool"
@@ -222,53 +207,49 @@
         (let [old-page-title (or original-name name)
               new-page-title (string/replace
                               old-page-title
-                              (util/format ns-prefix-format-str old-ns-name)
-                              (util/format ns-prefix-format-str new-ns-name))]
+                              (common-util/format ns-prefix-format-str old-ns-name)
+                              (common-util/format ns-prefix-format-str new-ns-name))]
           (when (and old-page-title new-page-title)
-            (rename-page-aux conn config old-page-title new-page-title)
+            (rename-page-aux repo conn config old-page-title new-page-title)
             (println "Renamed " old-page-title " to " new-page-title)))))))
 
 (defn rename!
-  ([old-name new-name]
-   (rename! old-name new-name true true))
-  ([old-name new-name redirect? persist-op?]
-   (let [repo (state/get-current-repo)
-         conn (db/get-db false)
-         db @conn
-         config (state/get-config repo)
-         old-name      (string/trim old-name)
-         new-name      (string/trim new-name)
-         old-page-name (util/page-name-sanity-lc old-name)
-         page-e (db/entity [:block/name old-page-name])
-         new-page-name (util/page-name-sanity-lc new-name)
-         new-page-e (db/entity [:block/name new-page-name])
-         name-changed? (not= old-name new-name)]
-     (cond
-       (string/blank? new-name)
-       (do
-         (notification/show! "Please use a valid name, empty name is not allowed!" :error)
-         :invalid-empty-name)
-
-       (and page-e new-page-e
-            (or (contains? (:block/type page-e) "whiteboard")
-                (contains? (:block/type new-page-e) "whiteboard")))
-       (do
-         (notification/show! "Can't merge whiteboard pages" :error)
-         :merge-whiteboard-pages)
-
-       (and old-name new-name name-changed?)
-       (do
-         (cond
+  [repo conn config old-name new-name & {:keys [persist-op?]
+                                         :or {persist-op? true}}]
+  (let [db @conn
+        old-name      (string/trim old-name)
+        new-name      (string/trim new-name)
+        old-page-name (common-util/page-name-sanity-lc old-name)
+        page-e (d/entity db [:block/name old-page-name])
+        new-page-name (common-util/page-name-sanity-lc new-name)
+        new-page-e (d/entity db [:block/name new-page-name])
+        name-changed? (not= old-name new-name)]
+    (cond
+      (string/blank? new-name)
+      (do
+        (notification/show! "Please use a valid name, empty name is not allowed!" :error)
+        :invalid-empty-name)
+
+      (and page-e new-page-e
+           (or (contains? (:block/type page-e) "whiteboard")
+               (contains? (:block/type new-page-e) "whiteboard")))
+      (do
+        (notification/show! "Can't merge whiteboard pages" :error)
+        :merge-whiteboard-pages)
+
+      (and old-name new-name name-changed?)
+      (do
+        (cond
           (= old-page-name new-page-name) ; case changed
-          (db/transact! repo
-                        [{:db/id (:db/id page-e)
-                          :block/original-name new-name}]
-                        {:persist-op? persist-op?})
+          (d/transact! conn
+                       [{:db/id (:db/id page-e)
+                         :block/original-name new-name}]
+                       {:persist-op? persist-op?})
 
           (and (not= old-page-name new-page-name)
-               (db/entity [:block/name new-page-name])) ; merge page
-          (based-merge-pages! db config old-page-name new-page-name persist-op? redirect?)
+               (d/entity @conn [:block/name new-page-name])) ; merge page
+          (based-merge-pages! repo conn config old-page-name new-page-name persist-op?)
 
           :else                          ; rename
-          (rename-namespace-pages! conn config repo old-name new-name))
-         (rename-nested-pages conn config old-name new-name))))))
+          (rename-namespace-pages! repo conn config old-name new-name))
+        (rename-nested-pages repo conn config old-name new-name)))))

+ 3 - 10
src/main/frontend/util.cljc

@@ -66,12 +66,7 @@
 #?(:cljs (defonce convert-to-letters utils/convertToLetters))
 #?(:cljs (defonce hsl2hex utils/hsl2hex))
 
-(defn string-join-path
-  #_:clj-kondo/ignore
-  "Replace all `strings/join` used to construct paths with this function to reduce lint output.
-  https://github.com/logseq/logseq/pull/8679"
-  [parts]
-  (string/join "/" parts))
+#?(:cljs (def string-join-path common-util/string-join-path))
 
 #?(:cljs
    (def safe-re-find common-util/safe-re-find))
@@ -1028,10 +1023,8 @@
 #?(:cljs
    (def safe-page-name-sanity-lc common-util/safe-page-name-sanity-lc))
 
-(defn get-page-original-name
-  [page]
-  (or (:block/original-name page)
-      (:block/name page)))
+#?(:cljs
+   (def get-page-original-name common-util/get-page-original-name))
 
 #?(:cljs
    (defn add-style!

+ 2 - 7
src/main/frontend/worker/handler/page.cljs

@@ -58,9 +58,8 @@
    * :persist-op?         - when true, add an update-page op
    TODO: Add other options"
   [repo conn config title
-   & {:keys [create-first-block? format properties uuid rename? persist-op? whiteboard? class?]
+   & {:keys [create-first-block? format properties uuid persist-op? whiteboard? class?]
       :or   {create-first-block? true
-             rename?             false
              format              nil
              properties          nil
              uuid                nil
@@ -76,7 +75,7 @@
         title      (common-util/remove-boundary-slashes title)
         page-name  (common-util/page-name-sanity-lc title)
         with-uuid? (if (uuid? uuid) uuid true)] ;; FIXME: prettier validation
-    (when (or (ldb/page-empty? @conn page-name) rename?)
+    (when (ldb/page-empty? @conn page-name)
       (let [pages    (if split-namespace?
                        (common-util/split-namespace-pages title)
                        [title])
@@ -117,10 +116,6 @@
                                   :block/content ""
                                   :block/format format})]))
             txs      (concat
-                      (when (and rename? uuid)
-                        (when-let [e (d/entity @conn [:block/uuid uuid])]
-                          [[:db/retract (:db/id e) :block/namespace]
-                           [:db/retract (:db/id e) :block/refs]]))
                       txs
                       page-txs
                       first-block-tx)]