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

refactor: rename namespace page uses pages instead of files

Also, fix #2319
Tienson Qin 4 жил өмнө
parent
commit
8efafd7d77

+ 12 - 12
src/main/frontend/components/hierarchy.cljs

@@ -4,20 +4,20 @@
             [frontend.components.block :as block]
             [rum.core :as rum]
             [frontend.ui :as ui]
-            [medley.core :as medley]))
+            [medley.core :as medley]
+            [frontend.db :as db]
+            [frontend.state :as state]
+            [frontend.text :as text]))
 
 (defn get-relation
-  ([page]
-   (get-relation page 100))
-  ([page limit]
-   (->> (search/page-search page limit)
-        (filter #(or
-                  (= page %)
-                  (string/starts-with? % (str page "/"))
-                  (string/includes? % (str "/" page "/"))
-                  (string/ends-with? % (str "/" page))))
-        (map #(string/split % #"/"))
-        (remove #(= % [page])))))
+  [page]
+  (when (text/namespace-page? page)
+    (->> (db/get-namespace-pages (state/get-current-repo) page)
+         (map (fn [page]
+                (or (:block/original-name page) (:block/name page))))
+         (map #(string/split % #"/"))
+         (remove #(= % [page]))
+         (sort))))
 
 (rum/defc structures
   [page]

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

@@ -50,7 +50,7 @@
   get-pages get-pages-relation get-pages-that-mentioned-page get-public-pages get-tag-pages
   journal-page? local-native-fs? mark-repo-as-cloned! page-alias-set page-blocks-transform pull-block
   set-file-last-modified-at! transact-files-db! with-block-refs-count get-modified-pages page-empty? page-empty-or-dummy? get-alias-source-page
-  set-file-content! has-children? get-namespace-files]
+  set-file-content! has-children? get-namespace-pages]
 
  [frontend.db.react
   get-current-marker get-current-page get-current-priority set-key-value

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

@@ -1252,16 +1252,21 @@
            page-id)
       ffirst))
 
-(defn get-namespace-files
+(defn get-namespace-pages
   [repo namespace]
   (assert (string? namespace))
-  (let [db (conn/get-conn repo)
-        namespace (string/replace namespace "/" ".")]
+  (let [db (conn/get-conn repo)]
     (when-not (string/blank? namespace)
-     (let [namespace (string/trim namespace)
-           pattern-1 (re-pattern (util/format "[\\.|/]%s\\." namespace))
-           pattern-2 (re-pattern (util/format "^%s\\." namespace))]
-       (->> (d/datoms db :aevt :file/path)
-            (filter (fn [datom]
-                      (or (re-find pattern-1 (:v datom))
-                          (re-find pattern-2 (:v datom))))))))))
+      (let [namespace (string/lower-case (string/trim namespace))
+            ids (->> (d/datoms db :aevt :block/name)
+                     (filter (fn [datom]
+                               (let [page (:v datom)]
+                                 (or
+                                  (= page namespace)
+                                  (string/starts-with? page (str namespace "/"))))))
+                     (map :e))]
+        (when (seq ids)
+          (db-utils/pull-many repo
+                              '[:db/id :block/name :block/original-name
+                                {:block/file [:db/id :file/path]}]
+                              ids))))))

+ 32 - 30
src/main/frontend/handler/page.cljs

@@ -266,38 +266,40 @@
   [old-page-title old-name new-name]
   (string/replace-first old-page-title old-name new-name))
 
-;; FIXME: get namespace pages instead of files for compatibility with the
-;; database-only version.
+(defn- get-new-file-path
+  [old-path old-name new-name]
+  (let [path-old-name (string/replace old-name "/" ".")
+        path-new-name (string/replace new-name "/" ".")
+        [search replace] (cond
+                           (string/includes? old-path (str "." path-old-name "."))
+                           [(str "." path-old-name ".") (str "." path-new-name ".")]
+
+                           (string/includes? old-path (str "/" path-old-name "."))
+                           [(str "/" path-old-name ".") (str "/" path-new-name ".")]
+
+                           :else
+                           [(str path-old-name ".") (str path-new-name ".")])]
+    (string/replace-first old-path search replace)))
+
 (defn- rename-namespace-pages!
   [repo old-name new-name]
-  (doseq [datom (db/get-namespace-files repo old-name)]
-    (let [old-path (:v datom)
-          path-old-name (string/replace old-name "/" ".")
-          path-new-name (string/replace new-name "/" ".")
-          [search replace] (cond
-                             (string/includes? old-path (str "." path-old-name "."))
-                             [(str "." path-old-name ".") (str "." path-new-name ".")]
-
-                             (string/includes? old-path (str "/" path-old-name "."))
-                             [(str "/" path-old-name ".") (str "/" path-new-name ".")]
-
-                             :else
-                             [(str path-old-name ".") (str path-new-name ".")])
-          new-path (string/replace-first old-path search replace)
-          tx {:db/id (:e datom)
-              :file/path new-path}
-          page (db/entity (db/get-file-page-id old-path))
-          page-tx (when page
-                    (let [old-page-title (or (:block/original-name page)
-                                             (:block/name page))
-                          new-page-title (build-new-namespace-page-title old-page-title old-name new-name)]
-                      {:db/id (:db/id page)
-                       :block/original-name new-page-title
-                       :block/name (string/lower-case new-page-title)}))
-          txs (->> [tx page-tx] (remove nil?))]
-      (db/transact! repo txs)
-      (p/let [_ (rename-file-aux! repo old-path new-path)]
-        (println "Renamed " old-path " to " new-path ".")))))
+  (let [pages (db/get-namespace-pages repo old-name)]
+    (doseq [{:block/keys [name original-name file] :as page} pages]
+      (let [old-page-title (or original-name name)
+            new-page-title (build-new-namespace-page-title old-page-title old-name new-name)
+            page-tx {:db/id (:db/id page)
+                     :block/original-name new-page-title
+                     :block/name (string/lower-case new-page-title)}
+            old-path (:file/path file)
+            new-path (when old-path
+                       (get-new-file-path old-path old-name new-name))
+            file-tx (when file
+                      {:db/id (:db/id file)
+                       :file/path new-path})
+            txs (->> [file-tx page-tx] (remove nil?))]
+        (db/transact! repo txs)
+        (p/let [_ (rename-file-aux! repo old-path new-path)]
+          (println "Renamed " old-path " to " new-path))))))
 
 (defn rename!
   [old-name new-name]

+ 1 - 2
src/main/frontend/text.cljs

@@ -188,8 +188,7 @@
 
 (defn namespace-page?
   [p]
-  (and (string/includes? p "/")
-       (not (string/starts-with? p "../"))
+  (and (not (string/starts-with? p "../"))
        (not (string/starts-with? p "./"))
        (not (string/starts-with? p "http"))
        (not