Apply token value changes to shapes in all pages

This commit is contained in:
Andrés Moya 2025-01-24 16:44:53 +01:00
parent c82e5f7681
commit 6cdef04968
10 changed files with 193 additions and 133 deletions

View File

@ -36,6 +36,12 @@
([state file-id]
(dm/get-in state [:files file-id :data])))
(defn get-all-page-ids
([state]
(get-all-page-ids state (:current-file-id state)))
([state file-id]
(dm/get-in state [:files file-id :data :pages])))
(defn get-page
[fdata page-id]
(dm/get-in fdata [:pages-index page-id]))
@ -61,7 +67,7 @@
(:current-file-id state)
page-id))
([state file-id page-id]
(-> (lookup-page state file-id page-id)
(-> (lookup-page state file-id (d/nilv page-id (:current-page-id state)))
(get :objects))))
(defn process-selected-shapes

View File

@ -84,7 +84,8 @@
(defn transform-fill
([state ids color transform] (transform-fill state ids color transform nil))
([state ids color transform options]
(let [objects (dsh/lookup-page-objects state)
(let [page-id (get options :page-id (get state :current-page-id))
objects (dsh/lookup-page-objects state page-id)
is-text? #(= :text (:type (get objects %)))
text-ids (filter is-text? ids)
shape-ids (remove is-text? ids)

View File

@ -328,14 +328,16 @@
(defn- calculate-modifiers
([state modif-tree]
(calculate-modifiers state false false modif-tree))
(calculate-modifiers state false false modif-tree nil))
([state ignore-constraints ignore-snap-pixel modif-tree]
(calculate-modifiers state ignore-constraints ignore-snap-pixel modif-tree nil))
([state ignore-constraints ignore-snap-pixel modif-tree page-id]
(calculate-modifiers state ignore-constraints ignore-snap-pixel modif-tree page-id nil))
([state ignore-constraints ignore-snap-pixel modif-tree params]
(let [objects
(dsh/lookup-page-objects state)
([state ignore-constraints ignore-snap-pixel modif-tree page-id params]
(let [page-id (d/nilv page-id (:current-page-id state))
objects
(dsh/lookup-page-objects state page-id)
snap-pixel?
(and (not ignore-snap-pixel) (contains? (:workspace-layout state) :snap-pixel-grid))
@ -438,11 +440,12 @@
;; - It consideres the center for everyshape instead of the center of the total selrect
;; - The angle param is the desired final value, not a delta
(defn set-delta-rotation-modifiers
[angle shapes {:keys [center delta?] :or {center nil delta? false}}]
[angle shapes {:keys [center delta? page-id] :or {center nil delta? false}}]
(ptk/reify ::set-delta-rotation-modifiers
ptk/UpdateEvent
(update [_ state]
(let [objects (dsh/lookup-page-objects state)
(let [page-id (d/nilv page-id (:current-page-id state))
objects (dsh/lookup-page-objects state page-id)
ids
(->> shapes
(remove #(get % :blocked false))
@ -466,18 +469,19 @@
(apply-modifiers nil))
([{:keys [modifiers undo-transation? stack-undo? ignore-constraints
ignore-snap-pixel ignore-touched undo-group]
ignore-snap-pixel ignore-touched undo-group page-id]
:or {undo-transation? true stack-undo? false ignore-constraints false
ignore-snap-pixel false ignore-touched false}}]
(ptk/reify ::apply-modifiers
ptk/WatchEvent
(watch [_ state _]
(let [text-modifiers (get state :workspace-text-modifier)
objects (dsh/lookup-page-objects state)
(let [text-modifiers (get state :workspace-text-modifier)
page-id (d/nilv page-id (:current-page-id state))
objects (dsh/lookup-page-objects state page-id)
object-modifiers
(if (some? modifiers)
(calculate-modifiers state ignore-constraints ignore-snap-pixel modifiers)
(calculate-modifiers state ignore-constraints ignore-snap-pixel modifiers page-id)
(get state :workspace-modifiers))
ids
@ -519,6 +523,7 @@
:ignore-tree ignore-tree
:ignore-touched ignore-touched
:undo-group undo-group
:page-id page-id
;; Attributes that can change in the transform. This way we don't have to check
;; all the attributes
:attrs [:selrect

View File

@ -524,7 +524,8 @@
(ptk/reify ::update-layout-child
ptk/WatchEvent
(watch [_ state _]
(let [objects (dsh/lookup-page-objects state)
(let [page-id (get options :page-id)
objects (dsh/lookup-page-objects state page-id)
children-ids (->> ids (mapcat #(cfh/get-children-ids objects %)))
parent-ids (->> ids (map #(cfh/get-parent-id objects %)))
undo-id (js/Symbol)]

View File

@ -446,8 +446,9 @@
(when (or
(and (features/active-feature? state "text-editor/v2") (nil? (:workspace-editor state)))
(and (not (features/active-feature? state "text-editor/v2")) (nil? (get-in state [:workspace-editor-state id]))))
(let [objects (dsh/lookup-page-objects state)
shape (get objects id)
(let [page-id (get options :page-id (:current-page-id state))
objects (dsh/lookup-page-objects state page-id)
shape (get objects id)
update-node? (some-fn txt/is-text-node? txt/is-paragraph-node?)
shape-ids

View File

@ -314,7 +314,8 @@
(ptk/reify ::update-dimensions
ptk/UpdateEvent
(update [_ state]
(let [objects (dsh/lookup-page-objects state)
(let [page-id (get options :page-id)
objects (dsh/lookup-page-objects state page-id)
get-modifier
(fn [shape] (ctm/change-dimensions-modifiers shape attr value))
modif-tree
@ -403,16 +404,16 @@
"Rotate shapes a fixed angle, from a keyboard action."
([ids rotation]
(increase-rotation ids rotation nil))
([ids rotation params & options]
([ids rotation params & {:as options}]
(ptk/reify ::increase-rotation
ptk/WatchEvent
(watch [_ state _]
(let [page-id (:current-page-id state)
(let [page-id (d/nilv (:page-id params) (:current-page-id state))
objects (dsh/lookup-page-objects state page-id)
shapes (->> ids (map #(get objects %)))]
(rx/concat
(rx/of (dwm/set-delta-rotation-modifiers rotation shapes params))
(rx/of (dwm/apply-modifiers options))))))))
(rx/of (dwm/apply-modifiers (assoc options :page-id page-id)))))))))
;; -- Move ----------------------------------------------------------
@ -787,14 +788,14 @@
(defn update-position
"Move shapes to a new position"
([id position] (update-position id position nil))
([id position opts]
([id position] (update-position nil id position nil))
([page-id id position opts]
(dm/assert! (uuid? id))
(ptk/reify ::update-position
ptk/WatchEvent
(watch [_ state _]
(let [page-id (:current-page-id state)
(let [page-id (d/nilv page-id (:current-page-id state))
objects (dsh/lookup-page-objects state page-id)
shape (get objects id)
;; FIXME: performance rect
@ -809,6 +810,7 @@
modif-tree (dwm/create-modif-tree [id] (ctm/move-modifiers delta))]
(rx/of (dwm/apply-modifiers {:modifiers modif-tree
:page-id page-id
:ignore-constraints false
:ignore-touched (:ignore-touched opts)
:ignore-snap-pixel true})))))))

View File

@ -52,8 +52,10 @@
(dwu/start-undo-transaction undo-id)
(dwsh/update-shapes shape-ids (fn [shape]
(cond-> shape
attributes-to-remove (update :applied-tokens #(apply (partial dissoc %) attributes-to-remove))
:always (update :applied-tokens merge tokenized-attributes))))
attributes-to-remove
(update :applied-tokens #(apply (partial dissoc %) attributes-to-remove))
:always
(update :applied-tokens merge tokenized-attributes))))
(when on-update-shape
(on-update-shape resolved-value shape-ids attributes))
(dwu/commit-undo-transaction undo-id))))))))))
@ -94,126 +96,163 @@
;; Shape Updates ---------------------------------------------------------------
(defn update-shape-radius-all [value shape-ids]
(dwsh/update-shapes shape-ids
(fn [shape]
(when (ctsr/can-get-border-radius? shape)
(ctsr/set-radius-to-all-corners shape value)))
{:reg-objects? true
:ignore-touched true
:attrs ctt/border-radius-keys}))
(defn update-shape-radius-all
([value shape-ids attributes] (update-shape-radius-all value shape-ids attributes nil))
([value shape-ids _attributes page-id] ; The attributes param is needed to have the same arity that other update functions
(dwsh/update-shapes shape-ids
(fn [shape]
(when (ctsr/can-get-border-radius? shape)
(ctsr/set-radius-to-all-corners shape value)))
{:reg-objects? true
:ignore-touched true
:page-id page-id
:attrs ctt/border-radius-keys})))
(defn update-shape-radius-single-corner [value shape-ids attributes]
;; NOTE: This key should be namespaced on data tokens, but these events are not there.
(st/emit! (ptk/data-event :expand-border-radius))
(dwsh/update-shapes shape-ids
(fn [shape]
(when (ctsr/can-get-border-radius? shape)
(ctsr/set-radius-to-single-corner shape (first attributes) value)))
{:reg-objects? true
:ignore-touched true
:attrs ctt/border-radius-keys}))
(defn update-shape-radius-corners
([value shape-ids attributes] (update-shape-radius-corners value shape-ids attributes nil))
([value shape-ids attributes page-id]
(when (< (count attributes) 4)
;; NOTE: This key should be namespaced on data tokens, but these events are not there.
(st/emit! (ptk/data-event :expand-border-radius)))
(dwsh/update-shapes shape-ids
(fn [shape]
(when (ctsr/can-get-border-radius? shape)
(reduce (fn [shape attr]
(ctsr/set-radius-to-single-corner shape attr value))
shape
attributes)))
{:reg-objects? true
:ignore-touched true
:page-id page-id
:attrs ctt/border-radius-keys})))
(defn update-opacity [value shape-ids]
(when (<= 0 value 1)
(dwsh/update-shapes shape-ids
#(assoc % :opacity value)
{:ignore-touched true})))
(defn update-opacity
([value shape-ids attributes] (update-opacity value shape-ids attributes nil))
([value shape-ids _attributes page-id] ; The attributes param is needed to have the same arity that other update functions
(when (<= 0 value 1)
(dwsh/update-shapes shape-ids
#(assoc % :opacity value)
{:ignore-touched true
:page-id page-id}))))
(defn update-rotation [value shape-ids]
(ptk/reify ::update-shape-rotation
ptk/WatchEvent
(watch [_ _ _]
(rx/of
(udw/trigger-bounding-box-cloaking shape-ids)
(udw/increase-rotation shape-ids value nil :ignore-touched true)))))
(defn update-rotation
([value shape-ids attributes] (update-rotation value shape-ids attributes nil))
([value shape-ids _attributes page-id] ; The attributes param is needed to have the same arity that other update functions
(ptk/reify ::update-shape-rotation
ptk/WatchEvent
(watch [_ _ _]
(rx/of
(udw/trigger-bounding-box-cloaking shape-ids)
(udw/increase-rotation shape-ids value
{:page-id page-id}
:ignore-touched true))))))
(defn update-stroke-width
[value shape-ids]
(dwsh/update-shapes shape-ids
(fn [shape]
(when (seq (:strokes shape))
(assoc-in shape [:strokes 0 :stroke-width] value)))
{:reg-objects? true
:ignore-touched true
:attrs [:strokes]}))
([value shape-ids attributes] (update-stroke-width value shape-ids attributes nil))
([value shape-ids _attributes page-id] ; The attributes param is needed to have the same arity that other update functions
(dwsh/update-shapes shape-ids
(fn [shape]
(when (seq (:strokes shape))
(assoc-in shape [:strokes 0 :stroke-width] value)))
{:reg-objects? true
:ignore-touched true
:page-id page-id
:attrs [:strokes]})))
(defn update-color [f value shape-ids]
(defn update-color [f value shape-ids page-id]
(when-let [tc (tinycolor/valid-color value)]
(let [hex (tinycolor/->hex-string tc)
opacity (tinycolor/alpha tc)]
(f shape-ids {:color hex :opacity opacity} 0 {:ignore-touched true}))))
(f shape-ids {:color hex :opacity opacity} 0 {:ignore-touched true
:page-id page-id}))))
(defn update-fill
[value shape-ids]
(update-color wdc/change-fill value shape-ids))
([value shape-ids attributes] (update-fill value shape-ids attributes nil))
([value shape-ids _attributes page-id] ; The attributes param is needed to have the same arity that other update functions
(update-color wdc/change-fill value shape-ids page-id)))
(defn update-stroke-color
[value shape-ids]
(update-color wdc/change-stroke-color value shape-ids))
([value shape-ids attributes] (update-stroke-color value shape-ids attributes nil))
([value shape-ids _attributes page-id] ; The attributes param is needed to have the same arity that other update functions
(update-color wdc/change-stroke-color value shape-ids page-id)))
(defn update-fill-stroke [value shape-ids attributes]
(ptk/reify ::update-fill-stroke
ptk/WatchEvent
(watch [_ _ _]
(rx/of
(when (:fill attributes) (update-fill value shape-ids))
(when (:stroke-color attributes) (update-stroke-color value shape-ids))))))
(defn update-fill-stroke
([value shape-ids attributes] (update-fill-stroke value shape-ids attributes nil))
([value shape-ids attributes page-id]
(ptk/reify ::update-fill-stroke
ptk/WatchEvent
(watch [_ _ _]
(rx/of
(when (:fill attributes) (update-fill value shape-ids attributes page-id))
(when (:stroke-color attributes) (update-stroke-color value shape-ids attributes page-id)))))))
(defn update-shape-dimensions [value shape-ids attributes]
(ptk/reify ::update-shape-dimensions
ptk/WatchEvent
(watch [_ _ _]
(rx/of
(when (:width attributes) (dwt/update-dimensions shape-ids :width value {:ignore-touched true}))
(when (:height attributes) (dwt/update-dimensions shape-ids :height value {:ignore-touched true}))))))
(defn update-shape-dimensions
([value shape-ids attributes] (update-shape-dimensions value shape-ids attributes nil))
([value shape-ids attributes page-id]
(ptk/reify ::update-shape-dimensions
ptk/WatchEvent
(watch [_ _ _]
(rx/of
(when (:width attributes) (dwt/update-dimensions shape-ids :width value {:ignore-touched true :page-id page-id}))
(when (:height attributes) (dwt/update-dimensions shape-ids :height value {:ignore-touched true :page-id page-id})))))))
(defn- attributes->layout-gap [attributes value]
(let [layout-gap (-> (set/intersection attributes #{:column-gap :row-gap})
(zipmap (repeat value)))]
{:layout-gap layout-gap}))
(defn- shape-ids-with-layout [state shape-ids]
(->> (dsh/lookup-shapes state shape-ids)
(defn- shape-ids-with-layout [state page-id shape-ids]
(->> (dsh/lookup-shapes state page-id shape-ids)
(eduction
(filter ctsl/any-layout?)
(map :id))))
(defn update-layout-padding [value shape-ids attrs]
(ptk/reify ::update-layout-padding
ptk/WatchEvent
(watch [_ state _]
(let [ids-with-layout (shape-ids-with-layout state shape-ids)]
(rx/of
(dwsl/update-layout ids-with-layout
{:layout-padding (zipmap attrs (repeat value))}
{:ignore-touched true}))))))
(defn update-layout-padding
([value shape-ids attrs] (update-layout-padding value shape-ids attrs nil))
([value shape-ids attrs page-id]
(ptk/reify ::update-layout-padding
ptk/WatchEvent
(watch [_ state _]
(let [ids-with-layout (shape-ids-with-layout state page-id shape-ids)]
(rx/of
(dwsl/update-layout ids-with-layout
{:layout-padding (zipmap attrs (repeat value))}
{:ignore-touched true
:page-id page-id})))))))
(defn update-layout-spacing [value shape-ids attributes]
(ptk/reify ::update-layout-spacing
ptk/WatchEvent
(watch [_ state _]
(let [ids-with-layout (shape-ids-with-layout state shape-ids)
layout-attributes (attributes->layout-gap attributes value)]
(rx/of
(dwsl/update-layout ids-with-layout
layout-attributes
{:ignore-touched true}))))))
(defn update-layout-spacing
([value shape-ids attributes] (update-layout-spacing value shape-ids attributes nil))
([value shape-ids attributes page-id]
(ptk/reify ::update-layout-spacing
ptk/WatchEvent
(watch [_ state _]
(let [ids-with-layout (shape-ids-with-layout state page-id shape-ids)
layout-attributes (attributes->layout-gap attributes value)]
(rx/of
(dwsl/update-layout ids-with-layout
layout-attributes
{:ignore-touched true
:page-id page-id})))))))
(defn update-shape-position [value shape-ids attributes]
(ptk/reify ::update-shape-position
ptk/WatchEvent
(watch [_ _ _]
(rx/concat
(map #(dwt/update-position % (zipmap attributes (repeat value)) {:ignore-touched true}) shape-ids)))))
(defn update-shape-position
([value shape-ids attributes] (update-shape-position value shape-ids attributes nil))
([value shape-ids attributes page-id]
(ptk/reify ::update-shape-position
ptk/WatchEvent
(watch [_ _ _]
(rx/concat
(map #(dwt/update-position page-id % (zipmap attributes (repeat value)) {:ignore-touched true}) shape-ids))))))
(defn update-layout-sizing-limits [value shape-ids attributes]
(ptk/reify ::update-layout-sizing-limits
ptk/WatchEvent
(watch [_ _ _]
(let [props (-> {:layout-item-min-w value
:layout-item-min-h value
:layout-item-max-w value
:layout-item-max-h value}
(select-keys attributes))]
(dwsl/update-layout-child shape-ids props {:ignore-touched true})))))
(defn update-layout-sizing-limits
([value shape-ids attributes] (update-layout-sizing-limits value shape-ids attributes nil))
([value shape-ids attributes page-id]
(ptk/reify ::update-layout-sizing-limits
ptk/WatchEvent
(watch [_ _ _]
(let [props (-> {:layout-item-min-w value
:layout-item-min-h value
:layout-item-max-w value
:layout-item-max-h value}
(select-keys attributes))]
(dwsl/update-layout-child shape-ids props {:ignore-touched true
:page-id page-id}))))))

View File

@ -186,7 +186,7 @@
:r4 "Bottom Left"
:r3 "Bottom Right"}
:on-update-shape-all wtch/update-shape-radius-all
:on-update-shape wtch/update-shape-radius-single-corner})
:on-update-shape wtch/update-shape-radius-corners})
:color (fn [context-data]
[(generic-attribute-actions #{:fill} "Fill" (assoc context-data :on-update-shape wtch/update-fill))
(generic-attribute-actions #{:stroke-color} "Stroke" (assoc context-data :on-update-shape wtch/update-stroke-color))])

View File

@ -110,7 +110,6 @@
(fn [event token]
(dom/stop-propagation event)
(when (seq selected-shapes)
(st/emit!
(wtch/toggle-token {:token token
:shapes selected-shapes

View File

@ -17,7 +17,7 @@
(def filter-existing-values? false)
(def attributes->shape-update
{#{:r1 :r2 :r3 :r4} wtch/update-shape-radius-all
{#{:r1 :r2 :r3 :r4} wtch/update-shape-radius-corners
ctt/color-keys wtch/update-fill-stroke
ctt/stroke-width-keys wtch/update-stroke-width
ctt/sizing-keys wtch/update-shape-dimensions
@ -86,30 +86,36 @@
(->> (map (fn [[value attrs]] [attrs {value #{object-id}}]) attrs-values-map)
(into {})))
(defn collect-shapes-update-info [resolved-tokens shapes]
(defn collect-shapes-update-info [resolved-tokens objects]
(reduce
(fn [acc [object-id {:keys [applied-tokens] :as shape}]]
(fn [acc [shape-id {:keys [applied-tokens] :as shape}]]
(if (seq applied-tokens)
(let [applied-tokens (-> (invert-collect-key-vals applied-tokens resolved-tokens shape)
(shape-ids-by-values object-id)
(shape-ids-by-values shape-id)
(split-attribute-groups))]
(deep-merge acc applied-tokens))
acc))
{} shapes))
{} objects))
(defn actionize-shapes-update-info [shapes-update-info]
(defn actionize-shapes-update-info [page-id shapes-update-info]
(mapcat (fn [[attrs update-infos]]
(let [action (some attribute-actions-map attrs)]
(map
(fn [[v shape-ids]]
(action v shape-ids attrs))
(action v shape-ids attrs page-id))
update-infos)))
shapes-update-info))
(defn update-tokens [state resolved-tokens]
(->> (dsh/lookup-page-objects state)
(defn update-tokens-in-page [state page-id resolved-tokens]
(->> (dsh/lookup-page-objects state page-id)
(collect-shapes-update-info resolved-tokens)
(actionize-shapes-update-info)))
(actionize-shapes-update-info page-id)))
(defn update-tokens [state resolved-tokens]
(reduce (fn [events page-id]
(rx/concat events (update-tokens-in-page state page-id resolved-tokens)))
(rx/empty)
(dsh/get-all-page-ids state)))
(defn update-workspace-tokens []
(ptk/reify ::update-workspace-tokens