mirror of
https://github.com/penpot/penpot.git
synced 2026-11-28 14:14:23 +00:00
🐛 Restore a deleted main at its old place in a flex layout
Restoring a deleted variant put its main last in the variant set: Value 1, first at X 30, came back after Value 2 at X 150. A restored main was always added at the end of its parent's layout order. A deleted component keeps the position its main had. When the parent has a flex layout, the restored main now goes before the first sibling laid out at or after that position, which is where the layout put the siblings that followed it; with none, it goes last as before. Other parents keep adding it last. Found by the components Qase run (PENPOT-2416). Test: `restored-variant-takes-its-old-place-in-a-flex-set`, `restored-last-variant-stays-last-in-a-flex-set` (comp-creation-test); composable, golden master and variant suites pass. Checked in the browser: deleting the first variant of a set and restoring it puts it back first. The frontend suites were not run (memory). AI-assisted-by: claude-opus-5-5
This commit is contained in:
parent
306862b11c
commit
7d125eac02
@ -433,22 +433,52 @@
|
||||
|
||||
[root (:id root-shape) changes]))
|
||||
|
||||
(defn- laid-out-from?
|
||||
"Whether `child` of the flex `parent` is laid out at or after `position`,
|
||||
the old place of a shape that left the layout."
|
||||
[parent position child]
|
||||
(let [row? (ctsl/row? parent)
|
||||
sign (if (ctsl/reverse? parent) -1 1)
|
||||
main (fn [p] (* sign (if row? (:x p) (:y p))))
|
||||
cross (fn [p] (if row? (:y p) (:x p)))
|
||||
tolerance 0.5]
|
||||
(if (and (ctsl/wrap? parent)
|
||||
(> (cross child) (+ (cross position) tolerance)))
|
||||
true
|
||||
(and (>= (+ (cross child) tolerance) (cross position))
|
||||
(>= (+ (main child) tolerance) (main position))))))
|
||||
|
||||
(defn- restored-flex-children
|
||||
"The children of the flex `parent` with `shape` restored at its old place:
|
||||
before the first child laid out at or after the position it was saved
|
||||
with, or last when there is none."
|
||||
[objects parent shape]
|
||||
(let [reverse? (ctsl/reverse? parent)
|
||||
;; A non-reversed flex lays its children out last to first
|
||||
in-order (fn [ids] (if reverse? (vec ids) (vec (rseq (vec ids)))))
|
||||
laid-out (in-order (:shapes parent))
|
||||
position (select-keys shape [:x :y])
|
||||
index (d/index-of-pred laid-out #(some->> (get objects %) (laid-out-from? parent position)))
|
||||
laid-out (if (some? index)
|
||||
(d/insert-at-index laid-out index [(:id shape)])
|
||||
(conj laid-out (:id shape)))]
|
||||
(in-order laid-out)))
|
||||
|
||||
(defn generate-restore-component
|
||||
"Restore a deleted component, with the given id, in the given file library."
|
||||
"Restore a deleted component, with the given id, in the given file library.
|
||||
In a flex layout the main takes back its old place among its siblings."
|
||||
[changes library-data component-id library-id current-page objects]
|
||||
(let [{:keys [changes shape]} (prepare-restore-component changes library-data component-id current-page)
|
||||
parent-id (:parent-id shape)
|
||||
|
||||
insert-before?
|
||||
(and (ctsl/flex-layout? objects parent-id)
|
||||
(not (ctsl/reverse? objects parent-id)))
|
||||
parent (get objects parent-id)
|
||||
|
||||
objects
|
||||
(-> objects
|
||||
(assoc (:id shape) shape)
|
||||
(cond-> (and (some? parent-id) insert-before?)
|
||||
(update-in [parent-id :shapes] #(d/concat-vec [(:id shape)] %)))
|
||||
(cond-> (and (some? parent-id) (not insert-before?))
|
||||
(cond-> (and (some? parent) (ctsl/flex-layout? parent))
|
||||
(assoc-in [parent-id :shapes]
|
||||
(restored-flex-children objects parent shape)))
|
||||
(cond-> (and (some? parent) (not (ctsl/flex-layout? parent)))
|
||||
(update-in [parent-id :shapes] conj (:id shape))))
|
||||
|
||||
;; Adds a resize-parents operation so the groups are updated. We add all the new objects
|
||||
|
||||
@ -11,6 +11,7 @@
|
||||
[app.common.files.shapes-helpers :as cfsh]
|
||||
[app.common.files.validate :as cfv]
|
||||
[app.common.geom.point :as gpt]
|
||||
[app.common.geom.shapes :as gsh]
|
||||
[app.common.logic.duplicate :as cld]
|
||||
[app.common.logic.instances :as cli]
|
||||
[app.common.logic.libraries :as cll]
|
||||
@ -20,6 +21,7 @@
|
||||
[app.common.test-helpers.files :as thf]
|
||||
[app.common.test-helpers.ids-map :as thi]
|
||||
[app.common.test-helpers.shapes :as ths]
|
||||
[app.common.test-helpers.variants :as thv]
|
||||
[app.common.types.component :as ctk]
|
||||
[app.common.types.components-list :as ctkl]
|
||||
[app.common.types.file :as ctf]
|
||||
@ -494,6 +496,51 @@
|
||||
(t/is (ctk/main-instance-of? (:id main1-root') (:id page) component1'))
|
||||
(t/is (nil? saved-objects'))))
|
||||
|
||||
(defn- delete-and-restore-variant
|
||||
"Delete the variant whose main is `deleted-label` from a row flex set where
|
||||
:root1 is laid out at x 30 and :root2 at x 150, move the other variant to
|
||||
`remaining-x` as the layout would, and restore the deleted one. Returns
|
||||
the restored file."
|
||||
[deleted-label remaining-label remaining-x component-label]
|
||||
(let [file (-> (thf/sample-file :file1)
|
||||
(thv/add-variant :variant :component1 :root1 :component2 :root2
|
||||
:variant1-params {:x 30 :y 30 :width 100 :height 100}
|
||||
:variant2-params {:x 150 :y 30 :width 100 :height 100})
|
||||
(ths/update-shape :variant :layout :flex)
|
||||
(ths/update-shape :variant :layout-flex-dir :row))
|
||||
page (thf/current-page file)
|
||||
[_ changes]
|
||||
(cls/generate-delete-shapes (pcb/empty-changes) file page (:objects page)
|
||||
#{(thi/id deleted-label)} {})
|
||||
file (thf/apply-changes file changes)
|
||||
page (thf/current-page file)
|
||||
remaining (ths/get-shape file remaining-label)
|
||||
file (thf/apply-changes
|
||||
file
|
||||
(-> (pcb/empty-changes)
|
||||
(pcb/with-page page)
|
||||
(pcb/with-objects (:objects page))
|
||||
(pcb/update-shapes [(:id remaining)]
|
||||
#(gsh/move % (gpt/point (- remaining-x (:x remaining)) 0)))))
|
||||
page (thf/current-page file)
|
||||
changes (cli/generate-restore-component (pcb/empty-changes) (:data file)
|
||||
(thi/id component-label) (:id file)
|
||||
page (:objects page))]
|
||||
(thf/apply-changes file changes)))
|
||||
|
||||
(t/deftest restored-variant-takes-its-old-place-in-a-flex-set
|
||||
;; :root1 is laid out first (x 30); after its delete :root2 moves there
|
||||
(let [file' (delete-and-restore-variant :root1 :root2 30 :component1)]
|
||||
(t/is (= [(thi/id :root2) (thi/id :root1)]
|
||||
(:shapes (ths/get-shape file' :variant)))
|
||||
"the flex set lays :root1 out first again")))
|
||||
|
||||
(t/deftest restored-last-variant-stays-last-in-a-flex-set
|
||||
(let [file' (delete-and-restore-variant :root2 :root1 30 :component2)]
|
||||
(t/is (= [(thi/id :root2) (thi/id :root1)]
|
||||
(:shapes (ths/get-shape file' :variant)))
|
||||
"the flex set lays :root2 out last again")))
|
||||
|
||||
(defn- restore-after-page-deletion
|
||||
"Delete :component1, whose main lives on :page-2, then delete that page,
|
||||
and restore the component from `current-page`. Returns the restored
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user