Ver código fonte

fix(electron): fix relative asset path from current page file path

charlie 4 anos atrás
pai
commit
3981224616

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

@@ -380,7 +380,7 @@
 (rum/defc asset-reference
   [title path]
   (let [repo-path (config/get-repo-dir (state/get-current-repo))
-        full-path (str repo-path (string/replace path "../" "/"))]
+        full-path (.. util/node-path (join repo-path (config/get-local-asset-absolute-path path)))]
     [:a.asset-ref {:target "_blank" :href full-path} (or title path)]))
 
 (rum/defc page-reference < rum/reactive

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

@@ -1565,14 +1565,14 @@
         (if (util/electron?)
           (let [from (.-path file)]
             (p/then (js/window.apis.copyFileToAssets dir filename from)
-                    #(p/resolved [filename (if (string? %) (js/File. #js[] %) file)])))
+                    #(p/resolved [filename (if (string? %) (js/File. #js[] %) file) (.join util/node-path dir filename)])))
           (p/then (fs/write-file! repo dir filename (.stream file) nil)
                   #(p/resolved [filename file]))))))))
 
 (defonce *assets-url-cache (atom {}))
 
 (defn make-asset-url
-  [path]                                                    ;; path start with "/assets" or compatible for "../assets"
+  [path] ;; path start with "/assets" or compatible for "../assets"
   (let [repo-dir (config/get-repo-dir (state/get-current-repo))
         path (string/replace path "../" "/")]
     (if (util/electron?)
@@ -1622,11 +1622,11 @@
       (-> (save-assets! block repo (js->clj files))
           (p/then
            (fn [res]
-             (when-let [[url file] (and (seq res) (first res))]
-               (let [image? (util/ext-of-image? url)]
+             (when-let [[asset-file-name file full-file-path] (and (seq res) (first res))]
+               (let [image? (util/ext-of-image? asset-file-name)]
                  (insert-command!
                   id
-                  (get-asset-file-link format (resolve-relative-path url)
+                  (get-asset-file-link format (resolve-relative-path (or full-file-path asset-file-name))
                                        (if file (.-name file) (if image? "image" "asset"))
                                        image?)
                   format

+ 2 - 1
src/main/frontend/util.cljc

@@ -8,6 +8,7 @@
       #?(:cljs ["/frontend/caret_pos" :as caret-pos])
       #?(:cljs ["/frontend/selection" :as selection])
       #?(:cljs ["/frontend/utils" :as utils])
+      #?(:cljs ["path" :as nodePath])
       #?(:cljs [goog.dom :as gdom])
       #?(:cljs [goog.object :as gobj])
       #?(:cljs [goog.string :as gstring])
@@ -29,7 +30,7 @@
       (-pr-writer [sym writer _]
         (-write writer (str "\"" (.toString sym) "\"")))))
 
-;; doms
+#?(:cljs (defonce ^js node-path nodePath))
 #?(:cljs (defn app-scroll-container-node []  js/document.documentElement))
 
 #?(:cljs