0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-04-13 07:21:40 -05:00

♻️ Changes to update-shape parameters

This commit is contained in:
alonso.torres 2024-01-02 17:59:07 +01:00 committed by Andrey Antukh
parent 48e283812e
commit c9200f235e
12 changed files with 76 additions and 63 deletions

View file

@ -358,13 +358,15 @@
(defn changed-attrs
"Returns the list of attributes that will change when `update-fn` is applied"
[object objects update-fn {:keys [attrs]}]
[object objects update-fn {:keys [attrs with-objects?]}]
(let [changed?
(fn [old new attr]
(let [old-val (get old attr)
new-val (get new attr)]
(not= old-val new-val)))
new-obj (update-fn object objects)]
new-obj (if with-objects?
(update-fn object objects)
(update-fn object))]
(when-not (= object new-obj)
(let [attrs (or attrs (d/concat-set (keys object) (keys new-obj)))]
(filter (partial changed? object new-obj) attrs)))))
@ -375,8 +377,8 @@
([changes ids update-fn]
(update-shapes changes ids update-fn nil))
([changes ids update-fn {:keys [attrs ignore-geometry? ignore-touched]
:or {ignore-geometry? false ignore-touched false}}]
([changes ids update-fn {:keys [attrs ignore-geometry? ignore-touched with-objects?]
:or {ignore-geometry? false ignore-touched false with-objects? false}}]
(assert-container-id! changes)
(assert-objects! changes)
(let [page-id (::page-id (meta changes))
@ -412,7 +414,7 @@
update-shape
(fn [changes id]
(let [old-obj (get objects id)
new-obj (update-fn old-obj objects)]
new-obj (if with-objects? (update-fn old-obj objects) (update-fn old-obj))]
(if (= old-obj new-obj)
changes
(let [[rops uops] (-> (or attrs (d/concat-set (keys old-obj) (keys new-obj)))

View file

@ -34,7 +34,7 @@
(cond-> (some? cell)
(pcb/update-shapes [(:parent-id shape)] #(ctl/push-into-cell % [id] row column)))
(cond-> (ctl/grid-layout? objects (:parent-id shape))
(pcb/update-shapes [(:parent-id shape)] ctl/assign-cells)))]
(pcb/update-shapes [(:parent-id shape)] ctl/assign-cells {:with-objects? true})))]
[shape changes]))
(defn prepare-move-shapes-into-frame
@ -50,7 +50,7 @@
(pcb/update-shapes ordered-indexes #(cond-> % (cfh/frame-shape? %) (assoc :hide-in-viewer true)))
(pcb/change-parent frame-id to-move-shapes 0)
(cond-> (ctl/grid-layout? objects frame-id)
(pcb/update-shapes [frame-id] ctl/assign-cells))
(pcb/update-shapes [frame-id] ctl/assign-cells {:with-objects? true}))
(pcb/reorder-grid-children [frame-id]))
changes)))
@ -113,7 +113,7 @@
(assoc :layout-grid-cells (:layout-grid-cells base-parent))
(assoc-in [:layout-grid-cells target-cell-id :shapes] [id])
(assoc :position :auto)))))
(pcb/update-shapes [(:parent-id shape)] ctl/assign-cells)
(pcb/update-shapes [(:parent-id shape)] ctl/assign-cells {:with-objects? true})
(pcb/reorder-grid-children [(:parent-id shape)])))]
[shape changes])))))
@ -163,7 +163,7 @@
(assoc :layout-grid-cells (:layout-grid-cells base-parent))
(assoc-in [:layout-grid-cells target-cell-id :shapes] [frame-id])
(assoc :position :auto)))))
(pcb/update-shapes [(:parent-id shape)] ctl/assign-cells)
(pcb/update-shapes [(:parent-id shape)] ctl/assign-cells {:with-objects? true})
(pcb/reorder-grid-children [(:parent-id shape)])))]
[shape changes]))

View file

@ -855,7 +855,8 @@
(fn [parent objects]
(cond-> parent
(ctl/grid-layout? parent)
(ctl/assign-cells objects))))
(ctl/assign-cells objects)))
{:with-objects? true})
(pcb/reorder-grid-children parents)

View file

@ -51,8 +51,8 @@
(defn update-shapes
([ids update-fn] (update-shapes ids update-fn nil))
([ids update-fn {:keys [reg-objects? save-undo? stack-undo? attrs ignore-tree page-id ignore-remote? ignore-touched undo-group]
:or {reg-objects? false save-undo? true stack-undo? false ignore-remote? false ignore-touched false}}]
([ids update-fn {:keys [reg-objects? save-undo? stack-undo? attrs ignore-tree page-id ignore-remote? ignore-touched undo-group with-objects?]
:or {reg-objects? false save-undo? true stack-undo? false ignore-remote? false ignore-touched false with-objects? false}}]
(dm/assert!
"expected a valid coll of uuid's"
@ -70,14 +70,15 @@
update-layout-ids
(->> ids
(map (d/getf objects))
(filter #(some update-layout-attr? (pcb/changed-attrs % objects update-fn {:attrs attrs})))
(filter #(some update-layout-attr? (pcb/changed-attrs % objects update-fn {:attrs attrs :with-objects? with-objects?})))
(map :id))
changes (reduce
(fn [changes id]
(let [opts {:attrs attrs
:ignore-geometry? (get ignore-tree id)
:ignore-touched ignore-touched}]
:ignore-touched ignore-touched
:with-objects? with-objects?}]
(pcb/update-shapes changes [id] update-fn (d/without-nils opts))))
(-> (pcb/empty-changes it page-id)
(pcb/set-save-undo? save-undo?)

View file

@ -13,7 +13,7 @@
[app.common.schema :as sm]
[app.common.types.shape-tree :as ctst]
[app.main.data.comments :as dcm]
[app.main.data.workspace.changes :as dwc]
[app.main.data.workspace.changes :as dch]
[app.main.data.workspace.common :as dwco]
[app.main.data.workspace.drawing :as dwd]
[app.main.data.workspace.state-helpers :as wsh]
@ -148,7 +148,7 @@
(pcb/update-page-option :comment-threads-position assoc thread-id (select-keys thread [:position :frame-id])))]
(rx/merge
(rx/of (dwc/commit-changes changes))
(rx/of (dch/commit-changes changes))
(->> (rp/cmd! :update-comment-thread-position thread)
(rx/catch #(rx/throw {:type :update-comment-thread-position}))
(rx/ignore))))))))

View file

@ -125,7 +125,7 @@
[parent-id]
(fn [parent]
(assoc-in parent [:layout-grid-cells (:id target-cell) :shapes] [(:id group)]))))
(pcb/update-shapes grid-parents ctl/assign-cells)
(pcb/update-shapes grid-parents ctl/assign-cells {:with-objects? true})
(pcb/remove-objects ids-to-delete))]
[group changes]))
@ -216,7 +216,7 @@
(cond-> changes
(ctl/grid-layout? objects (:parent-id shape))
(pcb/update-shapes [(:parent-id shape)] ctl/assign-cells))))
(pcb/update-shapes [(:parent-id shape)] ctl/assign-cells {:with-objects? true}))))
selected (->> (wsh/lookup-selected state)
(remove #(ctn/has-any-copy-parent? objects (get objects %)))

View file

@ -11,7 +11,7 @@
[app.common.geom.point :as gpt]
[app.common.geom.shapes :as gsh]
[app.common.types.page :as ctp]
[app.main.data.workspace.changes :as dwc]
[app.main.data.workspace.changes :as dch]
[app.main.data.workspace.state-helpers :as wsh]
[beicon.v2.core :as rx]
[potok.v2.core :as ptk]))
@ -35,7 +35,7 @@
(-> (pcb/empty-changes it)
(pcb/with-page page)
(pcb/update-page-option :guides assoc (:id guide) guide))]
(rx/of (dwc/commit-changes changes))))))
(rx/of (dch/commit-changes changes))))))
(defn remove-guide [guide]
(dm/assert!
@ -56,7 +56,7 @@
(-> (pcb/empty-changes it)
(pcb/with-page page)
(pcb/update-page-option :guides dissoc (:id guide)))]
(rx/of (dwc/commit-changes changes))))))
(rx/of (dch/commit-changes changes))))))
(defn remove-guides
[ids]

View file

@ -185,7 +185,8 @@
(fn [shape objects]
(-> shape
(ctl/push-into-cell [(:id first-shape)] row column)
(ctl/assign-cells objects))))
(ctl/assign-cells objects)))
{:with-objects? true})
(pcb/reorder-grid-children [(:parent-id first-shape)])))
changes)

View file

@ -499,8 +499,8 @@
changes (-> (pcb/add-object changes new-obj {:ignore-touched (and duplicating-component? child?)})
(pcb/amend-last-change #(assoc % :old-id (:id obj)))
(cond-> (ctl/grid-layout? objects (:parent-id obj))
(-> (pcb/update-shapes [(:parent-id obj)] ctl/assign-cells)
(pcb/update-shapes [(:parent-id obj)] ctl/check-deassigned-cells)
(-> (pcb/update-shapes [(:parent-id obj)] ctl/assign-cells {:with-objects? true})
(pcb/update-shapes [(:parent-id obj)] ctl/check-deassigned-cells {:with-objects? true})
(pcb/reorder-grid-children [(:parent-id obj)]))))
changes (cond-> changes

View file

@ -19,7 +19,7 @@
[app.common.types.modifiers :as ctm]
[app.common.types.shape.layout :as ctl]
[app.common.uuid :as uuid]
[app.main.data.workspace.changes :as dwc]
[app.main.data.workspace.changes :as dch]
[app.main.data.workspace.colors :as cl]
[app.main.data.workspace.grid-layout.editor :as dwge]
[app.main.data.workspace.modifiers :as dwm]
@ -71,13 +71,13 @@
:layout-grid-columns []})
(defn get-layout-initializer
[type from-frame? objects]
[type from-frame?]
(let [[initial-layout-data calculate-params]
(case type
:flex [initial-flex-layout flex/calculate-params]
:grid [initial-grid-layout grid/calculate-params])]
(fn [shape]
(fn [shape objects]
(let [shape
(-> shape
(merge initial-layout-data)
@ -130,11 +130,11 @@
(let [objects (wsh/lookup-page-objects state)
parent (get objects id)
undo-id (js/Symbol)
layout-initializer (get-layout-initializer type from-frame? objects)]
layout-initializer (get-layout-initializer type from-frame?)]
(rx/of (dwu/start-undo-transaction undo-id)
(dwc/update-shapes [id] layout-initializer)
(dwc/update-shapes (dm/get-prop parent :shapes) #(dissoc % :constraints-h :constraints-v))
(dch/update-shapes [id] layout-initializer {:with-objects? true})
(dch/update-shapes (dm/get-prop parent :shapes) #(dissoc % :constraints-h :constraints-v))
(ptk/data-event :layout/update [id])
(dwu/commit-undo-transaction undo-id))))))
@ -173,8 +173,8 @@
(dws/create-artboard-from-selection new-shape-id parent-id group-index (:name (first selected-shapes)))
(cl/remove-all-fills [new-shape-id] {:color clr/black :opacity 1})
(create-layout-from-id new-shape-id type false)
(dwc/update-shapes [new-shape-id] #(assoc % :layout-item-h-sizing :auto :layout-item-v-sizing :auto))
(dwc/update-shapes selected #(assoc % :layout-item-h-sizing :fix :layout-item-v-sizing :fix))
(dch/update-shapes [new-shape-id] #(assoc % :layout-item-h-sizing :auto :layout-item-v-sizing :auto))
(dch/update-shapes selected #(assoc % :layout-item-h-sizing :fix :layout-item-v-sizing :fix))
(dws/delete-shapes page-id selected)
(ptk/data-event :layout/update [new-shape-id])
(dwu/commit-undo-transaction undo-id)))
@ -184,8 +184,8 @@
(dws/create-artboard-from-selection new-shape-id)
(cl/remove-all-fills [new-shape-id] {:color clr/black :opacity 1})
(create-layout-from-id new-shape-id type false)
(dwc/update-shapes [new-shape-id] #(assoc % :layout-item-h-sizing :auto :layout-item-v-sizing :auto))
(dwc/update-shapes selected #(assoc % :layout-item-h-sizing :fix :layout-item-v-sizing :fix))))
(dch/update-shapes [new-shape-id] #(assoc % :layout-item-h-sizing :auto :layout-item-v-sizing :auto))
(dch/update-shapes selected #(assoc % :layout-item-h-sizing :fix :layout-item-v-sizing :fix))))
(rx/of (ptk/data-event :layout/update [new-shape-id])
(dwu/commit-undo-transaction undo-id)))))))
@ -198,7 +198,7 @@
(let [undo-id (js/Symbol)]
(rx/of
(dwu/start-undo-transaction undo-id)
(dwc/update-shapes ids #(apply dissoc % layout-keys))
(dch/update-shapes ids #(apply dissoc % layout-keys))
(ptk/data-event :layout/update ids)
(dwu/commit-undo-transaction undo-id))))))
@ -245,7 +245,7 @@
(watch [_ _ _]
(let [undo-id (js/Symbol)]
(rx/of (dwu/start-undo-transaction undo-id)
(dwc/update-shapes ids (d/patch-object changes))
(dch/update-shapes ids (d/patch-object changes))
(ptk/data-event :layout/update ids)
(dwu/commit-undo-transaction undo-id))))))
@ -259,7 +259,7 @@
(watch [_ _ _]
(let [undo-id (js/Symbol)]
(rx/of (dwu/start-undo-transaction undo-id)
(dwc/update-shapes
(dch/update-shapes
ids
(fn [shape]
(case type
@ -277,12 +277,13 @@
(watch [_ _ _]
(let [undo-id (js/Symbol)]
(rx/of (dwu/start-undo-transaction undo-id)
(dwc/update-shapes
(dch/update-shapes
ids
(fn [shape objects]
(case type
:row (ctl/remove-grid-row shape index objects)
:column (ctl/remove-grid-column shape index objects))))
:column (ctl/remove-grid-column shape index objects)))
{:with-objects? true})
(ptk/data-event :layout/update ids)
(dwu/commit-undo-transaction undo-id))))))
@ -335,7 +336,7 @@
undo-id (js/Symbol)]
(rx/of (dwu/start-undo-transaction undo-id)
(dwc/commit-changes changes)
(dch/commit-changes changes)
(ptk/data-event :layout/update ids)
(dwu/commit-undo-transaction undo-id))))))
@ -348,7 +349,7 @@
(watch [_ _ _]
(let [undo-id (js/Symbol)]
(rx/of (dwu/start-undo-transaction undo-id)
(dwc/update-shapes
(dch/update-shapes
ids
(fn [shape]
(case type
@ -394,7 +395,7 @@
:row :layout-grid-rows
:column :layout-grid-columns)]
(rx/of (dwu/start-undo-transaction undo-id)
(dwc/update-shapes
(dch/update-shapes
ids
(fn [shape]
(-> shape
@ -486,15 +487,16 @@
parent-ids (->> ids (map #(cfh/get-parent-id objects %)))
undo-id (js/Symbol)]
(rx/of (dwu/start-undo-transaction undo-id)
(dwc/update-shapes ids (d/patch-object changes))
(dwc/update-shapes children-ids (partial fix-child-sizing objects changes))
(dwc/update-shapes
(dch/update-shapes ids (d/patch-object changes))
(dch/update-shapes children-ids (partial fix-child-sizing objects changes))
(dch/update-shapes
parent-ids
(fn [parent objects]
(-> parent
(fix-parent-sizing objects (set ids) changes)
(cond-> (ctl/grid-layout? parent)
(ctl/assign-cells objects)))))
(ctl/assign-cells objects))))
{:with-objects? true})
(ptk/data-event :layout/update ids)
(dwu/commit-undo-transaction undo-id))))))
@ -507,7 +509,7 @@
(rx/of
(dwu/start-undo-transaction undo-id)
(dwc/update-shapes
(dch/update-shapes
[layout-id]
(fn [shape]
(->> ids
@ -530,7 +532,7 @@
(let [undo-id (js/Symbol)]
(rx/of
(dwu/start-undo-transaction undo-id)
(dwc/update-shapes
(dch/update-shapes
[layout-id]
(fn [shape objects]
(case mode
@ -579,7 +581,8 @@
(ctl/assign-cells objects))]
(-> shape
(d/update-in-when [:layout-grid-cells (:id target-cell)] assoc :position :area))))))
(d/update-in-when [:layout-grid-cells (:id target-cell)] assoc :position :area)))))
{:with-objects? true})
(dwge/clean-selection layout-id)
(ptk/data-event :layout/update [layout-id])
(dwu/commit-undo-transaction undo-id))))))
@ -593,7 +596,7 @@
(let [undo-id (js/Symbol)]
(rx/of
(dwu/start-undo-transaction undo-id)
(dwc/update-shapes
(dch/update-shapes
[layout-id]
(fn [shape objects]
(let [cells (->> ids (map #(get-in shape [:layout-grid-cells %])))
@ -610,7 +613,8 @@
first-column
(inc (- last-row first-row))
(inc (- last-column first-column)))
(ctl/assign-cells objects)))))
(ctl/assign-cells objects))))
{:with-objects? true})
(dwge/clean-selection layout-id)
(ptk/data-event :layout/update [layout-id])
(dwu/commit-undo-transaction undo-id))))))
@ -620,14 +624,13 @@
(ptk/reify ::update-grid-cell-position
ptk/WatchEvent
(watch [_ state _]
(let [objects (wsh/lookup-page-objects state)
undo-id (js/Symbol)]
(watch [_ _ _]
(let [undo-id (js/Symbol)]
(rx/of
(dwu/start-undo-transaction undo-id)
(dwc/update-shapes
(dch/update-shapes
[layout-id]
(fn [shape]
(fn [shape objects]
(let [prev-data (-> (dm/get-in shape [:layout-grid-cells cell-id])
(select-keys [:row :column :row-span :column-span]))
@ -636,7 +639,8 @@
(ctl/resize-cell-area (:row prev-data) (:column prev-data)
(:row new-data) (:column new-data)
(:row-span new-data) (:column-span new-data))
(ctl/assign-cells objects)))))
(ctl/assign-cells objects))))
{:with-objects? true})
(ptk/data-event :layout/update [layout-id])
(dwu/commit-undo-transaction undo-id))))))
@ -691,6 +695,6 @@
(rx/of
(dwu/start-undo-transaction undo-id)
(dwc/commit-changes changes)
(dch/commit-changes changes)
(ptk/data-event :layout/update [layout-id])
(dwu/commit-undo-transaction undo-id))))))

View file

@ -628,9 +628,13 @@
(ctl/swap-shapes id (:id next-cell)))))
parent))]
(-> changes
(pcb/update-shapes [(:id parent)] (fn [shape] (-> shape
(assoc :layout-grid-cells layout-grid-cells)
(ctl/assign-cells objects))))
(pcb/update-shapes
[(:id parent)]
(fn [shape]
(-> shape
(assoc :layout-grid-cells layout-grid-cells)
;; We want the previous objects value
(ctl/assign-cells objects))))
(pcb/reorder-grid-children [(:id parent)]))))
changes

View file

@ -21,7 +21,7 @@
[app.main.data.preview :as dp]
[app.main.data.viewer.shortcuts]
[app.main.data.workspace :as dw]
[app.main.data.workspace.changes :as dwc]
[app.main.data.workspace.changes :as dch]
[app.main.data.workspace.path.shortcuts]
[app.main.data.workspace.selection :as dws]
[app.main.data.workspace.shortcuts]
@ -297,7 +297,7 @@
(let [file-id (:current-file-id @st/state)
changes (t/decode-str changes*)]
(st/emit! (dwc/commit-changes {:redo-changes changes
(st/emit! (dch/commit-changes {:redo-changes changes
:undo-changes []
:save-undo? true
:file-id file-id}))))