Improve color asignation to profile and sessions.

This commit is contained in:
Andrey Antukh 2020-04-27 13:58:56 +02:00 committed by Alonso Torres
parent f2e4417d87
commit 0ee25b6f42
7 changed files with 97 additions and 67 deletions

View file

@ -44,15 +44,21 @@
;; --- Profile Fetched ;; --- Profile Fetched
(defn profile-fetched (defn profile-fetched
[data] [{:keys [fullname] :as data}]
(us/verify ::profile data) (us/verify ::profile data)
(ptk/reify ::profile-fetched (ptk/reify ::profile-fetched
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
(let [profile (avatars/assign data)] (assoc state :profile
(assoc state :profile (cond-> profile (cond-> data
(nil? (:lang data)) (assoc :lang cfg/default-language) (nil? (:photo-uri data))
(nil? (:theme data)) (assoc :theme cfg/default-theme))))) (assoc :photo-uri (avatars/generate {:name fullname}))
(nil? (:lang data))
(assoc :lang cfg/default-language)
(nil? (:theme data))
(assoc :theme cfg/default-theme))))
ptk/EffectEvent ptk/EffectEvent
(effect [_ state stream] (effect [_ state stream]

View file

@ -295,26 +295,52 @@
(ws/-close (get-in state [:ws file-id])) (ws/-close (get-in state [:ws file-id]))
(rx/of ::finalize-ws)))) (rx/of ::finalize-ws))))
;; --- Handle: Who ;; --- Handle: Presence
(def ^:private presence-palette
#{"#2e8b57" ; seagreen
"#808000" ; olive
"#b22222" ; firebrick
"#ff8c00" ; darkorage
"#ffd700" ; gold
"#ba55d3" ; mediumorchid
"#00fa9a" ; mediumspringgreen
"#00bfff" ; deepskyblue
"#dda0dd" ; plum
"#ff1493" ; deeppink
"#ffa07a" ; lightsalmon
})
(defn handle-presence (defn handle-presence
[{:keys [sessions] :as msg}] [{:keys [sessions] :as msg}]
(ptk/reify ::handle-presence (letfn [(assign-color [sessions session]
ptk/UpdateEvent (if (string? (:color session))
(update [_ state] session
(let [users (:workspace-users state)] (let [used (into #{}
(update state :workspace-presence (comp (map second)
(fn [prev-sessions] (map :color)
(reduce (fn [acc [sid pid]] (remove nil?))
(if-let [prev (get prev-sessions sid)] sessions)
(assoc acc sid prev) avail (set/difference presence-palette used)
(let [profile (get users pid) color (or (first avail) "#000000")]
session {:id sid (assoc session :color color))))
:fullname (:fullname profile) (update-sessions [previous profiles]
:photo-uri (:photo-uri profile)}] (reduce (fn [current [session-id profile-id]]
(assoc acc sid (avatars/assign session))))) (let [profile (get profiles profile-id)
{} session {:id session-id
sessions))))))) :fullname (:fullname profile)
:photo-uri (or (:photo-uri profile)
(avatars/generate {:name (:fullname profile)}))}
session (assign-color current session)]
(assoc current session-id session)))
(select-keys previous (map first sessions))
(filter (fn [[sid]] (not (contains? previous sid))) sessions)))]
(ptk/reify ::handle-presence
ptk/UpdateEvent
(update [_ state]
(let [profiles (:workspace-users state)]
(update state :workspace-presence update-sessions profiles))))))
(defn handle-pointer-update (defn handle-pointer-update
[{:keys [page-id profile-id session-id x y] :as msg}] [{:keys [page-id profile-id session-id x y] :as msg}]

View file

@ -45,7 +45,7 @@
([editor props options] ([editor props options]
(when (and (nil? (obj/get editor "selection")) (when (and (nil? (obj/get editor "selection"))
(nil? (obj/get options "at"))) (nil? (obj/get options "at")))
(obj/assoc! options "at" (calculate-full-selection editor))) (obj/set! options "at" (calculate-full-selection editor)))
(.setNodes Transforms editor props options) (.setNodes Transforms editor props options)
editor)) editor))
@ -106,10 +106,10 @@
(let [options #js {:match pred :universal universal?}] (let [options #js {:match pred :universal universal?}]
(cond (cond
(object? at) (object? at)
(obj/assoc! options "at" at) (obj/set! options "at" at)
(nil? (obj/get editor "selection")) (nil? (obj/get editor "selection"))
(obj/assoc! options "at" (calculate-full-selection editor))) (obj/set! options "at" (calculate-full-selection editor)))
(let [result (.nodes Editor editor options) (let [result (.nodes Editor editor options)
match (ffirst (es6-iterator-seq result))] match (ffirst (es6-iterator-seq result))]

View file

@ -80,9 +80,9 @@
:width "100%" :width "100%"
:display "flex"}] :display "flex"}]
(cond-> base (cond-> base
(= valign "top") (obj/assoc! "alignItems" "flex-start") (= valign "top") (obj/set! "alignItems" "flex-start")
(= valign "center") (obj/assoc! "alignItems" "center") (= valign "center") (obj/set! "alignItems" "center")
(= valign "bottom") (obj/assoc! "alignItems" "flex-end")))) (= valign "bottom") (obj/set! "alignItems" "flex-end"))))
(mf/defc rt-text-box (mf/defc rt-text-box
{::mf/wrap-props false {::mf/wrap-props false
@ -93,8 +93,8 @@
data (obj/get props "element") data (obj/get props "element")
type (obj/get data "type") type (obj/get data "type")
style (generate-text-box-styles data) style (generate-text-box-styles data)
attrs (obj/assoc! attrs "style" style) attrs (obj/set! attrs "style" style)
attrs (obj/assoc! attrs "className" type)] attrs (obj/set! attrs "className" type)]
[:> :div attrs childs])) [:> :div attrs childs]))
(defn- generate-text-styles (defn- generate-text-styles
@ -113,8 +113,8 @@
data (obj/get props "element") data (obj/get props "element")
type (obj/get data "type") type (obj/get data "type")
style (generate-text-styles data) style (generate-text-styles data)
attrs (obj/assoc! attrs "style" style) attrs (obj/set! attrs "style" style)
attrs (obj/assoc! attrs "className" type)] attrs (obj/set! attrs "className" type)]
[:> :div attrs childs])) [:> :div attrs childs]))
(defn- generate-paragraph-styles (defn- generate-paragraph-styles
@ -125,8 +125,8 @@
lh (obj/get data "lineHeight") lh (obj/get data "lineHeight")
ta (obj/get data "textAlign")] ta (obj/get data "textAlign")]
(cond-> base (cond-> base
ta (obj/assoc! "textAlign" ta) ta (obj/set! "textAlign" ta)
lh (obj/assoc! "lineHeight" lh)))) lh (obj/set! "lineHeight" lh))))
(mf/defc rt-pharagraph (mf/defc rt-pharagraph
{::mf/wrap-props false {::mf/wrap-props false
@ -136,7 +136,7 @@
childs (obj/get props "children") childs (obj/get props "children")
data (obj/get props "element") data (obj/get props "element")
style (generate-paragraph-styles data) style (generate-paragraph-styles data)
attrs (obj/assoc! attrs "style" style)] attrs (obj/set! attrs "style" style)]
[:> :p attrs childs])) [:> :p attrs childs]))
(defn- generate-leaf-styles (defn- generate-leaf-styles
@ -162,11 +162,11 @@
(when (and (string? letter-spacing) (when (and (string? letter-spacing)
(pos? (alength letter-spacing))) (pos? (alength letter-spacing)))
(obj/assoc! base "letterSpacing" (str letter-spacing "px"))) (obj/set! base "letterSpacing" (str letter-spacing "px")))
(when (and (string? font-size) (when (and (string? font-size)
(pos? (alength font-size))) (pos? (alength font-size)))
(obj/assoc! base "fontSize" (str font-size "px"))) (obj/set! base "fontSize" (str font-size "px")))
(when (and (string? font-id) (when (and (string? font-id)
(pos? (alength font-id))) (pos? (alength font-id)))
@ -180,9 +180,9 @@
(obj/get data "fontStyle")) (obj/get data "fontStyle"))
font-weight (or (:weight font-variant) font-weight (or (:weight font-variant)
(obj/get data "fontWeight"))] (obj/get data "fontWeight"))]
(obj/assoc! base "fontFamily" font-family) (obj/set! base "fontFamily" font-family)
(obj/assoc! base "fontStyle" font-style) (obj/set! base "fontStyle" font-style)
(obj/assoc! base "fontWeight" font-weight)))) (obj/set! base "fontWeight" font-weight))))
base)) base))
@ -194,7 +194,7 @@
childs (obj/get props "children") childs (obj/get props "children")
data (obj/get props "leaf") data (obj/get props "leaf")
style (generate-leaf-styles data) style (generate-leaf-styles data)
attrs (obj/assoc! attrs "style" style)] attrs (obj/set! attrs "style" style)]
[:> :span attrs childs])) [:> :span attrs childs]))
(defn- render-element (defn- render-element
@ -298,7 +298,7 @@
(dom/prevent-default event) (dom/prevent-default event)
(dom/stop-propagation event) (dom/stop-propagation event)
;; WARN: monky patch ;; WARN: monky patch
(obj/assoc! slate/Transforms "deselect" (constantly nil))) (obj/set! slate/Transforms "deselect" (constantly nil)))
:placeholder "Type some text here..."}]]])) :placeholder "Type some text here..."}]]]))
;; --- Text Shape Wrapper ;; --- Text Shape Wrapper

View file

@ -13,7 +13,7 @@
[uxbox.util.object :as obj] [uxbox.util.object :as obj]
["randomcolor" :as rdcolor])) ["randomcolor" :as rdcolor]))
(defn- impl-generate-image (defn generate
[{:keys [name color size] [{:keys [name color size]
:or {color "#303236" size 128}}] :or {color "#303236" size 128}}]
(let [parts (str/words (str/upper name)) (let [parts (str/words (str/upper name))
@ -23,19 +23,15 @@
canvas (.createElement js/document "canvas") canvas (.createElement js/document "canvas")
context (.getContext canvas "2d")] context (.getContext canvas "2d")]
(set! (.-width canvas) size) (obj/set! canvas "width" size)
(set! (.-height canvas) size) (obj/set! canvas "height" size)
(set! (.-fillStyle context) "#303236")
(obj/set! context "fillStyle" color)
(.fillRect context 0 0 size size) (.fillRect context 0 0 size size)
(set! (.-font context) (str (/ size 2) "px Arial")) (obj/set! context "font" (str (/ size 2) "px Arial"))
(set! (.-textAlign context) "center") (obj/set! context "textAlign" "center")
(set! (.-fillStyle context) "#FFFFFF") (obj/set! context "fillStyle" "#FFFFFF")
(.fillText context letters (/ size 2) (/ size 1.5)) (.fillText context letters (/ size 2) (/ size 1.5))
(.toDataURL canvas)))
(defn assign (.toDataURL canvas)))
[{:keys [id photo-uri fullname color] :as profile}]
(cond-> profile
(not photo-uri) (assoc :photo-uri (impl-generate-image {:name fullname}))
(not color) (assoc :color (rdcolor))))

View file

@ -2,14 +2,17 @@
;; License, v. 2.0. If a copy of the MPL was not distributed with this ;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/. ;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;; ;;
;; Copyright (c) 2015-2016 Andrey Antukh <niwi@niwi.nz> ;; This Source Code Form is "Incompatible With Secondary Licenses", as
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com> ;; defined by the Mozilla Public License, v. 2.0.
;;
;; Copyright (c) 2020 UXBOX Labs SL
(ns uxbox.util.color (ns uxbox.util.color
"Color conversion utils." "Color conversion utils."
(:require [cuerdas.core :as str] (:require
[uxbox.util.math :as math] [cuerdas.core :as str]
[goog.color :as gcolor])) [uxbox.util.math :as math]
[goog.color :as gcolor]))
(defn rgb->str (defn rgb->str
[color] [color]

View file

@ -34,14 +34,13 @@
(rest keys) (rest keys)
(unchecked-get res key)))))) (unchecked-get res key))))))
(defn assign! (defn merge!
([a b] ([a b]
(js/Object.assign a b)) (js/Object.assign a b))
([a b & more] ([a b & more]
(reduce assign! (assign! a b) more))) (reduce merge! (merge! a b) more)))
(defn assoc! (defn set!
[obj attr value] [obj key value]
(when (object? obj) (unchecked-set obj key value)
(unchecked-set obj attr value) obj)
obj))