diff --git a/frontend/src/app/main/data/workspace/colors.cljs b/frontend/src/app/main/data/workspace/colors.cljs index 5ce92ae8df..22e72e4c91 100644 --- a/frontend/src/app/main/data/workspace/colors.cljs +++ b/frontend/src/app/main/data/workspace/colors.cljs @@ -347,8 +347,13 @@ ;; FIXME: this function initializes an empty stroke, maybe we can move ;; it to common.types (defn- build-stroke-style-attrs + "Completes the stroke style attributes, preserving the effective + `:center` alignment of existing strokes that have no explicit + `:stroke-alignment` (e.g. strokes imported from SVG). A nil stroke + means no stroke exists at that position and uses the `:inner` default." [stroke] - (let [attrs (select-keys stroke stroke-style-attrs)] + (let [existing-stroke? (some? stroke) + attrs (select-keys stroke stroke-style-attrs)] (cond-> attrs (not (contains? attrs :stroke-width)) (assoc :stroke-width 1) @@ -357,7 +362,7 @@ (assoc :stroke-style :solid) (not (contains? attrs :stroke-alignment)) - (assoc :stroke-alignment :inner) + (assoc :stroke-alignment (if existing-stroke? :center :inner)) :always (d/without-nils)))) diff --git a/frontend/test/frontend_tests/basic_shapes_test.cljs b/frontend/test/frontend_tests/basic_shapes_test.cljs index de9bca248b..0e8e46bbec 100644 --- a/frontend/test/frontend_tests/basic_shapes_test.cljs +++ b/frontend/test/frontend_tests/basic_shapes_test.cljs @@ -71,11 +71,84 @@ stroke' (first (:strokes shape1'))] ;; ==== Check - ;; (println stroke') (t/is (some? shape1')) - (t/is (= (:stroke-alignment stroke') :inner)) + ;; Old shapes without stroke-alignment are rendered as if it is + ;; centered (Taiga #7673); an unrelated color change must not + ;; switch the effective alignment to the new-stroke default. + (t/is (= (:stroke-alignment stroke') :center)) (t/is (= (:stroke-color stroke') "#FABADA")) (t/is (= (:stroke-width stroke') 2)))))))) + +(t/deftest test-update-stroke-color-preserves-inner-alignment + (t/async + done + (let [store (ths/setup-store + (-> (cthf/sample-file :file1 :page-label :page1) + (cths/add-sample-shape :shape1 :strokes [{:stroke-color "#000000" + :stroke-opacity 1 + :stroke-width 2 + :stroke-alignment :inner}]))) + events [(dc/change-stroke-color #{(cthi/id :shape1)} {:color "#FABADA"} 0)]] + (ths/run-store + store done events + (fn [new-state] + (let [objects (dsh/lookup-page-objects new-state) + stroke' (first (:strokes (get objects (cthi/id :shape1))))] + (t/is (= (:stroke-alignment stroke') :inner)) + (t/is (= (:stroke-color stroke') "#FABADA")))))))) + +(t/deftest test-update-stroke-attrs-preserves-missing-alignment + ;; Existing strokes without :stroke-alignment keep effective :center + ;; on the change-stroke-attrs path; explicit alignments are untouched. + (t/async + done + (let [store (ths/setup-store + (-> (cthf/sample-file :file1 :page-label :page1) + (cths/add-sample-shape :shape1 :strokes [{:stroke-color "#000000" + :stroke-opacity 1 + :stroke-width 2}]) + (cths/add-sample-shape :shape2 :strokes [{:stroke-color "#000000" + :stroke-opacity 1 + :stroke-width 2 + :stroke-alignment :inner}]) + (cths/add-sample-shape :shape3 :strokes [{:stroke-color "#000000" + :stroke-opacity 1 + :stroke-width 2 + :stroke-alignment :outer}]))) + events [(dc/change-stroke-attrs #{(cthi/id :shape1)} {:stroke-width 4} 0) + (dc/change-stroke-attrs #{(cthi/id :shape2)} {:stroke-width 4} 0) + (dc/change-stroke-attrs #{(cthi/id :shape3)} {:stroke-width 4} 0)]] + (ths/run-store + store done events + (fn [new-state] + (let [objects (dsh/lookup-page-objects new-state) + stroke1' (first (:strokes (get objects (cthi/id :shape1)))) + stroke2' (first (:strokes (get objects (cthi/id :shape2)))) + stroke3' (first (:strokes (get objects (cthi/id :shape3))))] + (t/is (= (:stroke-alignment stroke1') :center)) + (t/is (= (:stroke-width stroke1') 4)) + (t/is (= (:stroke-alignment stroke2') :inner)) + (t/is (= (:stroke-alignment stroke3') :outer)))))))) + +(t/deftest test-new-stroke-keeps-inner-default + ;; A shape with no strokes at all still gets the current new-stroke + ;; default (:inner); the fix must not revert the new-stroke default + ;; to :center. + (t/async + done + (let [store (ths/setup-store + (-> (cthf/sample-file :file1 :page-label :page1) + (cths/add-sample-shape :shape1 :strokes []))) + events [(dc/change-stroke-color #{(cthi/id :shape1)} {:color "#FABADA"} 0)]] + (ths/run-store + store done events + (fn [new-state] + (let [objects (dsh/lookup-page-objects new-state) + shape1' (get objects (cthi/id :shape1)) + stroke' (first (:strokes shape1'))] + (t/is (some? shape1')) + (t/is (= (:stroke-alignment stroke') :inner)) + (t/is (= (:stroke-color stroke') "#FABADA")))))))) (t/deftest test-update-stroke-color-preserves-dash-gap ;; Custom dash/gap on a dashed stroke describe stroke geometry, not color; ;; a stroke color change must preserve them (issue #11549).