0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-24 15:39:50 -05:00

♻️ Refactor trigger download

This commit is contained in:
alonso.torres 2021-05-25 15:56:51 +02:00
parent 641e4080bc
commit 68d287ed82
3 changed files with 32 additions and 24 deletions

View file

@ -34,7 +34,7 @@
(fn [{:keys [status body] :as response}] (fn [{:keys [status body] :as response}]
(js/console.log status body) (js/console.log status body)
(if (= status 200) (if (= status 200)
(we/trigger-download (:name shape) body) (dom/trigger-download (:name shape) body)
(st/emit! (dm/error (t locale "errors.unexpected-error"))))) (st/emit! (dm/error (t locale "errors.unexpected-error")))))
(constantly nil) (constantly nil)
(fn [] (fn []

View file

@ -29,21 +29,6 @@
:name (:name shape) :name (:name shape)
:exports exports})) :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 (mf/defc exports-menu
[{:keys [shape page-id file-id] :as props}] [{:keys [shape page-id file-id] :as props}]
(let [locale (mf/deref i18n/locale) (let [locale (mf/deref i18n/locale)
@ -64,7 +49,7 @@
(->> (request-export (assoc shape :page-id page-id :file-id file-id) exports) (->> (request-export (assoc shape :page-id page-id :file-id file-id) exports)
(rx/subs (rx/subs
(fn [body] (fn [body]
(trigger-download filename body)) (dom/trigger-download filename body))
(fn [error] (fn [error]
(swap! loading? not) (swap! loading? not)
(st/emit! (dm/error (tr "errors.unexpected-error")))) (st/emit! (dm/error (tr "errors.unexpected-error"))))

View file

@ -54,6 +54,10 @@
[id] [id]
(dom/getElement id)) (dom/getElement id))
(defn get-elements-by-tag
[node tag]
(.getElementsByTagName node tag))
(defn stop-propagation (defn stop-propagation
[e] [e]
(when e (when e
@ -286,6 +290,7 @@
"image/png" "png" "image/png" "png"
"image/svg+xml" "svg" "image/svg+xml" "svg"
"image/webp" "webp" "image/webp" "webp"
"application/zip" "zip"
nil)) nil))
(defn set-attribute [^js node ^string attr value] (defn set-attribute [^js node ^string attr value]
@ -311,3 +316,21 @@
(>= (.-left rect) 0) (>= (.-left rect) 0)
(<= (.-bottom rect) height) (<= (.-bottom rect) height)
(<= (.-right rect) width)))) (<= (.-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)))