From 68d287ed821c9ea2977349f641a2d7b44ab33409 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Tue, 25 May 2021 15:56:51 +0200 Subject: [PATCH] :recycle: Refactor trigger download --- frontend/src/app/main/ui/handoff/exports.cljs | 2 +- .../sidebar/options/menus/exports.cljs | 17 +-------- frontend/src/app/util/dom.cljs | 37 +++++++++++++++---- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/frontend/src/app/main/ui/handoff/exports.cljs b/frontend/src/app/main/ui/handoff/exports.cljs index 6462a8101..a8a36b2bf 100644 --- a/frontend/src/app/main/ui/handoff/exports.cljs +++ b/frontend/src/app/main/ui/handoff/exports.cljs @@ -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 [] diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/menus/exports.cljs b/frontend/src/app/main/ui/workspace/sidebar/options/menus/exports.cljs index 65f05ff67..dd62aef45 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/options/menus/exports.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/options/menus/exports.cljs @@ -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")))) diff --git a/frontend/src/app/util/dom.cljs b/frontend/src/app/util/dom.cljs index 7601435ca..5864e285c 100644 --- a/frontend/src/app/util/dom.cljs +++ b/frontend/src/app/util/dom.cljs @@ -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)))