mirror of
https://github.com/penpot/penpot.git
synced 2025-01-10 08:50:57 -05:00
76 lines
2.5 KiB
Clojure
76 lines
2.5 KiB
Clojure
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
;; 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/.
|
|
;;
|
|
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
|
;; defined by the Mozilla Public License, v. 2.0.
|
|
;;
|
|
;; Copyright (c) 2020 UXBOX Labs SL
|
|
|
|
(ns app.common.attrs)
|
|
|
|
;; Extract some attributes of a list of shapes.
|
|
;; For each attribute, if the value is the same in all shapes,
|
|
;; wll take this value. If there is any shape that is different,
|
|
;; the value of the attribute will be the keyword :multiple.
|
|
;;
|
|
;; If some shape has the value nil in any attribute, it's
|
|
;; considered a different value. If the shape does not contain
|
|
;; the attribute, it's ignored in the final result.
|
|
;;
|
|
;; Example:
|
|
;; (def shapes [{:stroke-color "#ff0000"
|
|
;; :stroke-width 3
|
|
;; :fill-color "#0000ff"
|
|
;; :x 1000 :y 2000 :rx nil}
|
|
;; {:stroke-width "#ff0000"
|
|
;; :stroke-width 5
|
|
;; :x 1500 :y 2000}])
|
|
;;
|
|
;; (get-attrs-multi shapes [:stroke-color
|
|
;; :stroke-width
|
|
;; :fill-color
|
|
;; :rx
|
|
;; :ry])
|
|
;; >>> {:stroke-color "#ff0000"
|
|
;; :stroke-width :multiple
|
|
;; :fill-color "#0000ff"
|
|
;; :rx nil
|
|
;; :ry nil}
|
|
;;
|
|
|
|
(defn get-attrs-multi
|
|
([objs attrs]
|
|
(get-attrs-multi objs attrs = identity))
|
|
|
|
([objs attrs eqfn sel]
|
|
|
|
(loop [attr (first attrs)
|
|
attrs (rest attrs)
|
|
result (transient {})]
|
|
|
|
(if attr
|
|
(let [value
|
|
(loop [curr (first objs)
|
|
objs (rest objs)
|
|
value ::undefined]
|
|
|
|
(if (and curr (not= value :multiple))
|
|
;;
|
|
(let [new-val (get curr attr ::undefined)
|
|
value (cond
|
|
(= new-val ::undefined) value
|
|
(= value ::undefined) (sel new-val)
|
|
(eqfn new-val value) value
|
|
:else :multiple)]
|
|
(recur (first objs) (rest objs) value))
|
|
;;
|
|
value))]
|
|
(recur (first attrs)
|
|
(rest attrs)
|
|
(cond-> result
|
|
(not= value ::undefined)
|
|
(assoc! attr value))))
|
|
|
|
(persistent! result)))))
|
|
|