Explorar el Código

chore: remove repeated get-title-and-pagename calls

Tienson Qin hace 1 año
padre
commit
ebc801241d

+ 3 - 4
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])]

+ 48 - 47
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"