🔥 Remove unused and deprecated helpers from app.util.object ns

This commit is contained in:
Andrey Antukh 2024-08-27 16:43:05 +02:00
parent 569674452a
commit 25e9129a8e
8 changed files with 63 additions and 149 deletions

View file

@ -242,8 +242,8 @@
(defn ready (defn ready
[cb] [cb]
(-> (obj/get-in js/document ["fonts" "ready"]) (let [fonts (obj/get js/document "fonts")]
(p/then cb))) (p/then (obj/get fonts "ready") cb)))
(defn get-default-variant (defn get-default-variant
[{:keys [variants]}] [{:keys [variants]}]

View file

@ -96,14 +96,17 @@
[:ul {:class list-class :role "menu"} children])) [:ul {:class list-class :role "menu"} children]))
(mf/defc dropdown-menu (mf/defc dropdown-menu
{::mf/wrap-props false} {::mf/props :obj}
[props] [props]
(assert (fn? (gobj/get props "on-close")) "missing `on-close` prop") (assert (fn? (gobj/get props "on-close")) "missing `on-close` prop")
(assert (boolean? (gobj/get props "show")) "missing `show` prop") (assert (boolean? (gobj/get props "show")) "missing `show` prop")
(let [ids (obj/get props "ids") (let [ids (obj/get props "ids")
ids (d/nilv ids (->> (obj/get props "children") ids (or ids
(keep #(obj/get-in % ["props" "id"]))))] (->> (obj/get props "children")
(keep (fn [o]
(let [props (obj/get o "props")]
(obj/get props "id"))))))]
(when (gobj/get props "show") (when (gobj/get props "show")
(mf/element (mf/element
dropdown-menu' dropdown-menu'

View file

@ -16,10 +16,10 @@
[app.util.forms :as fm] [app.util.forms :as fm]
[app.util.i18n :as i18n :refer [tr]] [app.util.i18n :as i18n :refer [tr]]
[app.util.keyboard :as kbd] [app.util.keyboard :as kbd]
[app.util.object :as obj]
[cljs.core :as c] [cljs.core :as c]
[cuerdas.core :as str] [cuerdas.core :as str]
[rumext.v2 :as mf])) [rumext.v2 :as mf]
[rumext.v2.util :as mfu]))
(def form-ctx (mf/create-context nil)) (def form-ctx (mf/create-context nil))
(def use-form fm/use-form) (def use-form fm/use-form)
@ -102,7 +102,7 @@
(cond-> (and value is-checkbox?) (assoc :default-checked value)) (cond-> (and value is-checkbox?) (assoc :default-checked value))
(cond-> (and touched? (:message error)) (assoc "aria-invalid" "true" (cond-> (and touched? (:message error)) (assoc "aria-invalid" "true"
"aria-describedby" (dm/str "error-" input-name))) "aria-describedby" (dm/str "error-" input-name)))
(obj/map->obj obj/prop-key-fn)) (mfu/map->props))
checked? (and is-checkbox? (= value true)) checked? (and is-checkbox? (= value true))
show-valid? (and show-success? touched? (not error)) show-valid? (and show-success? touched? (not error))
@ -205,7 +205,7 @@
:on-blur on-blur :on-blur on-blur
;; :placeholder label ;; :placeholder label
:on-change on-change) :on-change on-change)
(obj/map->obj obj/prop-key-fn))] (mfu/map->props))]
[:div {:class (dm/str klass " " (stl/css :textarea-wrapper))} [:div {:class (dm/str klass " " (stl/css :textarea-wrapper))}
[:label {:class (stl/css :textarea-label)} label] [:label {:class (stl/css :textarea-label)} label]

View file

@ -11,6 +11,7 @@
[app.common.data.macros :as dm] [app.common.data.macros :as dm]
[app.common.files.helpers :as cfh] [app.common.files.helpers :as cfh]
[app.common.geom.shapes :as gsh] [app.common.geom.shapes :as gsh]
[app.common.json :as json]
[app.common.svg :as csvg] [app.common.svg :as csvg]
[app.common.types.shape :refer [stroke-caps-line stroke-caps-marker]] [app.common.types.shape :refer [stroke-caps-line stroke-caps-marker]]
[app.common.types.shape.radius :as ctsr] [app.common.types.shape.radius :as ctsr]
@ -154,6 +155,7 @@
[shape render-id] [shape render-id]
(let [attrs (get shape :svg-attrs {}) (let [attrs (get shape :svg-attrs {})
defs (get shape :svg-defs {})] defs (get shape :svg-defs {})]
(if (and (empty? defs) (if (and (empty? defs)
(empty? attrs)) (empty? attrs))
#js {} #js {}
@ -164,7 +166,7 @@
(dm/str render-id "-" id) (dm/str render-id "-" id)
id))) id)))
(dissoc :id) (dissoc :id)
(obj/map->obj))))) (json/->js :key-fn name)))))
(defn get-fill-style (defn get-fill-style
([fill-data index render-id type] ([fill-data index render-id type]

View file

@ -12,9 +12,9 @@
[app.common.data :as d] [app.common.data :as d]
[app.common.data.macros :as dm] [app.common.data.macros :as dm]
[app.common.geom.shapes :as gsh] [app.common.geom.shapes :as gsh]
[app.common.json :as json]
[app.common.svg :as csvg] [app.common.svg :as csvg]
[app.main.ui.context :as muc] [app.main.ui.context :as muc]
[app.util.json :as json]
[app.util.object :as obj] [app.util.object :as obj]
[cuerdas.core :as str] [cuerdas.core :as str]
[rumext.v2 :as mf])) [rumext.v2 :as mf]))
@ -29,9 +29,11 @@
(cond (cond
(map? node) (map? node)
[:> (d/name tag) (obj/map->obj (csvg/attrs->props attrs)) (let [props (-> (csvg/attrs->props attrs)
(for [child content] (json/->js :key-fn name))]
[:& render-xml {:xml child :key (swap! internal-counter inc)}])] [:> (d/name tag) props
(for [child content]
[:& render-xml {:xml child :key (swap! internal-counter inc)}])])
(string? node) (string? node)
node node
@ -39,14 +41,6 @@
:else :else
nil)) nil))
(defn uuid->string [m]
(->> m
(d/deep-mapm
(fn [[k v]]
(if (uuid? v)
[k (str v)]
[k v])))))
(defn bool->str [val] (defn bool->str [val]
(when (some? val) (str val))) (when (some? val) (str val)))
@ -130,8 +124,8 @@
(add! :width) (add! :width)
(add! :height) (add! :height)
(add! :grow-type) (add! :grow-type)
(add! :content (comp json/encode uuid->string)) (add! :content json/encode)
(add! :position-data (comp json/encode uuid->string)))) (add! :position-data json/encode)))
(cond-> mask? (cond-> mask?
(obj/set! "penpot:masked-group" "true")) (obj/set! "penpot:masked-group" "true"))

View file

@ -12,8 +12,8 @@
[app.common.geom.rect :as grc] [app.common.geom.rect :as grc]
[app.common.geom.shapes :as gsh] [app.common.geom.shapes :as gsh]
[app.common.geom.shapes.bounds :as gsb] [app.common.geom.shapes.bounds :as gsb]
[app.common.json :as json]
[app.common.svg :as csvg] [app.common.svg :as csvg]
[app.util.object :as obj]
[rumext.v2 :as mf])) [rumext.v2 :as mf]))
(defn add-matrix [attrs transform-key transform-matrix] (defn add-matrix [attrs transform-key transform-matrix]
@ -79,12 +79,16 @@
:data-old-width (:width attrs) :data-old-width (:width attrs)
:data-old-height (:height attrs)})) :data-old-height (:height attrs)}))
[wrapper wrapper-props] (if (= tag :mask) [wrapper wrapper-props]
["g" #js {:className "svg-mask-wrapper" (if (= tag :mask)
:transform (str transform)}] ["g" #js {:className "svg-mask-wrapper"
[mf/Fragment #js {}])] :transform (str transform)}]
[mf/Fragment #js {}])
[:> (name tag) (obj/map->obj attrs) props
(json/->js attrs :key-fn name)]
[:> (name tag) props
[:> wrapper wrapper-props [:> wrapper wrapper-props
(for [[index node] (d/enumerate content)] (for [[index node] (d/enumerate content)]
[:& svg-node {:key (dm/str "node-" index) [:& svg-node {:key (dm/str "node-" index)

View file

@ -35,7 +35,7 @@
(defn format-point (defn format-point
[{:keys [x y] :as point}] [{:keys [x y] :as point}]
(when (some? point) (when (some? point)
(obj/clear-empty (obj/without-empty
#js {:x x :y y}))) #js {:x x :y y})))
;;export type PenpotBounds = { ;;export type PenpotBounds = {
@ -47,7 +47,7 @@
(defn format-bounds (defn format-bounds
[{:keys [x y width height] :as bounds}] [{:keys [x y width height] :as bounds}]
(when (some? bounds) (when (some? bounds)
(obj/clear-empty (obj/without-empty
#js {:x x :y y :width width :height height}))) #js {:x x :y y :width width :height height})))
;; export interface PenpotColorShapeInfoEntry { ;; export interface PenpotColorShapeInfoEntry {
@ -58,7 +58,7 @@
(defn format-shape-info (defn format-shape-info
[{:keys [prop shape-id index] :as info}] [{:keys [prop shape-id index] :as info}]
(when (some? info) (when (some? info)
(obj/clear-empty (obj/without-empty
#js {:property (d/name prop) #js {:property (d/name prop)
:index index :index index
:shapeId (dm/str shape-id)}))) :shapeId (dm/str shape-id)})))
@ -75,12 +75,12 @@
(defn format-stop (defn format-stop
[{:keys [color opacity offset] :as stop}] [{:keys [color opacity offset] :as stop}]
(when (some? stop) (when (some? stop)
(obj/clear-empty #js {:color color :opacity opacity :offset offset}))) (obj/without-empty #js {:color color :opacity opacity :offset offset})))
(defn format-gradient (defn format-gradient
[{:keys [type start-x start-y end-x end-y width stops] :as gradient}] [{:keys [type start-x start-y end-x end-y width stops] :as gradient}]
(when (some? gradient) (when (some? gradient)
(obj/clear-empty (obj/without-empty
#js {:type (format-key type) #js {:type (format-key type)
:startX start-x :startX start-x
:startY start-y :startY start-y
@ -100,7 +100,7 @@
(defn format-image (defn format-image
[{:keys [name width height mtype id keep-aspect-ratio] :as image}] [{:keys [name width height mtype id keep-aspect-ratio] :as image}]
(when (some? image) (when (some? image)
(obj/clear-empty (obj/without-empty
#js {:name name #js {:name name
:width width :width width
:height height :height height
@ -122,7 +122,7 @@
(defn format-color (defn format-color
[{:keys [id name path color opacity ref-id ref-file gradient image] :as color-data}] [{:keys [id name path color opacity ref-id ref-file gradient image] :as color-data}]
(when (some? color-data) (when (some? color-data)
(obj/clear-empty (obj/without-empty
#js {:id (format-id id) #js {:id (format-id id)
:name name :name name
:path path :path path
@ -155,7 +155,7 @@
(defn format-shadow (defn format-shadow
[{:keys [id style offset-x offset-y blur spread hidden color] :as shadow}] [{:keys [id style offset-x offset-y blur spread hidden color] :as shadow}]
(when (some? shadow) (when (some? shadow)
(obj/clear-empty (obj/without-empty
#js {:id (-> id format-id) #js {:id (-> id format-id)
:style (-> style format-key) :style (-> style format-key)
:offsetX offset-x :offsetX offset-x
@ -181,7 +181,7 @@
(defn format-fill (defn format-fill
[{:keys [fill-color fill-opacity fill-color-gradient fill-color-ref-file fill-color-ref-id fill-image] :as fill}] [{:keys [fill-color fill-opacity fill-color-gradient fill-color-ref-file fill-color-ref-id fill-image] :as fill}]
(when (some? fill) (when (some? fill)
(obj/clear-empty (obj/without-empty
#js {:fillColor fill-color #js {:fillColor fill-color
:fillOpacity fill-opacity :fillOpacity fill-opacity
:fillColorGradient (format-gradient fill-color-gradient) :fillColorGradient (format-gradient fill-color-gradient)
@ -219,7 +219,7 @@
stroke-cap-start stroke-cap-end stroke-color-gradient] :as stroke}] stroke-cap-start stroke-cap-end stroke-color-gradient] :as stroke}]
(when (some? stroke) (when (some? stroke)
(obj/clear-empty (obj/without-empty
#js {:strokeColor stroke-color #js {:strokeColor stroke-color
:strokeColorRefFile (format-id stroke-color-ref-file) :strokeColorRefFile (format-id stroke-color-ref-file)
:strokeColorRefId (format-id stroke-color-ref-id) :strokeColorRefId (format-id stroke-color-ref-id)
@ -245,7 +245,7 @@
(defn format-blur (defn format-blur
[{:keys [id type value hidden] :as blur}] [{:keys [id type value hidden] :as blur}]
(when (some? blur) (when (some? blur)
(obj/clear-empty (obj/without-empty
#js {:id (format-id id) #js {:id (format-id id)
:type (format-key type) :type (format-key type)
:value value :value value
@ -259,7 +259,7 @@
(defn format-export (defn format-export
[{:keys [type scale suffix] :as export}] [{:keys [type scale suffix] :as export}]
(when (some? export) (when (some? export)
(obj/clear-empty (obj/without-empty
#js {:type (format-key type) #js {:type (format-key type)
:scale scale :scale scale
:suffix suffix}))) :suffix suffix})))
@ -280,7 +280,7 @@
(defn format-frame-guide-column-params (defn format-frame-guide-column-params
[{:keys [color type size margin item-length gutter] :as params}] [{:keys [color type size margin item-length gutter] :as params}]
(when (some? params) (when (some? params)
(obj/clear-empty (obj/without-empty
#js {:color (format-color color) #js {:color (format-color color)
:type (format-key type) :type (format-key type)
:size size :size size
@ -296,7 +296,7 @@
(defn format-frame-guide-column (defn format-frame-guide-column
[{:keys [type display params] :as guide}] [{:keys [type display params] :as guide}]
(when (some? guide) (when (some? guide)
(obj/clear-empty (obj/without-empty
#js {:type (format-key type) #js {:type (format-key type)
:display display :display display
:params (format-frame-guide-column-params params)}))) :params (format-frame-guide-column-params params)})))
@ -309,7 +309,7 @@
(defn format-frame-guide-row (defn format-frame-guide-row
[{:keys [type display params] :as guide}] [{:keys [type display params] :as guide}]
(when (some? guide) (when (some? guide)
(obj/clear-empty (obj/without-empty
#js {:type (format-key type) #js {:type (format-key type)
:display display :display display
:params (format-frame-guide-column-params params)}))) :params (format-frame-guide-column-params params)})))
@ -321,7 +321,7 @@
(defn format-frame-guide-square-params (defn format-frame-guide-square-params
[{:keys [color size] :as params}] [{:keys [color size] :as params}]
(when (some? params) (when (some? params)
(obj/clear-empty (obj/without-empty
#js {:color (format-color color) #js {:color (format-color color)
:size size}))) :size size})))
@ -334,7 +334,7 @@
(defn format-frame-guide-square (defn format-frame-guide-square
[{:keys [type display params] :as guide}] [{:keys [type display params] :as guide}]
(when (some? guide) (when (some? guide)
(obj/clear-empty (obj/without-empty
#js {:type (format-key type) #js {:type (format-key type)
:display display :display display
:params (format-frame-guide-column-params params)}))) :params (format-frame-guide-column-params params)})))
@ -382,7 +382,7 @@
(defn format-command-params (defn format-command-params
[{:keys [x y c1x c1y c2x c2y rx ry x-axis-rotation large-arc-flag sweep-flag] :as props}] [{:keys [x y c1x c1y c2x c2y rx ry x-axis-rotation large-arc-flag sweep-flag] :as props}]
(when (some? props) (when (some? props)
(obj/clear-empty (obj/without-empty
#js {:x x #js {:x x
:y y :y y
:c1x c1x :c1x c1x
@ -398,7 +398,7 @@
(defn format-command (defn format-command
[{:keys [command params] :as props}] [{:keys [command params] :as props}]
(when (some? props) (when (some? props)
(obj/clear-empty (obj/without-empty
#js {:command (format-key command) #js {:command (format-key command)
:params (format-command-params params)}))) :params (format-command-params params)})))
@ -416,7 +416,7 @@
(defn format-track (defn format-track
[{:keys [type value] :as track}] [{:keys [type value] :as track}]
(when (some? track) (when (some? track)
(obj/clear-empty (obj/without-empty
#js {:type (-> type format-key) #js {:type (-> type format-key)
:value value}))) :value value})))
@ -462,7 +462,7 @@
(defn format-animation (defn format-animation
[animation] [animation]
(when animation (when animation
(obj/clear-empty (obj/without-empty
(case (:animation-type animation) (case (:animation-type animation)
:dissolve :dissolve
@ -543,7 +543,7 @@
(defn format-action (defn format-action
[interaction plugin file-id page-id] [interaction plugin file-id page-id]
(when interaction (when interaction
(obj/clear-empty (obj/without-empty
(case (:action-type interaction) (case (:action-type interaction)
:navigate :navigate
#js {:type "navigate-to" #js {:type "navigate-to"

View file

@ -6,12 +6,7 @@
(ns app.util.object (ns app.util.object
"A collection of helpers for work with javascript objects." "A collection of helpers for work with javascript objects."
(:refer-clojure :exclude [set! new get get-in merge clone contains? array? into-array]) (:refer-clojure :exclude [set! new get merge clone contains? array? into-array]))
(:require
[cuerdas.core :as str]
;; FIXME: we use goog.string here for performance reasons, pending
;; to apply this optimizations directly to cuerdas.
[goog.string :as gstr]))
(defn array? (defn array?
[o] [o]
@ -36,24 +31,6 @@
(when (some? obj) (when (some? obj)
(js/Object.hasOwn obj k))) (js/Object.hasOwn obj k)))
(defn get-keys
[obj]
(js/Object.keys ^js obj))
(defn get-in
([obj keys]
(get-in obj keys nil))
([obj keys default]
(loop [key (first keys)
keys (rest keys)
res obj]
(if (or (nil? key) (nil? res))
(or res default)
(recur (first keys)
(rest keys)
(unchecked-get res key))))))
(defn clone (defn clone
[a] [a]
(js/Object.assign #js {} a)) (js/Object.assign #js {} a))
@ -80,86 +57,20 @@
(js-delete obj key) (js-delete obj key)
obj) obj)
(def ^:private not-found-sym (js/Symbol "not-found"))
(defn update! (defn update!
[obj key f & args] [obj key f & args]
(let [found (get obj key ::not-found)] (let [found (get obj key not-found-sym)]
(if-not (identical? ::not-found found) (when-not ^boolean (identical? found not-found-sym)
(do (unchecked-set obj key (apply f found args)) (unchecked-set obj key (apply f found args)))
obj) obj))
obj)))
(defn- props-key-fn
[k]
(if (or (keyword? k) (symbol? k))
(let [nword (name k)]
(cond
(= nword "class") "className"
(str/starts-with? nword "--") nword
(str/starts-with? nword "data-") nword
(str/starts-with? nword "aria-") nword
:else (str/camel nword)))
k))
(defn clj->props
[props]
(clj->js props :keyword-fn props-key-fn))
(defn ^boolean in? (defn ^boolean in?
[obj prop] [obj prop]
(js* "~{} in ~{}" prop obj)) (js* "~{} in ~{}" prop obj))
(defn- transform-prop-key (defn without-empty
[s]
(let [result (js* "~{}.replace(\":\", \"-\").replace(/-./g, x=>x[1].toUpperCase())", s)]
(if ^boolean (gstr/startsWith s "-")
(gstr/capitalize result)
result)))
(defn prop-key-fn
[k]
(when (string? k)
(cond
(or (= k "class")
(= k "class-name"))
"className"
(gstr/startsWith k "data-")
k
:else
(transform-prop-key k))))
;; FIXME: REPEATED from app.common.json
(defn map->obj
"A simplified version of clj->js with focus on performance"
([x] (map->obj x identity))
([x ^function key-fn]
(cond
(nil? x)
nil
(keyword? x)
(name x)
(map? x)
(reduce-kv (fn [m k v]
(let [k (if (keyword? k) (name k) k)]
(unchecked-set m (key-fn k) (map->obj v key-fn))
m))
#js {}
x)
(coll? x)
(reduce (fn [arr v]
(.push arr v)
arr)
(array)
x)
:else x)))
(defn clear-empty
[^js obj] [^js obj]
(when (some? obj) (when (some? obj)
(js* "Object.entries(~{}).reduce((a, [k,v]) => (v == null ? a : (a[k]=v, a)), {}) " obj))) (js* "Object.entries(~{}).reduce((a, [k,v]) => (v == null ? a : (a[k]=v, a)), {}) " obj)))