diff --git a/common/src/app/common/files/repair.cljc b/common/src/app/common/files/repair.cljc index f9b944a51d..e42b8c6a29 100644 --- a/common/src/app/common/files/repair.cljc +++ b/common/src/app/common/files/repair.cljc @@ -732,6 +732,20 @@ (pcb/with-file-data file-data) (pcb/update-shapes [(:id shape)] repair-shape))))) +(defmethod repair-error :variant-container-child-not-main + [_ {:keys [shape page-id args]} file-data _] + (let [objects (:objects (ctpl/get-page file-data page-id)) + board-id (:variant-id args) + parent-id (dm/get-in objects [board-id :parent-id]) + parent-shapes (:shapes (get objects parent-id)) + index (or (some-> (cfh/get-position-on-parent objects board-id) inc) + (count parent-shapes))] + (log/debug :hint "repairing shape :variant-container-child-not-main" :id (:id shape) :name (:name shape) :page-id page-id) + (log/debug :hint " -> move shape out of the variant container" :parent-id parent-id :index index) + (-> (pcb/empty-changes nil page-id) + (pcb/with-file-data file-data) + (pcb/change-parent parent-id [shape] index {:allow-altering-copies true})))) + (defmethod repair-error :main-instance-invalid-variant-id [_ {:keys [shape page-id args]} file-data _] (let [repair-shape diff --git a/common/src/app/common/files/validate.cljc b/common/src/app/common/files/validate.cljc index aeaa1a762d..720323f2ef 100644 --- a/common/src/app/common/files/validate.cljc +++ b/common/src/app/common/files/validate.cljc @@ -67,6 +67,7 @@ :missing-slot :shape-ref-cycle :main-instance-not-a-variant + :variant-container-child-not-main :main-instance-invalid-variant-id :invalid-variant-properties :variant-not-main @@ -623,6 +624,16 @@ (str/ffmt "Variant container % has invalid properties" (:id shape)) shape file page)))))) +(defn- variant-board-main? + "True when `shape` is a main, or is still the main of its live + component in this file even though it lost its :main-instance flag." + [shape file] + (or (ctk/main-instance? shape) + (let [component (ctkl/get-component (:data file) (:component-id shape))] + (and (some? component) + (= (:component-file shape) (:id file)) + (= (:main-instance-id component) (:id shape)))))) + (defn- check-variant-container "Shape is a variant container, so: -all its children should be variants with variant-id equals to the shape-id @@ -645,16 +656,25 @@ :clean-name clean-path)) (run! (fn [child-id] (when-let [child (get objects child-id)] - (if (not (ctk/is-variant? child)) - (report-error :main-instance-not-a-variant - (str/ffmt "Main instance shape % should be a variant" (:id child)) - child file page - :variant-id shape-id) + (cond + (ctk/is-variant? child) (when (not= (:variant-id child) shape-id) (report-error :main-instance-invalid-variant-id (str/ffmt "Main instance in variant % should have the variant-id of the container but has %" (:id child) (:variant-id child)) child file page - :variant-id shape-id))))) + :variant-id shape-id)) + + (variant-board-main? child file) + (report-error :main-instance-not-a-variant + (str/ffmt "Main instance shape % should be a variant" (:id child)) + child file page + :variant-id shape-id) + + :else + (report-error :variant-container-child-not-main + (str/ffmt "Shape % inside variant container % is not a variant main" (:id child) shape-id) + child file page + :variant-id shape-id)))) shapes) (check-variant-property-set shape file page objects file-data :compare-label false))) diff --git a/common/test/common_tests/files/repair_test.cljc b/common/test/common_tests/files/repair_test.cljc index f4214a7aef..5629d9dd76 100644 --- a/common/test/common_tests/files/repair_test.cljc +++ b/common/test/common_tests/files/repair_test.cljc @@ -12,6 +12,7 @@ generate accurate errors, and that the repair functions return the file to a stable state." (:require + [app.common.data :as d] [app.common.files.changes-builder :as pcb] [app.common.files.migrations :as cfm] [app.common.files.repair :as cfr] @@ -131,6 +132,96 @@ (t/is (nil? (ths/get-shape-by-id file' main-id))) (t/is (nil? (:errors result)))))) +(defn- variant-board-with-stray-group + [file] + (-> file + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (tho/add-group :group1 :parent-label :variant1) + (ths/add-sample-shape :child1 :parent-label :group1))) + +(t/deftest validate-variant-container-child-not-main + (t/testing "a plain shape inside a variant board reports its own code, not a main-instance one" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (variant-board-with-stray-group)) + errors (cfv/validate-file file {}) + error (first errors)] + (t/is (= 1 (count errors))) + (t/is (= :variant-container-child-not-main (:code error))) + (t/is (= (thi/id :group1) (:id (:shape error)))) + (t/is (= (thi/id :variant1) (:variant-id (:args error)))) + (t/is (= (str "Shape " (thi/id :group1) + " inside variant container " (thi/id :variant1) + " is not a variant main") + (:hint error))))) + + (t/testing "a variant main that lost :main-instance and :variant-id is still reported as a main" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (ths/update-shape :root1 :variant-id nil) + (ths/update-shape :root1 :main-instance nil)) + codes (->> (cfv/validate-file file {}) + (filter #(= (thi/id :root1) (:id (:shape %)))) + (map :code) + (set))] + (t/is (contains? codes :main-instance-not-a-variant)) + (t/is (not (contains? codes :variant-container-child-not-main)))))) + +(t/deftest repair-variant-container-child-not-main + (t/testing "a plain group inside a variant board moves out with its children" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (variant-board-with-stray-group)) + result (repair-until-valid file) + file' (:file result) + board' (ths/get-shape file' :variant1) + parent' (ths/get-shape-by-id file' (:parent-id board')) + group' (ths/get-shape file' :group1) + child' (ths/get-shape file' :child1)] + (t/is (nil? (:errors result))) + (t/is (some? group')) + (t/is (some? child')) + (t/is (= (:parent-id board') (:parent-id group'))) + (t/is (= (inc (d/index-of (:shapes parent') (:id board'))) + (d/index-of (:shapes parent') (:id group')))) + (t/is (= (:frame-id board') (:frame-id group'))) + (t/is (= (:frame-id board') (:frame-id child'))) + (t/is (nil? (:variant-id group'))) + (t/is (= #{(thi/id :root1) (thi/id :root2)} (set (:shapes board')))))) + + (t/testing "a component copy inside a variant board moves out and nothing is deleted" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (tho/add-simple-component :component3 :main3 :main3-child) + (thc/instantiate-component :component3 :copy1 :parent-label :variant1)) + result (repair-until-valid file) + file' (:file result) + board' (ths/get-shape file' :variant1) + copy' (ths/get-shape file' :copy1)] + (t/is (nil? (:errors result))) + (t/is (some? copy')) + (t/is (= (:parent-id board') (:parent-id copy'))) + (t/is (= #{(thi/id :root1) (thi/id :root2)} (set (:shapes board')))) + (t/is (some? (ths/get-shape file' :main3))) + (t/is (some? (ths/get-shape file' :main3-child))))) + + (t/testing "a stray child repair appends when the variant board is missing from its parent's children" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (variant-board-with-stray-group)) + page-id (thf/current-page-id file) + board-id (thi/id :variant1) + parent-id (get-in file [:data :pages-index page-id :objects board-id :parent-id]) + parent-shapes (get-in file [:data :pages-index page-id :objects parent-id :shapes]) + error (first (filter #(= :variant-container-child-not-main (:code %)) + (cfv/validate-file file {}))) + file (update-in file [:data :pages-index page-id :objects parent-id :shapes] + #(d/removev #{board-id} %)) + changes (cfr/repair-file file {} [error]) + file' (thf/apply-changes file {:redo-changes changes} :validate? false) + group' (ths/get-shape file' :group1) + parent' (ths/get-shape-by-id file' parent-id)] + (t/is (= (count (d/removev #{board-id} parent-shapes)) + (d/index-of (:shapes parent') (:id group')))) + (t/is (= parent-id (:parent-id group')))))) + (t/deftest repair-invalid-variant-id-variant-component-bad-id (t/testing "detect and repair a variant main whose variant id does not match the container's id" (let [file (-> (thf/sample-file :file1 :page-label :page1) @@ -1682,4 +1773,3 @@ (t/is (= [{:name "SizeBig" :value "ab"}] (:variant-properties comp'))) (t/is (nil? (ths/get-shape file' :root1))) (t/is (nil? (seq (property-errors result))))))) - diff --git a/frontend/src/app/main/data/workspace/svg_upload.cljs b/frontend/src/app/main/data/workspace/svg_upload.cljs index 3f438739f7..ab83172344 100644 --- a/frontend/src/app/main/data/workspace/svg_upload.cljs +++ b/frontend/src/app/main/data/workspace/svg_upload.cljs @@ -13,6 +13,7 @@ [app.common.files.helpers :as cfh] [app.common.files.shapes-builder :as sb] [app.common.svg :as csvg] + [app.common.types.container :as ctn] [app.common.types.shape-tree :as ctst] [app.common.uuid :as uuid] [app.main.data.changes :as dch] @@ -102,6 +103,10 @@ (dm/get-in objects [base-id :frame-id]))) parent-id (if keep-parent? base-id frame-id) + parent-id (:id (ctn/get-first-valid-parent objects parent-id)) + frame-id (if (cfh/frame-shape? objects parent-id) + parent-id + (dm/get-in objects [parent-id :frame-id])) [new-shape new-children] (sb/create-svg-shapes id svg-data position objects frame-id parent-id selected true) diff --git a/frontend/test/frontend_tests/logic/pasting_in_containers_test.cljs b/frontend/test/frontend_tests/logic/pasting_in_containers_test.cljs index 984e517ff3..134ba78245 100644 --- a/frontend/test/frontend_tests/logic/pasting_in_containers_test.cljs +++ b/frontend/test/frontend_tests/logic/pasting_in_containers_test.cljs @@ -6,6 +6,7 @@ (ns frontend-tests.logic.pasting-in-containers-test (:require [app.common.data :as d] + [app.common.files.helpers :as cfh] [app.common.geom.point :as gpt] [app.common.test-helpers.components :as cthc] [app.common.test-helpers.compositions :as ctho] @@ -15,6 +16,9 @@ [app.common.test-helpers.variants :as thv] [app.common.transit :as transit] [app.common.types.component :as ctk] + [app.common.types.pages-list :as ctpl] + [app.common.types.shape :as cts] + [app.common.types.shape-tree :as ctst] [app.common.uuid :as uuid] [app.main.data.workspace :as dw] [app.main.data.workspace.groups :as dwgr] @@ -1105,6 +1109,141 @@ (t/is (= (:id board-a) (:parent-id svg'))) (cthf/validate-file! file'))))))) +(def ^:private svg-test-data + {:name "test.svg" + :attrs {:width 100 :height 100} + :content [{:tag :rect + :attrs {:x "10" :y "10" :width "20" :height "20"}}]}) + +(defn- find-uploaded-svg + [objects] + (->> (vals objects) + (filter #(= (:name %) "test")) + first)) + +(defn- add-variants-for-svg-upload + "Variant set whose board is 100x100 and whose variants leave free space + inside it." + [file] + (let [file (thv/add-variant file :v01 :c01 :m01 :c02 :m02 + :variant1-params {:x 0 :y 0 :width 40 :height 40} + :variant2-params {:x 50 :y 0 :width 40 :height 40}) + page (cthf/current-page file) + board (cths/get-shape file :v01) + board (cts/setup-shape (-> board + (assoc :x 0 :y 0 :width 100 :height 100) + (dissoc :selrect :points)))] + (update file :data ctpl/update-page (:id page) #(ctst/set-shape % board)))) + +(defn- check-svg-outside-variant-board + [new-state file] + (let [file' (ths/get-file-from-state new-state) + objects (:objects (cthf/current-page file')) + svg' (find-uploaded-svg objects) + board-id (:id (cths/get-shape file :v01)) + board' (get objects board-id)] + (t/is (some? svg')) + (t/is (= (:parent-id board') (:parent-id svg'))) + (t/is (= (:frame-id board') (:frame-id svg'))) + (t/is (= (set (:shapes board')) + #{(:id (cths/get-shape file :m01)) (:id (cths/get-shape file :m02))})) + (cthf/validate-file! file'))) + +(t/deftest svg-upload-with-variants-selected-skips-variant-board + "Uploading an SVG with several variants selected places it outside the variant board" + (t/async + done + (let [;; ==== Setup + file (-> (cthf/sample-file :file1) + (add-variants-for-svg-upload)) + store (ths/setup-store file) + + ;; ==== Action + events [(dws/select-shapes (d/ordered-set (:id (cths/get-shape file :m01)) + (:id (cths/get-shape file :m02)))) + (dwsvg/add-svg-shapes nil svg-test-data (gpt/point 1000 1000) nil)]] + + (ths/run-store + store done events + (fn [new-state] + (check-svg-outside-variant-board new-state file)))))) + +(t/deftest svg-upload-on-empty-variant-board-area-skips-variant-board + "Uploading an SVG on an empty area of a variant board places it outside the board" + (t/async + done + (let [;; ==== Setup + file (-> (cthf/sample-file :file1) + (add-variants-for-svg-upload)) + store (ths/setup-store file) + + ;; ==== Action + events [(dwsvg/add-svg-shapes nil svg-test-data (gpt/point 80 80) nil)]] + + (ths/run-store + store done events + (fn [new-state] + (check-svg-outside-variant-board new-state file)))))) + +(t/deftest svg-upload-inside-variant-keeps-variant + "Uploading an SVG inside a variant keeps that variant as parent" + (t/async + done + (let [;; ==== Setup + file (-> (cthf/sample-file :file1) + (add-variants-for-svg-upload)) + store (ths/setup-store file) + + ;; ==== Action + m01 (cths/get-shape file :m01) + events [(dwsvg/add-svg-shapes nil svg-test-data (gpt/point 20 20) nil)]] + + (ths/run-store + store done events + (fn [new-state] + (let [;; ==== Get + file' (ths/get-file-from-state new-state) + svg' (find-uploaded-svg (:objects (cthf/current-page file')))] + + ;; ==== Check + (t/is (some? svg')) + (t/is (= (:id m01) (:parent-id svg'))) + (t/is (= (:id m01) (:frame-id svg'))) + (cthf/validate-file! file'))))))) + +(t/deftest svg-upload-on-component-copy-skips-copy + "Uploading an SVG on a component copy places it next to the copy" + (t/async + done + (let [;; ==== Setup + file (-> (cthf/sample-file :file1) + (ctho/add-simple-component-with-copy + :c01 :main :main-child :copy + :main-root-params {:width 100 :height 100})) + store (ths/setup-store file) + + ;; ==== Action + copy (cths/get-shape file :copy) + events [(dwsvg/add-svg-shapes nil svg-test-data + (gpt/point (+ (:x copy) 5) (+ (:y copy) 5)) + nil)]] + + (ths/run-store + store done events + (fn [new-state] + (let [;; ==== Get + file' (ths/get-file-from-state new-state) + objects (:objects (cthf/current-page file')) + svg' (find-uploaded-svg objects) + copy' (get objects (:id copy)) + inside (cfh/get-children-ids objects (:id copy))] + + ;; ==== Check + (t/is (some? svg')) + (t/is (= (:parent-id copy') (:parent-id svg'))) + (t/is (not-any? #(= % (:id svg')) inside)) + (cthf/validate-file! file'))))))) + (t/deftest props-paste-with-unloaded-page-is-noop "Pasting props while the page is not loaded is ignored instead of crashing" (t/async