From 8efafd7d77ff6b6a9308055d2e260e28c0b19136 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Tue, 29 Jun 2021 21:48:15 +0800 Subject: [PATCH] refactor: rename namespace page uses pages instead of files Also, fix #2319 --- src/main/frontend/components/hierarchy.cljs | 24 ++++---- src/main/frontend/db.cljs | 2 +- src/main/frontend/db/model.cljs | 25 +++++---- src/main/frontend/handler/page.cljs | 62 +++++++++++---------- src/main/frontend/text.cljs | 3 +- 5 files changed, 61 insertions(+), 55 deletions(-) diff --git a/src/main/frontend/components/hierarchy.cljs b/src/main/frontend/components/hierarchy.cljs index 5f9fdc67e4..45a8fb701d 100644 --- a/src/main/frontend/components/hierarchy.cljs +++ b/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] diff --git a/src/main/frontend/db.cljs b/src/main/frontend/db.cljs index df2d1d9e38..deeee81db7 100644 --- a/src/main/frontend/db.cljs +++ b/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 diff --git a/src/main/frontend/db/model.cljs b/src/main/frontend/db/model.cljs index 06b21a3825..930195e3c2 100644 --- a/src/main/frontend/db/model.cljs +++ b/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)))))) diff --git a/src/main/frontend/handler/page.cljs b/src/main/frontend/handler/page.cljs index 1a40f05b6c..73ff87e3f1 100644 --- a/src/main/frontend/handler/page.cljs +++ b/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] diff --git a/src/main/frontend/text.cljs b/src/main/frontend/text.cljs index 7282638621..a8b0c8d04e 100644 --- a/src/main/frontend/text.cljs +++ b/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