Просмотр исходного кода

fix: add assets support on memory-fs

Tienson Qin 2 лет назад
Родитель
Сommit
02e9f8eaa7

+ 4 - 1
src/main/frontend/components/block.cljs

@@ -375,7 +375,10 @@
   (let [src (::src state)
         granted? (state/sub [:nfs/user-granted? (state/get-current-repo)])
         href (config/get-local-asset-absolute-path href)]
-    (when (or granted? (util/electron?) (mobile-util/native-platform?))
+    (when (or granted?
+              (util/electron?)
+              (mobile-util/native-platform?)
+              (config/db-based-graph? (state/get-current-repo)))
       (p/then (editor-handler/make-asset-url href) #(reset! src %)))
 
     (when @src

+ 0 - 3
src/main/frontend/components/content.cljs

@@ -10,7 +10,6 @@
             [frontend.extensions.srs :as srs]
             [frontend.handler.common :as common-handler]
             [frontend.handler.editor :as editor-handler]
-            [frontend.handler.image :as image-handler]
             [frontend.handler.notification :as notification]
             [frontend.handler.page :as page-handler]
             [frontend.handler.common.developer :as dev-common-handler]
@@ -456,11 +455,9 @@
 (rum/defcs content < rum/reactive
   {:did-mount (fn [state]
                 (set-draw-iframe-style!)
-                (image-handler/render-local-images!)
                 state)
    :did-update (fn [state]
                  (set-draw-iframe-style!)
-                 (image-handler/render-local-images!)
                  state)}
   [state id {:keys [format
                     config

+ 6 - 7
src/main/frontend/components/select.cljs

@@ -139,13 +139,12 @@
                            (when multiple-choices?
                              [:div.p-4 (ui/button "Apply"
                                                   {:small? true
-                                                   :button-props
-                                                   {:on-mouse-down (fn [e]
-                                                                     (util/stop e)
-                                                                     (when @*toggle (@*toggle))
-                                                                     (when (fn? on-apply)
-                                                                       (on-apply selected-choices)
-                                                                       (when close-modal? (state/close-modal!))))}})])]]
+                                                   :on-mouse-down (fn [e]
+                                                                    (util/stop e)
+                                                                    (when @*toggle (@*toggle))
+                                                                    (when (fn? on-apply)
+                                                                      (on-apply selected-choices)
+                                                                      (when close-modal? (state/close-modal!))))})])]]
     (when (fn? tap-*input-val)
       (tap-*input-val input))
     [:div.cp__select

+ 34 - 33
src/main/frontend/config.cljs

@@ -396,47 +396,48 @@
 
 (defn get-repo-dir
   [repo-url]
-  (cond
-    (nil? repo-url)
-    (do
-      (js/console.error "BUG: nil repo")
-      nil)
+  (let [db-based? (db-based-graph? repo-url)]
+    (cond
+      (nil? repo-url)
+      (do
+        (js/console.error "BUG: nil repo")
+        nil)
+
+      (and (util/electron?) db-based-graph?)
+      (get-local-dir repo-url)
 
-    (and (util/electron?) (db-based-graph? repo-url))
-    (get-local-dir repo-url)
+      db-based?
+      (str "memory:///"
+           (string/replace-first repo-url db-version-prefix ""))
 
-    (and (util/electron?) (local-file-based-graph? repo-url))
-    (get-local-dir repo-url)
+      (and (util/electron?) (local-file-based-graph? repo-url))
+      (get-local-dir repo-url)
 
-    (and (mobile-util/native-platform?) (local-file-based-graph? repo-url))
-    (let [dir (get-local-dir repo-url)]
-      (if (string/starts-with? dir "file://")
-        dir
-        (path/path-join "file://" dir)))
+      (and (mobile-util/native-platform?) (local-file-based-graph? repo-url))
+      (let [dir (get-local-dir repo-url)]
+        (if (string/starts-with? dir "file://")
+          dir
+          (path/path-join "file://" dir)))
 
     ;; Special handling for demo graph
-    (= repo-url "local")
-    "memory:///local"
+      (= repo-url "local")
+      "memory:///local"
 
     ;; nfs, browser-fs-access
     ;; Format: logseq_local_{dir-name}
-    (local-file-based-graph? repo-url)
-    (string/replace-first repo-url local-db-prefix "")
-
-    ;; unit test
-    (= repo-url "test-db")
-    "/test-db"
-
-    ;; no dir for db-based-graph
-    (string/starts-with? repo-url "logseq_db")
-    nil
-
-    :else
-    (do
-      (js/console.error "Unknown Repo URL type:" repo-url)
-      (str "/"
-           (->> (take-last 2 (string/split repo-url #"/"))
-                (string/join "_"))))))
+      (local-file-based-graph? repo-url)
+      (string/replace-first repo-url local-db-prefix "")
+
+     ;; unit test
+      (= repo-url "test-db")
+      "/test-db"
+
+      :else
+      (do
+        (js/console.error "Unknown Repo URL type:" repo-url)
+        (str "/"
+             (->> (take-last 2 (string/split repo-url #"/"))
+                  (string/join "_")))))))
 
 (defn get-string-repo-dir
   [repo-dir]

+ 17 - 5
src/main/frontend/fs.cljs

@@ -37,11 +37,22 @@
     nfs-backend))
 
 (defn get-fs
-  [dir]
-  (let [bfs-local? (and dir
+  [dir & {:keys [repo rpath]}]
+  (let [repo (or repo (state/get-current-repo))
+        bfs-local? (and dir
                         (or (string/starts-with? dir "/local")
-                            (string/starts-with? dir "local")))]
+                            (string/starts-with? dir "local")))
+        db-assets? (and
+                    (config/db-based-graph? repo)
+                    rpath
+                    (string/starts-with? rpath "assets/"))]
     (cond
+      (and db-assets? (util/electron?))
+      node-backend
+
+      db-assets?
+      memory-backend
+
       (nil? dir) ;; global file op, use native backend
       (get-native-backend)
 
@@ -93,7 +104,8 @@
   [repo dir rpath content opts]
   (when content
     (let [path (gp-util/path-normalize rpath)
-          fs-record (get-fs dir)]
+          fs-record (get-fs dir {:repo repo
+                                 :rpath rpath})]
       (->
        (p/let [opts (assoc opts
                            :error-handler
@@ -103,7 +115,7 @@
                                                                           :fs (type fs-record)
                                                                           :user-agent (when js/navigator js/navigator.userAgent)
                                                                           :content-length (count content)}}])))
-               _ (protocol/write-file! (get-fs dir) repo dir path content opts)])
+               _ (protocol/write-file! fs-record repo dir path content opts)])
        (p/catch (fn [error]
                   (log/error :file/write-failed {:dir dir
                                                  :path path

+ 9 - 4
src/main/frontend/fs/memory_fs.cljs

@@ -45,7 +45,6 @@
       (p/catch (fn [_error]
                  (js/window.pfs.mkdir dir)))))
 
-
 (defrecord MemoryFs []
   protocol/Fs
   (mkdir! [_this dir]
@@ -53,6 +52,10 @@
       (let [fpath (path/url-to-path dir)]
         (-> (js/window.pfs.mkdir fpath)
             (p/catch (fn [error] (println "(memory-fs)Mkdir error: " error)))))))
+  (mkdir-recur! [this dir]
+    (p/let [parent (path/parent dir)
+            _ (when parent (<ensure-dir! parent))]
+      (protocol/mkdir! this dir)))
   (readdir [_this dir]
     (when js/window.pfs
       (let [fpath (path/url-to-path dir)]
@@ -76,13 +79,15 @@
   (read-file [_this dir path options]
     (let [fpath (path/url-to-path (path/path-join dir path))]
       (js/window.pfs.readFile fpath (clj->js options))))
-  (write-file! [_this repo dir rpath content _opts]
+  (write-file! [_this _repo dir rpath content _opts]
     (p/let [fpath (path/url-to-path (path/path-join dir rpath))
             containing-dir (path/parent fpath)
             _ (<ensure-dir! containing-dir)
             _ (js/window.pfs.writeFile fpath content)]
-      (db/set-file-content! repo rpath content)
-      (db/set-file-last-modified-at! repo rpath (js/Date.))
+
+      ;; TODO: store file metadata
+      ;; (db/set-file-content! repo rpath content)
+      ;; (db/set-file-last-modified-at! repo rpath (js/Date.))
       ))
   (rename! [_this _repo old-path new-path]
     (let [old-path (path/url-to-path old-path)

+ 17 - 3
src/main/frontend/handler/editor.cljs

@@ -1359,7 +1359,7 @@
   (p/let [repo-dir (config/get-repo-dir repo)
           assets-dir "assets"
           _ (fs/mkdir-if-not-exists (path/path-join repo-dir assets-dir))]
-    [repo-dir assets-dir]))
+        [repo-dir assets-dir]))
 
 (defn get-asset-path
   "Get asset path from filename, ensure assets dir exists"
@@ -1403,7 +1403,6 @@
             dir (or (:dir matched-alias) repo-dir)]
         (if (util/electron?)
           (let [from (not-empty (.-path file))]
-
             (js/console.debug "Debug: Copy Asset #" dir file-rpath from)
             (-> (js/window.apis.copyFileToAssets dir file-rpath from)
                 (p/then
@@ -1415,12 +1414,20 @@
                 (p/catch #(js/console.error "Debug: Copy Asset Error#" %))))
 
           (p/do! (js/console.debug "Debug: Writing Asset #" dir file-rpath)
-                 (if (mobile-util/native-platform?)
+                 (cond
+                   (mobile-util/native-platform?)
                    ;; capacitor fs accepts Blob, File implements Blob
                    (p/let [buffer (.arrayBuffer file)
                            content (base64/encodeByteArray (js/Uint8Array. buffer))
                            fpath (path/path-join dir file-rpath)]
                      (capacitor-fs/<write-file-with-base64 fpath content))
+
+                   (config/db-based-graph? repo) ;; memory-fs
+                   (p/let [buffer (.arrayBuffer file)
+                           content (js/Uint8Array. buffer)]
+                     (fs/write-file! repo dir file-rpath content nil))
+
+                   :else                ; nfs
                    (fs/write-file! repo dir file-rpath (.stream file) nil))
                  [file-rpath file (path/path-join dir file-rpath) matched-alias])))))))
 
@@ -1451,6 +1458,11 @@
         (mobile-util/native-platform?)
         (mobile-util/convert-file-src full-path)
 
+        (config/db-based-graph? (state/get-current-repo)) ; memory fs
+        (p/let [binary (fs/read-file repo-dir path {})
+                blob (js/Blob. (array binary) (clj->js {:type "image"}))]
+          (when blob (js/URL.createObjectURL blob)))
+
         :else ;; nfs
         (let [handle-path (str "handle/" full-path)
               cached-url  (get @*assets-url-cache (keyword handle-path))]
@@ -1526,6 +1538,8 @@
                   {:last-pattern (if drop-or-paste? "" commands/command-trigger)
                    :restore?     true
                    :command      :insert-asset})))))
+          (p/catch (fn [e]
+                     (js/console.error e)))
           (p/finally
             (fn []
               (reset! uploading? false)

+ 0 - 53
src/main/frontend/handler/image.cljs

@@ -1,53 +0,0 @@
-(ns ^:no-doc frontend.handler.image
-  (:require [clojure.string :as string]
-            [frontend.config :as config]
-            [frontend.fs :as fs]
-            [frontend.image :as image]
-            [frontend.state :as state]
-            [frontend.util :as util]
-            [goog.dom :as gdom]
-            [goog.object :as gobj]
-            [frontend.mobile.util :as mobile-util]))
-
-(defn render-local-images!
-  []
-  (when-not (and (or (util/electron?)
-                     (mobile-util/native-ios?))
-                 (or (config/local-file-based-graph? (state/get-current-repo))
-                     (config/db-based-graph? (state/get-current-repo))))
-    (try
-      (let [images (array-seq (gdom/getElementsByTagName "img"))
-            get-src (fn [image] (.getAttribute image "src"))
-            local-images (filter
-                          (fn [image]
-                            (let [src (get-src image)]
-                              (and src
-                                   (not (or (util/starts-with? src "http://")
-                                            (util/starts-with? src "https://")
-                                            (util/starts-with? src "blob:")
-                                            (util/starts-with? src "data:"))))))
-                          images)]
-        (doseq [img local-images]
-          (gobj/set img
-                    "onerror"
-                    (fn []
-                      (gobj/set (gobj/get img "style")
-                                "display" "none")))
-          (let [path (get-src img)
-                path (string/replace-first path "file:" "")
-                path (if (= (first path) \.)
-                       (subs path 1)
-                       path)]
-            (util/p-handle
-             (fs/read-file (config/get-repo-dir (state/get-current-repo)) path {})
-             (fn [blob]
-               (let [blob (js/Blob. (array blob) (clj->js {:type "image"}))
-                     img-url (image/create-object-url blob)]
-                 (gobj/set img "src" img-url)
-                 (gobj/set (gobj/get img "style")
-                           "display" "initial")))
-             (fn [error]
-               (println "Can't read local image file: ")
-               (js/console.dir error))))))
-      (catch :default _e
-        nil))))

+ 6 - 2
src/main/frontend/ui.cljs

@@ -1027,7 +1027,8 @@
 
 (rum/defc button-inner
   [text & {:keys [background href class intent on-click small? title icon icon-props disabled? button-props]
-           :or   {small? false}}]
+           :or   {small? false}
+           :as option}]
   (let [opts {:text text
               :theme (when (contains? #{"link" "border-link"} intent) :text)
               :href href
@@ -1035,7 +1036,10 @@
               :size (if small? :sm :md)
               :icon icon
               :icon-props icon-props
-              :button-props (merge button-props (when title {:title title}))
+              :button-props (merge
+                             (dissoc option
+                                     :background :href :class :intent :small? :large? :icon :icon-props :disabled? button-props)
+                             button-props)
               :class (if (= intent "border-link") (str class " border") class)
               :muted disabled?
               :disabled? disabled?}]