refactor: rename namespace page uses pages instead of files

Also, fix #2319
pull/2335/head
Tienson Qin 2021-06-29 21:48:15 +08:00
parent 3cfdbd6655
commit 8efafd7d77
5 changed files with 61 additions and 55 deletions

View File

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

View File

@ -50,7 +50,7 @@
get-pages get-pages-relation get-pages-that-mentioned-page get-public-pages get-tag-pages 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 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-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 [frontend.db.react
get-current-marker get-current-page get-current-priority set-key-value get-current-marker get-current-page get-current-priority set-key-value

View File

@ -1252,16 +1252,21 @@
page-id) page-id)
ffirst)) ffirst))
(defn get-namespace-files (defn get-namespace-pages
[repo namespace] [repo namespace]
(assert (string? namespace)) (assert (string? namespace))
(let [db (conn/get-conn repo) (let [db (conn/get-conn repo)]
namespace (string/replace namespace "/" ".")]
(when-not (string/blank? namespace) (when-not (string/blank? namespace)
(let [namespace (string/trim namespace) (let [namespace (string/lower-case (string/trim namespace))
pattern-1 (re-pattern (util/format "[\\.|/]%s\\." namespace)) ids (->> (d/datoms db :aevt :block/name)
pattern-2 (re-pattern (util/format "^%s\\." namespace))] (filter (fn [datom]
(->> (d/datoms db :aevt :file/path) (let [page (:v datom)]
(filter (fn [datom] (or
(or (re-find pattern-1 (:v datom)) (= page namespace)
(re-find pattern-2 (:v datom)))))))))) (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))))))

View File

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

View File

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