Browse Source

fix: files not saved when onboarding while canceld the file picker

Tienson Qin 4 years ago
parent
commit
12fe2f9fad

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

@@ -117,10 +117,13 @@
   (let [result (.showOpenDialogSync dialog (bean/->js
                                              {:properties ["openDirectory" "createDirectory" "promptToCreate"]}))
         path (first result)]
-    (.. ^js window -webContents
-        (send "open-dir-confirmed"
-              (bean/->js {:opened? true})))
-    (get-files path)))
+    (if path
+      (do
+        (.. ^js window -webContents
+           (send "open-dir-confirmed"
+                 (bean/->js {:opened? true})))
+        (get-files path))
+      (throw (js/Error "path empty")))))
 
 (defmethod handle :getFiles [window [_ path]]
   (get-files path))

+ 6 - 9
src/main/frontend/fs.cljs

@@ -129,15 +129,12 @@
 (defn open-dir
   [ok-handler]
   (let [record (get-record)]
-    (->
-     (p/let [result (protocol/open-dir record ok-handler)]
-       (if (or (util/electron?)
-               (mobile-util/is-native-platform?))
-         (let [[dir & paths] (bean/->clj result)]
-           [(:path dir) paths])
-         result))
-     (p/catch (fn [error]
-                (js/console.error error))))))
+    (p/let [result (protocol/open-dir record ok-handler)]
+      (if (or (util/electron?)
+              (mobile-util/is-native-platform?))
+        (let [[dir & paths] (bean/->clj result)]
+          [(:path dir) paths])
+        result))))
 
 (defn get-files
   [path-or-handle ok-handler]

+ 48 - 47
src/main/frontend/handler/web/nfs.cljs

@@ -133,54 +133,55 @@
              root-handle (first result)
              dir-name (if nfs?
                         (gobj/get root-handle "name")
-                        root-handle)
-             repo (str config/local-db-prefix dir-name)
-             root-handle-path (str config/local-handle-prefix dir-name)
-             _ (when nfs?
-                 (idb/set-item! root-handle-path root-handle)
-                 (nfs/add-nfs-file-handle! root-handle-path root-handle))
-             result (nth result 1)
-             files (-> (->db-files mobile-native? electron? dir-name result)
-                       remove-ignore-files)
-             _ (when nfs?
-                 (let [file-paths (set (map :file/path files))]
-                   (swap! path-handles (fn [handles]
-                                         (->> handles
-                                              (filter (fn [[path _handle]]
-                                                        (or
-                                                         (contains? file-paths
-                                                                    (string/replace-first path (str dir-name "/") ""))
-                                                         (let [last-part (last (string/split path "/"))]
-                                                           (contains? #{config/app-name
-                                                                        config/default-draw-directory
-                                                                        (config/get-journals-directory)
-                                                                        (config/get-pages-directory)}
-                                                                      last-part)))))
-                                              (into {})))))
+                        root-handle)]
+       (when-not (string/blank? dir-name)
+         (p/let [repo (str config/local-db-prefix dir-name)
+                 root-handle-path (str config/local-handle-prefix dir-name)
+                 _ (when nfs?
+                     (idb/set-item! root-handle-path root-handle)
+                     (nfs/add-nfs-file-handle! root-handle-path root-handle))
+                 result (nth result 1)
+                 files (-> (->db-files mobile-native? electron? dir-name result)
+                           remove-ignore-files)
+                 _ (when nfs?
+                     (let [file-paths (set (map :file/path files))]
+                       (swap! path-handles (fn [handles]
+                                             (->> handles
+                                                  (filter (fn [[path _handle]]
+                                                            (or
+                                                             (contains? file-paths
+                                                                        (string/replace-first path (str dir-name "/") ""))
+                                                             (let [last-part (last (string/split path "/"))]
+                                                               (contains? #{config/app-name
+                                                                            config/default-draw-directory
+                                                                            (config/get-journals-directory)
+                                                                            (config/get-pages-directory)}
+                                                                          last-part)))))
+                                                  (into {})))))
 
-                 (set-files! @path-handles))
-             markup-files (filter-markup-and-built-in-files files)]
-       (-> (p/all (map (fn [file]
-                         (p/let [content (if nfs?
-                                           (.text (:file/file file))
-                                           (:file/content file))
-                                 content (encrypt/decrypt content)]
-                           (assoc file :file/content content))) markup-files))
-           (p/then (fn [result]
-                     (let [files (map #(dissoc % :file/file) result)]
-                       (repo-handler/start-repo-db-if-not-exists! repo {:db-type :local-native-fs})
-                       (p/let [_ (repo-handler/load-repo-to-db! repo
-                                                                {:first-clone? true
-                                                                 :nfs-files    files})]
-                         (state/add-repo! {:url repo :nfs? true})
-                         (state/set-loading-files! false)
-                         (and ok-handler (ok-handler))
-                         (when (util/electron?)
-                           (fs/watch-dir! dir-name))
-                         (state/pub-event! [:graph/added repo])))))
-           (p/catch (fn [error]
-                      (log/error :nfs/load-files-error repo)
-                      (log/error :exception error)))))
+                     (set-files! @path-handles))
+                 markup-files (filter-markup-and-built-in-files files)]
+           (-> (p/all (map (fn [file]
+                             (p/let [content (if nfs?
+                                               (.text (:file/file file))
+                                               (:file/content file))
+                                     content (encrypt/decrypt content)]
+                               (assoc file :file/content content))) markup-files))
+               (p/then (fn [result]
+                         (let [files (map #(dissoc % :file/file) result)]
+                           (repo-handler/start-repo-db-if-not-exists! repo {:db-type :local-native-fs})
+                           (p/let [_ (repo-handler/load-repo-to-db! repo
+                                                                    {:first-clone? true
+                                                                     :nfs-files    files})]
+                             (state/add-repo! {:url repo :nfs? true})
+                             (state/set-loading-files! false)
+                             (and ok-handler (ok-handler))
+                             (when (util/electron?)
+                               (fs/watch-dir! dir-name))
+                             (state/pub-event! [:graph/added repo])))))
+               (p/catch (fn [error]
+                          (log/error :nfs/load-files-error repo)
+                          (log/error :exception error)))))))
      (p/catch (fn [error]
                 (if (contains? #{"AbortError" "Error"} (gobj/get error "name"))
                   (state/set-loading-files! false)

+ 1 - 1
src/main/frontend/state.cljs

@@ -414,7 +414,7 @@
 
 (defn add-repo!
   [repo]
-  (when repo
+  (when (not (string/blank? repo))
     (update-state! [:me :repos]
                    (fn [repos]
                      (->> (conj repos repo)