0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-20 05:34:23 -05:00

Some code enhancements

This commit is contained in:
Andrés Moya 2025-01-09 18:06:23 +01:00
parent 170a51f9e5
commit 6a68411120
8 changed files with 235 additions and 222 deletions

View file

@ -17,7 +17,9 @@
[app.common.types.token :as cto]
[app.common.uuid :as uuid]))
(defn- check-unapply-tokens
(defn- generate-unapply-tokens
"When updating attributes that have a token applied, we must unapply it, because the value
of the attribute now has been given directly, and does not come from the token."
[changes objects]
(let [mod-obj-changes (->> (:redo-changes changes)
(filter #(= (:type %) :mod-obj)))
@ -59,7 +61,7 @@
(pcb/reorder-grid-children ids)
(cond->
(not ignore-touched)
(check-unapply-tokens objects)))]
(generate-unapply-tokens objects)))]
changes))
(defn- generate-update-shape-flags

View file

@ -82,45 +82,46 @@
(assoc-in [:workspace-global :picked-shift?] shift?)))))
(defn transform-fill
[state ids color transform & options]
(let [objects (wsh/lookup-page-objects state)
([state ids color transform] (transform-fill state ids color transform nil))
([state ids color transform options]
(let [objects (wsh/lookup-page-objects state)
is-text? #(= :text (:type (get objects %)))
text-ids (filter is-text? ids)
shape-ids (remove is-text? ids)
is-text? #(= :text (:type (get objects %)))
text-ids (filter is-text? ids)
shape-ids (remove is-text? ids)
undo-id (js/Symbol)
undo-id (js/Symbol)
attrs
(cond-> {}
(contains? color :color)
(assoc :fill-color (:color color))
attrs
(cond-> {}
(contains? color :color)
(assoc :fill-color (:color color))
(contains? color :id)
(assoc :fill-color-ref-id (:id color))
(contains? color :id)
(assoc :fill-color-ref-id (:id color))
(contains? color :file-id)
(assoc :fill-color-ref-file (:file-id color))
(contains? color :file-id)
(assoc :fill-color-ref-file (:file-id color))
(contains? color :gradient)
(assoc :fill-color-gradient (:gradient color))
(contains? color :gradient)
(assoc :fill-color-gradient (:gradient color))
(contains? color :opacity)
(assoc :fill-opacity (:opacity color))
(contains? color :opacity)
(assoc :fill-opacity (:opacity color))
(contains? color :image)
(assoc :fill-image (:image color))
(contains? color :image)
(assoc :fill-image (:image color))
:always
(d/without-nils))
:always
(d/without-nils))
transform-attrs #(transform % attrs)]
transform-attrs #(transform % attrs)]
(rx/concat
(rx/of (dwu/start-undo-transaction undo-id))
(rx/from (map #(apply dwt/update-text-with-function % transform-attrs options) text-ids))
(rx/of (dwsh/update-shapes shape-ids transform-attrs options))
(rx/of (dwu/commit-undo-transaction undo-id)))))
(rx/concat
(rx/of (dwu/start-undo-transaction undo-id))
(rx/from (map #(dwt/update-text-with-function % transform-attrs options) text-ids))
(rx/of (dwsh/update-shapes shape-ids transform-attrs options))
(rx/of (dwu/commit-undo-transaction undo-id))))))
(defn swap-attrs [shape attr index new-index]
(let [first (get-in shape [attr index])
@ -146,81 +147,86 @@
(rx/of (dwsh/update-shapes shape-ids transform-attrs)))))))
(defn change-fill
[ids color position & options]
(ptk/reify ::change-fill
ptk/WatchEvent
(watch [_ state _]
(let [change-fn (fn [shape attrs]
(-> shape
(cond-> (not (contains? shape :fills))
(assoc :fills []))
(assoc-in [:fills position] (into {} attrs))))]
(apply transform-fill state ids color change-fn options)))))
([ids color position] (change-fill ids color position nil))
([ids color position options]
(ptk/reify ::change-fill
ptk/WatchEvent
(watch [_ state _]
(let [change-fn (fn [shape attrs]
(-> shape
(cond-> (not (contains? shape :fills))
(assoc :fills []))
(assoc-in [:fills position] (into {} attrs))))]
(transform-fill state ids color change-fn options))))))
(defn change-fill-and-clear
[ids color & options]
(ptk/reify ::change-fill-and-clear
ptk/WatchEvent
(watch [_ state _]
(let [set (fn [shape attrs] (assoc shape :fills [attrs]))]
(apply transform-fill state ids color set options)))))
([ids color] (change-fill-and-clear ids color nil))
([ids color options]
(ptk/reify ::change-fill-and-clear
ptk/WatchEvent
(watch [_ state _]
(let [set (fn [shape attrs] (assoc shape :fills [attrs]))]
(transform-fill state ids color set options))))))
(defn add-fill
[ids color & options]
([ids color] (add-fill ids color nil))
([ids color options]
(dm/assert!
"expected a valid color struct"
(ctc/check-color! color))
(dm/assert!
"expected a valid color struct"
(ctc/check-color! color))
(dm/assert!
"expected a valid coll of uuid's"
(every? uuid? ids))
(dm/assert!
"expected a valid coll of uuid's"
(every? uuid? ids))
(ptk/reify ::add-fill
ptk/WatchEvent
(watch [_ state _]
(let [add (fn [shape attrs]
(-> shape
(update :fills #(into [attrs] %))))]
(apply transform-fill state ids color add options)))))
(ptk/reify ::add-fill
ptk/WatchEvent
(watch [_ state _]
(let [add (fn [shape attrs]
(-> shape
(update :fills #(into [attrs] %))))]
(transform-fill state ids color add options))))))
(defn remove-fill
[ids color position & options]
([ids color position] (remove-fill ids color position nil))
([ids color position options]
(dm/assert!
"expected a valid color struct"
(ctc/check-color! color))
(dm/assert!
"expected a valid color struct"
(ctc/check-color! color))
(dm/assert!
"expected a valid coll of uuid's"
(every? uuid? ids))
(dm/assert!
"expected a valid coll of uuid's"
(every? uuid? ids))
(ptk/reify ::remove-fill
ptk/WatchEvent
(watch [_ state _]
(let [remove-fill-by-index (fn [values index] (->> (d/enumerate values)
(filterv (fn [[idx _]] (not= idx index)))
(mapv second)))
(ptk/reify ::remove-fill
ptk/WatchEvent
(watch [_ state _]
(let [remove-fill-by-index (fn [values index] (->> (d/enumerate values)
(filterv (fn [[idx _]] (not= idx index)))
(mapv second)))
remove (fn [shape _] (update shape :fills remove-fill-by-index position))]
(apply transform-fill state ids color remove options)))))
remove (fn [shape _] (update shape :fills remove-fill-by-index position))]
(transform-fill state ids color remove options))))))
(defn remove-all-fills
[ids color & options]
([ids color] (remove-all-fills ids color nil))
([ids color options]
(dm/assert!
"expected a valid color struct"
(ctc/check-color! color))
(dm/assert!
"expected a valid color struct"
(ctc/check-color! color))
(dm/assert!
"expected a valid coll of uuid's"
(every? uuid? ids))
(dm/assert!
"expected a valid coll of uuid's"
(every? uuid? ids))
(ptk/reify ::remove-all-fills
ptk/WatchEvent
(watch [_ state _]
(let [remove-all (fn [shape _] (assoc shape :fills []))]
(apply transform-fill state ids color remove-all options)))))
(ptk/reify ::remove-all-fills
ptk/WatchEvent
(watch [_ state _]
(let [remove-all (fn [shape _] (assoc shape :fills []))]
(transform-fill state ids color remove-all options))))))
(defn change-hide-fill-on-export
[ids hide-fill-on-export]
@ -237,57 +243,58 @@
(d/merge shape attrs)
shape))))))))
(defn change-stroke
[ids attrs index & options]
(ptk/reify ::change-stroke
ptk/WatchEvent
(watch [_ _ _]
(let [color-attrs (cond-> {}
(contains? attrs :color)
(assoc :stroke-color (:color attrs))
([ids attrs index] (change-stroke ids attrs index nil))
([ids attrs index options]
(ptk/reify ::change-stroke
ptk/WatchEvent
(watch [_ _ _]
(let [color-attrs (cond-> {}
(contains? attrs :color)
(assoc :stroke-color (:color attrs))
(contains? attrs :id)
(assoc :stroke-color-ref-id (:id attrs))
(contains? attrs :id)
(assoc :stroke-color-ref-id (:id attrs))
(contains? attrs :file-id)
(assoc :stroke-color-ref-file (:file-id attrs))
(contains? attrs :file-id)
(assoc :stroke-color-ref-file (:file-id attrs))
(contains? attrs :gradient)
(assoc :stroke-color-gradient (:gradient attrs))
(contains? attrs :gradient)
(assoc :stroke-color-gradient (:gradient attrs))
(contains? attrs :opacity)
(assoc :stroke-opacity (:opacity attrs))
(contains? attrs :opacity)
(assoc :stroke-opacity (:opacity attrs))
(contains? attrs :image)
(assoc :stroke-image (:image attrs)))
(contains? attrs :image)
(assoc :stroke-image (:image attrs)))
attrs (->
(merge attrs color-attrs)
(dissoc :image)
(dissoc :gradient))]
attrs (->
(merge attrs color-attrs)
(dissoc :image)
(dissoc :gradient))]
(rx/of (dwsh/update-shapes
ids
(fn [shape]
(let [new-attrs (merge (get-in shape [:strokes index]) attrs)
new-attrs (cond-> new-attrs
(not (contains? new-attrs :stroke-width))
(assoc :stroke-width 1)
(rx/of (dwsh/update-shapes
ids
(fn [shape]
(let [new-attrs (merge (get-in shape [:strokes index]) attrs)
new-attrs (cond-> new-attrs
(not (contains? new-attrs :stroke-width))
(assoc :stroke-width 1)
(not (contains? new-attrs :stroke-style))
(assoc :stroke-style :solid)
(not (contains? new-attrs :stroke-style))
(assoc :stroke-style :solid)
(not (contains? new-attrs :stroke-alignment))
(assoc :stroke-alignment :inner)
(not (contains? new-attrs :stroke-alignment))
(assoc :stroke-alignment :inner)
:always
(d/without-nils))]
(cond-> shape
(not (contains? shape :strokes))
(assoc :strokes [])
:always
(d/without-nils))]
(cond-> shape
(not (contains? shape :strokes))
(assoc :strokes [])
:always
(assoc-in [:strokes index] new-attrs))))
options))))))
:always
(assoc-in [:strokes index] new-attrs))))
options)))))))
(defn change-shadow
[ids attrs index]

View file

@ -262,15 +262,16 @@
(rx/of (with-meta event (meta it)))))))))
(defn update-layout
[ids changes & options]
(ptk/reify ::update-layout
ptk/WatchEvent
(watch [_ _ _]
(let [undo-id (js/Symbol)]
(rx/of (dwu/start-undo-transaction undo-id)
(dwsh/update-shapes ids (d/patch-object changes) options)
(ptk/data-event :layout/update {:ids ids})
(dwu/commit-undo-transaction undo-id))))))
([ids changes] (update-layout ids changes nil))
([ids changes options]
(ptk/reify ::update-layout
ptk/WatchEvent
(watch [_ _ _]
(let [undo-id (js/Symbol)]
(rx/of (dwu/start-undo-transaction undo-id)
(dwsh/update-shapes ids (d/patch-object changes) options)
(ptk/data-event :layout/update {:ids ids})
(dwu/commit-undo-transaction undo-id)))))))
(defn add-layout-track
([ids type value]
@ -518,27 +519,28 @@
(assoc :layout-item-v-sizing :fix))))
(defn update-layout-child
[ids changes & options]
(ptk/reify ::update-layout-child
ptk/WatchEvent
(watch [_ state _]
(let [objects (wsh/lookup-page-objects state)
children-ids (->> ids (mapcat #(cfh/get-children-ids objects %)))
parent-ids (->> ids (map #(cfh/get-parent-id objects %)))
undo-id (js/Symbol)]
(rx/of (dwu/start-undo-transaction undo-id)
(dwsh/update-shapes ids (d/patch-object changes) options)
(dwsh/update-shapes children-ids (partial fix-child-sizing objects changes) options)
(dwsh/update-shapes
parent-ids
(fn [parent objects]
(-> parent
(fix-parent-sizing objects (set ids) changes)
(cond-> (ctl/grid-layout? parent)
(ctl/assign-cells objects))))
(merge options {:with-objects? true}))
(ptk/data-event :layout/update {:ids ids})
(dwu/commit-undo-transaction undo-id))))))
([ids changes] (update-layout-child ids changes nil))
([ids changes options]
(ptk/reify ::update-layout-child
ptk/WatchEvent
(watch [_ state _]
(let [objects (wsh/lookup-page-objects state)
children-ids (->> ids (mapcat #(cfh/get-children-ids objects %)))
parent-ids (->> ids (map #(cfh/get-parent-id objects %)))
undo-id (js/Symbol)]
(rx/of (dwu/start-undo-transaction undo-id)
(dwsh/update-shapes ids (d/patch-object changes) options)
(dwsh/update-shapes children-ids (partial fix-child-sizing objects changes) options)
(dwsh/update-shapes
parent-ids
(fn [parent objects]
(-> parent
(fix-parent-sizing objects (set ids) changes)
(cond-> (ctl/grid-layout? parent)
(ctl/assign-cells objects))))
(merge options {:with-objects? true}))
(ptk/data-event :layout/update {:ids ids})
(dwu/commit-undo-transaction undo-id)))))))
(defn update-grid-cells
[layout-id ids props]

View file

@ -434,49 +434,50 @@
(txt/transform-nodes (some-fn txt/is-text-node? txt/is-paragraph-node?) migrate-node content))
(defn update-text-with-function
[id update-node-fn & options]
(ptk/reify ::update-text-with-function
ptk/UpdateEvent
(update [_ state]
(d/update-in-when state [:workspace-editor-state id] ted/update-editor-current-inline-styles-fn (comp update-node-fn migrate-node)))
([id update-node-fn] (update-text-with-function id update-node-fn nil))
([id update-node-fn options]
(ptk/reify ::update-text-with-function
ptk/UpdateEvent
(update [_ state]
(d/update-in-when state [:workspace-editor-state id] ted/update-editor-current-inline-styles-fn (comp update-node-fn migrate-node)))
ptk/WatchEvent
(watch [_ state _]
(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 (wsh/lookup-page-objects state)
shape (get objects id)
ptk/WatchEvent
(watch [_ state _]
(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 (wsh/lookup-page-objects state)
shape (get objects id)
update-node? (some-fn txt/is-text-node? txt/is-paragraph-node?)
update-node? (some-fn txt/is-text-node? txt/is-paragraph-node?)
shape-ids
(cond
(cfh/text-shape? shape) [id]
(cfh/group-shape? shape) (cfh/get-children-ids objects id))
shape-ids
(cond
(cfh/text-shape? shape) [id]
(cfh/group-shape? shape) (cfh/get-children-ids objects id))
update-content
(fn [content]
(->> content
(migrate-content)
(txt/transform-nodes update-node? update-node-fn)))
update-content
(fn [content]
(->> content
(migrate-content)
(txt/transform-nodes update-node? update-node-fn)))
update-shape
(fn [shape]
(-> shape
(dissoc :fills)
(d/update-when :content update-content)))]
(rx/of (dwsh/update-shapes shape-ids update-shape options)))))
update-shape
(fn [shape]
(-> shape
(dissoc :fills)
(d/update-when :content update-content)))]
(rx/of (dwsh/update-shapes shape-ids update-shape options)))))
ptk/EffectEvent
(effect [_ state _]
(when (features/active-feature? state "text-editor/v2")
(let [instance (:workspace-editor state)
styles (some-> (editor.v2/getCurrentStyle instance)
(styles/get-styles-from-style-declaration)
((comp update-node-fn migrate-node))
(styles/attrs->styles))]
(editor.v2/applyStylesToSelection instance styles))))))
ptk/EffectEvent
(effect [_ state _]
(when (features/active-feature? state "text-editor/v2")
(let [instance (:workspace-editor state)
styles (some-> (editor.v2/getCurrentStyle instance)
(styles/get-styles-from-style-declaration)
((comp update-node-fn migrate-node))
(styles/attrs->styles))]
(editor.v2/applyStylesToSelection instance styles)))))))
;; --- RESIZE UTILS

View file

@ -301,30 +301,31 @@
(defn update-dimensions
"Change size of shapes, from the sideber options form.
Will ignore pixel snap used in the options side panel"
[ids attr value & options]
(dm/assert! (number? value))
(dm/assert!
"expected valid coll of uuids"
(every? uuid? ids))
(dm/assert!
"expected valid attr"
(contains? #{:width :height} attr))
(ptk/reify ::update-dimensions
ptk/UpdateEvent
(update [_ state]
(let [objects (wsh/lookup-page-objects state)
get-modifier
(fn [shape] (ctm/change-dimensions-modifiers shape attr value))
([ids attr value] (update-dimensions ids attr value nil))
([ids attr value options]
(dm/assert! (number? value))
(dm/assert!
"expected valid coll of uuids"
(every? uuid? ids))
(dm/assert!
"expected valid attr"
(contains? #{:width :height} attr))
(ptk/reify ::update-dimensions
ptk/UpdateEvent
(update [_ state]
(let [objects (wsh/lookup-page-objects state)
get-modifier
(fn [shape] (ctm/change-dimensions-modifiers shape attr value))
modif-tree
(-> (dwm/build-modif-tree ids objects get-modifier)
(gm/set-objects-modifiers objects))]
modif-tree
(-> (dwm/build-modif-tree ids objects get-modifier)
(gm/set-objects-modifiers objects))]
(assoc state :workspace-modifiers modif-tree)))
(assoc state :workspace-modifiers modif-tree)))
ptk/WatchEvent
(watch [_ _ _]
(rx/of (dwm/apply-modifiers options)))))
ptk/WatchEvent
(watch [_ _ _]
(rx/of (dwm/apply-modifiers options))))))
(defn change-orientation
"Change orientation of shapes, from the sidebar options form.

View file

@ -468,7 +468,7 @@
(def workspace-active-theme-paths
(l/derived (d/nilf ctob/get-active-theme-paths) tokens-lib))
(defn token-sets-at-path-all-active?
(defn token-sets-at-path-all-active
[prefixed-path]
(l/derived
(fn [lib]

View file

@ -162,8 +162,8 @@
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))))))
(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- attributes->layout-gap [attributes value]
(let [layout-gap (-> (set/intersection attributes #{:column-gap :row-gap})
@ -173,7 +173,7 @@
(defn update-layout-padding [value shape-ids attrs]
(dwsl/update-layout shape-ids
{:layout-padding (zipmap attrs (repeat value))}
:ignore-touched true))
{:ignore-touched true}))
(defn update-layout-spacing [value shape-ids attributes]
(ptk/reify ::update-layout-spacing
@ -187,7 +187,7 @@
(rx/of
(dwsl/update-layout layout-shape-ids
layout-attributes
:ignore-touched true))))))
{:ignore-touched true}))))))
(defn update-shape-position [value shape-ids attributes]
(ptk/reify ::update-shape-position
@ -205,4 +205,4 @@
:layout-item-max-w value
:layout-item-max-h value}
(select-keys attributes))]
(dwsl/update-layout-child shape-ids props :ignore-touched true)))))
(dwsl/update-layout-child shape-ids props {:ignore-touched true})))))

View file

@ -357,7 +357,7 @@
(get active-token-set-names set-name)))
token-set-group-active? (mf/use-fn
(fn [prefixed-path]
@(refs/token-sets-at-path-all-active? prefixed-path)))]
@(refs/token-sets-at-path-all-active prefixed-path)))]
[:& controlled-sets-list
{:token-sets token-sets
:token-set-selected? token-set-selected?