瀏覽代碼

fix: create default files on desktop app

Tienson Qin 4 年之前
父節點
當前提交
25c7726331

+ 2 - 2
src/electron/electron/handler.cljs

@@ -80,8 +80,8 @@
   [win dir]
   (let [watcher (.watch watcher dir
                         (clj->js
-                         {:ignored #"^\."
-                          :ignoreInitial true
+                         {:ignored #"^\." ; FIXME read .gitignore and other ignore paths
+                          ;; :ignoreInitial true
                           :persistent true
                           :awaitWriteFinish true}))]
     (.on watcher "add"

+ 0 - 14
src/main/frontend/date.cljs

@@ -129,20 +129,6 @@
                  :minute "2-digit"
                  :hour12 false}))))
 
-(defn journals-path
-  [year month preferred-format]
-  (let [month (if (< month 10) (str "0" month) month)
-        format (string/lower-case (name preferred-format))
-        format (if (= format "markdown") "md" format)]
-    (str "journals/" year "_" month "." format)))
-
-(defn current-journal-path
-  [preferred-format]
-  (when preferred-format
-    (let [{:keys [year month]} (get-date)
-          preferred-format preferred-format]
-      (journals-path year month preferred-format))))
-
 (defn valid?
   [s]
   (some

+ 1 - 1
src/main/frontend/db/model.cljs

@@ -696,7 +696,7 @@
   [file ast]
   ;; headline
   (let [ast (map first ast)]
-    (if (util/starts-with? file "pages/contents.")
+    (if (string/includes? file "pages/contents.")
       "Contents"
       (let [first-block (last (first (filter block/heading-block? ast)))
             property-name (when (and (= "Properties" (ffirst ast))

+ 18 - 4
src/main/frontend/fs/watcher_handler.cljs

@@ -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

+ 6 - 1
src/main/frontend/handler/file.cljs

@@ -128,7 +128,12 @@
 
 (defn reset-file!
   [repo-url file content]
-  (let [new? (nil? (db/entity [:file/path file]))]
+  (let [file (if (and (util/electron?)
+                      (config/local-db? repo-url)
+                      (not= "/" (first file)))
+               (str (config/get-repo-dir repo-url) "/" file)
+               file)
+        new? (nil? (db/entity [:file/path file]))]
     (db/set-file-content! repo-url file content)
     (let [format (format/get-format file)
           utf8-content (utf8/encode content)

+ 9 - 4
src/main/frontend/handler/repo.cljs

@@ -139,11 +139,12 @@
                  (not page-exists?))
          (p/let [_ (nfs/check-directory-permission! repo-url)
                  _ (fs/mkdir-if-not-exists (str repo-dir "/" config/default-journals-directory))
-                 file-exists? (fs/create-if-not-exists repo-url repo-dir file-path content)]
+                 file-exists? (fs/file-exists? repo-dir file-path)]
            (when-not file-exists?
              (file-handler/reset-file! repo-url path content)
-             (ui-handler/re-render-root!)
-             (git-handler/git-add repo-url path))))))))
+             (p/let [_ (fs/create-if-not-exists repo-url repo-dir file-path content)]
+               (ui-handler/re-render-root!)
+               (git-handler/git-add repo-url path)))))))))
 
 (defn create-today-journal!
   []
@@ -152,7 +153,11 @@
     (when (or (db/cloned? repo)
               (and (config/local-db? repo)
                    ;; config file exists
-                   (db/get-file (str config/app-name "/" config/config-file))))
+                   (let [path (str config/app-name "/" config/config-file)
+                         path (if (and (util/electron?) (config/local-db? repo))
+                                (str (config/get-repo-dir repo) "/" path)
+                                path)]
+                     (db/get-file path))))
       (let [today-page (string/lower-case (date/today))]
         (when (empty? (db/get-page-blocks-no-cache repo today-page))
           (create-today-journal-if-not-exists repo))))))