Преглед изворни кода

chore: mv file asset upload to file-based ns

Noticed that this feature was the only use of get-pages-directory
that wasn't in a file specific ns
Gabriel Horner пре 7 месеци
родитељ
комит
499392cae0

+ 3 - 2
src/main/frontend/components/block.cljs

@@ -45,6 +45,7 @@
             [frontend.handler.db-based.property :as db-property-handler]
             [frontend.handler.dnd :as dnd]
             [frontend.handler.editor :as editor-handler]
+            [frontend.handler.file-based.editor :as file-editor-handler]
             [frontend.handler.export.common :as export-common-handler]
             [frontend.handler.file-based.property.util :as property-util]
             [frontend.handler.file-sync :as file-sync]
@@ -3328,7 +3329,7 @@
               (when (and (config/local-file-based-graph? repo) (not (state/editing?)))
                 ;; Basically the same logic as editor-handler/upload-asset,
                 ;; does not require edting
-                (-> (editor-handler/file-based-save-assets! repo (js->clj files))
+                (-> (file-editor-handler/file-based-save-assets! repo (js->clj files))
                     (p/then
                      (fn [res]
                        (when-let [[asset-file-name file-obj asset-file-fpath matched-alias] (first res)]
@@ -3338,7 +3339,7 @@
                                                                                   (str
                                                                                    (if image? "../assets/" "")
                                                                                    "@" (:name matched-alias) "/" asset-file-name)
-                                                                                  (editor-handler/resolve-relative-path (or asset-file-fpath asset-file-name)))
+                                                                                  (file-editor-handler/resolve-relative-path (or asset-file-fpath asset-file-name)))
                                                                                 (if file-obj (.-name file-obj) (if image? "image" "asset"))
                                                                                 image?)]
                            (editor-handler/api-insert-new-block!

+ 3 - 2
src/main/frontend/extensions/tldraw.cljs

@@ -15,6 +15,7 @@
             [frontend.extensions.pdf.assets :as pdf-assets]
             [frontend.handler.assets :as assets-handler]
             [frontend.handler.editor :as editor-handler]
+            [frontend.handler.file-based.editor :as file-editor-handler]
             [frontend.handler.history :as history]
             [frontend.handler.notification :as notification]
             [frontend.handler.page :as page-handler]
@@ -85,11 +86,11 @@
 
 (defn save-asset-handler
   [file]
-  (-> (editor-handler/file-based-save-assets! (state/get-current-repo) [(js->clj file)])
+  (-> (file-editor-handler/file-based-save-assets! (state/get-current-repo) [(js->clj file)])
       (p/then
        (fn [res]
          (when-let [[asset-file-name _ full-file-path] (and (seq res) (first res))]
-           (editor-handler/resolve-relative-path (or full-file-path asset-file-name)))))))
+           (file-editor-handler/resolve-relative-path (or full-file-path asset-file-name)))))))
 
 (defn references-count
   [props]

+ 25 - 0
src/main/frontend/handler/common/editor.cljs

@@ -0,0 +1,25 @@
+(ns ^:no-doc frontend.handler.common.editor
+  (:require [frontend.commands :as commands]))
+
+(defn insert-command!
+  [id command-output format {:keys [restore?]
+                             :or {restore? true}
+                             :as option}]
+  (cond
+    ;; replace string
+    (string? command-output)
+    (commands/insert! id command-output option)
+
+    ;; steps
+    (vector? command-output)
+    (commands/handle-steps command-output format)
+
+    (fn? command-output)
+    (let [s (command-output)]
+      (commands/insert! id s option))
+
+    :else
+    nil)
+
+  (when restore?
+    (commands/restore-state)))

+ 5 - 144
src/main/frontend/handler/editor.cljs

@@ -66,7 +66,8 @@
             [logseq.outliner.property :as outliner-property]
             [logseq.shui.popup.core :as shui-popup]
             [promesa.core :as p]
-            [rum.core :as rum]))
+            [rum.core :as rum]
+            [frontend.handler.common.editor :as editor-common-handler]))
 
 ;; FIXME: should support multiple images concurrently uploading
 
@@ -1379,98 +1380,7 @@
              (property-file/remove-properties-when-file-based repo format)
              string/trim)))
 
-(defn insert-command!
-  [id command-output format {:keys [restore?]
-                             :or {restore? true}
-                             :as option}]
-  (cond
-    ;; replace string
-    (string? command-output)
-    (commands/insert! id command-output option)
-
-    ;; steps
-    (vector? command-output)
-    (commands/handle-steps command-output format)
-
-    (fn? command-output)
-    (let [s (command-output)]
-      (commands/insert! id s option))
-
-    :else
-    nil)
-
-  (when restore?
-    (commands/restore-state)))
-
-(defn file-based-save-assets!
-  "Save incoming(pasted) assets to assets directory.
-
-   Returns: [file-rpath file-obj file-fpath matched-alias]"
-  ([repo files]
-   (p/let [[repo-dir assets-dir] (assets-handler/ensure-assets-dir! repo)]
-     (file-based-save-assets! repo repo-dir assets-dir files
-                              (fn [index file-stem]
-                     ;; TODO: maybe there're other chars we need to handle?
-                                (let [file-base (-> file-stem
-                                                    (string/replace " " "_")
-                                                    (string/replace "%" "_")
-                                                    (string/replace "/" "_"))
-                                      file-name (str file-base "_" (.now js/Date) "_" index)]
-                                  (string/replace file-name #"_+" "_"))))))
-  ([repo repo-dir asset-dir-rpath files gen-filename]
-   (p/all
-    (for [[index ^js file] (map-indexed vector files)]
-      ;; WARN file name maybe fully qualified path when paste file
-      (p/let [file-name (util/node-path.basename (.-name file))
-              [file-stem ext-full ext-base] (if file-name
-                                              (let [ext-base (util/node-path.extname file-name)
-                                                    ext-full (if-not (config/extname-of-supported? ext-base)
-                                                               (util/full-path-extname file-name) ext-base)]
-                                                [(subs file-name 0 (- (count file-name)
-                                                                      (count ext-full))) ext-full ext-base])
-                                              ["" "" ""])
-              filename  (str (gen-filename index file-stem) ext-full)
-              file-rpath  (str asset-dir-rpath "/" filename)
-              matched-alias (assets-handler/get-matched-alias-by-ext ext-base)
-              file-rpath (cond-> file-rpath
-                           (not (nil? matched-alias))
-                           (string/replace #"^[.\/\\]*assets[\/\\]+" ""))
-              dir (or (:dir matched-alias) repo-dir)]
-        (if (util/electron?)
-          (do (js/console.debug "Debug: Copy Asset #" dir file-rpath)
-              (-> (if-let [from (not-empty (.-path file))]
-                    (js/window.apis.copyFileToAssets dir file-rpath from)
-                    (p/let [content (.arrayBuffer file)]
-                      (fs/write-file! repo repo-dir file-rpath content {:skip-compare? true})))
-                  (p/then
-                   (fn [dest]
-                     [file-rpath
-                      (if (string? dest) (js/File. #js[] dest) file)
-                      (path/path-join dir file-rpath)
-                      matched-alias]))
-                  (p/catch #(js/console.error "Debug: Copy Asset Error#" %))))
-
-          (->
-           (p/do! (js/console.debug "Debug: Writing Asset #" dir file-rpath)
-                  (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])
-           (p/catch (fn [error]
-                      (prn :paste-file-error)
-                      (js/console.error error))))))))))
+(def insert-command! editor-common-handler/insert-command!)
 
 (defn delete-asset-of-block!
   [{:keys [repo asset-block href full-text block-id local? delete-local?] :as _opts}]
@@ -1495,55 +1405,6 @@
                                (path/resolve-relative-path block-file-rpath href)))]
             (fs/unlink! repo asset-fpath nil)))))))
 
-;; assets/journals_2021_02_03_1612350230540_0.png
-(defn resolve-relative-path
-  "Relative path to current file path.
-
-   Requires editing state"
-  [file-path]
-  (if-let [current-file-rpath (or (db-model/get-block-file-path (state/get-edit-block))
-                                  ;; fix dummy file path of page
-                                  (when (config/get-pages-directory)
-                                    (path/path-join (config/get-pages-directory) "_.md"))
-                                  "pages/contents.md")]
-    (let [repo-dir (config/get-repo-dir (state/get-current-repo))
-          current-file-fpath (path/path-join repo-dir current-file-rpath)]
-      (path/get-relative-path current-file-fpath file-path))
-    file-path))
-
-(defn file-upload-assets!
-  "Paste asset and insert link to current editing block"
-  [repo id ^js files format uploading? drop-or-paste?]
-  (when (config/local-file-based-graph? repo)
-    (-> (file-based-save-assets! repo (js->clj files))
-          ;; FIXME: only the first asset is handled
-        (p/then
-         (fn [res]
-           (when-let [[asset-file-name file-obj asset-file-fpath matched-alias] (first res)]
-             (let [image? (config/ext-of-image? asset-file-name)]
-               (insert-command!
-                id
-                (assets-handler/get-asset-file-link format
-                                                    (if matched-alias
-                                                      (str
-                                                       (if image? "../assets/" "")
-                                                       "@" (:name matched-alias) "/" asset-file-name)
-                                                      (resolve-relative-path (or asset-file-fpath asset-file-name)))
-                                                    (if file-obj (.-name file-obj) (if image? "image" "asset"))
-                                                    image?)
-                format
-                {:last-pattern (if drop-or-paste? "" commands/command-trigger)
-                 :restore?     true
-                 :command      :insert-asset})
-               (recur (rest res))))))
-        (p/catch (fn [e]
-                   (js/console.error e)))
-        (p/finally
-          (fn []
-            (reset! uploading? false)
-            (reset! *asset-uploading? false)
-            (reset! *asset-uploading-process 0))))))
-
 (defn- write-file!
   [repo dir file file-rpath file-name]
   (if (util/electron?)
@@ -1624,7 +1485,7 @@
                  (throw (ex-info "Can't save asset" {:files files}))))))))))
 
 (defn db-upload-assets!
-  "Paste asset and insert link to current editing block"
+  "Paste asset for db graph and insert link to current editing block"
   [repo id ^js files format uploading? drop-or-paste?]
   (when (or (config/local-file-based-graph? repo)
             (config/db-based-graph? repo))
@@ -1655,7 +1516,7 @@
   (let [repo (state/get-current-repo)]
     (if (config/db-based-graph? repo)
       (db-upload-assets! repo id ^js files format uploading? drop-or-paste?)
-      (file-upload-assets! repo id ^js files format uploading? drop-or-paste?))))
+      (file-editor-handler/file-upload-assets! repo id ^js files format uploading? *asset-uploading? *asset-uploading-process drop-or-paste?))))
 
 ;; Editor should track some useful information, like editor modes.
 ;; For example:

+ 123 - 1
src/main/frontend/handler/file_based/editor.cljs

@@ -5,25 +5,34 @@
             [frontend.config :as config]
             [frontend.date :as date]
             [frontend.db :as db]
+            [frontend.db.model :as db-model]
             [frontend.db.query-dsl :as query-dsl]
             [frontend.format.block :as block]
             [frontend.format.mldoc :as mldoc]
+            [frontend.fs :as fs]
+            [frontend.fs.capacitor-fs :as capacitor-fs]
+            [frontend.handler.assets :as assets-handler]
             [frontend.handler.block :as block-handler]
+            [frontend.handler.common.editor :as editor-common-handler]
             [frontend.handler.file-based.property :as file-property-handler]
             [frontend.handler.file-based.property.util :as property-util]
             [frontend.handler.file-based.repeated :as repeated]
             [frontend.handler.file-based.status :as status]
             [frontend.handler.property.file :as property-file]
+            [frontend.mobile.util :as mobile-util]
             [frontend.modules.outliner.op :as outliner-op]
             [frontend.modules.outliner.ui :as ui-outliner-tx]
             [frontend.state :as state]
             [frontend.util :as util]
             [frontend.util.file-based.clock :as clock]
             [frontend.util.file-based.drawer :as drawer]
+            [goog.crypt.base64 :as base64]
+            [logseq.common.path :as path]
             [logseq.common.util :as common-util]
             [logseq.common.util.block-ref :as block-ref]
             [logseq.db :as ldb]
-            [logseq.db.file-based.schema :as file-schema]))
+            [logseq.db.file-based.schema :as file-schema]
+            [promesa.core :as p]))
 
 (defn- remove-non-existed-refs!
   [refs]
@@ -273,3 +282,116 @@
                                 (date/get-date-time-string-3)))]
       content)
     content))
+
+(defn file-based-save-assets!
+  "Save incoming(pasted) assets to assets directory.
+
+   Returns: [file-rpath file-obj file-fpath matched-alias]"
+  ([repo files]
+   (p/let [[repo-dir assets-dir] (assets-handler/ensure-assets-dir! repo)]
+     (file-based-save-assets! repo repo-dir assets-dir files
+                              (fn [index file-stem]
+                     ;; TODO: maybe there're other chars we need to handle?
+                                (let [file-base (-> file-stem
+                                                    (string/replace " " "_")
+                                                    (string/replace "%" "_")
+                                                    (string/replace "/" "_"))
+                                      file-name (str file-base "_" (.now js/Date) "_" index)]
+                                  (string/replace file-name #"_+" "_"))))))
+  ([repo repo-dir asset-dir-rpath files gen-filename]
+   (p/all
+    (for [[index ^js file] (map-indexed vector files)]
+      ;; WARN file name maybe fully qualified path when paste file
+      (p/let [file-name (util/node-path.basename (.-name file))
+              [file-stem ext-full ext-base] (if file-name
+                                              (let [ext-base (util/node-path.extname file-name)
+                                                    ext-full (if-not (config/extname-of-supported? ext-base)
+                                                               (util/full-path-extname file-name) ext-base)]
+                                                [(subs file-name 0 (- (count file-name)
+                                                                      (count ext-full))) ext-full ext-base])
+                                              ["" "" ""])
+              filename  (str (gen-filename index file-stem) ext-full)
+              file-rpath  (str asset-dir-rpath "/" filename)
+              matched-alias (assets-handler/get-matched-alias-by-ext ext-base)
+              file-rpath (cond-> file-rpath
+                           (not (nil? matched-alias))
+                           (string/replace #"^[.\/\\]*assets[\/\\]+" ""))
+              dir (or (:dir matched-alias) repo-dir)]
+        (if (util/electron?)
+          (do (js/console.debug "Debug: Copy Asset #" dir file-rpath)
+              (-> (if-let [from (not-empty (.-path file))]
+                    (js/window.apis.copyFileToAssets dir file-rpath from)
+                    (p/let [content (.arrayBuffer file)]
+                      (fs/write-file! repo repo-dir file-rpath content {:skip-compare? true})))
+                  (p/then
+                   (fn [dest]
+                     [file-rpath
+                      (if (string? dest) (js/File. #js[] dest) file)
+                      (path/path-join dir file-rpath)
+                      matched-alias]))
+                  (p/catch #(js/console.error "Debug: Copy Asset Error#" %))))
+
+          (->
+           (p/do! (js/console.debug "Debug: Writing Asset #" dir file-rpath)
+                  (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))
+
+                    :else                ; nfs
+                    (fs/write-file! repo dir file-rpath (.stream file) nil))
+                  [file-rpath file (path/path-join dir file-rpath) matched-alias])
+           (p/catch (fn [error]
+                      (prn :paste-file-error)
+                      (js/console.error error))))))))))
+
+;; assets/journals_2021_02_03_1612350230540_0.png
+(defn resolve-relative-path
+  "Relative path to current file path.
+
+   Requires editing state"
+  [file-path]
+  (if-let [current-file-rpath (or (db-model/get-block-file-path (state/get-edit-block))
+                                  ;; fix dummy file path of page
+                                  (when (config/get-pages-directory)
+                                    (path/path-join (config/get-pages-directory) "_.md"))
+                                  "pages/contents.md")]
+    (let [repo-dir (config/get-repo-dir (state/get-current-repo))
+          current-file-fpath (path/path-join repo-dir current-file-rpath)]
+      (path/get-relative-path current-file-fpath file-path))
+    file-path))
+
+(defn file-upload-assets!
+  "Paste asset for file graph and insert link to current editing block"
+  [repo id ^js files format uploading? *asset-uploading? *asset-uploading-process drop-or-paste?]
+  (-> (file-based-save-assets! repo (js->clj files))
+      ;; FIXME: only the first asset is handled
+      (p/then
+       (fn [res]
+         (when-let [[asset-file-name file-obj asset-file-fpath matched-alias] (first res)]
+           (let [image? (config/ext-of-image? asset-file-name)]
+             (editor-common-handler/insert-command!
+              id
+              (assets-handler/get-asset-file-link format
+                                                  (if matched-alias
+                                                    (str
+                                                     (if image? "../assets/" "")
+                                                     "@" (:name matched-alias) "/" asset-file-name)
+                                                    (resolve-relative-path (or asset-file-fpath asset-file-name)))
+                                                  (if file-obj (.-name file-obj) (if image? "image" "asset"))
+                                                  image?)
+              format
+              {:last-pattern (if drop-or-paste? "" commands/command-trigger)
+               :restore?     true
+               :command      :insert-asset})
+             (recur (rest res))))))
+      (p/catch (fn [e]
+                 (js/console.error e)))
+      (p/finally
+        (fn []
+          (reset! uploading? false)
+          (reset! *asset-uploading? false)
+          (reset! *asset-uploading-process 0)))))