Pārlūkot izejas kodu

fix(fs): file editing in nfs

Andelf 2 gadi atpakaļ
vecāks
revīzija
97b534301b

+ 18 - 9
src/main/frontend/components/file.cljs

@@ -21,7 +21,8 @@
             [promesa.core :as p]
             [reitit.frontend.easy :as rfe]
             [rum.core :as rum]
-            [logseq.common.path :as path]))
+            [logseq.common.path :as path]
+            [frontend.handler.global-config :as global-config-handler]))
 
 (defn- get-path
   [state]
@@ -73,17 +74,17 @@
    (files-all)
    ])
 
+;; FIXME: misuse of rpath and fpath
 (rum/defcs file-inner < rum/reactive
   {:will-mount (fn [state]
                  (let [*content (atom nil)
                        [path format] (:rum/args state)
                        repo-dir (config/get-repo-dir (state/get-current-repo))
                        [dir path] (cond
-                                    (string/starts-with? path repo-dir)
-                                    [repo-dir (-> (string/replace-first path repo-dir "")
-                                                  (string/replace #"^/+" ""))]
+                                    (string/starts-with? path (global-config-handler/global-config-dir))
+                                    [nil path]
 
-                                    ;; browser-fs
+                                    ;; local file
                                     (not (string/starts-with? path "/"))
                                     [repo-dir path]
 
@@ -91,6 +92,7 @@
                                     [nil path])]
                    (when (and format (contains? (gp-config/text-formats) format))
                      (p/let [content (fs/read-file dir path)]
+                       (prn ::read-content content)
                        (reset! *content (or content ""))))
                    (assoc state ::file-content *content)))
    :did-mount (fn [state]
@@ -100,9 +102,16 @@
                    (state/clear-file-component!)
                    state)}
   [state path format]
-  (let [original-name (db/get-file-page path)
-        repo-dir (config/get-repo-dir (state/get-current-repo))
-        rel-path (path/trim-dir-prefix repo-dir path)
+  (let [repo-dir (config/get-repo-dir (state/get-current-repo))
+        rel-path (when (string/starts-with? path repo-dir)
+                   (path/trim-dir-prefix repo-dir path))
+        original-name (db/get-file-page (or path rel-path))
+        in-db? (boolean (db/get-file (or path rel-path)))
+
+        file-fpath (if in-db?
+                     (path/path-join repo-dir path)
+                     path)
+
         random-id (str (d/squuid))
         content (rum/react (::file-content state))]
     [:div.file {:id (str "file-edit-wrapper-" random-id)
@@ -141,7 +150,7 @@
        (let [content' (string/trim content)
              mode (util/get-file-ext path)]
          (lazy-editor/editor {:file?     true
-                              :file-path path}
+                              :file-path file-fpath}
                              (str "file-edit-" random-id)
                              {:data-lang mode}
                              content'

+ 4 - 3
src/main/frontend/fs/nfs.cljs

@@ -241,8 +241,10 @@
 
   (read-file [_this dir path _options]
     (prn ::read-file dir path)
-    (let [fpath (path/path-join dir path)
-          handle-path (str "handle/" fpath)]
+    (p/let [_ (when-not (string/includes? dir "/")
+                (await-get-nfs-file-handle (str "logseq_local_" dir) (str "handle/" dir)))
+            fpath (path/path-join dir path)
+            handle-path (str "handle/" fpath)]
       (p/let [handle (or (get-nfs-file-handle handle-path)
                          (idb/get-item handle-path))
               local-file (and handle (.getFile handle))]
@@ -278,7 +280,6 @@
           (p/let [basename (path/filename fpath)
                   parent-dir (path/parent fpath)
                   parent-dir-handle-path (str "handle/" parent-dir)
-                  _ (prn ::debug-0 parent-dir-handle-path)
                   parent-dir-handle (get-nfs-file-handle parent-dir-handle-path)]
 
             (if parent-dir-handle

+ 2 - 1
src/main/frontend/handler/code.cljs

@@ -41,7 +41,8 @@
             (let [path (:file-path config)
                   repo (state/get-current-repo)
                   repo-dir (config/get-repo-dir repo)
-                  rpath (path/trim-dir-prefix repo-dir path)]
+                  rpath (when (string/starts-with? path repo-dir)
+                          (path/trim-dir-prefix repo-dir path))]
               (if rpath
                 ;; in-db file
                 (let [db-content (db/get-file rpath)