mirror of
https://github.com/penpot/penpot.git
synced 2025-08-07 14:38:33 +02:00
Improved performance for obtaining the image size.
This commit is contained in:
parent
dcb35084a1
commit
c38079e03b
1 changed files with 12 additions and 13 deletions
|
@ -7,7 +7,8 @@
|
||||||
(ns uxbox.util.dom.files
|
(ns uxbox.util.dom.files
|
||||||
"A interop helpers for work with files."
|
"A interop helpers for work with files."
|
||||||
(:require [beicon.core :as rx]
|
(:require [beicon.core :as rx]
|
||||||
[cuerdas.core :as str]))
|
[cuerdas.core :as str]
|
||||||
|
[uxbox.util.blob :as blob]))
|
||||||
|
|
||||||
(defn read-as-text
|
(defn read-as-text
|
||||||
[file]
|
[file]
|
||||||
|
@ -27,17 +28,15 @@
|
||||||
(.readAsDataURL fr file))
|
(.readAsDataURL fr file))
|
||||||
(constantly nil))))
|
(constantly nil))))
|
||||||
|
|
||||||
(defn- retrieve-image-size
|
|
||||||
[data]
|
|
||||||
(rx/create
|
|
||||||
(fn [sick]
|
|
||||||
(let [img (js/Image.)]
|
|
||||||
(aset img "onload" #(sick (rx/end [(.-width img) (.-height img)])))
|
|
||||||
(set! (.-src img) data))
|
|
||||||
(constantly nil))))
|
|
||||||
|
|
||||||
(defn get-image-size
|
(defn get-image-size
|
||||||
"Get the real image size."
|
|
||||||
[file]
|
[file]
|
||||||
(->> (read-as-dataurl file)
|
(letfn [(on-load [sink img]
|
||||||
(rx/flat-map retrieve-image-size)))
|
(let [size [(.-width img) (.-height img)]]
|
||||||
|
(sink (rx/end size))))
|
||||||
|
(on-subscribe [sink]
|
||||||
|
(let [img (js/Image.)
|
||||||
|
uri (blob/create-uri file)]
|
||||||
|
(set! (.-onload img) (partial on-load sink img))
|
||||||
|
(set! (.-src img) uri)
|
||||||
|
#(blob/revoke-uri uri)))]
|
||||||
|
(rx/create on-subscribe)))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue