mirror of
https://github.com/penpot/penpot.git
synced 2025-02-03 21:09:00 -05:00
Reimplement shapes rendering (using new matrix based transformations).
This commit is contained in:
parent
68c153ac5d
commit
7fa7213e77
1 changed files with 41 additions and 46 deletions
|
@ -2,61 +2,56 @@
|
||||||
"A ui related implementation for uxbox.shapes ns."
|
"A ui related implementation for uxbox.shapes ns."
|
||||||
(:require [sablono.core :refer-macros [html]]
|
(:require [sablono.core :refer-macros [html]]
|
||||||
[cuerdas.core :as str]
|
[cuerdas.core :as str]
|
||||||
|
[uxbox.state :as st]
|
||||||
[uxbox.shapes :as shapes]
|
[uxbox.shapes :as shapes]
|
||||||
|
[uxbox.svg :as svg]
|
||||||
[uxbox.util.data :refer (remove-nil-vals)]))
|
[uxbox.util.data :refer (remove-nil-vals)]))
|
||||||
|
|
||||||
(defn- transform-attr
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
[data acc key value]
|
;; Attribute transformations
|
||||||
(case key
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
:view-box
|
|
||||||
(assoc! acc key (apply str (interpose " " value)))
|
|
||||||
|
|
||||||
:lock
|
|
||||||
(assoc! acc :preserveAspectRatio (if value "xMidYMid" "none"))
|
|
||||||
|
|
||||||
:rotation
|
|
||||||
(let [center-x (+ (:x data) (/ (:width data) 2))
|
|
||||||
center-y (+ (:y data) (/ (:height data) 2))]
|
|
||||||
(assoc! acc :transform (str/format "rotate(%s %s %s)"
|
|
||||||
value center-x center-y)))
|
|
||||||
|
|
||||||
(assoc! acc key value)))
|
|
||||||
|
|
||||||
(defn- transform-attrs
|
|
||||||
[data]
|
|
||||||
(persistent!
|
|
||||||
(reduce-kv (partial transform-attr data)
|
|
||||||
(transient {})
|
|
||||||
data)))
|
|
||||||
|
|
||||||
(defn- extract-attrs
|
(defn- extract-attrs
|
||||||
"Extract predefinet attrs from shapes."
|
"Extract predefinet attrs from shapes."
|
||||||
[shape]
|
[shape]
|
||||||
(select-keys shape [:rotation :lock :width :height
|
(select-keys shape [:rotation :width :height
|
||||||
:view-box :x :y :cx :cy :fill
|
:x :y :opacity :fill]))
|
||||||
:opacity]))
|
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
;; Implementation
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
(defmethod shapes/-render :builtin/icon
|
(defmethod shapes/-render :builtin/icon
|
||||||
[{:keys [data id] :as shape} attrs]
|
[{:keys [data id] :as shape} attrs]
|
||||||
(let [attrs (as-> shape $
|
(let [key (str "use-" id)
|
||||||
(extract-attrs $)
|
transform (-> (merge shape attrs)
|
||||||
(remove-nil-vals $)
|
(svg/calculate-transform))
|
||||||
(merge $ attrs {:lock false})
|
attrs {:id key
|
||||||
(transform-attrs $)
|
:key key
|
||||||
(merge $ {:key (str id)}))]
|
:transform transform}]
|
||||||
|
(html
|
||||||
|
[:g attrs data])))
|
||||||
|
|
||||||
|
(defmethod shapes/-render-svg :builtin/icon
|
||||||
|
[{:keys [data id view-box] :as shape} attrs]
|
||||||
|
(let [key (str "icon-svg-" id)
|
||||||
|
view-box (apply str (interpose " " view-box))
|
||||||
|
props {:view-box view-box :id key :key key}
|
||||||
|
attrs (-> shape
|
||||||
|
(extract-attrs)
|
||||||
|
(remove-nil-vals)
|
||||||
|
(merge attrs props))]
|
||||||
|
(html
|
||||||
|
[:svg attrs data])))
|
||||||
|
|
||||||
|
(defmethod shapes/-render :builtin/group
|
||||||
|
[{:keys [items id] :as shape} attrs]
|
||||||
|
(let [key (str "group-" id)
|
||||||
|
tfm (-> (merge shape attrs)
|
||||||
|
(svg/calculate-transform))
|
||||||
|
attrs {:id key :key key :transform tfm}
|
||||||
|
shapes-by-id (get @st/state :shapes-by-id)]
|
||||||
(html
|
(html
|
||||||
[:g attrs
|
[:g attrs
|
||||||
[:svg attrs data]])))
|
(for [item (map #(get shapes-by-id %) items)]
|
||||||
|
(shapes/-render item nil))])))
|
||||||
(defmethod shapes/-render :builtin/icon-svg
|
|
||||||
[{:keys [image id] :as shape} attrs]
|
|
||||||
(let [attrs (as-> shape $
|
|
||||||
(extract-attrs $)
|
|
||||||
(remove-nil-vals $)
|
|
||||||
(merge $ attrs)
|
|
||||||
(transform-attrs $))]
|
|
||||||
(html
|
|
||||||
[:svg (merge attrs {:key (str id)})
|
|
||||||
[:image image]])))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue