🐛 Fix thumbnails being rendered with previous size

This commit is contained in:
Aitor 2023-05-24 12:40:28 +02:00 committed by Alonso Torres
parent fb3655506f
commit 48b0df8e75
3 changed files with 17 additions and 13 deletions

View file

@ -273,7 +273,7 @@
:response-type :blob :response-type :blob
:method :get}) :method :get})
(rx/map :body) (rx/map :body)
(rx/map (fn [blob] (.createObjectURL js/URL blob))))) (rx/map (fn [blob] (wapi/create-uri blob)))))
(defn- fetch-thumbnail-blobs (defn- fetch-thumbnail-blobs
[file-id] [file-id]

View file

@ -38,9 +38,19 @@
[value])) [value]))
(defn- create-svg-blob-uri-from (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 (let [{:keys [x y width height]} rect
viewbox (dm/str x " " y " " width " " height) 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 ;; This is way faster than creating a node
;; through the DOM API ;; through the DOM API
@ -53,8 +63,8 @@
(dom/node->xml node)) (dom/node->xml node))
;; create SVG blob ;; create SVG blob
blob (js/Blob. #js [svg-data] #js {:type "image/svg+xml;charset=utf-8"}) blob (wapi/create-blob svg-data "image/svg+xml;charset=utf-8")
url (dm/str (.createObjectURL js/URL blob) "#svg")] url (dm/str (wapi/create-uri blob) "#svg")]
;; returns the url and the node ;; returns the url and the node
url)) url))
@ -76,13 +86,6 @@
(gsh/selection-rect (concat [shape] all-children)) (gsh/selection-rect (concat [shape] all-children))
(-> shape :points gsh/points->selrect)) (-> 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) svg-uri* (mf/use-state nil)
bitmap-uri* (mf/use-state nil) bitmap-uri* (mf/use-state nil)
observer* (mf/use-var nil) observer* (mf/use-var nil)
@ -131,7 +134,7 @@
(if (dom/has-children? node) (if (dom/has-children? node)
;; The frame-content need to have children in order to generate the thumbnail ;; The frame-content need to have children in order to generate the thumbnail
(let [style-node (dom/query (dm/str "#frame-container-" id " style")) (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)) (reset! svg-uri* url))
;; Node not yet ready, we schedule a new generation ;; Node not yet ready, we schedule a new generation

View file

@ -15,6 +15,7 @@
[app.main.render :as render] [app.main.render :as render]
[app.util.http :as http] [app.util.http :as http]
[app.util.time :as ts] [app.util.time :as ts]
[app.util.webapi :as wapi]
[app.worker.impl :as impl] [app.worker.impl :as impl]
[beicon.core :as rx] [beicon.core :as rx]
[debug :refer [debug?]] [debug :refer [debug?]]
@ -143,7 +144,7 @@
(->> (.convertToBlob ^js canvas #js {:type "image/png"}) (->> (.convertToBlob ^js canvas #js {:type "image/png"})
(p/fmap (fn [blob] (p/fmap (fn [blob]
{:result (.createObjectURL js/URL blob)})) {:result (wapi/create-uri blob)}))
(p/fnly (fn [_] (p/fnly (fn [_]
(log/debug :hint "generated thumbnail" :elapsed (dm/str (tp) "ms")) (log/debug :hint "generated thumbnail" :elapsed (dm/str (tp) "ms"))
(.close ^js ibpm)))))) (.close ^js ibpm))))))