mirror of
https://github.com/penpot/penpot.git
synced 2025-01-09 00:10:11 -05:00
♻️ Refactor trigger download
This commit is contained in:
parent
641e4080bc
commit
68d287ed82
3 changed files with 32 additions and 24 deletions
|
@ -34,7 +34,7 @@
|
|||
(fn [{:keys [status body] :as response}]
|
||||
(js/console.log status body)
|
||||
(if (= status 200)
|
||||
(we/trigger-download (:name shape) body)
|
||||
(dom/trigger-download (:name shape) body)
|
||||
(st/emit! (dm/error (t locale "errors.unexpected-error")))))
|
||||
(constantly nil)
|
||||
(fn []
|
||||
|
|
|
@ -29,21 +29,6 @@
|
|||
:name (:name shape)
|
||||
:exports exports}))
|
||||
|
||||
(defn- trigger-download
|
||||
[filename blob]
|
||||
(let [link (dom/create-element "a")
|
||||
uri (dom/create-uri blob)
|
||||
extension (dom/mtype->extension (.-type ^js blob))
|
||||
filename (if extension
|
||||
(str filename "." extension)
|
||||
filename)]
|
||||
(obj/set! link "href" uri)
|
||||
(obj/set! link "download" filename)
|
||||
(obj/set! (.-style ^js link) "display" "none")
|
||||
(.appendChild (.-body ^js js/document) link)
|
||||
(.click link)
|
||||
(.remove link)))
|
||||
|
||||
(mf/defc exports-menu
|
||||
[{:keys [shape page-id file-id] :as props}]
|
||||
(let [locale (mf/deref i18n/locale)
|
||||
|
@ -64,7 +49,7 @@
|
|||
(->> (request-export (assoc shape :page-id page-id :file-id file-id) exports)
|
||||
(rx/subs
|
||||
(fn [body]
|
||||
(trigger-download filename body))
|
||||
(dom/trigger-download filename body))
|
||||
(fn [error]
|
||||
(swap! loading? not)
|
||||
(st/emit! (dm/error (tr "errors.unexpected-error"))))
|
||||
|
|
|
@ -54,6 +54,10 @@
|
|||
[id]
|
||||
(dom/getElement id))
|
||||
|
||||
(defn get-elements-by-tag
|
||||
[node tag]
|
||||
(.getElementsByTagName node tag))
|
||||
|
||||
(defn stop-propagation
|
||||
[e]
|
||||
(when e
|
||||
|
@ -279,13 +283,14 @@
|
|||
(defn mtype->extension [mtype]
|
||||
;; https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
|
||||
(case mtype
|
||||
"image/apng" "apng"
|
||||
"image/avif" "avif"
|
||||
"image/gif" "gif"
|
||||
"image/jpeg" "jpg"
|
||||
"image/png" "png"
|
||||
"image/svg+xml" "svg"
|
||||
"image/webp" "webp"
|
||||
"image/apng" "apng"
|
||||
"image/avif" "avif"
|
||||
"image/gif" "gif"
|
||||
"image/jpeg" "jpg"
|
||||
"image/png" "png"
|
||||
"image/svg+xml" "svg"
|
||||
"image/webp" "webp"
|
||||
"application/zip" "zip"
|
||||
nil))
|
||||
|
||||
(defn set-attribute [^js node ^string attr value]
|
||||
|
@ -311,3 +316,21 @@
|
|||
(>= (.-left rect) 0)
|
||||
(<= (.-bottom rect) height)
|
||||
(<= (.-right rect) width))))
|
||||
|
||||
(defn trigger-download-uri
|
||||
[filename mtype uri]
|
||||
(let [link (create-element "a")
|
||||
extension (mtype->extension mtype)
|
||||
filename (if extension
|
||||
(str filename "." extension)
|
||||
filename)]
|
||||
(obj/set! link "href" uri)
|
||||
(obj/set! link "download" filename)
|
||||
(obj/set! (.-style ^js link) "display" "none")
|
||||
(.appendChild (.-body ^js js/document) link)
|
||||
(.click link)
|
||||
(.remove link)))
|
||||
|
||||
(defn trigger-download
|
||||
[filename blob]
|
||||
(trigger-download-uri filename (.-type ^js blob) (dom/create-uri blob)))
|
||||
|
|
Loading…
Reference in a new issue