Przeglądaj źródła

enhance: re-enable file watcher when the folder is back

By following the great suggestions from https://github.com/hakon-j-d-johnsen.
Tienson Qin 3 lat temu
rodzic
commit
8cc4b357a4

+ 1 - 2
src/electron/electron/fs_watcher.cljs

@@ -4,8 +4,7 @@
             ["chokidar" :as watcher]
             [electron.utils :as utils]
             ["electron" :refer [app]]
-            [electron.window :as window]
-            ["path" :as path]))
+            [electron.window :as window]))
 
 ;; TODO: explore different solutions for different platforms
 ;; 1. https://github.com/Axosoft/nsfw

+ 10 - 7
src/electron/electron/handler.cljs

@@ -52,13 +52,16 @@
 (defmethod handle :unlink [_window [_ repo path]]
   (if (plugin/dotdir-file? path)
     (fs/unlinkSync path)
-    (let [file-name   (-> (string/replace path (str repo "/") "")
-                          (string/replace "/" "_")
-                          (string/replace "\\" "_"))
-          recycle-dir (str repo "/logseq/.recycle")
-          _           (fs-extra/ensureDirSync recycle-dir)
-          new-path    (str recycle-dir "/" file-name)]
-      (fs/renameSync path new-path))))
+    (try
+      (let [file-name   (-> (string/replace path (str repo "/") "")
+                           (string/replace "/" "_")
+                           (string/replace "\\" "_"))
+           recycle-dir (str repo "/logseq/.recycle")
+           _           (fs-extra/ensureDirSync recycle-dir)
+           new-path    (str recycle-dir "/" file-name)]
+        (fs/renameSync path new-path))
+      (catch :default _e
+        nil))))
 
 (defonce Diff (google-diff.))
 (defn string-some-deleted?

+ 16 - 5
src/main/frontend/fs/watcher_handler.cljs

@@ -8,13 +8,15 @@
             [frontend.handler.page :as page-handler]
             [frontend.handler.repo :as repo-handler]
             [frontend.handler.ui :as ui-handler]
+            [frontend.handler.notification :as notification]
             [logseq.graph-parser.util :as gp-util]
             [frontend.util.text :as text-util]
             [lambdaisland.glogi :as log]
             [electron.ipc :as ipc]
             [promesa.core :as p]
             [frontend.state :as state]
-            [frontend.encrypt :as encrypt]))
+            [frontend.encrypt :as encrypt]
+            [frontend.fs :as fs]))
 
 ;; all IPC paths must be normalized! (via gp-util/path-normalize)
 
@@ -61,7 +63,14 @@
             (state/update-state! :file/unlinked-dirs (fn [dirs] (conj dirs dir))))
 
           (= "addDir" type)
-          (state/update-state! :file/unlinked-dirs (fn [dirs] (disj dirs dir)))
+          (when (contains? (:file/unlinked-dirs @state/state) dir)
+            (notification/clear-all!)
+            (state/pub-event! [:notification/show
+                               {:content (str "The directory " dir " has been back, you can edit your graph now.")
+                                :status :success
+                                :clear? true}])
+            (fs/watch-dir! dir)
+            (state/update-state! :file/unlinked-dirs (fn [dirs] (disj dirs dir))))
 
           (contains? (:file/unlinked-dirs @state/state) dir)
           nil
@@ -90,9 +99,11 @@
 
           (and (= "unlink" type)
                (db/file-exists? repo path))
-          (when-let [page-name (db/get-file-page path)]
-            (println "Delete page: " page-name ", file path: " path ".")
-            (page-handler/delete! page-name #() :unlink-file? true))
+          (p/let [dir-exists? (fs/file-exists? dir "")]
+            (when dir-exists?
+              (when-let [page-name (db/get-file-page path)]
+                (println "Delete page: " page-name ", file path: " path ".")
+                (page-handler/delete! page-name #() :unlink-file? true))))
 
           (and (contains? #{"add" "change" "unlink"} type)
                (string/ends-with? path "logseq/custom.css"))

+ 4 - 0
src/main/frontend/handler/notification.cljs

@@ -7,6 +7,10 @@
   (let [contents (state/get-notification-contents)]
     (state/set-state! :notification/contents (dissoc contents uid))))
 
+(defn clear-all!
+  []
+  (state/set-state! :notification/contents nil))
+
 (defn show!
   ([content status]
    (show! content status true nil 1500))