0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-12 07:41:43 -05:00

Merge pull request #3127 from penpot/niwinz-bugfixes-6

🐛 Fix inconsistencies on drop datauri images (mainly for firefox …
This commit is contained in:
Alejandro 2023-04-13 09:16:17 +02:00 committed by GitHub
commit 0f9b50de50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 9 deletions

View file

@ -26,6 +26,7 @@
[app.util.keyboard :as kbd] [app.util.keyboard :as kbd]
[app.util.object :as obj] [app.util.object :as obj]
[app.util.timers :as timers] [app.util.timers :as timers]
[app.util.webapi :as wapi]
[beicon.core :as rx] [beicon.core :as rx]
[cuerdas.core :as str] [cuerdas.core :as str]
[rumext.v2 :as mf])) [rumext.v2 :as mf]))
@ -434,7 +435,9 @@
(:id component) (:id component)
(gpt/point final-x final-y)))) (gpt/point final-x final-y))))
;; Will trigger when the user drags an image from a browser to the viewport (firefox and chrome do it a bit different depending on the origin) ;; Will trigger when the user drags an image from a browser
;; to the viewport (firefox and chrome do it a bit different
;; depending on the origin)
(dnd/has-type? event "Files") (dnd/has-type? event "Files")
(let [files (dnd/get-files event) (let [files (dnd/get-files event)
params {:file-id (:id file) params {:file-id (:id file)
@ -442,16 +445,19 @@
:blobs (seq files)}] :blobs (seq files)}]
(st/emit! (dwm/upload-media-workspace params))) (st/emit! (dwm/upload-media-workspace params)))
;; Will trigger when the user drags an image from a browser to the viewport (firefox and chrome do it a bit different depending on the origin) ;; Will trigger when the user drags an image (usually rendered as datauri) from a
;; browser to the viewport (mainly on firefox, all depending on the origin of the
;; drag event).
(dnd/has-type? event "text/uri-list") (dnd/has-type? event "text/uri-list")
(let [data (dnd/get-data event "text/uri-list") (let [data (dnd/get-data event "text/uri-list")
lines (str/lines data) lines (str/lines data)
uris (filter #(and (not (str/blank? %)) uris (->> lines (filter #(str/starts-with? % "http")))
(not (str/starts-with? % "#"))) data (->> lines (filter #(str/starts-with? % "data:image/")))
lines)
params {:file-id (:id file) params {:file-id (:id file)
:position viewport-coord :position viewport-coord}
:uris uris}] params (if (seq uris)
(assoc params :uris uris)
(assoc params :blobs (map wapi/data-uri->blob data)))]
(st/emit! (dwm/upload-media-workspace params))) (st/emit! (dwm/upload-media-workspace params)))
;; Will trigger when the user drags an SVG asset from the assets panel ;; Will trigger when the user drags an SVG asset from the assets panel

View file

@ -60,6 +60,23 @@
(assert (blob? b) "invalid arguments") (assert (blob? b) "invalid arguments")
(js/URL.createObjectURL b)) (js/URL.createObjectURL b))
(defn data-uri?
[s]
(str/starts-with? s "data:"))
(defn data-uri->blob
[data-uri]
(let [[mtype b64-data] (str/split data-uri ";base64,")
mtype (subs mtype (inc (str/index-of mtype ":")))
decoded (.atob js/window b64-data)
size (.-length ^js decoded)
content (js/Uint8Array. size)]
(doseq [i (range 0 size)]
(aset content i (.charCodeAt ^js decoded i)))
(create-blob content mtype)))
(defn write-to-clipboard (defn write-to-clipboard
[data] [data]
(assert (string? data) "`data` should be string") (assert (string? data) "`data` should be string")