0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-23 23:18:48 -05:00

Improve icon rendering.

This commit is contained in:
Andrey Antukh 2015-12-26 15:18:36 +02:00
parent 50c0b9420b
commit 517b3f6db3
2 changed files with 34 additions and 10 deletions

View file

@ -1,17 +1,35 @@
(ns uxbox.shapes
(:require [sablono.core :refer-macros [html]]))
(:require [sablono.core :refer-macros [html]]
[uxbox.util.data :refer (remove-nil-vals)]))
(defmulti render
(fn [shape & params]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Api
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmulti -render
(fn [shape attrs]
(:type shape)))
(defmethod render :builtin/icon
[{:keys [data width height view-box]} & [attrs]]
(let [attrs (merge
(when width {:width width})
(when height {:height height})
(when view-box {:viewBox (apply str (interpose " " view-box))})
attrs)]
(defn render
([shape] (-render shape nil))
([shape attrs] (-render shape attrs)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Implementation
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn transform-attrs
[{:keys [view-box] :as data}]
(if view-box
(assoc data :view-box (apply str (interpose " " view-box)))
data))
(defmethod -render :builtin/icon
[{:keys [data width height view-box] :as shape} attrs]
(let [attrs (as-> shape $
(select-keys $ [:width :height :view-box])
(remove-nil-vals $)
(merge $ attrs)
(transform-attrs $))]
(html
[:svg attrs data])))

View file

@ -11,3 +11,9 @@
(persistent! data)))
(def ^:static index-by-id #(index-by % :id))
(defn remove-nil-vals
"Given a map, return a map removing key-value
pairs when value is `nil`."
[data]
(into {} (remove (comp nil? second) data)))