Browse Source

Clean setTimeout

Tienson Qin 2 years ago
parent
commit
7600a53929

+ 7 - 8
src/main/frontend/handler/common/page.cljs

@@ -36,14 +36,13 @@
    (let [repo (state/get-current-repo)
          conn (db/get-db repo false)
          config (state/get-config repo)]
-     (when-let [page-name (worker-page/create! repo conn config title options)]
-       (when redirect?
-         (route-handler/redirect-to-page! page-name))
-       (js/setTimeout
-        #(when-let [first-block (first (:block/_left (db/entity [:block/name page-name])))]
-          (block-handler/edit-block! first-block :max nil))
-        100)
-       page-name))))
+     (p/let [_ (worker-page/create! repo conn config title options)]
+       (let [[_ page-name] (worker-page/get-title-and-pagename title)]
+         (when redirect?
+           (route-handler/redirect-to-page! page-name))
+         (when-let [first-block (first (:block/_left (db/entity [:block/name page-name])))]
+         (block-handler/edit-block! first-block :max nil))
+         page-name)))))
 
 ;; favorite fns
 ;; ============

+ 1 - 3
src/main/frontend/handler/editor/lifecycle.cljs

@@ -21,9 +21,7 @@
       (when content
         (editor-handler/restore-cursor-pos! id content)))
 
-    ;; Here we delay this listener, otherwise the click to edit event will trigger a outside click event,
-    ;; which will hide the editor so no way for editing.
-    (js/setTimeout #(keyboards-handler/esc-save! state) 100)
+    (keyboards-handler/esc-save! state)
 
     (when-let [element (gdom/getElement id)]
       (.focus element)

+ 9 - 9
src/main/frontend/handler/page.cljs

@@ -284,15 +284,15 @@
               format (state/get-preferred-format repo)
               template (state/get-default-journal-template)
               create-f (fn []
-                         (create! title {:redirect? false
-                                         :split-namespace? false
-                                         :create-first-block? (not template)
-                                         :journal? true
-                                         :today-journal? true})
-                         (js/setTimeout #(do
-                                           (state/pub-event! [:journal/insert-template today-page])
-                                           (ui-handler/re-render-root!)
-                                           (plugin-handler/hook-plugin-app :today-journal-created {:title today-page})) 100))]
+                         (p/do!
+                           (create! title {:redirect? false
+                                          :split-namespace? false
+                                          :create-first-block? (not template)
+                                          :journal? true
+                                          :today-journal? true})
+                           (state/pub-event! [:journal/insert-template today-page])
+                           (ui-handler/re-render-root!)
+                           (plugin-handler/hook-plugin-app :today-journal-created {:title today-page})))]
           (when (db/page-empty? repo today-page)
             (if (config/db-based-graph? repo)
               (let [page-exists (db/get-page today-page)]

+ 13 - 8
src/main/frontend/worker/handler/page.cljs

@@ -48,6 +48,16 @@
         [page (properties-block repo conn config date-formatter properties format page-entity)]
         [page]))))
 
+(defn get-title-and-pagename
+  [title]
+  (let [title      (-> (string/trim title)
+                       (text/page-ref-un-brackets!)
+                        ;; remove `#` from tags
+                       (string/replace #"^#+" ""))
+        title      (common-util/remove-boundary-slashes title)
+        page-name  (common-util/page-name-sanity-lc title)]
+    [title page-name]))
+
 (defn create!
   "Create page. Has the following options:
 
@@ -69,12 +79,8 @@
   (let [date-formatter (common-config/get-date-formatter config)
         split-namespace? (not (or (string/starts-with? title "hls__")
                                   (date/valid-journal-title? date-formatter title)))
-        title      (-> (string/trim title)
-                       (text/page-ref-un-brackets!)
-                        ;; remove `#` from tags
-                       (string/replace #"^#+" ""))
-        title      (common-util/remove-boundary-slashes title)
-        page-name  (common-util/page-name-sanity-lc 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?
@@ -124,8 +130,7 @@
           (ldb/transact! conn txs (cond-> {:persist-op? persist-op?}
                                     today-journal?
                                     (assoc :create-today-journal? true
-                                           :today-journal-name page-name)))
-          page-name)))))
+                                           :today-journal-name page-name))))))))
 
 (defn db-refs->page
   "Replace [[page name]] with page name"