From d71bb9d22d0c2c0fc1244b75d4e49895bb8f0dc0 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Thu, 7 Jan 2016 01:24:11 +0200 Subject: [PATCH] Add implementation for fill shapes. --- frontend/uxbox/data/workspace.cljs | 8 +++ frontend/uxbox/schema.cljs | 11 ++++ frontend/uxbox/ui/shapes.cljs | 2 +- frontend/uxbox/ui/workspace/options.cljs | 66 ++++++++++++++---------- frontend/uxbox/util/data.cljs | 9 ++++ 5 files changed, 69 insertions(+), 27 deletions(-) diff --git a/frontend/uxbox/data/workspace.cljs b/frontend/uxbox/data/workspace.cljs index d656c5777..f3079526e 100644 --- a/frontend/uxbox/data/workspace.cljs +++ b/frontend/uxbox/data/workspace.cljs @@ -201,3 +201,11 @@ (when height {:height height}))] (update-in state [:shapes-by-id sid] shapes/-resize size))))) + +(defn update-shape-color + [sid color] + (sc/valid? sc/color color) + (reify + rs/UpdateEvent + (-apply-update [_ state] + (assoc-in state [:shapes-by-id sid :fill] color)))) diff --git a/frontend/uxbox/schema.cljs b/frontend/uxbox/schema.cljs index d9f355e4e..ee08816c8 100644 --- a/frontend/uxbox/schema.cljs +++ b/frontend/uxbox/schema.cljs @@ -2,6 +2,7 @@ (:refer-clojure :exclude [keyword uuid vector]) (:require [bouncer.core :as b] [bouncer.validators :as v] + [cuerdas.core :as str] [uxbox.shapes :refer (shape?)])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -55,3 +56,13 @@ ([schema data] (when-let [errors (validate schema data)] (throw (ex-info "Invalid data" errors))))) + +(defn valid? + [validator data] + (let [result (validator data)] + (if result + result + (let [message (:default-message-format (meta validator)) + message (str/format message data)] + (throw (ex-info message {})))))) + diff --git a/frontend/uxbox/ui/shapes.cljs b/frontend/uxbox/ui/shapes.cljs index c47e8737b..c0445d251 100644 --- a/frontend/uxbox/ui/shapes.cljs +++ b/frontend/uxbox/ui/shapes.cljs @@ -33,7 +33,7 @@ "Extract predefinet attrs from shapes." [shape] (select-keys shape [:rotation :lock :width :height - :view-box :x :y :cx :cy])) + :view-box :x :y :cx :cy :fill])) (defmethod shapes/-render :builtin/icon [{:keys [data id] :as shape} attrs] diff --git a/frontend/uxbox/ui/workspace/options.cljs b/frontend/uxbox/ui/workspace/options.cljs index d53289571..1743d423a 100644 --- a/frontend/uxbox/ui/workspace/options.cljs +++ b/frontend/uxbox/ui/workspace/options.cljs @@ -9,7 +9,7 @@ [uxbox.ui.mixins :as mx] [uxbox.ui.dom :as dom] [uxbox.ui.workspace.base :as wb] - [uxbox.util.data :refer (parse-int)])) + [uxbox.util.data :refer (parse-int parse-float)])) (def +menus-map+ {:builtin/icon [:menu/measures :menu/fill] @@ -44,33 +44,47 @@ (defmethod -render-menu :menu/fill [menu own shape] - (html - [:div.element-set {:key (str (:id menu))} - [:div.element-set-title (:name menu)] - [:div.element-set-content - ;; SLIDEBAR FOR ROTATION AND OPACITY - [:span "Color"] - [:div.row-flex - [:input#width.input-text - {:placeholder "#" - :type "text" - :value (:color shape "") - :on-change (constantly nil)}]] + (letfn [(on-color-change [event] + (let [value (dom/event->value event) + sid (:id shape)] + (-> (dw/update-shape-color sid value) + (rs/emit!)))) + (on-opacity-change [event] + (let [value (dom/event->value event) + value (parse-float value 1)] + (println "opacity:" value)))] + (html + [:div.element-set {:key (str (:id menu))} + [:div.element-set-title (:name menu)] + [:div.element-set-content + ;; SLIDEBAR FOR ROTATION AND OPACITY + [:span "Color"] + [:div.row-flex + [:input#width.input-text + {:placeholder "#" + :type "text" + :value (:fill shape "") + :on-change on-color-change}]] - ;; RECENT COLORS - [:span "Recent colors"] - [:div.row-flex - [:span.color-th] - [:span.color-th {:style {:background "#c5cb7f"}}] - [:span.color-th {:style {:background "#6cb533"}}] - [:span.color-th {:style {:background "#67c6b5"}}] - [:span.color-th {:style {:background "#a178e3"}}] - [:span.color-th.palette-th i/palette]] + ;; RECENT COLORS + [:span "Recent colors"] + [:div.row-flex + [:span.color-th] + [:span.color-th {:style {:background "#c5cb7f"}}] + [:span.color-th {:style {:background "#6cb533"}}] + [:span.color-th {:style {:background "#67c6b5"}}] + [:span.color-th {:style {:background "#a178e3"}}] + [:span.color-th.palette-th i/palette]] - ;; SLIDEBAR FOR ROTATION AND OPACITY - [:span "Opacity"] - [:div.row-flex - [:input.slidebar {:type "range"}]]]])) + ;; SLIDEBAR FOR ROTATION AND OPACITY + [:span "Opacity"] + [:div.row-flex + [:input.slidebar + {:type "range" + :min "0" + :max "1" + :step "0.02" + :on-change on-opacity-change}]]]]))) (defmethod -render-menu :menu/measures [menu own shape] diff --git a/frontend/uxbox/util/data.cljs b/frontend/uxbox/util/data.cljs index 903978ec5..43bff3dc5 100644 --- a/frontend/uxbox/util/data.cljs +++ b/frontend/uxbox/util/data.cljs @@ -49,3 +49,12 @@ (if (or (not v) (nan? v)) default v)))) + +(defn parse-float + ([v] + (js/parseFloat v)) + ([v default] + (let [v (js/parseFloat v)] + (if (or (not v) (nan? v)) + default + v))))