fix copying non-png images

enhance/block-order-list
Konstantinos Kaloutas 2024-08-02 20:09:59 +03:00 committed by Gabriel Horner
parent f74e5fbad7
commit 6c05ca3f65
1 changed files with 28 additions and 2 deletions

View File

@ -1417,6 +1417,31 @@
(.append (.-head js/document) link))
(resolve))))))))
#?(:cljs
(defn image-blob->png
[blob cb]
(let [image (js/Image.)
off-canvas (js/document.createElement "canvas")
data-url (js/URL.createObjectURL blob)
ctx (.getContext off-canvas "2d")]
(set! (.-onload image)
#(let [width (.-width image)
height (.-height image)]
(set! (.-width off-canvas) width)
(set! (.-height off-canvas) height)
(.drawImage ctx image 0 0 width height)
(.toBlob off-canvas cb)))
(set! (.-src image) data-url))))
#?(:cljs
(defn write-blob-to-clipboard
[blob]
(->> blob
(js-obj (.-type blob))
(js/ClipboardItem.)
(array)
(js/navigator.clipboard.write))))
#?(:cljs
(defn copy-image-to-clipboard
[src]
@ -1424,10 +1449,11 @@
(.then (fn [data]
(-> (.blob data)
(.then (fn [blob]
(js/navigator.clipboard.write (clj->js [(js/ClipboardItem. (clj->js {(.-type blob) blob}))]))))
(if (= (.-type blob) "image/png")
(write-blob-to-clipboard blob)
(image-blob->png blob write-blob-to-clipboard))))
(.catch js/console.error)))))))
(defn memoize-last
"Different from core.memoize, it only cache the last result.
Returns a memoized version of a referentially transparent function. The