mirror of
https://github.com/penpot/penpot.git
synced 2026-11-21 01:22:01 +00:00
🐛 Ungroup the copies of a group a main ungroups in the same change
Ungrouping a group, bool or board inside a main removed it, while its copies kept a shape that referred to it until the next sync, so the file failed validation in between. The ungroup now takes the file's pages and ungroups every shape of the file that refers to an ungrouped one, directly or through other copies, in the same change: its children move to its parent at its place, keeping their overrides, and it is removed. The frontend's ungroup passes the file's pages. Test: ungrouping-a-group-a-main-holds-leaves-no-copy-referring-to-it (the copy's rectangle moves to the copy root with its opacity override). Benchmark (scripts/bench-components, ms, lower 2.5% quantile): | :world | :baseline | :now | :mean | :ratio | |------------------------------------+-----------+--------+---------+--------| | deepest-edit-through-three-levels | 35.215 | 21.54 | 21.937 | 0.61 | | edit-through-a-chain-of-dependents | 68.931 | 42.565 | 43.748 | 0.62 | | library-update-into-copies | 83.083 | 58.686 | 78.974 | 0.71 | | main-edit-among-many-shapes | 4.837 | 2.503 | 3.171 | 0.52 | | main-edit-into-10-copies | 1.79 | 1.039 | 1.105 | 0.58 | | main-edit-into-100-copies | 15.415 | 10.205 | 10.895 | 0.66 | | main-edit-into-1000-copies | 153.086 | 102.64 | 106.511 | 0.67 | | variant-switch-into-copies | 32.052 | 21.481 | 22.871 | 0.67 | AI-assisted-by: claude-opus-5-5
This commit is contained in:
parent
8bf641ee81
commit
59e8da6070
@ -144,41 +144,45 @@
|
||||
[group changes]))
|
||||
|
||||
(defn remove-group-changes
|
||||
[it page-id group objects]
|
||||
(let [children (->> (:shapes group)
|
||||
(cfh/order-by-indexed-shapes objects)
|
||||
(mapv #(get objects %)))
|
||||
parent-id (cfh/get-parent-id objects (:id group))
|
||||
parent (get objects parent-id)
|
||||
([it page-id group objects]
|
||||
(remove-group-changes it page-id group objects nil))
|
||||
([it page-id group objects options]
|
||||
(let [children (->> (:shapes group)
|
||||
(cfh/order-by-indexed-shapes objects)
|
||||
(mapv #(get objects %)))
|
||||
parent-id (cfh/get-parent-id objects (:id group))
|
||||
parent (get objects parent-id)
|
||||
|
||||
index-in-parent
|
||||
(->> (:shapes parent)
|
||||
(map-indexed vector)
|
||||
(filter #(#{(:id group)} (second %)))
|
||||
(ffirst)
|
||||
inc)]
|
||||
index-in-parent
|
||||
(->> (:shapes parent)
|
||||
(map-indexed vector)
|
||||
(filter #(#{(:id group)} (second %)))
|
||||
(ffirst)
|
||||
inc)]
|
||||
|
||||
(-> (pcb/empty-changes it page-id)
|
||||
(pcb/with-objects objects)
|
||||
(pcb/change-parent parent-id children index-in-parent)
|
||||
(pcb/remove-objects [(:id group)]))))
|
||||
(-> (pcb/empty-changes it page-id)
|
||||
(pcb/with-objects objects)
|
||||
(pcb/change-parent parent-id children index-in-parent options)
|
||||
(pcb/remove-objects [(:id group)] options)))))
|
||||
|
||||
(defn remove-frame-changes
|
||||
[it page-id frame objects]
|
||||
(let [children (->> (:shapes frame)
|
||||
(cfh/order-by-indexed-shapes objects)
|
||||
(mapv #(get objects %)))
|
||||
parent-id (cfh/get-parent-id objects (:id frame))
|
||||
idx-in-parent (->> (:id frame)
|
||||
(cfh/get-position-on-parent objects)
|
||||
inc)]
|
||||
([it page-id frame objects]
|
||||
(remove-frame-changes it page-id frame objects nil))
|
||||
([it page-id frame objects options]
|
||||
(let [children (->> (:shapes frame)
|
||||
(cfh/order-by-indexed-shapes objects)
|
||||
(mapv #(get objects %)))
|
||||
parent-id (cfh/get-parent-id objects (:id frame))
|
||||
idx-in-parent (->> (:id frame)
|
||||
(cfh/get-position-on-parent objects)
|
||||
inc)]
|
||||
|
||||
(-> (pcb/empty-changes it page-id)
|
||||
(pcb/with-objects objects)
|
||||
(cond-> (ctl/any-layout? frame)
|
||||
(pcb/update-shapes (:shapes frame) ctl/remove-layout-item-data))
|
||||
(pcb/change-parent parent-id children idx-in-parent)
|
||||
(pcb/remove-objects [(:id frame)]))))
|
||||
(-> (pcb/empty-changes it page-id)
|
||||
(pcb/with-objects objects)
|
||||
(cond-> (ctl/any-layout? frame)
|
||||
(pcb/update-shapes (:shapes frame) ctl/remove-layout-item-data (select-keys options [:ignore-touched])))
|
||||
(pcb/change-parent parent-id children idx-in-parent options)
|
||||
(pcb/remove-objects [(:id frame)] options)))))
|
||||
|
||||
(defn ungroup-ids
|
||||
"The shapes of `ids` an ungroup takes apart: not inside a copy, not a
|
||||
@ -189,29 +193,65 @@
|
||||
(remove #(ctk/instance-head? (get objects %)))
|
||||
(remove #(ctk/is-variant-container? (get objects %)))))
|
||||
|
||||
(defn- copies-of
|
||||
"The `[page-id id]` of the shapes of `pages-index` that refer to a shape
|
||||
of `ids`, directly or through other copies."
|
||||
[pages-index ids]
|
||||
(let [ref-index (reduce-kv (fn [index page-id page]
|
||||
(reduce (fn [index shape]
|
||||
(cond-> index
|
||||
(:shape-ref shape)
|
||||
(update (:shape-ref shape) (fnil conj []) [page-id (:id shape)])))
|
||||
index
|
||||
(vals (:objects page))))
|
||||
{}
|
||||
pages-index)]
|
||||
(loop [pending (seq ids) result []]
|
||||
(if-let [id (first pending)]
|
||||
(let [hits (get ref-index id)]
|
||||
(recur (concat (rest pending) (map second hits)) (into result hits)))
|
||||
result))))
|
||||
|
||||
(defn- ungroup-shape-changes
|
||||
"The changes that ungroup the group, bool or board `shape-id` of the
|
||||
page `page-id`, whose shapes are `objects`; `options` of the moves and
|
||||
removals."
|
||||
[it page-id objects shape-id options]
|
||||
(let [shape (get objects shape-id)
|
||||
changes
|
||||
(cond
|
||||
(or (cfh/group-shape? shape) (cfh/bool-shape? shape))
|
||||
(remove-group-changes it page-id shape objects options)
|
||||
|
||||
(cfh/frame-shape? shape)
|
||||
(remove-frame-changes it page-id shape objects options))]
|
||||
|
||||
(cond-> changes
|
||||
(and changes (ctl/grid-layout? objects (:parent-id shape)))
|
||||
(pcb/update-shapes [(:parent-id shape)] ctl/assign-cells
|
||||
(merge {:with-objects? true} (select-keys options [:ignore-touched]))))))
|
||||
|
||||
(defn generate-ungroup-changes
|
||||
"The changes that ungroup the groups, bools and boards of `ids`, which
|
||||
`ungroup-ids` gives."
|
||||
[it page-id objects ids]
|
||||
(let [prepare
|
||||
(fn [shape-id]
|
||||
(let [shape (get objects shape-id)
|
||||
changes
|
||||
(cond
|
||||
(or (cfh/group-shape? shape) (cfh/bool-shape? shape))
|
||||
(remove-group-changes it page-id shape objects)
|
||||
|
||||
(cfh/frame-shape? shape)
|
||||
(remove-frame-changes it page-id shape objects))]
|
||||
|
||||
(cond-> changes
|
||||
(ctl/grid-layout? objects (:parent-id shape))
|
||||
(pcb/update-shapes [(:parent-id shape)] ctl/assign-cells {:with-objects? true}))))
|
||||
|
||||
changes-list (sequence (keep prepare) ids)]
|
||||
{:redo-changes (vec (mapcat :redo-changes changes-list))
|
||||
:undo-changes (vec (mapcat :undo-changes changes-list))
|
||||
:origin it}))
|
||||
`ungroup-ids` gives. With the file's `pages-index`, the copies of each
|
||||
one in the file are ungrouped alike: their children keep their
|
||||
overrides and nothing is left referring to a removed shape."
|
||||
([it page-id objects ids]
|
||||
(generate-ungroup-changes it page-id objects ids nil))
|
||||
([it page-id objects ids pages-index]
|
||||
(let [copies (copies-of pages-index ids)
|
||||
changes-list (concat
|
||||
(keep #(ungroup-shape-changes it page-id objects % nil) ids)
|
||||
(keep (fn [[copy-page-id id]]
|
||||
(let [copy-objects (if (= copy-page-id page-id)
|
||||
objects
|
||||
(get-in pages-index [copy-page-id :objects]))]
|
||||
(ungroup-shape-changes it copy-page-id copy-objects id
|
||||
{:allow-altering-copies true :ignore-touched true})))
|
||||
copies))]
|
||||
{:redo-changes (vec (mapcat :redo-changes changes-list))
|
||||
:undo-changes (vec (mapcat :undo-changes changes-list))
|
||||
:origin it})))
|
||||
|
||||
(defn generate-mask-changes
|
||||
"The group that masks `shapes`, which `shapes-for-group-or-mask` gives,
|
||||
|
||||
@ -9,6 +9,7 @@
|
||||
[app.common.files.changes-builder :as pcb]
|
||||
[app.common.logic.bool :as clb]
|
||||
[app.common.logic.groups :as clg]
|
||||
[app.common.test-helpers.components :as thc]
|
||||
[app.common.test-helpers.compositions :as tho]
|
||||
[app.common.test-helpers.files :as thf]
|
||||
[app.common.test-helpers.ids-map :as thi]
|
||||
@ -100,3 +101,29 @@
|
||||
file (thf/apply-changes file changes)
|
||||
grid (get (objects file) (thi/id :grid))]
|
||||
(t/is (some #(= [(:id shape)] (:shapes %)) (vals (:layout-grid-cells grid))))))
|
||||
|
||||
(t/deftest ungrouping-a-group-a-main-holds-leaves-no-copy-referring-to-it
|
||||
(let [file (-> (thf/sample-file :file1)
|
||||
(tho/add-frame :main-root)
|
||||
(ths/add-sample-shape :rect :parent-label :main-root))
|
||||
page (thf/current-page file)
|
||||
[group changes] (clg/prepare-create-group (pcb/empty-changes nil) (uuid/next) (:objects page) (:id page)
|
||||
(clg/shapes-for-group-or-mask (:objects page) [(thi/id :rect)])
|
||||
"Group" false)
|
||||
file (-> (thf/apply-changes file changes)
|
||||
(thc/make-component :component :main-root)
|
||||
(thc/instantiate-component :component :copy-root))
|
||||
copy-group (first (:shapes (get (objects file) (thi/id :copy-root))))
|
||||
copy-rect (first (:shapes (get (objects file) copy-group)))
|
||||
file (-> file
|
||||
(ths/update-shape-by-id copy-rect :opacity 0.5)
|
||||
(ths/update-shape-by-id copy-rect :touched #{:layer-effects-group}))
|
||||
page (thf/current-page file)
|
||||
ids (clg/ungroup-ids (:objects page) [(:id group)])
|
||||
file (thf/apply-changes file (clg/generate-ungroup-changes nil (:id page) (:objects page) ids
|
||||
(get-in file [:data :pages-index])))
|
||||
rect (get (objects file) copy-rect)]
|
||||
(t/is (nil? (get (objects file) copy-group)))
|
||||
(t/is (= (thi/id :copy-root) (:parent-id rect)))
|
||||
(t/is (= 0.5 (:opacity rect)))
|
||||
(t/is (= #{:layer-effects-group} (:touched rect)))))
|
||||
|
||||
@ -67,7 +67,8 @@
|
||||
|
||||
child-ids (clg/ungrouped-children objects ids)
|
||||
|
||||
changes (clg/generate-ungroup-changes it page-id objects ids)
|
||||
changes (clg/generate-ungroup-changes it page-id objects ids
|
||||
(:pages-index (dsh/lookup-file-data state)))
|
||||
undo-id (js/Symbol)]
|
||||
|
||||
(when-not (empty? ids)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user