0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-10 08:50:57 -05:00

Upload fill-image data

This commit is contained in:
alonso.torres 2021-06-14 17:04:14 +02:00
parent 8952cb4e00
commit 9fc633080a
2 changed files with 61 additions and 22 deletions

View file

@ -30,14 +30,28 @@
(and (vector? node) (and (vector? node)
(= ::close (first node)))) (= ::close (first node))))
(defn find-node
[node tag]
(when (some? node)
(->> node :content (d/seek #(= (:tag %) tag)))))
(defn find-node-by-id
[id coll]
(->> coll (d/seek #(= id (-> % :attrs :id)))))
(defn find-all-nodes
[node tag]
(when (some? node)
(->> node :content (filterv #(= (:tag %) :defs)))))
(defn get-data (defn get-data
([node] ([node]
(->> node :content (d/seek #(or (= :penpot:shape (:tag %)) (or (find-node node :penpot:shape)
(= :penpot:page (:tag %)))))) (find-node node :penpot:page)))
([node tag]
(->> (get-data node) ([node tag]
:content (-> (get-data node)
(d/seek #(= tag (:tag %)))))) (find-node tag))))
(defn get-type (defn get-type
[node] [node]
@ -184,6 +198,7 @@
selrect (gsh/content->selrect content-tr) selrect (gsh/content->selrect content-tr)
points (-> (gsh/rect->points selrect) points (-> (gsh/rect->points selrect)
(gsh/transform-points center transform))] (gsh/transform-points center transform))]
(-> props (-> props
(assoc :content content) (assoc :content content)
(assoc :selrect selrect) (assoc :selrect selrect)
@ -202,9 +217,6 @@
(def url-regex #"url\(#([^\)]*)\)") (def url-regex #"url\(#([^\)]*)\)")
(defn seek-node
[id coll]
(->> coll (d/seek #(= id (-> % :attrs :id)))))
(defn parse-stops (defn parse-stops
[gradient-node] [gradient-node]
@ -219,7 +231,7 @@
(defn parse-gradient (defn parse-gradient
[node ref-url] [node ref-url]
(let [[_ url] (re-find url-regex ref-url) (let [[_ url] (re-find url-regex ref-url)
gradient-node (->> node (node-seq) (seek-node url)) gradient-node (->> node (node-seq) (find-node-by-id url))
stops (parse-stops gradient-node)] stops (parse-stops gradient-node)]
(when (contains? (:attrs gradient-node) :penpot:gradient) (when (contains? (:attrs gradient-node) :penpot:gradient)
@ -276,14 +288,18 @@
(assoc :name name) (assoc :name name)
(assoc :blocked blocked) (assoc :blocked blocked)
(assoc :hidden hidden) (assoc :hidden hidden)
(assoc :transform transform)
(assoc :transform-inverse transform-inverse)
(assoc :flip-x flip-x) (assoc :flip-x flip-x)
(assoc :flip-y flip-y) (assoc :flip-y flip-y)
(assoc :proportion proportion) (assoc :proportion proportion)
(assoc :proportion-lock proportion-lock) (assoc :proportion-lock proportion-lock)
(assoc :rotation rotation) (assoc :rotation rotation)
(cond-> (some? transform)
(assoc :transform transform))
(cond-> (some? transform-inverse)
(assoc :transform-inverse transform-inverse))
(cond-> (some? constraints-h) (cond-> (some? constraints-h)
(assoc :constraints-h constraints-h)) (assoc :constraints-h constraints-h))
@ -378,12 +394,17 @@
(assoc :rx rx :ry ry)))) (assoc :rx rx :ry ry))))
(defn add-image-data (defn add-image-data
[props node] [props type node]
(-> props (let [metadata {:id (get-meta node :media-id)
(assoc-in [:metadata :id] (get-meta node :media-id)) :width (get-meta node :media-width)
(assoc-in [:metadata :width] (get-meta node :media-width)) :height (get-meta node :media-height)
(assoc-in [:metadata :height] (get-meta node :media-height)) :mtype (get-meta node :media-mtype)}]
(assoc-in [:metadata :mtype] (get-meta node :media-mtype)))) (cond-> props
(= type :image)
(assoc :metadata metadata)
(not= type :image)
(assoc :fill-image metadata))))
(defn add-text-data (defn add-text-data
[props node] [props node]
@ -570,13 +591,30 @@
(not (empty? grids)) (not (empty? grids))
(assoc :grids grids)))) (assoc :grids grids))))
(defn has-image?
[type node]
(let [pattern-image
(-> node
(find-node :defs)
(find-node :pattern)
(find-node :image))]
(or (= type :image)
(some? pattern-image))))
(defn get-image-name (defn get-image-name
[node] [node]
(get-in node [:attrs :penpot:name])) (get-in node [:attrs :penpot:name]))
(defn get-image-data (defn get-image-data
[node] [node]
(let [svg-data (get-svg-data :image node)] (let [pattern-data
(-> node
(find-node :defs)
(find-node :pattern)
(find-node :image)
:attrs)
image-data (get-svg-data :image node)
svg-data (or image-data pattern-data)]
(:xlink:href svg-data))) (:xlink:href svg-data)))
(defn parse-data (defn parse-data
@ -607,8 +645,8 @@
(cond-> (= :rect type) (cond-> (= :rect type)
(add-rect-data node svg-data)) (add-rect-data node svg-data))
(cond-> (= :image type) (cond-> (some? (get-in node [:attrs :penpot:media-id]))
(add-image-data node)) (add-image-data type node))
(cond-> (= :text type) (cond-> (= :text type)
(add-text-data node)))))) (add-text-data node))))))

View file

@ -117,7 +117,8 @@
(defn resolve-images (defn resolve-images
[file-id node] [file-id node]
(if (and (cip/shape? node) (= (cip/get-type node) :image) (not (cip/close? node))) (if (and (not (cip/close? node))
(cip/has-image? type node))
(let [name (cip/get-image-name node) (let [name (cip/get-image-name node)
data-uri (cip/get-image-data node)] data-uri (cip/get-image-data node)]
(->> (upload-media-files file-id name data-uri) (->> (upload-media-files file-id name data-uri)