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])))