From 48b0df8e7547a84c0343cea865ef9538dd5e4226 Mon Sep 17 00:00:00 2001 From: Aitor Date: Wed, 24 May 2023 12:40:28 +0200 Subject: [PATCH] :bug: Fix thumbnails being rendered with previous size --- frontend/src/app/main/data/workspace.cljs | 2 +- .../shapes/frame/thumbnail_render.cljs | 25 +++++++++++-------- frontend/src/app/worker/thumbnails.cljs | 3 ++- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/frontend/src/app/main/data/workspace.cljs b/frontend/src/app/main/data/workspace.cljs index 38b3ad16e3..d6177bcc1d 100644 --- a/frontend/src/app/main/data/workspace.cljs +++ b/frontend/src/app/main/data/workspace.cljs @@ -273,7 +273,7 @@ :response-type :blob :method :get}) (rx/map :body) - (rx/map (fn [blob] (.createObjectURL js/URL blob))))) + (rx/map (fn [blob] (wapi/create-uri blob))))) (defn- fetch-thumbnail-blobs [file-id] diff --git a/frontend/src/app/main/ui/workspace/shapes/frame/thumbnail_render.cljs b/frontend/src/app/main/ui/workspace/shapes/frame/thumbnail_render.cljs index 3c658bdcba..2b14872f48 100644 --- a/frontend/src/app/main/ui/workspace/shapes/frame/thumbnail_render.cljs +++ b/frontend/src/app/main/ui/workspace/shapes/frame/thumbnail_render.cljs @@ -38,9 +38,19 @@ [value])) (defn- create-svg-blob-uri-from - [fixed-width fixed-height rect node style-node] + [rect node style-node] (let [{:keys [x y width height]} rect viewbox (dm/str x " " y " " width " " height) + + ;; Calculate the fixed width and height + ;; We don't want to generate thumbnails + ;; bigger than 2000px + [fixed-width fixed-height] + (if (> width height) + [(mth/clamp width 250 2000) + (/ (* height (mth/clamp width 250 2000)) width)] + [(/ (* width (mth/clamp height 250 2000)) height) + (mth/clamp height 250 2000)]) ;; This is way faster than creating a node ;; through the DOM API @@ -53,8 +63,8 @@ (dom/node->xml node)) ;; create SVG blob - blob (js/Blob. #js [svg-data] #js {:type "image/svg+xml;charset=utf-8"}) - url (dm/str (.createObjectURL js/URL blob) "#svg")] + blob (wapi/create-blob svg-data "image/svg+xml;charset=utf-8") + url (dm/str (wapi/create-uri blob) "#svg")] ;; returns the url and the node url)) @@ -76,13 +86,6 @@ (gsh/selection-rect (concat [shape] all-children)) (-> shape :points gsh/points->selrect)) - [fixed-width fixed-height] - (if (> width height) - [(mth/clamp width 250 2000) - (/ (* height (mth/clamp width 250 2000)) width)] - [(/ (* width (mth/clamp height 250 2000)) height) - (mth/clamp height 250 2000)]) - svg-uri* (mf/use-state nil) bitmap-uri* (mf/use-state nil) observer* (mf/use-var nil) @@ -131,7 +134,7 @@ (if (dom/has-children? node) ;; The frame-content need to have children in order to generate the thumbnail (let [style-node (dom/query (dm/str "#frame-container-" id " style")) - url (create-svg-blob-uri-from fixed-width fixed-height @shape-bb* node style-node)] + url (create-svg-blob-uri-from @shape-bb* node style-node)] (reset! svg-uri* url)) ;; Node not yet ready, we schedule a new generation diff --git a/frontend/src/app/worker/thumbnails.cljs b/frontend/src/app/worker/thumbnails.cljs index 327d246666..f17f4d85a1 100644 --- a/frontend/src/app/worker/thumbnails.cljs +++ b/frontend/src/app/worker/thumbnails.cljs @@ -15,6 +15,7 @@ [app.main.render :as render] [app.util.http :as http] [app.util.time :as ts] + [app.util.webapi :as wapi] [app.worker.impl :as impl] [beicon.core :as rx] [debug :refer [debug?]] @@ -143,7 +144,7 @@ (->> (.convertToBlob ^js canvas #js {:type "image/png"}) (p/fmap (fn [blob] - {:result (.createObjectURL js/URL blob)})) + {:result (wapi/create-uri blob)})) (p/fnly (fn [_] (log/debug :hint "generated thumbnail" :elapsed (dm/str (tp) "ms")) (.close ^js ibpm))))))