From ebc801241d1b4541b91c8cd0b0380fa0d58f8ef4 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Tue, 30 Jan 2024 15:57:33 +0800 Subject: [PATCH] chore: remove repeated get-title-and-pagename calls --- src/main/frontend/handler/common/page.cljs | 7 +- src/main/frontend/worker/handler/page.cljs | 95 +++++++++++----------- 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/main/frontend/handler/common/page.cljs b/src/main/frontend/handler/common/page.cljs index 2d007514ab..b8e99afc61 100644 --- a/src/main/frontend/handler/common/page.cljs +++ b/src/main/frontend/handler/common/page.cljs @@ -50,8 +50,7 @@ (let [repo (state/get-current-repo) conn (db/get-db repo false) config (state/get-config repo) - _ (worker-page/create! repo conn config title options) - [_ page-name] (worker-page/get-title-and-pagename title)] + [_ page-name] (worker-page/create! repo conn config title options)] (when redirect? (route-handler/redirect-to-page! page-name)) (when-let [first-block (first (:block/_left (db/entity [:block/name page-name])))] @@ -67,8 +66,8 @@ (p/let [repo (state/get-current-repo) conn (db/get-db repo false) config (state/get-config repo) - _ (worker-page/create! repo conn config title options) - [_ page-name] (worker-page/get-title-and-pagename title)] + [p page-name] (worker-page/create! repo conn config title options) + _result p] (when redirect? (route-handler/redirect-to-page! page-name)) (let [page (db/entity [:block/name page-name])] diff --git a/src/main/frontend/worker/handler/page.cljs b/src/main/frontend/worker/handler/page.cljs index e3491f0028..57184167da 100644 --- a/src/main/frontend/worker/handler/page.cljs +++ b/src/main/frontend/worker/handler/page.cljs @@ -81,56 +81,57 @@ (date/valid-journal-title? date-formatter title))) [title page-name] (get-title-and-pagename title) - with-uuid? (if (uuid? uuid) uuid true)] ;; FIXME: prettier validation - (when (ldb/page-empty? @conn page-name) - (let [pages (if split-namespace? - (common-util/split-namespace-pages title) - [title]) - format (or format (common-config/get-preferred-format config)) - pages (map (fn [page] + with-uuid? (if (uuid? uuid) uuid true) + result (when (ldb/page-empty? @conn page-name) + (let [pages (if split-namespace? + (common-util/split-namespace-pages title) + [title]) + format (or format (common-config/get-preferred-format config)) + pages (map (fn [page] ;; only apply uuid to the deepest hierarchy of page to create if provided. - (-> (gp-block/page-name->map page (if (= page title) with-uuid? true) @conn true date-formatter) - (assoc :block/format format))) - pages) - txs (->> pages + (-> (gp-block/page-name->map page (if (= page title) with-uuid? true) @conn true date-formatter) + (assoc :block/format format))) + pages) + txs (->> pages ;; for namespace pages, only last page need properties - drop-last - (mapcat #(build-page-tx repo conn config date-formatter format nil % {})) - (remove nil?)) - txs (map-indexed (fn [i page] - (if (zero? i) - page - (assoc page :block/namespace - [:block/uuid (:block/uuid (nth txs (dec i)))]))) - txs) - page-txs (build-page-tx repo conn config date-formatter format properties (last pages) (select-keys options [:whiteboard? :class? :tags])) - page-txs (if (seq txs) - (update page-txs 0 - (fn [p] - (assoc p :block/namespace [:block/uuid (:block/uuid (last txs))]))) - page-txs) - first-block-tx (when (and - create-first-block? - (not (or whiteboard? class?)) - (ldb/page-empty? @conn (:db/id (d/entity @conn [:block/name page-name]))) + drop-last + (mapcat #(build-page-tx repo conn config date-formatter format nil % {})) + (remove nil?)) + txs (map-indexed (fn [i page] + (if (zero? i) + page + (assoc page :block/namespace + [:block/uuid (:block/uuid (nth txs (dec i)))]))) + txs) + page-txs (build-page-tx repo conn config date-formatter format properties (last pages) (select-keys options [:whiteboard? :class? :tags])) + page-txs (if (seq txs) + (update page-txs 0 + (fn [p] + (assoc p :block/namespace [:block/uuid (:block/uuid (last txs))]))) page-txs) - (let [page-id [:block/uuid (:block/uuid (first page-txs))]] - [(sqlite-util/block-with-timestamps - {:block/uuid (ldb/new-block-id) - :block/page page-id - :block/parent page-id - :block/left page-id - :block/content "" - :block/format format})])) - txs (concat - txs - page-txs - first-block-tx)] - (when (seq txs) - (ldb/transact! conn txs (cond-> {:persist-op? persist-op?} - today-journal? - (assoc :create-today-journal? true - :today-journal-name page-name)))))))) + first-block-tx (when (and + create-first-block? + (not (or whiteboard? class?)) + (ldb/page-empty? @conn (:db/id (d/entity @conn [:block/name page-name]))) + page-txs) + (let [page-id [:block/uuid (:block/uuid (first page-txs))]] + [(sqlite-util/block-with-timestamps + {:block/uuid (ldb/new-block-id) + :block/page page-id + :block/parent page-id + :block/left page-id + :block/content "" + :block/format format})])) + txs (concat + txs + page-txs + first-block-tx)] + (when (seq txs) + (ldb/transact! conn txs (cond-> {:persist-op? persist-op?} + today-journal? + (assoc :create-today-journal? true + :today-journal-name page-name))))))] ;; FIXME: prettier validation + [result page-name])) (defn db-refs->page "Replace [[page name]] with page name"