diff --git a/common/src/app/common/logic/instances.cljc b/common/src/app/common/logic/instances.cljc index caa31ddce3..38aba35ca1 100644 --- a/common/src/app/common/logic/instances.cljc +++ b/common/src/app/common/logic/instances.cljc @@ -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 diff --git a/common/test/common_tests/logic/comp_creation_test.cljc b/common/test/common_tests/logic/comp_creation_test.cljc index 69f9156e50..607ee21ded 100644 --- a/common/test/common_tests/logic/comp_creation_test.cljc +++ b/common/test/common_tests/logic/comp_creation_test.cljc @@ -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