fix: create default files on desktop app

This commit is contained in:
Tienson Qin
2021-01-24 16:59:26 +08:00
parent f31f5cd0a2
commit 25c7726331
6 changed files with 36 additions and 26 deletions

View File

@@ -4,9 +4,12 @@
[frontend.handler.file :as file-handler]
[frontend.handler.page :as page-handler]
[frontend.handler.notification :as notification]
[frontend.handler.route :as route-handler]
[frontend.config :as config]
[cljs-bean.core :as bean]
[frontend.db :as db]))
[frontend.db :as db]
[frontend.state :as state]
[clojure.string :as string]))
(defn handle-changed!
[type {:keys [dir path content stat] :as payload}]
@@ -15,8 +18,16 @@
{:keys [mtime]} stat]
(cond
(= "add" type)
(when (not= content (db/get-file path))
(file-handler/alter-file repo path content {:re-render-root? true}))
(let [db-content (db/get-file path)]
(when (and (not= content db-content)
;; Avoid file overwrites
;; 1. create a new page which writes a new file
;; 2. add some new content
;; 3. file watcher notified it with the old content
;; 4. old content will overwrites the new content in step 2
(not (and db-content
(string/starts-with? db-content content))))
(file-handler/alter-file repo path content {:re-render-root? true})))
(and (= "change" type)
(not= content (db/get-file path))
@@ -30,7 +41,10 @@
page-name
(fn []
(notification/show! (str "Page " page-name " was deleted on disk.")
:success))))
:success)
(when (= (state/get-current-page) page-name)
;; redirect to home
(route-handler/redirect-to-home!)))))
(contains? #{"add" "change" "unlink"} type)
nil