소스 검색

fix: can't create page with the same name if namespace page exists

fixes https://github.com/logseq/db-test/issues/429
Tienson Qin 2 달 전
부모
커밋
063aec02a7

+ 3 - 2
deps/db/src/logseq/db.cljs

@@ -218,7 +218,7 @@
 (defn page-exists?
   "Returns truthy value if page exists.
    For db graphs, returns all page db ids that given title and one of the given `tags`.
-   For file graphs, returns page entity if it exists"
+   For file graphs, returns page db/id vector if it exists"
   [db page-name tags]
   (when page-name
     (if (db-based-graph? db)
@@ -249,7 +249,8 @@
             db
             (common-util/page-name-sanity-lc page-name)
             tags'))))
-      (d/entity db [:block/name (common-util/page-name-sanity-lc page-name)]))))
+      (when-let [id (:db/id (d/entity db [:block/name (common-util/page-name-sanity-lc page-name)]))]
+        [id]))))
 
 (defn get-page
   "Get a page given its unsanitized name or uuid"

+ 11 - 25
src/main/frontend/components/cmdk/core.cljs

@@ -34,7 +34,6 @@
             [logseq.common.util :as common-util]
             [logseq.common.util.block-ref :as block-ref]
             [logseq.db :as ldb]
-            [logseq.graph-parser.text :as text]
             [logseq.shui.hooks :as hooks]
             [logseq.shui.ui :as shui]
             [promesa.core :as p]
@@ -128,22 +127,11 @@
                             (take (get-group-limit group) items))))
         node-exists? (let [blocks-result (keep :source-block (get-in results [:nodes :items]))]
                        (when-not (string/blank? input)
-                         (or (let [page (some-> (text/get-namespace-last-part input)
-                                                string/trim
-                                                db/get-page)
-                                   parent-title (:block/title (:block/parent page))
-                                   namespace? (string/includes? input "/")]
-                               (and page
-                                    (or (not namespace?)
-                                        (and
-                                         parent-title
-                                         (= (util/page-name-sanity-lc parent-title)
-                                            (some-> (util/nth-safe (reverse (string/split input "/")) 1)
-                                                    util/page-name-sanity-lc))))))
-                             (some (fn [block]
-                                     (and
-                                      (:block/tags block)
-                                      (= input (util/page-name-sanity-lc (:block/title block))))) blocks-result))))
+                         (some (fn [block]
+                                 (and
+                                  (:block/tags block)
+                                  (not (:block/parent block))
+                                  (= input (util/page-name-sanity-lc (:block/title block))))) blocks-result)))
         include-slash? (or (string/includes? input "/")
                            (string/starts-with? input "/"))
         start-with-slash? (string/starts-with? input "/")
@@ -203,8 +191,7 @@
 (defn state->action [state]
   (let [highlighted-item (state->highlighted-item state)
         action (get-action)]
-    (cond (and (:source-page highlighted-item) (= action :move-blocks)) :trigger
-          (:source-page highlighted-item) :open
+    (cond (and (:source-block highlighted-item) (= action :move-blocks)) :trigger
           (:source-block highlighted-item) :open
           (:file-path highlighted-item) :open
           (:source-search highlighted-item) :search
@@ -310,7 +297,7 @@
                         (block/breadcrumb {:disable-preview? true
                                            :search? true} repo (:block/uuid page) {}))
               :alias (:alias page)
-              :source-page (or source-page page))))
+              :source-block (or source-page page))))
 
 (defn block-item
   [repo block current-page input]
@@ -461,8 +448,7 @@
   [state]
   (let [highlighted-item (some-> state state->highlighted-item)
         block (or (:alias highlighted-item)
-                  (:source-block highlighted-item)
-                  (:source-page highlighted-item))]
+                  (:source-block highlighted-item))]
     (:block/uuid block)))
 
 (defmethod handle-action :open-page [_ state _event]
@@ -527,7 +513,7 @@
 (defn- page-item?
   [item]
   (let [block-uuid (:block/uuid (:source-block item))]
-    (or (boolean (:source-page item))
+    (or (boolean (:source-block item))
         (and block-uuid (:block/name (db/entity [:block/uuid block-uuid]))))))
 
 (defmethod handle-action :open [_ state event]
@@ -694,8 +680,8 @@
              :let [highlighted? (= item highlighted-item)
                    page? (= "file" (some-> item :icon))
                    text (some-> item :text)
-                   source-page (some-> item :source-page)
-                   hls-page? (and page? (pdf-utils/hls-file? (:block/title source-page)))]]
+                   source-block (some-> item :source-block)
+                   hls-page? (and page? (pdf-utils/hls-file? (:block/title source-block)))]]
          (let [item (list-item/root
                      (assoc item
                             :group group

+ 17 - 16
src/main/frontend/components/editor.cljs

@@ -135,23 +135,24 @@
     (page-handler/on-chosen-handler input id pos format)))
 
 (defn- matched-pages-with-new-page [partial-matched-pages db-tag? q]
-  (if (or
-       (db/page-exists? q (if db-tag?
-                            #{:logseq.class/Tag}
-                            ;; Page existence here should be the same as entity-util/page?.
-                            ;; Don't show 'New page' if a page has any of these tags
-                            db-class/page-classes))
-       (and db-tag? (some ldb/class? (:block/_alias (db/get-page q)))))
-    partial-matched-pages
-    (if db-tag?
-      (concat
+  (let [ids (db/page-exists? q (if db-tag?
+                                 #{:logseq.class/Tag}
+                                 ;; Page existence here should be the same as entity-util/page?.
+                                 ;; Don't show 'New page' if a page has any of these tags
+                                 db-class/page-classes))
+        page-exists? (some (fn [id] (nil? (:block/parent (db/entity id)))) ids)]
+    (if (or page-exists?
+            (and db-tag? (some ldb/class? (:block/_alias (db/get-page q)))))
+      partial-matched-pages
+      (if db-tag?
+        (concat
        ;; Don't show 'New tag' for an internal page because it already shows 'Convert ...'
-       (when-not (let [entity (db/get-page q)]
-                   (and (ldb/internal-page? entity) (= (:block/title entity) q)))
-         [{:block/title (str (t :new-tag) " " q)}])
-       partial-matched-pages)
-      (cons {:block/title (str (t :new-page) " " q)}
-            partial-matched-pages))))
+         (when-not (let [entity (db/get-page q)]
+                     (and (ldb/internal-page? entity) (= (:block/title entity) q)))
+           [{:block/title (str (t :new-tag) " " q)}])
+         partial-matched-pages)
+        (cons {:block/title (str (t :new-page) " " q)}
+              partial-matched-pages)))))
 
 (defn- search-pages
   [q db-tag? db-based? set-matched-pages!]

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

@@ -891,7 +891,7 @@
                                       :trigger (fn [chosen]
                                                  (state/pub-event! [:editor/hide-action-bar])
                                                  (state/clear-selection!)
-                                                 (move-blocks! blocks (:source-page chosen) {:bottom? true}))}))
+                                                 (move-blocks! blocks (:source-block chosen) {:bottom? true}))}))
       (notification/show! "There's no block selected, please select blocks first." :warning))))
 
 (defn delete-block!

+ 4 - 4
src/main/frontend/worker/handler/page/db_based/page.cljs

@@ -186,10 +186,10 @@
                     (set (map :db/ident tags))
                     :else
                     #{:logseq.class/Page})
-        existing-page-id (first (ldb/page-exists? db title types))]
-    (if existing-page-id
-      (let [existing-page (d/entity db existing-page-id)
-            tx-meta {:persist-op? persist-op?
+        existing-page-id (first (ldb/page-exists? db title types))
+        existing-page (some->> existing-page-id (d/entity db))]
+    (if (and existing-page (not (:block/parent existing-page)))
+      (let [tx-meta {:persist-op? persist-op?
                      :outliner-op :save-block}]
         (when (and class?
                    (not (ldb/class? existing-page))

+ 6 - 2
src/main/frontend/worker/search.cljs

@@ -320,11 +320,14 @@ DROP TRIGGER IF EXISTS blocks_au;
                             k-result (first (filter #(= (:id %) id) keyword-results))
                             s-result (first (filter #(= (:id %) id) semantic-results))
                             result (merge s-result k-result)
-                            k-score (or (:keyword-score k-result) 0.0)
+                            page? (ldb/page? block)
+                            keyword-score (if page? (+ (:keyword-score k-result) 2) (:keyword-score k-result))
+                            k-score (or keyword-score 0.0)
                             s-score (or (:semantic-score s-result) 0.0)
                             norm-k-score (normalize-score k-score k-min k-max)
                             ;; Weighted combination
-                            combined-score (+ (* (:keyword-weight config) norm-k-score)
+                            combined-score (+ (* (:keyword-weight config)
+                                                 norm-k-score)
                                               (* (:semantic-weight config) s-score)
                                               (cond
                                                 (ldb/page? block)
@@ -429,6 +432,7 @@ DROP TRIGGER IF EXISTS blocks_au;
                                                         (if (common-util/uuid-string? page)
                                                           (uuid page)
                                                           nil)))
+                                         :block/parent (:db/id (:block/parent block))
                                          :block/tags (seq (map :db/id (:block/tags block)))
                                          :page? (ldb/page? block)
                                          :alias (some-> (first (:block/_alias block))

+ 2 - 2
src/main/mobile/components/search.cljs

@@ -119,8 +119,8 @@
        {:class (when (and (not (string/blank? input))
                           (seq search-result))
                  "as-results")}
-       (for [{:keys [icon text header source-page source-block]} result]
-         (let [block (or source-page source-block)]
+       (for [{:keys [icon text header source-block]} result]
+         (let [block source-block]
            [:li.flex.gap-1
             {:on-click (fn []
                          (when-let [id (:block/uuid block)]