Add dynamic properties modifiers to WASM

This commit is contained in:
alonso.torres 2025-04-24 12:05:52 +02:00 committed by Alonso Torres
parent 44d68ad723
commit 8ce71e792e
5 changed files with 238 additions and 123 deletions

View file

@ -108,106 +108,135 @@
;; --- SHAPE IMPL
(defn- set-wasm-attrs
(defn set-wasm-single-attr!
[shape k]
(let [v (get shape k)]
(case k
:parent-id (api/set-parent-id v)
:type (api/set-shape-type v)
:bool-type (api/set-shape-bool-type v)
:selrect (api/set-shape-selrect v)
:show-content (if (= (:type shape) :frame)
(api/set-shape-clip-content (not v))
(api/set-shape-clip-content false))
:rotation (api/set-shape-rotation v)
:transform (api/set-shape-transform v)
:fills (into [] (api/set-shape-fills v))
:strokes (into [] (api/set-shape-strokes v))
:blend-mode (api/set-shape-blend-mode v)
:opacity (api/set-shape-opacity v)
:hidden (api/set-shape-hidden v)
:shapes (api/set-shape-children v)
:blur (api/set-shape-blur v)
:shadow (api/set-shape-shadows v)
:constraints-h (api/set-constraints-h v)
:constraints-v (api/set-constraints-v v)
:svg-attrs
(when (= (:type shape) :path)
(api/set-shape-path-attrs v))
:masked-group
(when (and (= (:type shape) :group) (:masked-group shape))
(api/set-masked (:masked-group shape)))
:content
(cond
(or (= (:type shape) :path)
(= (:type shape) :bool))
(api/set-shape-path-content v)
(= (:type shape) :svg-raw)
(api/set-shape-svg-raw-content (api/get-static-markup shape))
(= (:type shape) :text)
(into [] (api/set-shape-text-content v)))
(:layout-item-margin
:layout-item-margin-type
:layout-item-h-sizing
:layout-item-v-sizing
:layout-item-max-h
:layout-item-min-h
:layout-item-max-w
:layout-item-min-w
:layout-item-absolute
:layout-item-z-index)
(api/set-layout-child shape)
:layout-grid-rows
(api/set-grid-layout-rows v)
:layout-grid-columns
(api/set-grid-layout-columns v)
:layout-grid-cells
(api/set-grid-layout-cells v)
(:layout
:layout-flex-dir
:layout-gap-type
:layout-gap
:layout-align-items
:layout-align-content
:layout-justify-items
:layout-justify-content
:layout-wrap-type
:layout-padding-type
:layout-padding)
(cond
(ctl/grid-layout? shape)
(api/set-grid-layout-data shape)
(ctl/flex-layout? shape)
(api/set-flex-layout shape))
nil)))
(defn set-wasm-multi-attrs!
[shape properties]
(api/use-shape (:id shape))
(let [pending
(->> properties
(mapcat #(set-wasm-single-attr! shape %)))]
(if (and pending (seq pending))
(->> (rx/from pending)
(rx/mapcat identity)
(rx/reduce conj [])
(rx/subs!
(fn [_]
(api/update-shape-tiles)
(api/clear-drawing-cache)
(api/request-render "set-wasm-attrs-pending"))))
(do
(api/update-shape-tiles)
(api/request-render "set-wasm-attrs")))))
(defn set-wasm-attrs!
[shape k v]
(let [shape (assoc shape k v)]
(api/use-shape (:id shape))
(let [pending (set-wasm-single-attr! shape k)]
;; TODO: set-wasm-attrs is called twice with every set
(if (and pending (seq pending))
(->> (rx/from pending)
(rx/mapcat identity)
(rx/reduce conj [])
(rx/subs!
(fn [_]
(api/update-shape-tiles)
(api/clear-drawing-cache)
(api/request-render "set-wasm-attrs-pending"))))
(do
(api/update-shape-tiles)
(api/request-render "set-wasm-attrs"))))))
(defn- impl-assoc
[self k v]
(when ^boolean shape/*wasm-sync*
(binding [shape/*wasm-sync* false]
(let [self (assoc self k v)]
(api/use-shape (:id self))
(let [pending (case k
:parent-id (api/set-parent-id v)
:type (api/set-shape-type v)
:bool-type (api/set-shape-bool-type v)
:selrect (api/set-shape-selrect v)
:show-content (if (= (:type self) :frame)
(api/set-shape-clip-content (not v))
(api/set-shape-clip-content false))
:rotation (api/set-shape-rotation v)
:transform (api/set-shape-transform v)
:fills (into [] (api/set-shape-fills v))
:strokes (into [] (api/set-shape-strokes v))
:blend-mode (api/set-shape-blend-mode v)
:opacity (api/set-shape-opacity v)
:hidden (api/set-shape-hidden v)
:shapes (api/set-shape-children v)
:blur (api/set-shape-blur v)
:shadow (api/set-shape-shadows v)
:constraints-h (api/set-constraints-h v)
:constraints-v (api/set-constraints-v v)
(set-wasm-attrs! self k v)))
:svg-attrs
(when (= (:type self) :path)
(api/set-shape-path-attrs v))
:masked-group
(when (and (= (:type self) :group) (:masked-group self))
(api/set-masked (:masked-group self)))
:content
(cond
(or (= (:type self) :path)
(= (:type self) :bool))
(api/set-shape-path-content v)
(= (:type self) :svg-raw)
(api/set-shape-svg-raw-content (api/get-static-markup self))
(= (:type self) :text)
(into [] (api/set-shape-text-content v)))
(:layout-item-margin
:layout-item-margin-type
:layout-item-h-sizing
:layout-item-v-sizing
:layout-item-max-h
:layout-item-min-h
:layout-item-max-w
:layout-item-min-w
:layout-item-absolute
:layout-item-z-index)
(api/set-layout-child self)
(:layout-grid-rows
:layout-grid-columns
:layout-grid-cells)
(when (ctl/grid-layout? self)
(api/set-grid-layout self))
(:layout
:layout-flex-dir
:layout-gap-type
:layout-gap
:layout-align-items
:layout-align-content
:layout-justify-items
:layout-justify-content
:layout-wrap-type
:layout-padding-type
:layout-padding)
(cond
(ctl/grid-layout? self)
(api/set-grid-layout self)
(ctl/flex-layout? self)
(api/set-flex-layout self))
nil)]
;; TODO: set-wasm-attrs is called twice with every set
(if (and pending (seq pending))
(->> (rx/from pending)
(rx/mapcat identity)
(rx/reduce conj [])
(rx/subs! (fn [_]
(api/update-shape-tiles)
(api/clear-drawing-cache)
(api/request-render "set-wasm-attrs-pending"))))
(do
(api/update-shape-tiles)
(api/request-render "set-wasm-attrs"))))))))
(defn- impl-assoc
[self k v]
(set-wasm-attrs self k v)
(case k
:id
(ShapeProxy. v
@ -228,7 +257,10 @@
(defn- impl-dissoc
[self k]
(set-wasm-attrs self k nil)
(when ^boolean shape/*wasm-sync*
(binding [shape/*wasm-sync* false]
(set-wasm-attrs! self k nil)))
(case k
:id
(ShapeProxy. nil