Przeglądaj źródła

fix: asset file link in markdown shouldn't prefixed with !

Tienson Qin 4 lat temu
rodzic
commit
83fda19660

+ 3 - 3
src/main/frontend/components/editor.cljs

@@ -367,7 +367,7 @@
                      (add-watch editor-handler/*asset-pending-file ::pending-asset
                                 (fn [_ _ _ f]
                                   (reset! *slash-caret-pos (util/get-caret-pos (gdom/getElement id)))
-                                  (editor-handler/upload-image id #js[f] format editor-handler/*asset-uploading? true))))
+                                  (editor-handler/upload-asset id #js[f] format editor-handler/*asset-uploading? true))))
                    state)
    :will-unmount (fn [state]
                    (remove-watch editor-handler/*asset-pending-file ::pending-asset))}
@@ -378,7 +378,7 @@
      :type      "file"
      :on-change (fn [e]
                   (let [files (.-files (.-target e))]
-                    (editor-handler/upload-image id files format editor-handler/*asset-uploading? false)))
+                    (editor-handler/upload-asset id files format editor-handler/*asset-uploading? false)))
      :hidden    true}]
    (when-let [uploading? (util/react editor-handler/*asset-uploading?)]
      (let [processing (util/react editor-handler/*asset-uploading-process)]
@@ -655,7 +655,7 @@
                         input
                         :upload-images
                         {:drop (fn [e files]
-                                 (editor-handler/upload-image id files format editor-handler/*asset-uploading? true))}))
+                                 (editor-handler/upload-asset id files format editor-handler/*asset-uploading? true))}))
 
                                     ;; Here we delay this listener, otherwise the click to edit event will trigger a outside click event,
                                     ;; which will hide the editor so no way for editing.

+ 14 - 12
src/main/frontend/handler/editor.cljs

@@ -1518,10 +1518,10 @@
                                      true)]
       (commands/restore-state restore-slash-caret-pos?))))
 
-(defn- get-image-link
-  [format url file-name]
+(defn- get-asset-file-link
+  [format url file-name image?]
   (case (keyword format)
-    :markdown (util/format "![%s](%s)" file-name url)
+    :markdown (util/format (str (when image? "!") "[%s](%s)") file-name url)
     :org (util/format "[[%s][%s]]" url file-name)
     nil))
 
@@ -1599,7 +1599,7 @@
                             (string/replace #"^../" "/")
                             (string/replace #"^assets://" ""))) nil))))
 
-(defn upload-image
+(defn upload-asset
   [id ^js files format uploading? drop-or-paste?]
   (let [repo (state/get-current-repo)
         block (state/get-edit-block)]
@@ -1608,13 +1608,15 @@
           (p/then
            (fn [res]
              (when-let [[url file] (and (seq res) (first res))]
-               (insert-command!
-                id
-                (get-image-link format (get-asset-link url)
-                                (if file (.-name file) (if (util/ext-of-image? url) "image" "asset")))
-                format
-                {:last-pattern (if drop-or-paste? "" commands/slash)
-                 :restore?     true}))))
+               (let [image? (util/ext-of-image? url)]
+                 (insert-command!
+                  id
+                  (get-asset-file-link format (get-asset-link url)
+                                       (if file (.-name file) (if image? "image" "asset"))
+                                       image?)
+                  format
+                  {:last-pattern (if drop-or-paste? "" commands/slash)
+                   :restore?     true})))))
           (p/finally
             (fn []
               (reset! uploading? false)
@@ -1628,7 +1630,7 @@
            uploading?
            (fn [signed-url]
             (insert-command! id
-                             (get-image-link format signed-url file-name)
+                             (get-asset-file-link format signed-url file-name true)
                              format
                              {:last-pattern (if drop-or-paste? "" commands/slash)
                               :restore?     true})